/**
 * @author bdgeorge
 * Whitepixels common Javascript routines for Xcart - requires jQuery and the color plugin
 */
$(document).ready(function(){
	//Day/Night detection and skin changes
    var now = new Date();
    var hours = now.getHours();
	if (hours > 7 && hours < 20){
	        $('.day').show();
	        $('.night').remove();    
	}
	else {
	        $('.day').remove();
	        $('.night').show();
	        $('link[@href*=wh_day]').each(function(){
	        	this.href=remSuffix(this.href,"_day");
	        	this.href=addSuffix(this.href,"_night");	     	        	
	        });  	                
	}
	//Attach pop-up windows to any external_link anchors
	$('a.external_small').click(function(){
		return popupWindow(this.href,'450','350','SmallPopUp1');
		});
	//Preload any hover images
	$('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
	$('img.wh-roll').hover(
		function(){
			//Over function
			this.src = remSuffix(this.src,'-off');
			this.src = addSuffix(this.src,'-on');
		},
		function(){
			//Out function
			this.src = remSuffix(this.src,'-on');
			this.src = addSuffix(this.src,'-off');
		});
	//If the user clicks in the subscribe input then clear out the default text
	$('#xjhkk-xjhkk').one("click", function(){
		$(this).attr("value","");
	}); 
	//Add the classic BaseCamp Yellow Fade Technique to any div with the class wh-yft - its not really YFT as its a dialog message not ajax page highlighting. But it helps.
	$("div.wh-yft:has(div)").animate({ backgroundColor: "yellow" }, 750);
	setTimeout(function(){
	 	$('div.wh-yft').animate({ backgroundColor: "#ffe"}, 3000)
	 	.click(function(){
			$('div.wh-yft').slideUp("slow");
			});
	},1500);
	//Primarily for the admin's benefit - if you click in the text area, remove the saved message box
	$('textarea[name=filebody]').click(function(){
		$('div.wh-yft').stop().slideUp("slow");
	});

});
/**
* Helper functions
*/
function popupWindow(myurl, myheight, mywidth, mywindowID) {
 	if(window.open){
		window.open(myurl,mywindowID,'alwaysRaised=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=' + mywidth
		+ ',height=' + myheight
		+ ',screenX=150,screenY=150,top=150,left=150');
		return false;
 	}else{
 		return true;
 	}
}
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;
	}
	
}