// ajax variables
// sliding div variables
var timerlen = 5;
var slideAniLen = 250;
var linkEl;

var timerID = new Array();
var startTime = new Array();
var obj = new Array();
var endHeight = new Array();
var moving = new Array();
var dir = new Array();

// scrolling
var automaticInterval = 0;
var automaticTimeout = 0;
var manualInterval = 0;
var scrollSpeed	= timerlen * 7;
var scrollWidth	= 20;
var liWidth = 0;
var featuredPanel = "";

function isDefined(property) {
	return (typeof property != 'undefined');
}

function externalLinks() {
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	for (var i=0;i<anchors.length;i++) { 
		var anchor = anchors[i];
		if ((anchor.getAttribute("href")) && (anchor.getAttribute("rel") == "external")) {
			anchor.target = "_blank";
		}
	}
}

function blurLinks() {
	lnks = document.getElementsByTagName("a");
	inpts = document.getElementsByName("input");
	for(i=0;i<lnks.length;i++) {	
		if ((lnks[i].getAttribute("rel") == null) || (lnks[i].getAttribute("rel") == "")) {
			lnks[i].onfocus = new Function("this.blur()");
		}
	}
	for(i=0;i<inpts.length;i++) {
		inpts[i].onfocus= new Function("this.blur()");
	}
}

function scrollInit() {
	if (document.getElementById("featured")) {
		featuredPanel = document.getElementById("featured").getElementsByTagName("div");
		featuredPanel = featuredPanel[0];
		var featuredPanelPanes = featuredPanel.getElementsByTagName("div");
		liWidth = featuredPanelPanes[0].offsetWidth;
		featuredPanel.style.width = (featuredPanelPanes.length * liWidth) + "px";
		setAuto();
	}
}

function autoScroll() {
	featuredPanel.style.left = (!featuredPanel.style.left) ? 0 : parseFloat(featuredPanel.style.left);
	if (parseFloat(featuredPanel.style.left) > ((0-parseFloat(featuredPanel.style.width)) + liWidth)) {
		endPos = Math.abs(parseFloat(featuredPanel.style.left) - liWidth);
		direction = "Left";
	} else {
		endPos = 0;
		direction = "Right";
	}
	automaticInterval = setInterval("scroll"+direction+"('" + endPos + "')", scrollSpeed);
}

function clearAuto() {
	clearInterval(automaticInterval);
	clearTimeout(automaticTimeout);
}

function setAuto() {
	automaticTimeout = setTimeout("autoScroll()", 10000);
}

function scrollStart(endPos) {
	clearAuto();
	featuredPanel.style.left = (!featuredPanel.style.left) ? 0 : parseFloat(featuredPanel.style.left);
	direction = ((0 - parseInt(endPos)) >= parseFloat(featuredPanel.style.left)) ? "Right" : "Left";
	automaticInterval = setInterval("scroll"+direction+"('" + endPos + "')", scrollSpeed);
}

function scrollEnd() {
	if (manualInterval != 0) {
		clearInterval(manualInterval);
	}
	if (automaticInterval != 0) {
		clearAuto();
	}
	currentPos = parseInt(Math.abs(parseFloat(featuredPanel.style.left)) / liWidth);
	setAuto();
}

function scrollLeft(endPos) {
	if (parseFloat(featuredPanel.style.left) > (0-endPos)) {
		featuredPanel.style.left = parseFloat(featuredPanel.style.left) - parseInt(scrollWidth) + "px";
	} else {
		scrollEnd();
		featuredPanel.style.left = (0 - parseInt(endPos)) + "px";
	}
}

function scrollRight(endPos) {
	if (parseFloat(featuredPanel.style.left) < parseInt(endPos)) {
		featuredPanel.style.left = parseFloat(featuredPanel.style.left) + parseInt(scrollWidth) + "px";
	} else {
		scrollEnd();
		featuredPanel.style.left = parseInt(endPos) + "px";
	}
}

function stringToArray(string) {
	var array = new Array();
	if (string) {
		arrayString = string.split("&");
		for (var i=0;i<arrayString.length;i++) {
			if (arrayString[i]) {
				tmpArray = arrayString[i].split("=");
				var arrayObject = new Object();
				arrayObject.name = tmpArray[0];
				arrayObject.value = tmpArray[1];
				array.push(arrayObject);
			}
		}
	}
	return array;
}

function jQueryFunctions() {
	jQuery(document).ready(function() {
		$(".wrapper #content .box_mini dl .picture").mouseover(function(){
			$src = $(this);
			$aniObj = $src.find("h3");
			if ($aniObj.attr("class") != "going") {
				$aniObj.attr("class", "going");
				$src.append('<div class="overlay"></div>')
				$aniObj.animate({top: '144px'}, 300, function() {
					//$aniObj.removeClass("going");
					setTimeout(function(){ 
							$aniObj.attr("class", "going");
							$src.find(".overlay").remove();
							$aniObj.animate({top: '203px'}, 300, function() {
							$aniObj.removeClass("going");
						});
					}
					, 2000);
				});
			}
		});
		gallerySlider();
	});	
}



function gallerySlider() {
	if (document.getElementById("Gallery")) {
		stepcarousel.setup({
			galleryid: 'Gallery', //id of carousel DIV
			beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
			panelclass: 'panel', //class of panel DIVs each holding content
			autostep: {enable:true, moveby:1, pause:3000},
			panelbehavior: {speed:500, wraparound:false, wrapbehavior: 'slide'},
			defaultbuttons: {enable: true, moveby: 1, leftnav: ['/images/buttons/left.gif', 10, 190], rightnav: ['/images/buttons/right.gif', -30, 190]},
			contenttype: ['inline'] //content setting ['inline'] or ['ajax', 'path_to_external_file']
		});
	}
}

if (isDefined(window.addEventListener)) {
	window.addEventListener("load", RunFunctions, false);
}

else if (isDefined(window.attachEvent)) {
	window.attachEvent("onload", RunFunctions);
}

function RunFunctions() {
	externalLinks();
	blurLinks();
	scrollInit();
	jQueryFunctions();
}


