// Written by Eric Harris 2012-01

var browser_version = '';
var browserExp = /^.+(MSIE[\s\d\.]+|Chrome\/(\d+\.){3}\d+|\/\d+\.\d+\s+Safari|Firefox\/[\d\.]+$).*/;
browser_version = navigator.userAgent.replace(browserExp, "$1");
browser_version = browser_version.replace(/^[\/\s]+(.*)$/g, "$1");

var IE = false;
var IE7 = false;
if(browser_version.indexOf('MSIE 6') > -1) {
	IE6 = true;
}
if(navigator.appName != 'Netscape') {
	IE = true;
}

var onload_list = new Array();
onload = function() {
	for(var loadIndex=0; loadIndex<=onload_list.length; loadIndex++) {
		eval(onload_list[loadIndex]);
	}
}

var positionExp = /relative|absolute/;
var percent = 0; var newValue = 0;
var intervals = {}; //how many iterations this loop has done for each object being animated
function easeOut(obj, dim, start, finish, time, steps, evt, reSet) {	//alert('easeOut starting...');
	/*
	obj = the HTML object this animation is being applied to
	dim = the CSS property of obj to animate
	start = the starting value of dim
	finish = the value of dim when finished
	time = the total time this animation will take to complete in milliseconds
	steps = how many steps is time split into. I.e., steps/time is like frames/second
	evt = a function call to process when the animation has finished
	interval = how many iterations this loop has done.(Not a passed-in argument)
			   (store a different interval for each oject, so multiple animations can happen at once).
	reSet = true if starting an animation on the same object. This arg is NOT re-inserted into the loop.
	*/
	if(obj == null) {
		return;
	}
	start = Number(start);	//to prevent math errors if this value comes in as a string
	if(typeof(intervals[obj.id]) == "undefined") {
		intervals[obj.id] = {dim:{'interval':0,'currentDir':''}};
	}
	if(typeof(intervals[obj.id][dim]) == "undefined" || reSet) {
		intervals[obj.id][dim] = {'interval':0,'currentDir':''};
	}
	intervals[obj.id][dim].finishedEvent = evt;//function(){eval(evt)};
	if(intervals[obj.id][dim].interval == 0) {
		if(dim == 'opacity') {
			obj.style.opacity = (start / 100); 
			obj.style.MozOpacity = (start / 100); 
			obj.style.KhtmlOpacity = (start / 100); 
			obj.style.filter = "alpha(opacity=" + start + ")";
		} else {	//alert('assigning start value of '+ start +'px');
			obj.style[dim] = start +'px';
			if(IE7 && !positionExp.test(obj.style.position)) {/*Old IE won't hide the overflow unless position is set*/
				intervals[obj.id][dim].IE7_position = obj.style.position;
				obj.style.position = 'relative';
			}
		}
	} else if(intervals[obj.id][dim].interval != steps) {
		if((start < finish && intervals[obj.id][dim].currentDir == 'down') ||
			(start > finish && intervals[obj.id][dim].currentDir == 'up')) { //switching directions
			intervals[obj.id][dim].interval = (steps - intervals[obj.id][dim].interval);
			clearTimeout(intervals[obj.id][dim].timerID);
		}
	}
	
	intervals[obj.id][dim].interval++;
	var x = (1/time) * (time/steps) * intervals[obj.id][dim].interval;
	if(start < finish) {
		intervals[obj.id][dim].currentDir = 'up';
		//var percent = .5*(sin(x * Math.PI - Math.PI/2)+1);
		percent = (2-x)*x;
		newValue = 0 + start + (percent * (finish - start));
	} else {
		intervals[obj.id][dim].currentDir = 'down';
		percent = (2-x)*(-x)+1;
		newValue = Number(finish + (percent * (start-finish)));
		//alert('start:'+start+', finish:'+finish+', percent:'+percent+', newValue:'+newValue);
		//var newValue = 0 + (((start - finish) * percent) + finish); //same thing
	}
	if(dim == 'opacity') {	//alert('fading '+ intervals[obj.id][dim].currentDir);
		obj.style.opacity = newValue/100; 
		obj.style.MozOpacity = newValue/100; 
		obj.style.KhtmlOpacity = newValue/100; 
		obj.style.filter = "alpha(opacity=" + newValue + ")";
	} else {	//alert('moving '+ intervals[obj.id][dim].currentDir);
		if(dim == 'scale') {
			obj.style.width = newValue +'px';
			obj.style.height = newValue +'px';
		} else {
			obj.style[dim] = newValue +'px';
		}
		if(newValue>1000){/*alert('going '+ intervals[obj.id][dim].currentDir +': '+ obj.style[dim]);*/ return;}
	}
	if(intervals[obj.id][dim].interval == steps) {
		if(IE && dim == 'opacity' && intervals[obj.id][dim].currentDir == 'up') {
			//obj.style.filter = "";//<-works, but causes gradient overlay to become 100% opaque
			//-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(opacity=100)";//<-doesn't work
		}
		if(IE7 && dim != 'opacity' && obj.style.position == 'relative') {
			/*Since Old IE won't hide the overflow unless position is set*/
			if(intervals[obj.id][dim].IE7_position != undefined && intervals[obj.id][dim].IE7_position != '') {
				obj.style.position = intervals[obj.id][dim].IE7_position; //reset to original value
			}
			/*if(intervals[obj.id][dim].IE7_position != '') {
				obj.style.position = intervals[obj.id][dim].IE7_position; //reset to original value
				alert('setting '+obj.id+' to '+intervals[obj.id][dim].IE7_position);
			} else if(obj.className == 'hidden') {
				alert('setting '+obj.id+' to absolute');
				obj.style.position = 'absolute';
			} else {
				//just leave it relative//obj.style.position = '';
				alert('leaving '+obj.id+' at relative');
			}*/
		}
		intervals[obj.id][dim].interval = 0;
		intervals[obj.id][dim].currentDir = '';
		eval(intervals[obj.id][dim].finishedEvent);
		return;
	}
	
	clearTimeout(intervals[obj.id][dim].timerID);
	intervals[obj.id][dim].timerID = setTimeout(function(){ easeOut(obj, dim, start, finish, time, steps, evt) }, time/steps);
}


