/**
 * @author bdgeorge
 * Whitepixels common Javascript routines for Xcart - requires jQuery and the color plugin
 */
$(window).load(function(){
	//Day/Night detection and skin changes
    var now = new Date();
    var hours = now.getHours();
	if (hours > 6 && hours < 19){		
		$('#logo').flash('black logo.swf',{
			height:121, 
			width:280,
			parameters: {
				loop:false,
				wmode:'transparent',
				quality:'high'
			}});
	}
	else {
		$('#logo').flash('white logo.swf',{
			height:121, 
			width:280,
			parameters: {
				loop:false,
				wmode:'transparent',
				quality:'high'
			}});						                
	}	
});
$(document).ready(function(){
	//Day/Night detection and skin changes
    var now = new Date();
    var hours = now.getHours();
	if (hours > 6 && hours < 19){		
		//By default we are in the day, but clean up any rogue nights that may creep in.
        $('.night').addClass('day').removeClass('night');
		//Now use the jQuery flash loader to show the correct logo
	}
	else {
		$('.day').addClass('night').removeClass('day');			  	
		$('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');
		});
	//Remove roll over class for any png's in Internet Explorer 6
	$("img[src$='.png']").each(
		function(i){
			if($.browser.msie){
				if(parseInt(jQuery.browser.version)<=6){
					$(this).removeClass('wh-roll');
				}				
			}
		});		
	//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 = addSuffix(this.src,'_on');
		},
		function(){
			//Out function
			this.src = remSuffix(this.src,'_on');
		});
	//If the user clicks in the subscribe input then clear out the default text
	//The funny ID is because this is a campaign monitor element
	$('#viuir-viuir').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");
	});
	//Start up the carousel thing
	$('#mycarousel').jcarousel({
	        scroll: 1
	    });
});
/**
* 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;
	}
	
}