var galleryPhotoTimer, galleryPhotoFx;

function photoGalleryInit(galleryID,fxID) {
	var animOpac  = 1;
	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+" .banner-controls a").click(function(){
		galleryPhotoSwitch($(this));
	});
	
	$("#"+galleryID+" .banner-slides ul").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});
	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("name");
		
		if (thumbCap.length > 0) {
		}
			
		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 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 = 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;
			}
	}
	
	galleryPhotoStartTimer();
}
