/**
 * @author bdgeorge
 * Whitepixels common Javascript routines for Xcart - requires jQuery and the color plugin
 */

$(document).ready(function(){	
	//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
	$('#side_about input[type="text"]').one("click", function(){
		$(this).attr("value","");
	});
	/**
	 * Image swapper for product pages, swaps the detailed images onclick.
	 * Preloads them as well.
	 */
	//Remove the height and width from the product thumbnail on the product page
	$("#product_thumbnail").each(function(){
		this.removeAttribute('width');
		this.removeAttribute('height');
		});
	$('#product_option_thumbs .swapImage').each(function(){
		//preload the images
		this.ExtraImage = new Image();
		this.ExtraImage.src = this.href;
		//Add a click handler to these
		$(this).click(function(){
			$('#product_thumbnail').attr('src',this.href);
			$('#product_thumbnail').each(function(){
				this.removeAttribute('width');
				this.removeAttribute('height');
				});
			return false;
		});
	});
	
	/**
	 * height 'snapper' for the main content wrapper.
	 * Measure the height of the main content div, then inject a new div at the end to pad out its parent to a multiple of the background image.
	 */

	/**
	 * Dialog Message Controls	
	 */
	//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;
		}	
	}	
	
});

