$(document).ready(function(){
	photoGalleryInit("photoGallery");
	photoGalleryInit("productFullGallery");
});

/*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) {
	
	$("#"+galleryID+" .galleryThumbs 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':'0.6'});
			//} else {
				$(this).animate({
					opacity:0.6
				},100,function(){});
			//}
		}
	});
	
	$("#"+galleryID+" .galleryThumbs a").click(function(){
		galleryPhotoSwitch($(this));
	});
	
	$("#"+galleryID+" .galleryImages ul").html("");
	
	/*photoGalleryClear();
	//$("a[rel^='photoGallery']:first").addClass("active").find("img").css({opacity:1});
	$(".galleryImage img.holder").css({opacity:0});
	$(".galleryThumbs a:first").addClass("active").find("img").css({opacity:1});
	galleryPhotoSwitch($(".galleryThumbs a:first"));
	//$(".galleryThumbs a").click(function(){ gallerySwitch($(this),$(".galleryMain")) });*/
	galleryLoadImages(galleryID);
	galleryInitialise(galleryID);
}

function galleryInitialise(galleryID) {
	var theGallery = $("#"+galleryID);
	theGallery.find(".galleryThumbs li").find("img").css({opacity:0.6});
	theGallery.find(".galleryThumbs li:first").addClass('active').find("img").css({opacity:1});
	theGallery.find(".galleryImages li:first").addClass('active').css({opacity:1});
}

function photoGalleryClear() {
	var opac, thisImg;
	$(".galleryThumbs a").each(function() {
		$(this).removeAttr("href").removeClass("active");
		thisImg = $(this).find("img");
		opac = thisImg.attr('rel');
		if (opac=="" || opac==null) opac = 0.6;
		thisImg.css({opacity:opac});
	});
}

function galleryLoadImages(galleryID) {
	var theGallery = $("#"+galleryID);
	var theThumbs = theGallery.find(".galleryThumbs li");
	var theSlides = theGallery.find(".galleryImages 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");
		
		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;">'+thumbCap+'<img src="'+thumbRel+'" alt="" />');
		
		thumbOBJ.removeAttr("href");
		thumbOBJ.attr("rel",thumbID);
	} );
}

function galleryPhotoSwitch(thisObj) {
	var ImageTxt, ImageSrc
	ImageTxt	= thisObj.attr("title");
	ImageSrc = thisObj.attr("rel");

	var animSpeed = 300;
	var ImageOBJ  = $(".galleryImages 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:0.6});
		
		thisObj.parent().addClass("active").find("img").css({opacity:1});
		
		currImage = ImageOBJ.find("li.active");
		//currImage.css({'position':'absolute','zIndex':'2'});
		
		nextImage = ImageOBJ.find("#"+ImageSrc);
		
		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);
				});
			});
		}
		
		/*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() {
			
			
		//});
		*/
	}
	
}

