//jquery scripts
$(function() {
	$absPath = $('body').attr('id'); $('#content').hide().fadeIn('slow');
	if ( $.browser.msie ) { $.ifixpng('../images/pixel.gif'); /*$('body *').addClass('bgPng');*/ $('img,.bgPng').ifixpng(); }
	$hash = location.hash.split("#")[1];
	
	/* logo hover
	$('#logo').hover( function(){ $('#logo_hover').fadeIn(); });
	$('#logo_hover').hover( function(){ }, function(){ $('#logo_hover').fadeOut(); }); */
	
	//bluelight media credits
	$('body').append('<a id="bluelightmedia" href="http://www.inbluelight.com" target="_blank"><img src="http://www.inbluelight.com/bluelightmedia.png"/></a></div>'); $('#bluelightmedia').css({'margin':'0 3px', 'position':'fixed', 'bottom':0, 'left':0, 'z-index':9999}).fadeTo("normal", 0.3); $('#bluelightmedia').hover( function(){ $(this).fadeTo("normal", 1); }, function(){ $(this).fadeTo("normal", 0.3); });	
	
	//homepage slideshow
	if($('#intro_slideshow')){ $numIntroImgs = $('#intro_slideshow li').size(); $('#intro_slideshow li').hide(); $curIntroImg = -1; $.nextIntroImg(); }
		
	//nav behaviour (fades in on homepage)
	if($('#nav a.active').length < 1){ $('#nav').hide().fadeIn('slow');	}
	
	//dropdown nav
	if($(".category_list")){
		$(".category_list ul").hide(); $(".category_list a:first").attr('href','#');
		$(".category_list").hover(function(){ $(this).find("ul").slideDown(); $(this).addClass("ieHover"); }, function(){ $(this).find("ul").slideUp().removeClass("ieHover"); });
	}
	
	//pictures pages
	if($('#pictures')){
		$numPics = $('#pictures li').size();
		$picInfoOpen = true; $picInfoBtnTxt = 'pic info'; $picInfoBtnHideTxt = 'hide';
		$infoOpen = true; $infoBtnTxt = 'info'; $infoBtnHideTxt = 'hide';
		if($numPics > 0){
			$("#pictures a").fadeTo("normal", 0.2);
			$('#pictures li:last').css('margin-right','0px');
			$("#pictures a").each(function(){ $(this).attr('href','#'+$(this).attr('href')); });
			if(!$hash){ $hash = $('#pictures a:first').attr('href').substring(1); location.hash = $hash; }
			$.loadNewImg();
			$.showImgNavBtns();
			
			//click a thumbnail
			$("#pictures a").click(function(){ 
				$hash = $(this).attr('href').substring(1); location.hash = $hash;
				$.loadNewImg(); 
			});
			
			//hover a thumbnail
			$("#pictures a:not(.active)").live('mouseover',function(){ $(this).fadeTo("normal", 1); });
			$("#pictures a:not(.active)").live('mouseout',function(){ $(this).fadeTo("normal", 0.2); });
			
			//set up the thumbnail container
			$imgWidth = $("#pictures li:first").outerWidth(true);
			$imgMarginRight = parseFloat($("#pictures li:first").css('marginRight'));
			$containerWidth = $("#content").outerWidth();		
			$picsPerPage = Math.floor($containerWidth / $imgWidth);
			$picsWidth = $imgWidth*$numPics-$imgMarginRight; $('#pictures').css({ width:$picsWidth });
			$viewableWidth = $imgWidth*$picsPerPage-$imgMarginRight;
			if ($picsWidth < $viewableWidth){ $viewableWidth = $picsWidth; } $('#viewable').css({ width:$viewableWidth });
			$.showThumbsNavBtns();
			
			//click the thumbnail container's next/prev buttons
			$('#thumbs_next').click(function(){ $.navigateThumbContainer('next'); });
			$('#thumbs_prev').click(function(){ $.navigateThumbContainer('prev'); });
			
			//click the next/prev image buttons OR left/right arrow buttons on keyboard
			$('#img_next').click(function(){ $.imgNavBtnClick("next"); });
			$('#img_prev').click(function(){ $.imgNavBtnClick("prev"); });
			$(document).keyup(function(e){ if(e.keyCode == 37){ $.imgNavBtnClick("prev"); }else if(e.keyCode == 39){ $.imgNavBtnClick("next"); }});
		}
		
		//click the pic info or gallery info hide/show button
		$("#picInfoBtn").live('click',function(){ if ($picInfoOpen == false){ $picInfoOpen = true; $('#picInfo').slideToggle(); $(this).html('<a>hide</a>'); }else{ $picInfoOpen = false; $('#picInfo').slideToggle(); $(this).html('<a>'+$picInfoBtnTxt+'</a>');	} });
		$("#infoBtn").live('click',function(){ if ($infoOpen == false){ $infoOpen = true; $('#pictures_text_area').slideToggle(); $(this).html('<a>hide</a>');}else{ $infoOpen = false; $('#pictures_text_area').slideToggle(); $(this).html('<a>'+$infoBtnTxt+'</a>'); } });
	}	
	
	//slides in the name of the gallery or category
	$('#galleries li a span, #categories li a span, #subcategories li a span').hide();
	$('#galleries li, #categories li, #subcategories li').hover(function(){ $(this).find('span').slideDown('fast'); }, function() { $(this).find('span').slideUp('fast'); });	
	
	resizeWindow();
	$(window).bind("resize", resizeWindow);
});
	
