var galleryPhotoTimer, galleryPhotoFx;

/*function gallerySwitch(thumbDiv,imgDiv) {
	var imgObj = imgDiv.find(".galleryImage img");
	var imgSrc = thumbDiv.attr("rel");
	var imgTxt = thumbDiv.attr("title");
	
	imgDiv.find(".galleryCaption").html(imgTxt);
	imgObj.attr("src",imgSrc);
}*/

function photoGalleryInit(galleryID,fxID) {
	var animOpac  = 1;
	galleryPhotoFx = fxID;
	
	$("#"+galleryID+" .banner-controls").css({"display":"block"});
																			  
	$("#"+galleryID+" .banner-controls img").hover(function(){
		//if (IS_LTIE6) {
		//	$(this).css({'opacity':'1'});
		//} else {
			$(this).animate({
				opacity:1
			},100,function(){});
		//}
	},function(){
		if (!$(this).parent().parent().hasClass("active")) {
			//if (IS_LTIE6) {
			//	$(this).css({'opacity':animOpac});
			//} else {
				$(this).animate({
					opacity:animOpac
				},100,function(){});
			//}
		}
	});

	$("#"+galleryID+" .banner-controls a").click(function(){
		galleryPhotoSwitch($(this));
	});
	
	$("#"+galleryID+" .banner-slides ul").html("");
	
	/*photoGalleryClear();
	//$("a[rel^='photoGallery']:first").addClass("active").find("img").css({opacity:1});
	$(".galleryImage img.holder").css({opacity:0});
	$(".banner-controls a:first").addClass("active").find("img").css({opacity:1});
	galleryPhotoSwitch($(".banner-controls a:first"));
	//$(".banner-controls a").click(function(){ gallerySwitch($(this),$(".galleryMain")) });*/
	galleryLoadImages(galleryID);
	galleryInitialise(galleryID);
}

function galleryInitialise(galleryID) {
	var theGallery = $("#"+galleryID);
	//theGallery.find(".banner-controls li").find("img").css({opacity:0.6});
	theGallery.find(".banner-controls li:first").addClass('active').find("img").css({opacity:1});
	theGallery.find(".banner-slides li:first").addClass('active').css({opacity:1});
	theGallery.find(".banner-controls li:last").addClass('last');
	
	galleryPhotoStartTimer();
}

function photoGalleryClear() {
	var thisImg;
	var animOpac  = 1;
	$(".banner-controls a").each(function() {
		$(this).removeAttr("href").removeClass("active");
		thisImg = $(this).find("img");
		thisImg.css({opacity:animOpac});
	});
}

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(".banner-controls li");
	var theSlides = theGallery.find(".banner-slides ul");
	
	var thumbOBJ, thumbID, thumbRel;
	
	theThumbs.each( function(index){
		thumbOBJ = $(this).find("a");
		
		thumbID  = thumbOBJ.attr("href");
			thumbID = thumbID.substring(1);
		thumbRel = thumbOBJ.attr("rel");
		thumbCap = thumbOBJ.attr("title");
		thumbUrl = thumbOBJ.attr("value");
		
		if (thumbCap.length > 0) {
			//thumbCap = '<div class="galleryCaption">'+thumbCap+'</div>';
		}
			
		theSlides.append('<li id="'+thumbID+'" style="position:absolute;left:0px;z-index:1;opacity:0;background-image:url('+thumbRel+');"><a href="'+thumbUrl+'"><img src="images/blank.gif" alt="'+thumbCap+'" /></a></li>');
		
		thumbOBJ.removeAttr("href");
		thumbOBJ.attr("rel",thumbID);
	} );
}

function galleryPhotoSwitch(thisObj) {
	var ImageTxt, ImageSrc
	ImageTxt	= thisObj.attr("title");
	ImageSrc = thisObj.attr("rel");

	var animSpeed;
	var animOpac  = 1;
	var ImageOBJ  = $(".banner-slides ul");
	//var ImageCAP  = $(".galleryCaption");
	
	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");
		//currImage.css({'position':'absolute','zIndex':'2'});
		nextImage = ImageOBJ.find("#"+ImageSrc);
		
		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");
					//ImageCAP.html(ImageTxt);
				} 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});
								//ImageCAP.html(ImageTxt);
						});
					});
				}
				break;
			}

		/*photoGalleryClear();
		
		//nextImage.css({'display':'block'});
		var posCenter = ($(".galleryImage").width()/2) - ((nextImage.width())/2);
		
		var holderHeight, holderWidth;
		if (nextImage.width() > currImage.width()) {
			holderWidth = nextImage.width();
		} else {
			holderWidth = currImage.width();
		}
		if (nextImage.height() > currImage.height()) {
			holderHeight = nextImage.height();
		} else {
			holderHeight = currImage.height();
		}
		$(".galleryMain img.holder").attr("src","images/blank.gif").css({
			'width':holderWidth,
			'height':holderHeight});
		//}, 200, function() {
			
			
		//});
		*/
	}
	
	galleryPhotoStartTimer();
}


