//jquery scripts
$(function() {
	$imgWidth = 44; /* width plus margin-right */
	$pageWidth = 890;
	
	$hash = location.hash.split("#")[1];
	
	$absPath = $('body').attr('id');
	if ( $.browser.msie ) { $.ifixpng('../images/pixel.gif'); $('body *').addClass('bgPng'); $('img, .bgPng').ifixpng(); }
	if ( !$.browser.msie ) { 
		$('#content, #page_turn').hide().fadeIn('slow'); 
		//logo hover
		$('#logo').hover( function(){ $('#logo_hover').fadeIn(); $('#logo').fadeOut(); });
		$('#logo_hover').hover( function(){ },function(){ $('#logo_hover').fadeOut(); $('#logo').fadeIn(); });
	}
	
	
	
	//homepage slideshow
	$numIntroImgs = $('#intro_slideshow li').size();
	$('#intro_slideshow li').hide();
	$curIntroImg = -1;
	nextIntroImg();
	
	function nextIntroImg(){
		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}, 2000, function(){
				nextIntroImg();
			});
		});
	}
	
	//nav behaviour
	if($('#nav a.active').length < 1){
		$('#nav').hide().fadeIn('slow');
	}
	//dropdown nav
	$(".category_list").find("ul").hide();
	$(".category_list").hover(
		function(){ $(this).find("ul").slideDown("fast"); $(this).addClass("ieHover"); }, 
		function(){ $(this).find("ul").slideUp("fast").removeClass("ieHover"); } 
	);
	

	//$('#container #pictures_text_area').jScrollPane( { showArrows:true, wheelSpeed:30, scrollbarMargin:15 } );
	//$('.jScrollPaneDrag, .jScrollArrowUp, .jScrollArrowDown').css('background-color','#555');
	
	//pictures pages
	$numPics = $('#pictures li').size(); 
	if ($numPics > 0) {	
		$('#pictures li:last').css('margin-right','0px');
		$('#thumbs_next, #thumbs_prev').hide();
		$("#pictures a").each(function(){
			$link = $(this).attr('href');
			$(this).attr('href','#'+$link);
		});
		if ($hash) {
			$pic = $hash;
			$picInfo = $('#pictures a[href="#'+$hash+'"]').attr('title');
			$('#pictures a[href="#'+$hash+'"]').addClass('active');
			$curPic = $('#pictures a').index($('#pictures a[href="#'+$hash+'"]'));
		} else {
			$pic = $('#pictures a:first').attr('href').substring(1);
			$picInfo = $('#pictures a:first').attr('title');
			$('#pictures a:first').addClass('active');
			location.hash = $pic;
			$curPic = $('#pictures a').index($('#pictures a:first'));
		}
		$('#pictures a:not(.active)').fadeTo("normal", 0.2);
		$("#picInfo").html($picInfo);
		$("#picContainer").html('<span id="loader"></span><img src="'+$pic+'" style="display:none;"/>');
		$("#picContainer img").load(function(){ $(this).fadeIn('slow'); $("#loader").hide(); if($picInfo!=''){$("#picInfoBtn").fadeIn();}else{$("#picInfoBtn").fadeOut();} });
			
		$("#pictures a").click(function(){
			$("#pictures a.active").fadeTo("normal", 0.2);
			$("#pictures a").removeClass('active');
			$curPic = $('#pictures a').index(this);
			$pic = $(this).attr('href').substring(1);
			$picInfo = $(this).attr('title');
			$(this).addClass('active');
			$("#pictures a.active").fadeTo("normal", 1);
			$("#picInfo").html($picInfo);
			$("#picContainer").html('<span id="loader"></span><img src="'+$pic+'" style="display:none;"/>');
			$("#picContainer img").load(function(){ $(this).fadeIn('slow'); $("#loader").hide(); if($picInfo!=''){$("#picInfoBtn").fadeIn();}else{$("#picInfoBtn").fadeOut();} });
			$.showImgNavBtns();
		});

		$("#pictures a:not(.active)").live('mouseover',function(){ $(this).fadeTo("normal", 1); });
		$("#pictures a:not(.active)").live('mouseout',function(){ $(this).fadeTo("normal", 0.2); });
		
		$picsPerPage = Math.floor($pageWidth / $imgWidth);
		$('#pictures').css({ width:$imgWidth*$numPics });
		$.showThumbsNavBtns();
		
		$('#thumbs_next').click(function(){
			$('#thumbs_prev').fadeIn();
			if ($curPage+1 < $numPages){ $curPage += 1; }else{ $curPage = 0; }
			if ($curPage == $numPages-1){ $('#thumbs_next').fadeOut(); }else{ $('#thumbs_next').fadeIn(); }
			$('#pictures').animate({ left:-($curPage*$pageWidth)+'px' });
		});
		$('#thumbs_prev').click(function(){
			$('#thumbs_next').fadeIn();
			if ($curPage-1 >= 0){ $curPage -= 1; }else{ $curPage = $numPages-1; }
			if ($curPage == 0){ $('#thumbs_prev').fadeOut(); }else{ $('#thumbs_prev').fadeIn(); }
			$('#pictures').animate({ left:-($curPage*$pageWidth)+'px' });
		});
		
		$.showImgNavBtns();
		$('#img_next .btn, #img_prev .btn').hide();
		$('#img_next, #img_prev').hover(function(){
			$(this).find('.btn').fadeIn();
		}, function(){
			$(this).find('.btn').fadeOut();
		});
		$('#img_next').click(function(){
			$("#pictures a.active").fadeTo("normal", 0.2);
			$("#pictures a").removeClass('active');
			$curPic = $curPic+1;
			$pic = $('#pictures a:eq('+$curPic+')').attr('href').substring(1);
			location.hash = $pic;
			$picInfo = $('#pictures a:eq('+$curPic+')').attr('title');
			$('#pictures a:eq('+$curPic+')').addClass('active');
			$("#pictures a.active").fadeTo("normal", 1);
			$("#picInfo").html($picInfo);
			$("#picContainer").html('<span id="loader"></span><img src="'+$pic+'" style="display:none;"/>');
			$("#picContainer img").load(function(){ $(this).fadeIn('slow'); $("#loader").hide(); if($picInfo!=''){$("#picInfoBtn").fadeIn();}else{$("#picInfoBtn").fadeOut();} }); 
			$.showThumbsNavBtns();
			$.showImgNavBtns();
			$('#img_prev .btn').hide();
		});
		$('#img_prev').click(function(){
			$("#pictures a.active").fadeTo("normal", 0.2);
			$("#pictures a").removeClass('active');
			$curPic = $curPic-1;
			$pic = $('#pictures a:eq('+$curPic+')').attr('href').substring(1);
			location.hash = $pic;
			$picInfo = $('#pictures a:eq('+$curPic+')').attr('title');
			$('#pictures a:eq('+$curPic+')').addClass('active');
			$("#pictures a.active").fadeTo("normal", 1);
			$("#picInfo").html($picInfo);
			$("#picContainer").html('<span id="loader"></span><img src="'+$pic+'" style="display:none;"/>');
			$("#picContainer img").load(function(){ $(this).fadeIn('slow'); $("#loader").hide(); if($picInfo!=''){$("#picInfoBtn").fadeIn();}else{$("#picInfoBtn").fadeOut();} }); 
			$.showThumbsNavBtns();
			$.showImgNavBtns();
			$('#img_next .btn').hide();
		});
	}
		
	$infoOpen = false; 
	if ($('#pictures_text_area').text() != ''){ $("#infoBtn").fadeIn(); }
	$("#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>info</a>');
		}
	});
		
	$picInfoOpen = false;
	$("#picInfoBtn").live('click',function(){
		if ($picInfoOpen == false){
			$picInfoOpen = true;
			$('#picInfo').slideToggle();
			$(this).html('<a>-</a>');
		} else {
			$picInfoOpen = false;
			$('#picInfo').slideToggle();
			$(this).html('<a>+</a>');
		}
	});
	
	/*$thumbsOpen = false;
	if ($('#pictures li').size() != 0){ 
		$("#thumbsBtn").fadeIn(); 
		$("#viewable").slideToggle();
		$("#viewable").pause(2000).slideToggle();
	}
	$("#thumbsBtn").live('click',function(){
		if ($thumbsOpen == false){
			$thumbsOpen = true;
			$('#viewable').slideToggle();
			$(this).html('<a>hide</a>');
		} else {
			$thumbsOpen = false;
			$('#viewable').slideToggle();
			$(this).html('<a>images</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 a, #categories li a, #subcategories li a').hover(function(){
			$(this).find('span').slideDown('fast');
		}, function() {
			$(this).find('span').slideUp('fast');
		}
	);	
	
	
	
	//form messages
	if ($hash == "sent"){
		$('form').hide();
		$('.formWrap .thanks').show();
	} else if ($hash == "error") {
		$('.formWrap .error').show();
	}
	/*$('#form_contact .submit').click(function(){
		if ($('#form_contact input[name="name"]').val == ''){
			$('#form_contact').parent().find('.error').show();
			return false;
		}
	});*/

});

$.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();
	}
};
$.showThumbsNavBtns = function(){
	$numPages = Math.ceil($numPics/$picsPerPage); 
	$curPage = Math.floor(($curPic)/$picsPerPage);
	$('#pictures').animate({ left:-($curPage*$pageWidth)+'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(); 
		}
	}
};

$.fn.pause = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};
