var imageindex = 1;
var currentPage = 1;
var playSpeed = 3000;
var playpause,totalimages,carouselUbound;

$.fn.updateControls = function()
{
	$.fn.updateControlViews(imageindex == totalimages,'pic-next');
	$.fn.updateControlViews(imageindex == 1,'pic-prev');
	$.fn.updateControlViews(currentPage == carouselUbound,'page-next');
	$.fn.updateControlViews(currentPage == 1,'page-prev');
	$.fn.imageListing();
}

$.fn.updateControlViews = function(pCond, pBut)
{	
	$("."+pBut).css("display",((pCond) ? "none" : "block"));
	$("."+pBut+"-disabled").css("display",((pCond) ? "block" : "none"));
}	

$.fn.imageListing = function()
{
	$(".imageListing").html(imageindex+" of "+totalimages);
}

$.fn.updateImage = function(path,title,description)
{
	$(".imageTitle").html(title);
	$(".imageDescription").html(description);
	$("#largeImg").fadeTo("normal",0,function(){
		if (description == "")
			description = "Image "+imageindex+" of "+totalimages;
		$("#largeImg").attr({"src":path,"alt":description}).fadeTo("slow",1);
	});
	$.fn.updateControls();
}

$(document).ready(function(){
	totalimages = $(".thumbs a").size();
	 if (totalimages <= 5){
		$(".page-next").hide();
		$(".page-next-disabled").show();
	}
	carouselUbound = Math.ceil(totalimages/5);	
	$.fn.imageListing();
	
	$(".thumbs a:eq(0)").addClass("selected");
	
	$(".thumbs a").click(function(){
		imageindex = parseInt($(this).attr("hrefindex"));
		currentPage = Math.ceil(imageindex/5);		
		$(".thumbs a[class='selected']").removeClass("selected");
		$(this).addClass("selected");
		$.fn.updateImage($(this).attr("href"),$(this).attr("title"),$(this).attr("description"));	
		return false;			
	});
	
	$("#mycarousel").jcarousel({
		scroll: 1, visible: 1, initCallback: $.fn.mycarousel_initCallback, buttonNextHTML: null, buttonPrevHTML: null
	});	
});

$.fn.mycarousel_initCallback = function(carousel)
{	
	$(".pic-next, .pic-prev").click(function(){
		imageindex = ($(this).attr("direction") == "next") ? imageindex+1 : imageindex-1;
		currentPage = Math.ceil(imageindex/5);		
		var currentThumb = $(".thumbs a[hrefindex='"+imageindex+"']");		
		$(".thumbs a[class='selected']").removeClass("selected");			
		currentThumb.addClass("selected");		
		$.fn.updateImage(currentThumb.attr("href"),currentThumb.attr("title"),currentThumb.attr("description"));
		carousel.scroll(Math.ceil(imageindex/5));
	});	
		
	$(".play").click(function(){
		$(this).hide();
		$(".pause").show();
		playpause = setInterval(function(){
			imageindex = (imageindex == totalimages) ? 1 : imageindex + 1;
			currentPage = Math.ceil(imageindex/5);			
			var currentThumb = $(".thumbs a[hrefindex='"+imageindex+"']");			
			$(".thumbs a[class='selected']").removeClass("selected");	
			currentThumb.addClass("selected");					
			$.fn.updateImage(currentThumb.attr("href"),currentThumb.attr("title"),currentThumb.attr("description"));
			carousel.scroll(Math.ceil(imageindex/5));
		},playSpeed); 
	});	
		
	$(".pause").click(function(){
		$(this).hide();
		$(".play").show();
		clearInterval(playpause);
	});					

    $("#mycarousel-next").click(function(){
        carousel.next();
		currentPage = currentPage+1;
		$.fn.updateControls();
    });

    $("#mycarousel-prev").click(function(){
        carousel.prev();
		currentPage = currentPage-1;
		$.fn.updateControls();
    });
};
