/**
 * @author wes
 */

function prepGallery() {
	
	//Image gallery interactions
		$('#imageGallery #meds img').hide();
		$('#imageGallery #larges img').hide();
		$('#imageGallery #meds img.med_0').show();
		
		$('#imageGallery .nextImgArrow').css('cursor','pointer');
		$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(0); });
		$('#imageGallery img.nextImgArrow').bind("mouseover", function() { $('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_rd.gif'}); });
		$('#imageGallery img.nextImgArrow').bind("mouseout", function() { $('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_wt.gif'}); });
		var caption = $('#imageGallery img.med_0').attr("alt");
		
		$('#imageGallery #caption p').text(caption);
		
		//enlarge button on gallery
		$('#enlarge').css('cursor','pointer');
		$('#enlarge').bind("click", function() { enlargeImage(0); });
		$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'img/icn_enlarge_rd.gif'}); });
		$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'img/icn_enlarge_wt.gif'}); });
		
		//grid image gallery control
		$('#imageGallery #thumbs img.thumb').hover(function(event) {
			$('#imageGallery #thumbs img.thumb').clearQueue();
			$('#imageGallery #gridCaption p').clearQueue();
			
			var copy = this.alt;
			$('#imageGallery #gridCaption p').show();
			$('#imageGallery #gridCaption p').animate({'color':'#000'},200, function () {
				$('#imageGallery #gridCaption p').text(copy);
				$('#imageGallery #gridCaption p').animate({'color':'#fff'},200);
			});
			
			$('#imageGallery #thumbs img.thumb').fadeTo(200,0.4);
			$(this).stop().fadeTo(200,1.0);
			
			//show red bar
			var thumbLeft = $(this).offset().left;
			var thumbTop = $(this).offset().top;
			$('#redHooverBar').css({position:'absolute',top:thumbTop+82,left:thumbLeft}).show();
			
		}, function(event) {
			$('#imageGallery #thumbs img.thumb').pause(300).fadeTo("fast",1);
			$('#redHooverBar').hide();
			$('#imageGallery #gridCaption p').animate({'color':'#000'},200);
		});
		
		$('#imageGallery #thumbs img.thumb').click(function(event){
			var copy = this.alt;
			var href = this.parentNode.href;
			var id = href.substr(href.indexOf('#')+1);
			enlargeGridImage(id,copy);
		});
		
		//toggle between linear and grid
		$('#linearIcn').css('cursor','pointer');
		$('#linearIcn').click(function() {
			$('#imageGallery #gridCaption').slideUp(300);
			$('#imageGallery #thumbs').SlideOutUp(400, function() {
				$('#imageGallery img.nextImgArrow').unbind();
				
				$('#imageGallery #meds img').hide();
				$('#imageGallery #larges img').hide();
				$('#imageGallery #meds img.med_0').show();
				
				$('#imageGallery img.prevImgArrow').unbind().css('cursor','default').attr({src:'img/btn_arrow_lft_gry.gif'});
				$('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_wt.gif'}).bind("click", function() { nextImage(0,"foo",false); });
				$('#imageGallery img.nextImgArrow').bind("mouseover", function() { $('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_rd.gif'}); });
				$('#imageGallery img.nextImgArrow').bind("mouseout", function() { $('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_wt.gif'}); });

				var caption = $('#imageGallery img.med_0').attr("alt");
				$('#imageGallery #caption p').text(caption);
				
				$('#imgNo .imgOf').empty().text("1");
				$('#toggles').show();
				$('#enlarge').show().unbind().bind("click", function() { enlargeImage(0); });
				$('#enlarge img').attr({src:'img/icn_enlarge_wt.gif'});
				$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'img/icn_enlarge_rd.gif'}); });
				$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'img/icn_enlarge_wt.gif'}); });
				
				$('#imageGallery #meds').SlideInUp(400);
				$('#imageGallery #caption').slideDown(300);
			});
		});
		$('#linearIcn').bind("mouseover", function() { $('#linearIcn').attr({src:'img/gallery/icn_linear_rd.gif'}); });
		$('#linearIcn').bind("mouseout", function() { $('#linearIcn').attr({src:'img/gallery/icn_linear_wht.gif'}); });
		
		$('#gridIcn').css('cursor','pointer');
		$('#gridIcn').click(function() {
			$('#imageGallery #caption').slideUp(300);
			$('#imageGallery #meds').SlideOutUp(400, function() {
				$('#toggles').hide();
				$('#imageGallery #thumbs').SlideInUp(400);
				$('#imageGallery #gridCaption').slideDown(300);
			});
		});
		$('#gridIcn').bind("mouseover", function() { $('#gridIcn').attr({src:'img/gallery/icn_grid_rd.gif'}); });
		$('#gridIcn').bind("mouseout", function() { $('#gridIcn').attr({src:'img/gallery/icn_grid_wht.gif'}); });
		
		$('a.bookThumb img').bind("mouseover", function(data) {
			//alert("test"+$(this).height());
			//show red bar
			var thumbLeft = $(this).offset().left;
			var thumbTop = $(this).offset().top;
			var thumbHeight = $(this).height();
			var thumbWidth = $(this).width();
			$('#redHooverBar').css({position:'absolute',top:thumbTop+thumbHeight-3,left:thumbLeft,width:thumbWidth}).show();
		});
		$('a.bookThumb img').bind("mouseout", function(data) {
			$('#redHooverBar').hide();
		});
}

