// Gallery Variables
var galleryInterval;
var galleryCurrent = 1;
var galleryTotal;
var galleryDelay = 9000;

var itemWidth = 322;
var imageWidth = 590;
//swing, linear, Quad, Quart, Cubic, Quint, Sine, Expo, Circ  
var animType = "Quad";

$(document).ready(function(){
	initGallery();
});



function initGallery(){
	//hide all items from gallery except first item
	$(".galleryitem:not(#galleryitem1)").hide();
	$(".galleryimage:not(#galleryimage1)").hide();

	galleryTotal = $(".galleryitem").length;
	
	enableGalleryThumbs();
	
	galleryInterval = setTimeout(gotoNext, galleryDelay);
}

function gotoGalleryItem(item){
	clearTimeout(galleryInterval);
	disableGalleryThumbs();
	//create variables for gallery assets
	var nextitem = $("#galleryitem"+item);
	var nextimage = $("#galleryimage"+item);
	var nextthumb = $("#gallerythumb"+item);
	
	//settup next item by bring to front and setting its location
	$("#galleryitems").append(nextitem);
	$("#galleryimages").append(nextimage);
	
	$(nextitem).animate({opacity:0}, 0);
	$(nextitem).show();
	
	$(nextimage).animate({left: imageWidth, opacity: 0}, 0);
	$(nextimage).show();
	
	///animate galleryitems
	$("#galleryitem"+galleryCurrent).animate({opacity: 0}, 500, "easeOut"+animType, function() {
	});
	$(nextitem).animate({opacity:1}, 1000, "easeOut"+animType, function(){
		$(this).css('filter','');
		Cufon.refresh();
	});
	
	///animate galleryimage
	$(nextimage).animate({left: 0, opacity: 1}, 1000, "easeOut"+animType, enableGalleryThumbs);
	
	
	//reset current class
	$("#gallerythumb"+galleryCurrent).removeClass("current");
	$(nextthumb).addClass("current");
	//set current to new item
	
	galleryCurrent = item;
	
	//enableGalleryThumbs();
	
	galleryInterval = setTimeout(gotoNext, galleryDelay);
}


function gotoNext(){
	var next = galleryCurrent + 1;
	if(next > galleryTotal)
		next = 1;
	
	gotoGalleryItem(next);
}

function clickThumb(){
	var id = parseInt($(this).attr("id").replace("gallerythumb", ""));
	if(id != galleryCurrent)
		gotoGalleryItem(id);
	
}


function disableGalleryThumbs(){
	$(".gallerythumb").unbind('click');	
}

function enableGalleryThumbs(){
	$(".gallerythumb").bind('click', clickThumb);	
}

