Sanoma.Site.imageGallery= {

    _THUMBNAILWIDTH: 44+4, //image width + margins
    thumbnailListWrapperWidth: 0,
    thumbnailListWidth: 0,
    imageGallery: null,
	itemsLength: 0,
    currentItemIndex: 0,
	previousItemIndex: 0,
	nextItemIndex: 0,
	pagesLength: 0,
	itemsPerPage: 0,
    currentPageIndex: 0,
	previousPageIndex: 0,
	nextPageIndex: 0,

	init: function(){
		if(jQuery("div.imageGallery").length == 0){ //abort if no imageGallery exist
			return;
		}

        //add image container
        jQuery("div.imageGallery .imageGalleryThumbnailRow").before('<div class="imageGalleryLargeImageRow"><a class="previous" href="#" title="Edellinen kuva">Edellinen</a><a class="next" href="#" title="Seuraava kuva">Seuraava</a><div class="imageGalleryLargeImage"></div></div>');

        //add previous & next buttons beside thumbnails
        jQuery("div.imageGallery .imageGalleryThumbnails").before('<a class="previous" href="#" title="Edellinen sivu">Edellinen</a><a class="next" href="#" title="Seuraava sivu">Seuraava</a>');

		this.bindEvents();
        jQuery("div.imageGallery .imageGalleryThumbnails ul li:first a").click(); //show first image
        this.initPaging();
	},

	bindEvents: function(){
		jQuery("div.imageGallery ul li a").click(
			function (e) {
                e.preventDefault();
				Sanoma.Site.imageGallery.toggleImage(this);
			}
		);
		jQuery("div.imageGallery .imageGalleryLargeImageRow a.previous").click(
			function (e) {
                e.preventDefault();
				Sanoma.Site.imageGallery.previousImage();
			}
		);
		jQuery("div.imageGallery .imageGalleryLargeImageRow a.next").click(
			function (e) {
                e.preventDefault();
				Sanoma.Site.imageGallery.nextImage();
			}
		);
		jQuery("div.imageGallery .imageGalleryThumbnailRow a.previous").click(
			function (e) {
                e.preventDefault();
				Sanoma.Site.imageGallery.previousPage();
			}
		);
		jQuery("div.imageGallery .imageGalleryThumbnailRow a.next").click(
			function (e) {
                e.preventDefault();
				Sanoma.Site.imageGallery.nextPage();
			}
		);
	},

	previousImage: function (){
        //check if on different page and change page if yes, otherwise just toggle active image
        var pageOfItem = this.checkPageOfItem(this.previousItemIndex);
        if (this.currentPageIndex != pageOfItem) {
            this.togglePage(pageOfItem);
        }
		this.toggleImage( jQuery(this.imageGallery).find('.imageGalleryThumbnails ul li:eq('+this.previousItemIndex+') a') );
	},

	nextImage: function (){
        //check if on different page and change page if yes, otherwise just toggle active image
        var pageOfItem = this.checkPageOfItem(this.nextItemIndex);
        if (this.currentPageIndex != pageOfItem) {
            this.togglePage(pageOfItem);
        }
		this.toggleImage( jQuery(this.imageGallery).find('.imageGalleryThumbnails ul li:eq('+this.nextItemIndex+') a') );
	},

	checkPageOfItem: function (itemIndex){
        return Math.floor( itemIndex / this.itemsPerPage );
	},

	toggleImage: function(el){
		var li = jQuery(el).parent();
		var ul = jQuery(li).parent();
		var liList = jQuery(ul).children();
        this.itemsLength = liList.length;
		this.currentItemIndex = jQuery(liList).index(li);
		this.imageGallery = jQuery(ul).parent().parent().parent();

        //hide previous & next buttons if less than two images
        if (this.itemsLength < 2){
            jQuery(this.imageGallery).find(".imageGalleryLargeImageRow a.previous").hide().end().find(".imageGalleryLargeImageRow a.next").hide();
        }
        this.thumbnailListWidth = this.itemsLength * this._THUMBNAILWIDTH;
        jQuery(ul).css("width", this.thumbnailListWidth + "px");
		jQuery(liList).removeClass("active");
		jQuery(li).addClass("active");
		var src = jQuery(li).find("a").attr("href");
        var title = jQuery(li).find("img").attr("title");
        jQuery(this.imageGallery).find(".imageGalleryLargeImage").html('<img src="'+ src +'" alt="" /><div class="caption">' + title + '</div>');

        if (this.currentItemIndex < (this.itemsLength-1)){
            this.nextItemIndex = this.currentItemIndex+1;
        } else {
            this.nextItemIndex = 0;
        }
        if (this.currentItemIndex > 0){
            this.previousItemIndex = this.currentItemIndex-1;
        } else {
            this.previousItemIndex = this.itemsLength-1;
        }
	},

	initPaging: function (){
        this.thumbnailListWrapperWidth = jQuery(this.imageGallery).find(".imageGalleryThumbnails").width();
        this.pagesLength = Math.ceil( this.thumbnailListWidth / this.thumbnailListWrapperWidth ); //count the amount of pages (round up to next integer)
        this.itemsPerPage = Math.floor( this.thumbnailListWrapperWidth / this._THUMBNAILWIDTH ); //count the amount of thumbnails per page (round down to previous integer)
        this.previousPageIndex = this.pagesLength -1;
        if (this.pagesLength > 1) {
            this.nextPageIndex = 1;
        }
        //hide previous & next buttons if less than two pages
        if (this.pagesLength < 2){
            jQuery(this.imageGallery).addClass("noPaging");
        }
	},

	previousPage: function (){
		this.togglePage(this.previousPageIndex, true);
	},

	nextPage: function (){
		this.togglePage(this.nextPageIndex, true);
	},

	togglePage: function(pageIndex, setFirstItemActive){
        this.currentPageIndex = pageIndex;

        //move list to right position
        jQuery(this.imageGallery).find("ul").css("left", (this.currentPageIndex * this.thumbnailListWrapperWidth * -1) + "px");

        if (setFirstItemActive){ //set first image of page to active image when paging buttons clicked
            var firstItemOnPage = this.currentPageIndex * this.itemsPerPage;
            this.toggleImage( jQuery(this.imageGallery).find('.imageGalleryThumbnails ul li:eq('+firstItemOnPage+') a') );
        }

        if (this.currentPageIndex < (this.pagesLength-1)){
            this.nextPageIndex = this.currentPageIndex+1;
        } else {
            this.nextPageIndex = 0;
        }
        if (this.currentPageIndex > 0){
            this.previousPageIndex = this.currentPageIndex-1;
        } else {
            this.previousPageIndex = this.pagesLength-1;
        }
	}

};