//
//Image gallery, linear navigation
//
function prevImage(n,type,grid) {
	$('#imageGallery .prevImgArrow').unbind();
	$('#imageGallery .nextImgArrow').unbind();
	$('#enlarge').unbind();
	$(document).unbind();
	
		$('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_wt.gif'});
		$('#imageGallery .nextImgArrow').bind("mouseover", function() { $('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_rd.gif'}); });
		$('#imageGallery .nextImgArrow').bind("mouseout", function() { $('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_wt.gif'}); });

		n--;
		if(n > 0) {
			$('#imageGallery img.prevImgArrow').css('cursor','pointer');
			if(type == "large") {
				$('#imageGallery .prevImgArrow').bind("click", function() { prevImage(n,"large",grid); });
				
			} else {
				$('#imageGallery .prevImgArrow').bind("click", function() { prevImage(n); });
			}
			$('#imageGallery .prevImgArrow').bind("mouseover", function() { $('#imageGallery img.prevImgArrow').attr({src:'img/btn_arrow_lft_rd.gif'}); });
			$('#imageGallery .prevImgArrow').bind("mouseout", function() { $('#imageGallery img.prevImgArrow').attr({src:'img/btn_arrow_lft_wt.gif'}); });
		} else {
			//swap its source with the grey arrow
			$('#imageGallery .prevImgArrow').attr({src:'img/btn_arrow_lft_gry.gif'});
			$('#imageGallery .prevImgArrow').css('cursor','default');
		}
		
		var nextNum = n+1;
		$('#imgNo .imgOf').empty().text(""+nextNum+"");
		
		$('#imageGallery .nextImgArrow').css('cursor','pointer');
		
		//determine which image is currently active in list
		if(type == "large") {
			var activeImg = $('#imageGallery img.large:visible');
			$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n,"large",grid); });
			if(grid) {
				$('#enlarge').bind("click", function() { closeGridEnlarge(n); });
			} else {
				$('#enlarge').bind("click", function() { closeEnlarge(n); });
			}
			
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'img/icn_close_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'img/icn_close_wt.gif'}); });
			
			activeImg.fadeOut(250, function() {
				$('#imageGallery img.lg_'+n).fadeIn(250);
			});
		} else {
			var activeImg = $('#imageGallery img.med:visible');
			$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n); });
			$('#enlarge').bind("click", function() { enlargeImage(n); });
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'img/icn_enlarge_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'img/icn_enlarge_wt.gif'}); });
			
			activeImg.fadeOut(300, function() {
				$('#imageGallery img.med_'+n).fadeIn(300);
			});
		}
		
		activeImg.fadeOut(250, function() {
			activeImg.parent().prev().chid().fadeIn(250);
		});
		
		$('#imageGallery #caption p').animate({'color':'#000'},250,function() {
			var caption = "";
			caption = $('#imageGallery img.med_'+n).attr("alt");
			if(caption!=undefined) {
				$('#imageGallery #caption p').text(caption);
				$('#imageGallery #caption p').animate({'color':'#fff'},250);
			}
		});
}

