var galleryPhotoTimer, galleryPhotoFx;

/*
var photoGallery = function (photos, list) {
	var thisGallery = this;
	this.photos = $(photos);
	this.list = $(list).empty();
	this.current = 0;
	
	this.photos.children().each(function (i) {
		var listItem = $('<li>'+ i +'</li>').click(function () {
			thisGallery.moveTo(i);
		});
		thisGallery.list.append(listItem);
		if (i > 0)
			$(this).hide();
	});
};
photoGallery.prototype.moveTo = function(index) {
	if (!index)
		index = (++this.current) * this.photos.children().length;
	if (this.current == index)
		return;
	
	$()
	
	this.current = index;
}*/

function photoGalleryInit(galleryID,fxID) {
	var animOpac  = 0.8;
	galleryPhotoFx = fxID;
	
	$("#"+galleryID+" .banner-controls").css({"display":"block"});

	$("#"+galleryID+" .banner-controls img").hover(function(){
			$(this).animate({
				opacity:1
			},100,function(){});
	},function(){
		if (!$(this).parent().parent().hasClass("active")) {
				$(this).animate({
					opacity:animOpac
				},100,function(){});
		}
	});

	$("#"+galleryID+" ul.banner-controls").html("");
	
	galleryLoadImages(galleryID);
	galleryInitialise(galleryID);
}

function galleryInitialise(galleryID) {
	var theGallery = $("#"+galleryID);
	theGallery.find(".banner-controls li:first").addClass('active').find("img").css({opacity:1});
	theGallery.find(".banner-slides li:first").addClass('active').css({opacity:1, zIndex: 2 });
	theGallery.find(".banner-controls li:last").addClass('last');
	
	galleryPhotoStartTimer();
}

function galleryPhotoNext() {
	var activeObj = $(".banner-controls li.active");
	var nextObj;
	
	if ( activeObj.hasClass("last") ) {
		nextObj = $(".banner-controls li:first").find("a");
	} else {
		nextObj = activeObj.next().find("a");
	}
	
	galleryPhotoSwitch(nextObj);
}

function galleryPhotoStartTimer() {
	galleryPhotoStopTimer();
	galleryPhotoTimer = setInterval("galleryPhotoNext()", 5000);
}

function galleryPhotoStopTimer() {
	clearInterval(galleryPhotoTimer);
}

function galleryLoadImages(galleryID) {
	var theGallery = $("#"+galleryID);
	var theThumbs = theGallery.find("ul.banner-controls");
	var theSlides = theGallery.find(".banner-slides li");
	
	var thumbOBJ, thumbID, thumbRel;
	
	theSlides.each( function(index){
		theThumbs.append('<li><a href="#'+ (index + 1) +'" class="hoverButtonImage transparent"><span>'+ (index + 1) +'</span><img src="images/banner_numbers.png" /></a></li>');
		
		$(theSlides[index]).css({ position: 'absolute', 'left': '0px', opacity: '0'});
		//theSlides.append('<li id="'+thumbID+'" style="position:absolute;left:0px;z-index:1;opacity:0;">'+thumbCap+'<img src="'+thumbRel+'" alt="" />');
	} );
	
	$("#"+galleryID+" .banner-controls a").click(function(){
		galleryPhotoSwitch($(this));
	});
}

function galleryPhotoSwitch(thisObj) {
	var ImageNum = parseInt(thisObj.parent().find('a span').text());
	
	var animSpeed;
	var animOpac  = 1;
	var ImageOBJ  = $(".banner-slides");
	
	var currImage, nextImage;
	
	if (ImageOBJ.find("li:animated").length == 0) {
		
		//clear active button
		thisObj.parent().parent().find("li.active").removeClass('active').find("img").css({opacity:animOpac});
		thisObj.parent().addClass("active").find("img").css({opacity:1});
		
		currImage = ImageOBJ.find("li.active");
		nextImage = $('#slide-'+ ImageNum, ImageOBJ);
		if (nextImage.hasClass('active')) return;
		
		switch (galleryPhotoFx) {
			case "crossFade" :
			// CROSS FADE
				animSpeed = 700;
					currImage.css({'z-index':2}).animate({
							opacity: 0
						}, animSpeed, function(){
							currImage.removeClass("active").css({'z-index':1});
					});
					nextImage.css({'z-index':1}).animate({
							opacity: 1
						}, animSpeed, function(){
							nextImage.addClass("active").css({'z-index':2});
					});
				break;
			case "fade" :
			// FADE
				animSpeed = 500;
					nextImage.css({'z-index':1,'opacity':1}).addClass("active");
					currImage.css({'z-index':2}).animate({
							opacity: 0
						}, animSpeed, function(){
							currImage.removeClass("active").css({'z-index':1});
							nextImage.addClass("active").css({'z-index':2});
					});
				break;
			case "fadeInOut" :
			default :
			// FADE OUT / IN
				animSpeed = 300;
				if (IS_LTIE6) {
					currImage.css({'opacity':'0','z-index':1}).removeClass("active");
					nextImage.css({'opacity':'1','z-index':2}).addClass("active");
				} else {
					currImage.animate({
							opacity: 0
						}, animSpeed, function(){
							currImage.removeClass("active").css({'z-index':1});
						nextImage.animate({
								opacity: 1
							}, animSpeed, function(){
								nextImage.addClass("active").css({'z-index':2});
						});
					});
				}
				break;
			}
	}
	
	galleryPhotoStartTimer();
}
