/**
 * @author bdgeorge
 * Whitepixels common Javascript routines - requires jQuery
 */

jQuery(document).ready(
function(){
	//Preload any hover images
	jQuery('img.wh-roll').each(function(){
		this.ExtraImage= new Image();
		this.ExtraImage.src = addSuffix(this.src,'_on');
	});
	//Create roll over images on any image with wh-roll as its class
	jQuery('img.wh-roll').hover(
		function(){
			//Over function
			this.src = addSuffix(this.src,'_on');
		},
		function(){
			//Out function
			this.src = remSuffix(this.src,'_on');
		});	
	//General helpers		
	function remSuffix(a,b){
		//removes suffix b from filename a without removing the file extension
		var lastdot = a.lastIndexOf(b + '.');
		if (lastdot==-1){
			return a;
		} else {
			var ext = a.slice(lastdot+b.length,a.length);
			return a.slice(0,lastdot) + ext;
		}		
	}
	function addSuffix(a,b){
		//adds suffix b to filename a without removing the file extension
		var lastdot = a.lastIndexOf(".");
		if (lastdot==-1){
			return a;
		} else {
			var ext = a.slice(lastdot,a.length);
			return a.slice(0,lastdot) + b + ext;
		}
	}		
});

jQuery(window).load(
function(){	
	//Image and navigation controller
	jQuery('.category-gallery-1, .category-gallery-2, .category-gallery-3').each(function(){
		//Preload the extra images
		jQuery('.post textarea').parents('.post').each(function(i){
		    newHtml = jQuery(this).find('textarea').val();
		    jQuery(this).find('.entry').html(newHtml);
		});	
		//Animate the thumbnails
		jQuery('#navigation li a').each(function(index){
		    jQuery(this).click(function(){
				if (jQuery('.post').eq(index).is(':visible')) { 
					//No action required
				} else {
					old = jQuery('.post:visible');
					jQuery('.post').eq(index).css('z-index',1).show();
					jQuery(old).fadeOut(function(){
						jQuery('.post').eq(index).css('z-index',2);
						jQuery(old).css('z-index',1);
					});	
				}
				return false;
		    });
		});
		//Animate the navigation
		//Set bottom and top limits based on the navigation list height and the frame size
		if (jQuery('#navigation').height() > jQuery('.nav-frame').height()){
			var topLimit = -1 * (jQuery('#navigation').height() - jQuery('.nav-frame').height());
		} else {
			var topLimit = 0;
		}
		var bottomLimit = 0;
		var step = jQuery('#navigation li').height();
		
		jQuery('.nav-up').click(function(){
			if (!jQuery('#navigation').is(':animated')) {
				//var newTop = jQuery('#navigation').position().top - step;
				var newTop = topLimit;
				if (newTop >= topLimit) {
					jQuery('#navigation').stop(true, true).animate({
						top: newTop + 7
					}, 450, function(){
						setButtons();
					});
				}
			}
		});
		jQuery('.nav-down').click(function(){
			if (!jQuery('#navigation').is(':animated')) {
				//var newTop = jQuery('#navigation').position().top + step;
				var newTop = bottomLimit;
				if (newTop <= bottomLimit) {
					jQuery('#navigation').stop(true, true).animate({
						top: newTop
					}, 450, function(){
						setButtons();
					});
				}
			}
		});
		setButtons();	
		function setButtons(){
			//Enable/Disable the up and down buttons based on top position
			var nextUp = jQuery('#navigation').position().top - step;
			var nextDown = jQuery('#navigation').position().top + step;		
			if (nextUp < topLimit){
				jQuery('.nav-up').addClass('disable');
			} else {
				jQuery('.nav-up').removeClass('disable');
			}
			if (nextDown > bottomLimit){
				jQuery('.nav-down').addClass('disable');
			} else {
				jQuery('.nav-down').removeClass('disable');
			}		
		}	
	});		
});

