var scalableSite = true;
var logger=function(str){try{console.log(str);}catch(e){}};logger('logger loaded');
$(function() {
	$curPic = $numPics = 0;
	if(scalableSite){$.resizeWindow();$(window).bind("resize",function(){$.resizeWindow();});}
	$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
	$('#header').hover( function(){ $('#logo_hover').fadeIn(); }, 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, 'right':0, 'z-index':9999}).fadeTo("normal", 0.5); $('#bluelightmedia').hover( function(){ $(this).fadeTo("normal", 1); }, function(){ $(this).fadeTo("normal", 0.5); });	
	
	//homepage slideshow
	$numIntroPics = $('#intro_slideshow li').size();
	if($numIntroPics > 0){ $('#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").removeAttr('href');
		$(".category_list").hover(function(){ $(this).find("ul").fadeIn().addClass("ieHover"); }, function(){ $(this).find("ul").fadeOut().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
			$.viewableThumbs();
			
			//click the thumbnail container's next/prev buttons
			$('#thumbs_next').click(function(){ $.navigateThumbContainer('next'); });
			$('#thumbs_prev').click(function(){ $.navigateThumbContainer('prev'); });
			
			//click/hover the next/prev image buttons OR left/right arrow buttons on keyboard
			$('#img_next').click(function(){ $.imgNavBtnClick("next"); });
			$('#img_prev').click(function(){ $.imgNavBtnClick("prev"); });
			$('#img_next, #img_prev').hover(function(){ $(".tooltip").remove();	}, function(){ $(".tooltip").fadeOut('fast',function(){ $(this).remove(); }); });
			$('#img_next').live('mousemove',function(event){ $(".tooltip").remove(); showTooltip(mouseCoordinate(event,"X")+5, mouseCoordinate(event,"Y")+5, 'next photo >'); });
			$('#img_prev').live('mousemove',function(event){ $(".tooltip").remove(); showTooltip(mouseCoordinate(event,"X")+5, mouseCoordinate(event,"Y")+5, '< previous photo'); });
			$(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'); });
});
	
// FUNCTIONS	
$.loadNewImg = function(){
	$('#pictures a.active').fadeTo("normal", 0.2);
	$('#pictures a').removeClass('active');
	$('#picContainer').html('<span id="loader"></span><span id="overlay"></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'); $(this).data({ 'width':$(this).width(), 'height':$(this).height() }); $(this).resizeImg(); $.checkExif(); });
	$.showImgNavBtns();
}

$.nextIntroImg = function(){
	if ($curIntroImg+1 < $numIntroPics){ 
		$curIntroImg += 1; 
		$('#intro_slideshow li:eq('+($curIntroImg-1)+')').fadeOut('slow');
	} else { 
		$curIntroImg = 0; 
		$('#intro_slideshow li:eq('+($numIntroPics-1)+')').fadeOut('slow');
	}
	$('#intro_slideshow li:eq('+$curIntroImg+')').html($('#intro_slideshow li:eq('+$curIntroImg+')').html());
	$('#intro_slideshow li:eq('+$curIntroImg+') img').load(function(){ $(this).data({ 'width':$(this).width(), 'height':$(this).height() }).resizeImg(); });
	if($numIntroPics>1){
		$('#intro_slideshow li:eq('+$curIntroImg+')').fadeIn(4000, function(){
			$(this).animate({ 'opacity':1}, 2000, function(){ $.nextIntroImg(); });
		});
	}else{ $('#intro_slideshow li:eq('+$curIntroImg+')').fadeIn(4000); }
	
}
			
$.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' }); }
}

$.viewableThumbs = function(){
	$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();
}

$.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').fadeIn();
	} else if ($curPic > 0 && $curPic != ($numPics-1)){	
		$('#img_next, #img_prev').fadeIn();
	} else if ($curPic == ($numPics-1)){	
		$('#img_prev').fadeIn();
	}
};

$.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{ $.picInfoContainer(); }
}

$.resizeWindow = function(){
	$windowWidth = $(window).width();
	$windowHeight = $(window).height();
	$.positionElements();
}
$.positionElements = function(){
	$contentLRMargin = parseFloat($('#content').css('margin-left')) + parseFloat($('#content').css('margin-left'));	
	$contentTBMargin = parseFloat($('#content').css('margin-top')) + parseFloat($('#content').css('margin-bottom'));
	if($('#pictures li').size() > 0){
		$('#picContainer').width($windowWidth - $contentLRMargin);	
		$('#picContainer').height($windowHeight - 5 - $('#header').outerHeight(true) - $('#viewable').outerHeight(true) - $('#footer').outerHeight(true));
		$('#picContainer img').resizeImg();
	}
	if($('#intro_slideshow li').size() > 0){  
		$('#intro_slideshow,#intro_slideshow li').width($windowWidth - $contentLRMargin);
		$('#intro_slideshow,#intro_slideshow li').height($windowHeight - 5 - $('#header').outerHeight(true) - $('#footer').outerHeight(true));
		$('#intro_slideshow img').resizeImg();
	}
}
$.fn.resizeImg = function(){
	$borderWidth = 2*5;
	if($(this).data('width')+$borderWidth > $(this).parent().width() || $(this).data('height')+$borderWidth > $(this).parent().height()){
		$imgRatio = $(this).data('width') / $(this).data('height');
		if ($(this).data('width')>$(this).parent().width()){
			if($imgRatio>1){
				$(this).width($(this).parent().width());
				$(this).height($(this).parent().width() / $imgRatio);
			}else{
				$(this).width($(this).parent().height() * $imgRatio);
				$(this).height($(this).parent().height());
			}
		}
		if ($(this).data('height')+$borderWidth>$(this).parent().height()){
			if ($imgRatio>1){
				$(this).height($(this).parent().height()-$borderWidth);
				$(this).width($(this).height() * $imgRatio-$borderWidth);
			}else{
				$(this).width($(this).parent().height() * $imgRatio-$borderWidth);
				$(this).height($(this).parent().height()-$borderWidth);
			}
		}
	}else{
		$(this).height($(this).data('height'));
		$(this).width($(this).data('width'));
	}
	
	if($(this).data('width')){
		if($(this).data('height') < $(this).parent().height()){
			$(this).css({'margin-top':(($(this).parent().height()-($(this).data('height'))-$borderWidth)/2)+'px'});
		}else{ $(this).css({'margin-top':'0px'}); }
	}
	
	$('#img_prev,#img_next').css({'height':$(this).parent().height()});
	$.showImgNavBtns();
	$.viewableThumbs();
}

function showTooltip(x, y, contents, bkgColor) {
	if($windowWidth - x < 100){
		$('<div class="tooltip" style="background-color:' + bkgColor + '">' + contents + '</div>').css({ top: y-5, right: $windowWidth - x + 15}).appendTo("body").show();
	}else{
		$('<div class="tooltip" style="background-color:' + bkgColor + '">' + contents + '</div>').css({ top: y-5, left: x + 15 }).appendTo("body").show();
	}
}

function mouseCoordinate(event, axis) {
    var property = (axis == "X") ? "scrollLeft" : "scrollTop";
    if (event.pageX) {
        return event["page"+axis];
    } else {
        return event["client"+axis] + (document.documentElement[property] ? document.documentElement[property] : document.body[property]);;
    }
};
