// Open links in external window for XHTML 1.0 Strict compliancy
// To make a link open in external window add the "rel" attribute to the <a> tag
// and set its value to "external" example:
//     <a href="http://www.google.com" rel="external">Google</a>
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	var areas = document.getElementsByTagName("area");
	var forms = document.getElementsByTagName("form");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
	for (var x=0; x<areas.length; x++) {
		var area = areas[x];
		if (area.getAttribute("href") &&
		area.getAttribute("rel") == "external")
		area.target = "_blank";
	}
	for (var y=0; y<forms.length; y++) {
		var form = forms[y];
		if (form.getAttribute("rel") == "external")
		form.target = "_blank";
	}
}
window.onload = externalLinks;



// Image rollover - for use please make sure image names follow the following name scheme:
// Idle Image:  image_i.gif
// Hover Image: image_o.gif
// File extension and anything before the _i. does not matter so long as it is consistent between
// the two states (e.g. "image_i.gif" and "hoverimage_o.gif" does not work)
// Usage:
// <img src="image_i.gif" alt="" onmouseover="rollOver(this)" onmouseout="rollOut(this)" />
var oldImage = "";
function rollOver(oImg) {
	oldImage = oImg.src;
	var newImage = oldImage.replace("_i.","_o.");
	oImg.src = newImage;
	oImg.style.cursor = "pointer";
}
function rollOut(oImg) {
	oImg.src = oldImage;
	oImg.style.cursor = "default";
}





// Scrolling functions - allows for scrolling an element left or right. Place element in a containing
// element with set width and height and overflow set to hidden. The element you are moving should 
// be of significant length to allow for its child elements to float next to each other correctly.
var FPS = 30;
var curPos = 0;
var animating = false;
var curItem = 1;
var totalItems = 12; // this variable should be zero when the page first loads if loaded dynamically
var movementIncrement= 387; // distance to move in pixels

function sRight(sWhat,sDist,sTime) {
	if(animating == false && curItem != (totalItems / 3)){
		animating = true;
		perFrame = sTime / FPS;
		totalFrames = (sTime/1000)*FPS;
		distPerFrame = sDist / totalFrames;
		sDir = "left";
		curItem = curItem + 1;		
		animateCSS(document.getElementById(sWhat),totalFrames,perFrame,sDir,distPerFrame);
	}
}

function sLeft(sWhat,sDist,sTime) {
	if(animating == false && curItem != 1){
		animating = true;
		perFrame = sTime / FPS;
		totalFrames = (sTime/1000)*FPS;
		distPerFrame = sDist / totalFrames;
		sDir = "right";
		curItem = curItem - 1;		
		animateCSS(document.getElementById(sWhat),totalFrames,perFrame,sDir,distPerFrame);
	}

}

var curPos = 0;

function animateCSS(element,numFrames,timePerFrame,sDir,sDistPer) {
	var frame = 0;
	var time = 0;
	
	var intervalId = setInterval(displayNextFrame,timePerFrame);
	
	function displayNextFrame() {
		if (frame > numFrames) {
			clearInterval(intervalId);
			curPos = newPos;
			animating = false;
			return;
		}
		
		if(sDir == "left") {
			newPos = curPos - frame*sDistPer;
		} else if(sDir == "right") {
			newPos = curPos + frame*sDistPer;
		}
		
		element.style.left = newPos + 'px';
	
		frame++;
		time += timePerFrame;
	}
}



// Product List Hovers
var RollIt = {
	showPopup : function(elem,sec){
		z = document.getElementById(elem);
		z.style.display="block";
		document.getElementById("catBack").src = "images/"+sec+".jpg"
	},
	hidePopup : function(elem,sec){
		z = document.getElementById(elem);
		z.style.display="none";
		document.getElementById("catBack").src = "images/"+sec+".jpg"
	}    
}


function string_trim_spaces(strInput)
// Trims leading and trailing spaces off of strInput
{
	// Any data to work with?
	if (strInput.length == 0)	{ return null }
	
	// OK, we have data
	else
	{
		// First cut off leading spaces
		while(strInput.indexOf(" ") == 0)	{ strInput = strInput.slice(1,strInput.length) }
		
		// Next cut off trailing spaces
		while(strInput.indexOf(" ", strInput.length - 1) == (strInput.length - 1))
		{ strInput = strInput.slice(0,strInput.length - 1) }
		
		return strInput
	}
}