var slide_time = 7000;
var slide_timer;
var slide_height = 384;
var slides = new Array(
	'home_slides_img1',
	'home_slides_img2',
	'home_slides_img3'
);
var currentSlide;
var nextSlide; var nextSlideNum;
var slideEvent;
function next_slide() {
	if(currentSlide == undefined) {
		currentSlide = document.getElementById(slides[0]);
	}
	for(i=0; i<slides.length; i++) {
		if(currentSlide.id == slides[i]) {
			nextSlideNum = (i+1 == slides.length)? 0 : i+1;
			nextSlide = document.getElementById(slides[nextSlideNum]);
			break;
		}
	}
	currentSlide.style.zIndex = -1;
	with(nextSlide.style) {
		opacity = 0; MozOpacity = 0; KhtmlOpacity = 0; filter = "alpha(opacity=0)";
		top = (slide_height * (nextSlideNum + 1) * -1 + 1) +'px';
		zIndex = 1;
	}
	slideEvent = "with(currentSlide.style){zIndex=''; opacity=0; MozOpacity=0; KhtmlOpacity=0; filter='alpha(opacity=0)';} currentSlide=nextSlide; next_slide(); next_text();";
	clearTimeout(slide_timer);
	slide_timer = setTimeout(function(){ easeOut(nextSlide, 'opacity', 0, 100, 1000, 15, slideEvent) }, slide_time);
}
onload_list.push("next_slide()");


var text_height = 68;
var txts = new Array(
	'home_slides_txt1',
	'home_slides_txt2',
	'home_slides_txt3'
);
var currentTxt;
var nextTxt; var nextTxtNum;
var txtEvent;
function next_text() {
	if(currentTxt == undefined) {
		currentTxt = document.getElementById(txts[0]);
	}
	for(i=0; i<txts.length; i++) {
		if(currentTxt.id == txts[i]) {
			nextTxtNum = (i+1 == txts.length)? 0 : i+1;
			nextTxt = document.getElementById(txts[nextTxtNum]);
			break;
		}
	}
	currentTxt.style.zIndex = -1;
	with(nextTxt.style) {
		opacity = 0; MozOpacity = 0; KhtmlOpacity = 0; filter = "alpha(opacity=0)";
		top = (text_height * nextTxtNum * -1) + (2 * nextTxtNum) +'px';
		zIndex = 1;
	}
	txtEvent = "with(currentTxt.style){zIndex=''; opacity=0; MozOpacity=0; KhtmlOpacity=0; filter='alpha(opacity=0)';} currentTxt=nextTxt;";
	easeOut(nextTxt, 'opacity', 0, 100, 500, 15, txtEvent);
}

