function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function productImgSwap(){
	
	/* ================= 
	
	This script creates an image rollover effect for a single image
	The image must have an id of product_image
	and the image to be displayed on roll over needs to be named 
	something-detail.jpg, so if your original image is named
	image.jpg the roll over image needs to be named image-detail.jpg  	 
	
	==================== */
	
	// Check to see if the image is on this page
	if(document.getElementById('product_image') != null){
		// Find the image's source
		var productImg = document.getElementById('product_image');
		var productSrc = productImg.src;
	
		// Split the source on the forward slashes (/) and put it into an array
		var temp = new Array();
		temp = productSrc.split('/');
	
		// Split the last index in the array (the name of the image ex. image.jpg)
		var lastIndex = temp.length - 1;
		var temp2 = temp[lastIndex].split('.'); 
		productName = temp2[0];
	
		// Create a new array to hold the directory of the image
		var temp3 = new Array();
		for(i=0; i<lastIndex; i++) { 
			temp3.push(temp[i]);
		}
	
		// Reconstruct the image directory from the array
		var newSrc = temp3.join('/');
	
		// Append the original image name + detail.jpg to the image directory
		newSrc += '/' + productName + '-detail.jpg';
	
		// Preload Detail Image
	
		var detail = new Image();
		detail.src = newSrc;
	
		// Set Events
		productImg.onmouseover = function(){
			this.src = newSrc;
		}
	
		productImg.onmouseout = function(){
			this.src = productSrc;
		}
	}
}

function cartImgSwap(){
	
	// Check to see if the image is on this page
	if(document.getElementById('cart_image') != null){
		// Find the image's source
		var productImg = document.getElementById('cart_image');
		var productSrc = productImg.src;
	
		// Split the source on the forward slashes (/) and put it into an array
		var temp = new Array();
		temp = productSrc.split('/');
	
		// Split the last index in the array (the name of the image ex. image.jpg)
		var lastIndex = temp.length - 1;
		var temp2 = temp[lastIndex].split('.'); 
		productName = temp2[0];
	
		// Create a new array to hold the directory of the image
		var temp3 = new Array();
		for(i=0; i<lastIndex; i++) { 
			temp3.push(temp[i]);
		}
	
		// Reconstruct the image directory from the array
		var newSrc = temp3.join('/');
	
		// Append the original image name + detail.jpg to the image directory
		newSrc += '/' + productName + '-over.png';
	
		// Preload Detail Image
	
		var detail = new Image();
		detail.src = newSrc;
	
		// Set Events
		productImg.onmouseover = function(){
			this.src = newSrc;
		}
	
		productImg.onmouseout = function(){
			this.src = productSrc;
		}
	}
}
addLoadEvent(productImgSwap); 
addLoadEvent(cartImgSwap); 
