var api_gallery = [];
var api_titles = [];
var api_descriptions = [];
var rowItems = 3;

$(document).ready(function () {
    initGallery();
    $("#Canvas a[rel^='prettyPhoto']").prettyPhoto({ deeplinking: false, default_width: 600, default_height: 580, show_title: true });
    $("a[rel^='prettyPhoto']").prettyPhoto({ deeplinking: false, show_title: true });
});

$(window).load(function () {
    //    var img = $("#Canvas img:last");
    //    var h = img.height() + 0;
    //    var w = img.width() + 0;
    var h = 232;
    var w = 240;

    $("#Canvas").css({ 'width': w, 'height': h });
});

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' });
        //$("#Canvas").removeAttr("style");
        $("#Selector img").each(
            function (intIndex) {
                var imgSrc = $(this).attr("src");
                var imgTitle = $(this).attr("alt");
                if (imgTitle == 'undefined')
                    imgTitle = '';

                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 alt=\"' + imgTitle + '\" width=\"72\" height=\"70\" src=\"' + imgSrc + '\" /></a>');
                    else
                        $("#Selector").append('<a rel=\"prettyPhoto[gallery2]\" style=\"margin-right:9px;\" href=\"' + imgSrcBig + '\"><img alt=\"' + imgTitle + '\" width=\"72\" height=\"70\" 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 = $("#ImageContainer img:first");

        if (img) {

            var imgSrc = $(img).attr("src");
            var imgTitle = $(img).attr("alt");
            if (imgTitle == 'undefined')
                imgTitle = '';
            
            var imgWidth = 240;

            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 alt=\"' + imgTitle + '\" width=\"' + imgWidth + '\" src=\"' + imgSrc + '\" /></a>');

            $("#ImageContainer img:last").remove();
        }
    }

};


