var api_gallery = [];
var api_titles = [];
var api_descriptions = [];
var rowItems = 3;

$(document).ready(function () {
    initGallery();
    $("#Selector a[rel^='prettyPhoto']").prettyPhoto({ deeplinking: false, show_title: true });
    $("#Canvas a[rel^='prettyPhoto']").prettyPhoto({ deeplinking: false, show_title: true });
    $("a.bp[rel^='prettyPhoto']").prettyPhoto({ deeplinking: false, default_width: 762 });
});

function initGallery() {
    var slector = $("#Selector");
    var canvasImg = $("#Canvas img:first");
    if ($(slector).html() != "") {
        $(canvasImg).removeAttr("onmousedown");
        $(canvasImg).removeAttr("onclick");
        //$(canvasImg).attr("onclick", "$.prettyPhoto.open(api_gallery,api_titles,api_descriptions); return false");
        $("#Canvas").removeAttr("OnClick");
        $('#Canvas').bind('click', function () {
            $.prettyPhoto.open(api_gallery, api_titles, api_descriptions); return false
            //alert(api_gallery.toString());
        });
        //$("#Canvas").attr("onclick", "$.prettyPhoto.open(api_gallery,api_titles,api_descriptions); return false");
        //$("#Canvas").attr("onclick", "alert('test')");
        $("#Canvas").css({ 'width': '320px', 'height': '355px' });
        $("#Selector img").each(
            function (intIndex) {
                var imgSrc = $(this).attr("src");

                if (imgSrc != 'undefined') {
                    var imgSrcBig = imgSrc.replace("-t.", "-p.");
                    $(this).remove();
                    if ((intIndex + 1) % rowItems == 0)
                        $("#Selector").append('<a style=\"margin-right:0px;\" rel=\"prettyPhoto[gallery2]\" href=\"' + imgSrcBig + '\"><img width=\"60\" src=\"' + imgSrc + '\" /></a>');
                    else
                        $("#Selector").append('<a rel=\"prettyPhoto[gallery2]\" href=\"' + imgSrcBig + '\"><img width=\"60\" src=\"' + imgSrc + '\" /></a>');
                    api_gallery.push(imgSrcBig);
                    api_titles.push("&nbsp;");
                    api_descriptions.push("&nbsp;");
                }
                else
                    $(this).remove();
            }
        );
        $("#Selector").append("<div style=\"clear:both;\"></div>");
    }
    else {
        var img = $("#ImgContainer img:first");
        if (img) {
            var imgSrc = $(img).attr("src");
            var imgWidth = $(img).attr("width");

            var tempArray = imgSrc.split(".");

            var imgSrcBig = imgSrc.replace("." + tempArray[tempArray.length - 1], "-p." + tempArray[tempArray.length - 1]);
            $("#Canvas").append('<a rel=\"prettyPhoto\" href=\"' + imgSrcBig + '\"><img width=\"' + imgWidth + '\" src=\"' + imgSrc + '\" /></a>');
            $(img).remove();
        }
    }

};


