var firstID;
var nextID;
var timerID;
var nowID = 0;
var oldID;
var panelNum = 0;
var xTotalCount = 0,xTotalPages = 0,xCurrentPage = 0,fid;
var olderID=0;
var isLast = false;
var pageSize = 9;
var galleryWidth = 970;
var galleryHeight = 400;
var window_loaded = false;
var imageCount = 0;
var complete = 0;
$(function(){
	firstID = $(".current").children(".img_wrap").attr("id");
	nextID = $(".current").next().children(".img_wrap").attr("id");
	
	$(".strip_wrapper").append("<div class='loader2'></div>");
	$(".frame").fadeIn("slow",function(){
		$(".loader2").remove();/*!import! Do not delete!! */
	});
	$(".panel").each(function(){
		xTotalCount++;
	});
	$(".panel").each(function(){
		imageCount++;	  	
		if( imageCount == xTotalCount )window_loaded = true;
	});
	if(window_loaded)
	{
		$(".panel").eq(0).fadeIn(1000);
		if($(".panel").eq(0).find("img").attr("complete")!=true)
		{
			$("#gallery").append("<div class='loader'></div>");
			$(".panel").eq(0).find("img").hide();
			$(".panel").eq(0).find("img").bind('load',function(){
				$(".panel").eq(0).fadeIn(1000,function(){
					$(".loader").remove();$(".panel").eq(0).find("img").fadeIn("fast");});
			});
		}
		else
		{
			$(".panel").eq(0).find("img").bind('load',function(){});
		}
	}
	timerID2 = setInterval("chkimgloadcomplete()",1000);
	xTotalPages = Math.ceil(xTotalCount/pageSize);
	if(xTotalCount>1)
	{
		$(".img_wrap").bind("click",showItem);
		$(".img_wrap").bind("mouseover",showItem2);
		$(".img_wrap").bind("mouseout",showItem3);
	}
	$(".next").click(showNextPage);
	$(".prev").click(showPrevPage);
	
});
function chkimgloadcomplete()
{
	if($(".panel").eq((xTotalCount-1)).find("img").attr("complete"))
	{
		if(xTotalCount>1)
			timerID = setTimeout("showNextItem()",5000);
		clearInterval(timerID2);
	}
}
function showItem()
{
	clearTimeout(timerID);
	oldID = $(".current").find("div").attr("id");
	nowID = $(this).attr("id");
	
	if(oldID!=nowID)
	{
		$(".current").removeClass("current");
		$(this).parent().addClass("current");
		
		$("#p"+oldID).fadeOut(1000);
		$("#p"+oldID).find('span').fadeOut(1000);
		$("#p"+nowID).find('span').fadeIn(1000);
		$("#p"+nowID).fadeIn(1000,function(){});
	}
	nextID = $(".current").next().children(".img_wrap").attr("id");
	timerID = setTimeout("showNextItem()",5000);
}
function showItem2()
{
	$(this).parent().addClass("overhere");
}
function showItem3()
{
	$(this).parent().removeClass("overhere");
}
function showNextItem()
{
	clearTimeout(timerID);
	if(nextID==null)
	{
		if(xTotalPages == 1)
		{
			nextID = firstID;
			timerID = setTimeout("showNextItem()",800);
		}
		else if(xCurrentPage < (xTotalPages-1))
		{
			showNextPage();
		}
		else
		{
			isLast = true;
			showPrevPage();
		}
	}
	else
	{
		oldID = $(".current").find("div").attr("id");
		$("#"+nextID).parent().addClass("current").siblings().removeClass("current");
		if(olderID==0)
		{
			$("#p"+oldID).find('span').fadeOut(1000);
			$("#p"+oldID).fadeOut(1000);
		}
		else
		{
			$("#p"+olderID).find('span').fadeOut(1000);
			$("#p"+olderID).fadeOut(1000);
		}
		$("#p"+nextID).find('span').fadeIn(1000);	
		$("#p"+nextID).fadeIn(1000,function(){
			nextID = $(".current").next().children(".img_wrap").attr("id");
		});
		olderID = 0;
		timerID = setTimeout("showNextItem()",5000);	
	}
}
function showNextPage()
{
	if(xCurrentPage < (xTotalPages-1) )
	{
		clearTimeout(timerID);
		xCurrentPage = parseInt(xCurrentPage)+1;
		olderID = $(".current").find("div").attr("id");
		$(".filmstrip").animate({left:"-="+galleryWidth},500,function(){showNextItem();})/*.css({"left":"-680"})*/;
		$(".current").removeClass("current");
		$(".container2").eq(parseInt(xCurrentPage)).find("li").eq(0).addClass("current");
		nextID = $(".container2").eq(parseInt(xCurrentPage)).children().find("div").attr("id");
	}
}
function showPrevPage()
{
	if(xCurrentPage <= (xTotalPages-1) && xCurrentPage!=0)
	{
		clearTimeout(timerID);
		if(isLast)
		{
			$(".filmstrip").css({"left":0});
			isLast = false;
			xCurrentPage = 0;
			olderID = $(".current").find("div").attr("id");		
			$(".current").removeClass("current");
			$(".container2").eq(parseInt(xCurrentPage)).find("li").eq(0).addClass("current");
			nextID = $(".container2").eq(parseInt(xCurrentPage)).children().find("div").attr("id");
			showNextItem();
		}
		else
		{
			if(!isLast)
			{
				xCurrentPage = parseInt(xCurrentPage)-1;
				galleryWidth = (galleryWidth * xCurrentPage == 0)?galleryWidth:galleryWidth * xCurrentPage;
				$(".filmstrip").animate({left:"+="+galleryWidth},500,function(){showNextItem();});
			}
			olderID = $(".current").find("div").attr("id");		
			$(".current").removeClass("current");
			$(".container2").eq(parseInt(xCurrentPage)).find("li").eq(0).addClass("current");
			nextID = $(".container2").eq(parseInt(xCurrentPage)).children().find("div").attr("id");
		}
	}
}
