// Generic Popup Script
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=500');");
}

// 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";
	}
}


// 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;
}
function rollOut(oImg) {
	oImg.src = oldImage;
}


// Replace input field with default value on blur if nothing was entered
// Usage: <input type="text" value="Default Value" onfocus="clearText(this)" onblur="replaceText(this)" />
function clearText(thefield) {
	if (thefield.defaultValue==thefield.value) { thefield.value = "" }
} 
 
function replaceText(thefield) {
	if (thefield.value=="") { thefield.value = thefield.defaultValue }
}



function simplePreload() { 
	var args = simplePreload.arguments;
	document.imageArray = new Array(args.length);
	for(var i=0; i<args.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];
	}
}


function startup() {
	//List functions to be run on startup here:
	externalLinks();
	// simplePreload("images/image1.gif","images/image2.gif");
}

var oldonload = window.onload;

if (typeof window.onload != 'function') {
	window.onload = startup;
} else {
	window.onload = function() {
		oldonload();
		startup();
	}
}

//Toggle Function for the IntroText box on the home page
function toggletIt() {
		$(".toggled").slideToggle("slow");
		$(".toggler").toggle();
}
function toggleText(whatIs,whatLink) {
	var toggled = whatIs;
	var toggler = whatLink;
	$("#"+whatIs).slideToggle("slow");
	$("."+whatLink).toggle();
}

// Features Section on the home page
// 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 = 5; // this variable should be zero when the page first loads if loaded dynamically
var movementIncrement= 600; // distance to move in pixels

function sRight(sWhat,sDist,sTime) {
	if(animating == false && curItem != totalItems){
		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;
			if(curItem == totalItems){
				curPos = 0;
				curItem = 1;
				element.style.left = curPos + 'px';
			}
			return;
		}
		
		if(sDir == "left") {
			newPos = curPos - frame*sDistPer;
		} else if(sDir == "right") {
			newPos = curPos + frame*sDistPer;
		}
		
		element.style.left = newPos + 'px';
	
		frame++;
		time += timePerFrame;
	}
}
var slideShowAnim;
function slideShow(){
	slideShowAnim = setInterval("sRight('feature_list',movementIncrement,300)",5000);
}

