/*
 *	Advanced Gallery
 *	Author: Alan Agius
 *
 *	www.alert.com.mt
 *	Last Updated: 11th November 09
 */
var blnTransitionReady = true,
	blnScrollReady = true,
	intCurrentIndex = 0,
	intNoThumbsToShow = 0,
	intTotalImgs = 0,
	strLoader = "<div class=\"loading-bar\"></div>";

(function($){
//Gallery
$.fn.gallery = function(){
	try{
		$("ul","#albums-slider-items").hide();
		$("ul:eq(0)","#albums-slider-items").fadeIn(400);
		
		intNoThumbsToShow = Math.ceil(parseInt($("#thumbnail-holder").width(),10) / parseInt($("ul li", "#thumbnail-holder").outerWidth(true),10));

		//Total Images
		intTotalImgs = $("ul li","#thumbnail-holder").length;

		//Thumbnail click
		$("li","#thumbnail-holder ul").click(function(){
			if(blnTransitionReady){
				$().gallery.changeimage($("li","#thumbnail-holder ul").index($(this)));
			}
		})
		
		//Previous photo click
		$("#btn-photo-left").click(function(){
			if(blnTransitionReady){
				$().gallery.previous();
			}
		})
		
		//Next photo click
		$("#btn-photo-right").click(function(){
			if(blnTransitionReady){
				$().gallery.next();
			}
		})
		
		//Call thumbs scroller
		$().thumbnailscroller(); 
		
		//Change Image
		$().gallery.changeimage(0);

		//Hover Fade thumbnails
		$("img","#thumbnail-holder ul li").hover(function(){
			$(this).fadeTo(200, 1.0);
		},
		function(){
			$(this).fadeTo(200, 0.5);
		})
	}
	catch(err){
		//alert(err);
	}
}

//Thumbnail Scroller
$.fn.thumbnailscroller = function(){
	$("#btn-thumbs-right").click(function(){
		if(blnScrollReady){
			$().thumbnailscroller.right();
		}
	})
	
	$("#btn-thumbs-left").click(function(){
		if(blnScrollReady){
			$().thumbnailscroller.left();
		}
	})
}

//Thumbnail Scroller Right
$.fn.thumbnailscroller.right = function(){
	var objItems = $("ul li","#thumbnail-holder"),
		intItemWidth = parseInt(objItems.outerWidth(true),10),
		intCurrentMargin =	parseInt($("ul","#thumbnail-holder").css("margin-left"),10),
		intScrollWidth = +- intItemWidth * intNoThumbsToShow + intCurrentMargin,
		intMaxScroll =  +- (Math.floor((objItems.length / intNoThumbsToShow) * (intItemWidth * intNoThumbsToShow)) - (intItemWidth * intNoThumbsToShow));
	
	if(intCurrentMargin > intMaxScroll){
		blnScrollReady = false;
		$("ul","#thumbnail-holder").animate({marginLeft: intScrollWidth + "px"}, 800, function(){blnScrollReady = true;})
	}
}

//Thumbnail Scroller Left
$.fn.thumbnailscroller.left = function(){
	var intItemWidth = parseInt($("ul li","#thumbnail-holder").outerWidth(true),10),
		intCurrentMargin =	parseInt($("ul","#thumbnail-holder").css("margin-left"),10),
		intScrollWidth = intItemWidth * intNoThumbsToShow + intCurrentMargin;
	
	if(intCurrentMargin < 0){
		blnScrollReady = false;
		$("ul","#thumbnail-holder").animate({marginLeft: intScrollWidth + "px"}, 800, function(){blnScrollReady = true;})
	}
}

//Change Image, Captions and Number
$.fn.gallery.changeimage = function(intNewIndex){
  var strAlt = $("li:eq("+ intNewIndex +") > img","#thumbnail-holder ul").attr("alt"),
	  strSrc = $("li:eq("+ intNewIndex +") > input[title='full-image']","#thumbnail-holder ul").val(),
	  strSpanId = "gallery-image-" + intNewIndex;
	  
  $("li > img","#thumbnail-holder ul").removeClass("active");
  $("li:eq("+ intNewIndex +") > img","#thumbnail-holder ul").addClass("active");
													
  //Check if last banner is the same as the last one
  if(strSpanId != $("span:first", "#main-photo-holder").attr("id")){
	  blnTransitionReady = false;
	  intCurrentIndex = intNewIndex;

	  //Append new image holder
	  $("#main-photo-holder").append("<span class=\"image-holder\" id=\""+ strSpanId +"\"></span>");
	  
	  //200 millisecond to see if image is cached, Show preloader is not cached
	  var objLoaderTimeout = setTimeout("$(\"#main-photo-holder\").append(strLoader);", 200),
		  objImage = new Image();

	  $(objImage).load(function(){
		  clearTimeout(objLoaderTimeout);
		  $(".loading-bar", "#main-photo-holder").remove(); //remove loader
		  $("#" + strSpanId).append($(objImage)).fadeIn(500, function(){
			  //Remove holder class and remove previous image holder
			  $("#" + strSpanId).removeClass("image-holder");
			  $("span:first", "#main-photo-holder").remove();
			  blnTransitionReady = true;
		  });
	  }).attr("src", strSrc).attr("alt", strAlt);
	  
	  //Photos Text
	  $("#photo-numbers").html("Image "+ (intNewIndex + 1) + " of " + intTotalImgs);
  }else{
	  return false;
  }
}

//Next Image
$.fn.gallery.next = function(){
	if(intCurrentIndex < ($("li","#thumbnail-holder ul").length) - 1){
		var i = 1;
		while(i <= $("li","#thumbnail-holder ul").length / intNoThumbsToShow){		
			if(intCurrentIndex+1 == intNoThumbsToShow * i){
				$().thumbnailscroller.right();
				break;
			}
			i++;
		}
		
		$().gallery.changeimage(intCurrentIndex + 1);
	}
}

//Previous Image
$.fn.gallery.previous = function(){
	if(intCurrentIndex > 0){
		var i = 1;
		while(i <= $("li","#thumbnail-holder ul").length / intNoThumbsToShow){	
			if(intCurrentIndex == intNoThumbsToShow * i){
				$().thumbnailscroller.left();
				break;
			}
			i++;
		}
		
		$().gallery.changeimage(intCurrentIndex - 1);
	}
}
})(jQuery);
