/* parts of this script are copied from dhtmlgoodies.com see the following copyright message */

/************************************************************************************************************
(C) www.dhtmlgoodies.com, October 2005

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/	

var MSIE = navigator.userAgent.indexOf('MSIE')>=0?true:false;
var navigatorVersion = navigator.appVersion.replace(/.*?MSIE (\d\.\d).*/g,'$1')/1;

var form_widget_amount_slider_handle = 'images/slider_handle.gif';
var slider_handle_image_obj = false;
var sliderObjectArray = new Array();
var slider_counter = 0;
var slideInProgress = false;
var handle_start_x;
var event_start_x;
var currentSliderIndex;

var pennStarBlue = [91,131,184];
var pennStarBrightBlue = [73,152,255];
var red = [210,57,0]; // red
var orange = [255,162,0]; // orange
var fadeInSteps = 1;
var fadeOutSteps = 1;
var startBGRGB = pennStarBlue;
var endBGRGB = pennStarBrightBlue;
var startFontRGB = [255,255,255];
var endFontRGB = [255,255,255];
var menuBorderRGB = [0,0,0];
var moveitPrePos = [-150,200];
var moveitStartPos = [15,250];
var moveitSteps = 200;
var moveitInterval = 20;
var moveitPower = 0.4;


if (document.getElementById && document.getElementsByTagName) {
	if (window.addEventListener) window.addEventListener('load', init, false);
	else if (window.attachEvent) window.attachEvent('onload', init);
}
	
function init() {		
	
	document.body.color_picker_open = false;

	initColors();
	initOverElements();
	initControls();
	rotateImage('logo');
	
						
//	Init motion animation
	var moveIt = document.getElementById('moveit');
	if (moveIt) {
		moveIt.style.display="block";
		moveInitial(moveIt);
		if (moveIt != null) moveIt.onclick = moveRandom;
	}
	
}

function initColors() {
//	Init fade animation with memory, both directions
	var animElements = document.getElementById("nav").getElementsByTagName("li");
	for(var i=0; i<animElements.length; i++) {
		animElements[i].onmouseover = navMouseOver;
		animElements[i].onmouseout = navMouseOut;
		animElements[i].style.backgroundColor = getColor(startBGRGB);
		animElements[i].style.color = getColor(startFontRGB);
		animElements[i].onmouseenter=function() {
			this.className+=" jsHover";
			this.style.zIndex = 2;
		}
		animElements[i].onmouseleave=function() {
			this.className=this.className.replace(new RegExp(" jsHover\\b"), "");
			this.style.zIndex = 1;
		}
	}
}

function navMouseOver() {
	fadeColMem(this);
}

function navMouseOut() {
	fadeColRestore(this);
}

function initOverElements() {
//	Init fade animation with memory, both directions
	searchElement = document.getElementById("search");
	searchElement.onmouseover = document.getElementById("search-over").style.display = "inline";
	searchElement.onmouseout = document.getElementById("search-over").style.display = "none";
}

function initControls() {
	if (document.getElementById("controls")) {
		var controlElements = document.getElementById("controls").getElementsByTagName("tr");
		for(var i=0; i<controlElements.length; i++) {
			var cols = controlElements[i].getElementsByTagName("li");
			var curControl;
			var example;
			for (var j=0; j<cols.length; j++) {
				switch (cols[j].className) {
					case "control":
						curControl = cols[j].getElementsByTagName("input")[0];						
						if (controlElements[i].className == 'colorControl') {
							eval("curControl.value = getColorStr(" + curControl.id + ")");
							curControl.onchange = updateColorControl;
						} else {
							eval("curControl.value = " + curControl.id);
							curControl.onchange = updateControl;
						}
						break;
					case "example":
						if (controlElements[i].className == 'colorControl') {
							example = cols[j];
							if (example != null) {
								eval("example.style.backgroundColor = getColor(" + curControl.id + ")");
							}
						}
						break;
				}
			}
		}
	}
}


function moveInitial(elem) {
	if (!elem.currentPos) elem.currentPos = moveitPrePos; //if no mem is set, set it first;
	doPosChangeMem(elem,elem.currentPos,moveitStartPos,moveitSteps,moveitInterval,1);
		//move to a random position for demonstration
}

function moveRandom() {
	if (!this.currentPos) this.currentPos = [0,0]; //if no mem is set, set it first;
	doPosChangeMem(this,this.currentPos,[Math.floor(Math.random()*600),Math.floor(Math.random()*450)],moveitSteps,moveitInterval,moveitPower);
		//move to a random position for demonstration
}


function getColor(RGB) {
	return "rgb("+
				RGB[0]+","+
				RGB[1]+","+
				RGB[2]+")";
}

function getColorStr(RGB) {
	return 	RGB[0]+","+
				RGB[1]+","+
				RGB[2];
}


function update() {
	
	document.getElementById("startBGRGBEx").style.backgroundColor=getColor(startBGRGB);
	document.getElementById("endBGRGBEx").style.backgroundColor=getColor(endBGRGB);
	document.getElementById("startFontRGBEx").style.backgroundColor=getColor(startFontRGB);
	document.getElementById("endFontRGBEx").style.backgroundColor=getColor(endFontRGB);
	
	var animElements = document.getElementById("nav").getElementsByTagName("li");
	for(var i=0; i<animElements.length; i++) {	
		animElements[i].style.backgroundColor = getColor(startBGRGB);
		animElements[i].getElementsByTagName("a")[0].style.color = getColor(startFontRGB);
		animElements[i].currentbgRGB = startBGRGB;
		animElements[i].currentFontRGB = startFontRGB;
	}
	
	initColors();
}