// FUNCTIONS	
$.loadNewImg = function(){
	$("#pictures a.active").fadeTo("normal", 0.2);
	$("#pictures a").removeClass('active');
	$("#picContainer").html('<span id="loader"></span><img src="'+$hash+'" style="display:none;"/>');
	$curPic = $('#pictures a').index($('#pictures a[href="#'+$hash+'"]'));
	$('#pictures a:eq('+$curPic+')').addClass('active');
	$("#pictures a.active").fadeTo("normal", 1);
	$picInfo = $('#pictures a:eq('+$curPic+')').attr('title'); $("#picInfo").html($picInfo);
	$("#picContainer img").load(function(){ $("#loader").hide(); $(this).fadeIn('slow'); $.checkExif(); });
	$.showImgNavBtns();
}

$.nextIntroImg = function(){
	if ($curIntroImg+1 < $numIntroImgs){ 
		$curIntroImg += 1; 
		$('#intro_slideshow li:eq('+($curIntroImg-1)+')').fadeOut('slow');
	} else { 
		$curIntroImg = 0; 
		$('#intro_slideshow li:eq('+($numIntroImgs-1)+')').fadeOut('slow');
	}
	$('#intro_slideshow li:eq('+$curIntroImg+')').fadeIn('slow', function(){ 
		$(this).animate({ 'opacity':1}, 3000, function(){
			$.nextIntroImg();
		});
	});
}
			
$.navigateThumbContainer = function(direction){
	if(direction == 'next'){
		$('#thumbs_prev').fadeIn();
		if ($curPage+1 < $numPages){ $curPage += 1; }else{ $curPage = 0; }
		if ($curPage == $numPages-1){ $('#thumbs_next').fadeOut(); }else{ $('#thumbs_next').fadeIn(); }
	}else{
		$('#thumbs_next').fadeIn();
		if ($curPage-1 >= 0){ $curPage -= 1; }else{ $curPage = $numPages-1; }
		if ($curPage == 0){ $('#thumbs_prev').fadeOut(); }else{ $('#thumbs_prev').fadeIn(); }
	}
	if($curPage>0){ $('#pictures').animate({ left:-($curPage*$viewableWidth+($imgMarginRight*$curPage))+'px' }); }
	else{ $('#pictures').animate({ left:-($curPage*$viewableWidth)+'px' }); }
}

$.showThumbsNavBtns = function(){
	$numPages = Math.ceil($numPics/$picsPerPage); 
	$curPage = Math.floor(($curPic)/$picsPerPage);
	if($curPage>0){ $('#pictures').animate({ left:-($curPage*$viewableWidth+($imgMarginRight*$curPage))+'px' }); }
	else{ $('#pictures').animate({ left:-($curPage*$viewableWidth)+'px' }); }
	
	$('#thumbs_next, #thumbs_prev').hide();
	if ($numPages > 1){
		if ($curPage == 0){ 
			$('#thumbs_next').show(); 
		} else if ($curPage > 0 && $curPage != ($numPages-1)){ 
			$('#thumbs_next, #thumbs_prev').show();  
		} else if ($curPage == ($numPages-1)){
			$('#thumbs_prev').show(); 
		}
	}
};

$.showImgNavBtns = function(){
	$('#img_next, #img_prev').hide();
	if ($curPic == 0 && $curPic != ($numPics-1)){
		$('#img_next').show();
	} else if ($curPic > 0 && $curPic != ($numPics-1)){	
		$('#img_next, #img_prev').show();
	} else if ($curPic == ($numPics-1)){	
		$('#img_prev').show();
	}
};

$.imgNavBtnClick = function(direction){
	if (($curPic+1 == $numPics && direction == "next") || ($curPic == 0 && direction == "prev")){
	}else{ 
		if (direction == "next") { $curPic = $curPic+1; } else { $curPic = $curPic-1; }
		$hash = $('#pictures a:eq('+$curPic+')').attr('href').substring(1); location.hash = $hash;
		$.loadNewImg();
		$.showThumbsNavBtns();
		$.showImgNavBtns();		
	}
}

$.infoContainers = function(){
	// pic info
	if($picInfoOpen){
		if($('#picInfo').text()){
			$('#picInfo').fadeIn();
			$("#picInfoBtn").html('<a>'+$picInfoBtnHideTxt+'</a>').fadeIn();
		}else{
			$('#picInfo,#picInfoBtn').fadeOut();
		}
	}else{
		if($('#picInfo').text()){
			$("#picInfoBtn").html('<a>'+$picInfoBtnTxt+'</a>').fadeIn();
		}else{
			$('#picInfo,#picInfoBtn').fadeOut();
		}
	}
	
	//gallery info
	if($infoOpen){
		if($('#pictures_text_area').text()){
			$('#pictures_text_area').fadeIn();
			$("#infoBtn").html('<a>'+$infoBtnHideTxt+'</a>').fadeIn();
		}else{
			$('#pictures_text_area,#infoBtn').fadeOut();
		}
	}else{
		if($('#pictures_text_area').text()){
			$("#infoBtn").html('<a>'+$infoBtnTxt+'</a>').fadeIn();
		}else{
			$('#pictures_text_area,#infoBtn').fadeOut();
		}
	}
}

$.checkExif = function(){
	if(!$("#picInfo").text()){
		$("#picInfo").html('<span style="opacity:0.5;filter:alpha(opacity:50);">loading...</span>');
		$img = $("#picContainer img"); 
		$img.exifLoad(function(){ $picInfo = $img.exif("ImageDescription"); $("#picInfo").html($picInfo.toString()); $.infoContainers(); });
	}else{ $.infoContainers(); }
}

function resizeWindow(e){
	$pageWidth = $(window).width();
	$pageHeight = $(window).height();
	
	if ($('#intro_slideshow li').size() > 0) {
		$imgRatio = 1024/683;
		$('#intro_slideshow').width($pageWidth);
		$('#intro_slideshow').height(($pageWidth)/$imgRatio);
	}
}