//
//go to right image
//
function nextImage(n,type,grid) {
	$('#imageGallery .prevImgArrow').unbind();
	$('#imageGallery .nextImgArrow').unbind();
	$('#enlarge').unbind();
	$(document).unbind();
		
		//if it was 1, change the prev arrow from gray to white
		if(n==0) {
			$('#imageGallery img.prevImgArrow').attr({src:'img/btn_arrow_lft_wt.gif'});
		} 
		n++;
		$('#imageGallery img.prevImgArrow').bind("mouseover", function() { $('#imageGallery img.prevImgArrow').attr({src:'img/btn_arrow_lft_rd.gif'}); });
		$('#imageGallery img.prevImgArrow').bind("mouseout", function() { $('#imageGallery img.prevImgArrow').attr({src:'img/btn_arrow_lft_wt.gif'}); });
		
		if(n+1 == $('#imgNo .imgTotal').text()) {
			$('#imageGallery .nextImgArrow').css('cursor','default');
			$('#imageGallery .nextImgArrow').attr({src:'img/btn_arrow_rt_gry.gif'});
			
		} else {
			$('#imageGallery .nextImgArrow').css('cursor','pointer');
			if(type == "large") {
				$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n,"large",grid); });
				
			} else {
				$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n); });
			}
			$('#imageGallery .nextImgArrow').bind("mouseover", function() { $('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_rd.gif'}); });
			$('#imageGallery .nextImgArrow').bind("mouseout", function() { $('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_wt.gif'}); });
		}
		var nextNum = n+1;
		$('#imgNo .imgOf').empty().text(""+nextNum+"");
		
		$('#imageGallery .prevImgArrow').css('cursor','pointer');
		
		if(type == "large") {
			var activeImg = $('#imageGallery img.large:visible');
			$('#imageGallery .prevImgArrow').bind("click", function() { prevImage(n,"large",grid); });
			if(grid) {
				$('#enlarge').bind("click", function() { closeGridEnlarge(n); });
			} else {
				$('#enlarge').bind("click", function() { closeEnlarge(n); });
			}
			
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'img/icn_close_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'img/icn_close_wt.gif'}); });
			
			activeImg.fadeOut(300, function() {
				$('#imageGallery img.lg_'+n).fadeIn(300);
			});
			
		} else {
			var activeImg = $('#imageGallery img.med:visible');
			$('#imageGallery .prevImgArrow').bind("click", function() { prevImage(n); });
			$('#enlarge').bind("click", function() { enlargeImage(n); });
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'img/icn_enlarge_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'img/icn_enlarge_wt.gif'}); });
			
			activeImg.fadeOut(250, function() {
				$('#imageGallery img.med_'+n).fadeIn(250);
			});
		}
		
		$('#imageGallery #caption p').animate({'color':'#000'},250,function() {
			var caption = "";
			caption = $('#imageGallery img.med_'+n).attr("alt");
			if(caption!=undefined) {
				$('#imageGallery #caption p').text(caption);
				$('#imageGallery #caption p').animate({'color':'#fff'},250);
			}
		});
}
//from med linear image to large
function enlargeImage(n) {
	
	$('#imageGallery #meds img.med_'+n).SlideOutUp(500, function() {
		$('#imageGallery .prevImgArrow').unbind();
		$('#imageGallery .nextImgArrow').unbind();
		$('#enlarge').unbind();
		
		$('#imageGallery #caption').slideUp(250, function() {
			if(n > 0) {
				$('#imageGallery img.prevImgArrow').css('cursor','pointer');
				$('#imageGallery img.prevImgArrow').bind("click", function() { prevImage(n,"large"); });
			} 
			if(n < $('#imgNo .imgTotal').text()) {
				$('#imageGallery .nextImgArrow').css('cursor','pointer');
				$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n,"large"); });
			}
			$('#toggles').hide(10);
			//replace enlarge button with X and bind it to closeEnlarge
			$('#enlarge img').attr({src:'img/icn_close_wt.gif'});
			$('#enlarge').bind("click", function() { closeEnlarge(n); });
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'img/icn_close_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'img/icn_close_wt.gif'}); });
		});
		
		$('#imageGallery #larges img.lg_'+n).SlideInUp(550, function() {
			$('#imageGallery #caption').slideDown(350);
		});
	});
}

