﻿
var imgControl =
{
    //"Public"
    preventInit: false,

    //"Private"
    _count: 0,
    _flowcount: 0,
    _bigImg: null,
    _thumbs: null,
    _currentImg: null,
    _currentControl: "#slideshow-container",
    _currentImgLink: null,
    _isStartup: true,
    _isSlideShow: false,
    _leftbtn: ".img-leftbtn",
    _rightbtn: ".img-rightbtn",
    _itemsPerPage: 10,
    _currentPage: 1,

    closeSlideshow: function() {

        $("#slideshow-container").hide();
        $("#slideshow-alpha-box").hide();

        imgControl.initializeImgControl();

    },

    openSlideshow: function() {


        var slideShowDiv = $("#slideshow-container");
        var slideShowHolder = document.getElementById("slideshow-holder");
        imgControl._currentControl = "#slideshow-container";
        imgControl._isSlideShow = true;

        //if (slideShowHolder.className != "hasChildren") {
        //	$("#slideshow-holder").append(slideShowDiv);
        //	$("#slideshow-holder").addClass("hasChildren");
        //	}


        slideShowDiv.show();
        $("#slideshow-alpha-box").show();
        imgControl.initializeImgControl();

    },



    clickThumb: function(sender, imgUri, text) {
        if (imgUri != "flv") {
            $(imgControl._currentControl + " .flash-holder").hide();
            $(".disc-alpha").show();
            imgControl._bigImg.src = imgUri;
            imgControl._bigImg.alt = $(sender).children()[0].alt;
        }

        imgControl._count = imgControl._thumbs.index(sender);
        $(".thumb_span").text((imgControl._count + 1) + '');


        if (imgControl._currentImgLink != null) {
            $(imgControl._currentImgLink.firstChild).css("border", "none");

        }

        imgControl._currentImgLink = sender;
        $(imgControl._currentImgLink.firstChild).css("border", "1px solid #ded7c7");


    },


    initializeImgControl: function() {
        imgControl._bigImg = $(imgControl._currentControl + " .img-container").children()[0];
   
        if (imgControl._isStartup) {
            imgControl._thumbs = $(imgControl._currentControl + " .ul_thumbs li").children();
            imgControl._currentImgLink = imgControl._thumbs[0];
            $(imgControl._thumbs[0]).click();

            if ($.browser.msie) {
                $(imgControl._thumbs[0]).attr("id", "clickedButton");
                doFire("clickedButton");
            }

            if (imgControl._thumbs.length > imgControl._itemsPerPage)
                $(imgControl._currentControl + " .ul_thumbs li:gt(" + (imgControl._itemsPerPage - 1) + ")").hide();

            $(".thumb_count_span").text(imgControl._thumbs.length + '');
            $(".thumb_span").text(imgControl._currentPage + '');

            imgControl._isStartup = false;
        }
        else {
            var index = imgControl._thumbs.index(imgControl._currentImgLink);
            imgControl._thumbs = $(imgControl._currentControl + " .ul_thumbs li").children();
            $(imgControl._thumbs[index]).click();

            if ($.browser.msie) {
                $(imgControl._thumbs[index]).attr("id", "clickedButton");
                doFire("clickedButton");

            }

            if (imgControl._thumbs.length > imgControl._itemsPerPage) {
                if (imgControl._count < imgControl._itemsPerPage) {
                    $(imgControl._currentControl + " .ul_thumbs li:gt(" + (imgControl._itemsPerPage - 1) + ")").hide();
                    $(imgControl._currentControl + " .ul_thumbs li:lt(" + (imgControl._itemsPerPage + 1) + ")").show();
                }
                else {
                    $(imgControl._currentControl + " .ul_thumbs li:lt(" + (imgControl._thumbs.length - imgControl._itemsPerPage) + ")").hide();
                    $(imgControl._currentControl + " .ul_thumbs li:gt(" + (imgControl._thumbs.length - imgControl._itemsPerPage) + ")").show();
                }
            }
        }
    },

    parseString: function(stringToParse) {
        var parsedString = "";

        for (var i = stringToParse.length - 9; i < stringToParse.length; i++)
            parsedString += stringToParse.charAt(i);

        return parsedString;
    },

    initNavButtons: function() {
        $(imgControl._leftbtn).click(function() {

            if (imgControl._count > 0) {
                if (imgControl._thumbs.length > imgControl._itemsPerPage) {
                    $(imgControl._currentControl + " .ul_thumbs li:eq(" + (imgControl._count - 1) + ")").show();
                    $(imgControl._currentControl + " .ul_thumbs li:eq(" + ((imgControl._count - 1) + imgControl._itemsPerPage) + ")").hide();
                }

                imgControl._count--;

                $(".thumb_span").text((imgControl._count + 1) + '');
                $(imgControl._thumbs[imgControl._count]).click();

                if ($.browser.msie) {
                    $(imgControl._thumbs[imgControl._count]).attr("id", "clickedButton");
                    doFire("clickedButton");
                }
            }
            else {

                imgControl._currentPage = Math.floor((imgControl._thumbs.length + imgControl._itemsPerPage) / imgControl._itemsPerPage);
                imgControl._count = imgControl._thumbs.length - 1;
                $(".thumb_span").text((imgControl._count + 1) + '');

                $(imgControl._thumbs[imgControl._count]).click();

                if ($.browser.msie) {
                    $(imgControl._thumbs[imgControl._count]).attr("id", "clickedButton");
                    doFire("clickedButton");
                }

                if (imgControl._thumbs.length > imgControl._itemsPerPage) {
                    $(imgControl._currentControl + " .ul_thumbs li:lt(" + (imgControl._thumbs.length - imgControl._itemsPerPage) + ")").hide();
                    $(imgControl._currentControl + " .ul_thumbs li:gt(" + (imgControl._thumbs.length - imgControl._itemsPerPage) + ")").show();
                }
            }

            return false;
        });

        $(imgControl._rightbtn).click(function() {

            if ((imgControl._count + 1) < imgControl._thumbs.length) {
                if (imgControl._thumbs.length > imgControl._itemsPerPage) {
                    if ((imgControl._count + 1) >= imgControl._itemsPerPage) {
                        $(imgControl._currentControl + " .ul_thumbs li:eq(" + ((imgControl._count + 1) - imgControl._itemsPerPage) + ")").hide();
                        $(imgControl._currentControl + " .ul_thumbs li:eq(" + (imgControl._count + 1) + ")").show();
                    }
                }

                imgControl._count++;
                $(".thumb_span").text((imgControl._count + 1) + '');
                $(imgControl._thumbs[imgControl._count]).click();

                if ($.browser.msie) {
                    $(imgControl._thumbs[imgControl._count]).attr("id", "clickedButton");
                    doFire("clickedButton");
                }
            }
            else if ((imgControl._count + 1) == imgControl._thumbs.length) {
                imgControl._count = 0;
                imgControl._currentPage = 1;
                $(imgControl._thumbs[0]).click();
                $(".thumb_span").text((imgControl._count + 1) + '');

                if ($.browser.msie) {
                    $(imgControl._thumbs[imgControl._thumbs[0]]).attr("id", "clickedButton");
                    doFire("clickedButton");
                }

                if (imgControl._thumbs.length > imgControl._itemsPerPage) {
                    $(imgControl._currentControl + " .ul_thumbs li:gt(" + (imgControl._itemsPerPage - 1) + ")").hide();
                    $(imgControl._currentControl + " .ul_thumbs li:lt(" + (imgControl._itemsPerPage) + ")").show();
                }
            }

            return false;
        });
    }
}

$(document).ready(function() {
	if (!imgControl.preventInit) {
		imgControl.initializeImgControl();
		imgControl.initNavButtons();
	}
});

function doFire(elementId) {

    var element = document.getElementById(elementId);
    if (element == null)
        return;
    element.fireEvent("onclick");
    element.id = "";
}