function updateColorControl() {
	debug(this.id + "=[" + this.value + "]\n");
	eval(this.id + "=[" + this.value + "]");
	update();
}


function setColorControl(elem) {
	debug(elem.id + "=[" + elem.value + "]\n");
	eval(elem.id + "=[" + elem.value + "]");
	update();
}

function updateControl() {
	debug(this.id + "=" + this.value + "\n");
	eval(this.id + "=" + this.value + "");
	update();
}


function setControl(elem) {
	debug(elem.id + "=" + elem.value + "\n");
	eval(elem.id + "=" + elem.value + "");
	update();
}

function fadeColMem(elem) {	
	debug (fadeInSteps + "\n");
	if (!elem.currentbgRGB) {
		elem.currentbgRGB = startBGRGB; //if no mem is set, set it first;
	}
	doBGFadeMem(elem,elem.currentbgRGB,endBGRGB,fadeInSteps,20,1);
	
	if (!elem.currentFontRGB) {
		elem.currentFontRGB = startFontRGB; //if no mem is set, set it first;
	}
	doFontFadeMem(elem,elem.currentFontRGB,endFontRGB,fadeInSteps,20,1);
}

function fadeColRestore(elem) {
	debug (fadeOutSteps + "\n");
	if (elem.currentbgRGB) {
		doBGFadeMem(elem,elem.currentbgRGB,startBGRGB,fadeOutSteps,20,1);
	}
	
	if (elem.currentFontRGB){
		doFontFadeMem(elem,elem.currentFontRGB,startFontRGB,fadeOutSteps,20,1);		
	}
}
	
	
function doBGFadeMem(elem,startBGRGB,endBGRGB,steps,intervals,powr) {
	
//BG Fader with Memory by www.hesido.com
	if (elem.bgFadeMemInt) window.clearInterval(elem.bgFadeMemInt);
	var actStep = 0;
	elem.bgFadeMemInt = window.setInterval(
		function() {
			elem.currentbgRGB = [
				easeInOut(startBGRGB[0],endBGRGB[0],steps,actStep,powr),
				easeInOut(startBGRGB[1],endBGRGB[1],steps,actStep,powr),
				easeInOut(startBGRGB[2],endBGRGB[2],steps,actStep,powr)
				];
			elem.style.backgroundColor = getColor(elem.currentbgRGB);
			actStep++;
			if (actStep > steps) window.clearInterval(elem.bgFadeMemInt);
		}
		,intervals)
}


function doFontFadeMem(elem,startFontRGB,endFontRGB,steps,intervals,powr) {
	
//Font Fader with Memory by www.hesido.com
	if (elem.FontFadeMemInt) window.clearInterval(elem.FontFadeMemInt);
	var actStep = 0;
	elem.FontFadeMemInt = window.setInterval(
		function() {
			elem.currentFontRGB = [
				easeInOut(startFontRGB[0],endFontRGB[0],steps,actStep,powr),
				easeInOut(startFontRGB[1],endFontRGB[1],steps,actStep,powr),
				easeInOut(startFontRGB[2],endFontRGB[2],steps,actStep,powr)
				];
			elem.getElementsByTagName("a")[0].style.color = getColor(elem.currentFontRGB);
			actStep++;
			if (actStep > steps) window.clearInterval(elem.FontFadeMemInt);
		}
		,intervals)
}

function doPosChangeMem(elem,startPos,endPos,steps,intervals,powr) {
	debug (startPos + "|" + endPos + "\n");
	//Position changer with Memory by www.hesido.com
	if (elem.posChangeMemInt) window.clearInterval(elem.posChangeMemInt);
	var actStep = 0;
	elem.posChangeMemInt = window.setInterval(
		function() {
			elem.currentPos = [
				easeInOut(startPos[0],endPos[0],steps,actStep,powr),
				easeInOut(startPos[1],endPos[1],steps,actStep,powr)
				];
			elem.style.left = elem.currentPos[0]+"px";
			elem.style.top = elem.currentPos[1]+"px";
			actStep++;
			if (actStep > steps) window.clearInterval(elem.posChangeMemInt);
		}
		,intervals)

}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
//Generic Animation Step Value Generator By www.hesido.com
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}

function rgbToHtml(h) {
	return parseInt(h.substring(1,0),16);
}

function debug(str) {
	if (document.getElementById("debug")) {
		document.getElementById("debug").value = document.getElementById("debug").value + str;
	}
}

var interval = 2.5; // delay between rotating images (in seconds)
var random_display = 1; // 0 = no, 1 = yes
interval *= 1000;

var image_index = 0;
image_list = new Array();
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/1.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/2.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/3.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/4.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/5.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/6.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/7.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/8.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/9.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/10.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/11.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/12.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/13.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/14.gif");
image_list[image_index++] = new imageItem("http://www.pennstar.com/images/logo/15.gif");
var number_of_image = image_list.length;


function imageItem(image_location) {
	this.image_item = new Image();
	this.image_item.src = image_location;
}

function get_ImageItemLocation(imageObj) {
	return(imageObj.image_item.src)
}
function generate(x, y) {
	var range = y - x + 1;
	return Math.floor(Math.random() * range) + x;
}

function getNextImage() {
	if (random_display) {
		image_index = generate(0, number_of_image-1);
	}
	else {
		image_index = (image_index+1) % number_of_image;
	}
	var new_image = get_ImageItemLocation(image_list[image_index]);
	return(new_image);
}

function rotateImage(place) {
	var new_image = getNextImage();
	document[place].src = new_image;
	var recur_call = "rotateImage('"+place+"')";
	setTimeout(recur_call, interval);
}


function submitSelect(selectObj) {
	location.href=''+selectObj.options[selectObj.selectedIndex].value;
}