//from grid thumb image to large image
function enlargeGridImage(n,copy) {
	$('#enlarge').unbind();
	$('#imageGallery .prevImgArrow').unbind();
	$('#imageGallery .nextImgArrow').unbind();
	
	$('#redHooverBar').fadeOut(150,function() {
		
		$('#imageGallery #gridCaption p').fadeOut(150, function() {
		
			$('#imageGallery #thumbs').slideUp(400, function() {
				$('#imageGallery #gridCaption').slideUp(200);
				if(n > 0) {
					$('#imageGallery img.prevImgArrow').css('cursor','pointer').attr({src:'img/btn_arrow_lft_wt.gif'}).bind("click", function() { prevImage(n,"large",true); });
					$('#imageGallery img.prevImgArrow').bind("mouseover", function() { $('#imageGallery img.prevImgArrow').attr({src:'img/btn_arrow_lft_rd.gif'}); });
					$('#imageGallery img.prevImgArrow').bind("mouseout", function() { $('#imageGallery img.prevImgArrow').attr({src:'img/btn_arrow_lft_wt.gif'}); });
				} 
				if(parseInt(n) < parseInt($('#imgNo .imgTotal').text())) {
					$('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_wt.gif'});
					$('#imageGallery .nextImgArrow').css('cursor','pointer').bind("click", function() { nextImage(n,"large",true); });
				} else {
					$('#imageGallery .nextImgArrow').css('cursor','default');
					$('#imageGallery img.nextImgArrow').attr({src:'img/btn_arrow_rt_gry.gif'});
				}
				
				//replace enlarge button with X and bind it to closeEnlarge
				$('#enlarge img').attr({src:'img/icn_close_wt.gif'});
				$('#enlarge').show().bind("click", function() { closeGridEnlarge(n); });
				$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'img/icn_close_rd.gif'}); });
				$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'img/icn_close_wt.gif'}); });
				
				$('#toggles').hide();
				$('#grid').hide();
				
				$('#imageGallery #caption p').empty().text(copy);
				var num = parseInt(n)+1;
				$('#imgNo .imgOf').empty().text(""+num+"");
				
				$('#imageGallery #larges img.lg_'+n).SlideInUp(550, function() {
					$('#imageGallery #caption').slideDown(350);
				});
			});
		});
	});	
}

function closeEnlarge(n) {
	
	$('#imageGallery #larges img.lg_'+n).SlideOutUp(500, function() {
		$('#imageGallery #caption').slideUp(200, function() {
			
			$('#imageGallery .prevImgArrow').unbind();
			$('#imageGallery .nextImgArrow').unbind();
			$('#enlarge').unbind();
			
			if(n > 0) {
				$('#imageGallery img.prevImgArrow').css('cursor','pointer');
				$('#imageGallery img.prevImgArrow').bind("click", function() { prevImage(n); });
			} 
			if(n < $('#imgNo .imgTotal').text()) {
				$('#imageGallery .nextImgArrow').css('cursor','pointer');
				$('#imageGallery .nextImgArrow').bind("click", function() { nextImage(n); });
			}
			$('#toggles').show();
			//replace enlarge button with X and bind it to closeEnlarge
			$('#enlarge img').attr({src:'img/icn_enlarge_wt.gif'});
			$('#enlarge').bind("click", function() { enlargeImage(n); });
			$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'img/icn_enlarge_rd.gif'}); });
			$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'img/icn_enlarge_wt.gif'}); });
			
			$('#imageGallery #meds img.med_'+n).SlideInUp(550, function() {
				$('#imageGallery #caption').slideDown(350);
			});
		});
	});
}

function closeGridEnlarge(n) {
	$('#enlarge').unbind();
	
	$('#imageGallery #larges img.lg_'+n).SlideOutUp(550, function() {
		$('#imageGallery #caption').slideUp(350);
		
		//replace enlarge button with X and bind it to closeEnlarge
		
		$('#enlarge').hide();
		$('#grid').show();
		
		$('#imageGallery #thumbs').SlideInUp(550, function() {
			$('#imageGallery #gridCaption').slideDown(350);
		});
	});
}