function clearText(text, str, cls) {
	if(text.value == str) {
		text.value = "";
		text.className = cls;
	}
}

function defText(text, str, cls) {
	if(text.value == "") {
		text.className = cls;
		text.value = str;
	}
}

var Slide = {
	mutex: false,
	autoInterval: false,
	go: function(n, auto) {
		
		if(!auto && Slide.autoInterval) {
			clearInterval(Slide.autoInterval);
			Slide.autoInterval = false;
			$('toggle_btn').src = "/images/play.png";
		}
		
		if(Slide.mutex) { return; }
		Slide.mutex = true;
		var obj = $('feature_wrapper');
		var left = parseInt(obj.getStyle('left'));
		var width = parseInt(obj.getStyle('width'));
		
		if(isNaN(left)) { 
			left = 0; 
		}
		
		var amt = n * 200;
		var newLeft = left + amt;
		if(newLeft <= 0 && newLeft > -width) {
			new Effect.Move(
				obj, 
				{
					x: amt, 
					y: 0, 
					duration: 0.15,
					afterFinish: function() {
						
						if(newLeft >= 0) {
							obj.setStyle("left: " + (-width + 400) + "px");
						} else
						if(newLeft <= (-width + 200)) {
							obj.setStyle("left: -200px");
						}
						
						Slide.mutex = false; 
					}
				}
			);
		} else {
			Slide.mutex = false;
		}
		return false;
	},
	toggle: function() {
		
		if(!$('feature_wrapper')) { return false; }
		
		if(Slide.autoInterval) {
			clearInterval(Slide.autoInterval);
			Slide.autoInterval = false;
			$('toggle_btn').src = "/images/play.png";
		} else {
			Slide.autoInterval = setInterval(
				function() { 
					Slide.go(-1, true);
				}, 
				10000
			);
			$('toggle_btn').src = "/images/pause.png";
		}
		return false;
	}
};

function jumpTo(href) {
	if(href && "" != href) {
		window.location.href = href;
	}
	return false;
}

var DemoForm = {
	allControls: [ 
		'name', 
		'email', 
		'phone', 
		'company',
		'tz',
		'hours',
		'contactHours',
		'timeframe',
		'requirements'
	],
	controls: [
		[ 'name', 'email' ],
		[ 'name', 'email', 'phone', 'company', 'tz', 'hours' ],
		[ 'name', 'email', 'phone', 'company', 'tz', 'contactHours', 'timeframe', 'requirements' ]
	],
	show: function(type) {
		for(var i=0; i<DemoForm.allControls.length; i++) {
			$('formTable_' + DemoForm.allControls[i]).style.display = "none";
		}
		
		if(type == 0) {
			$('formTable').setStyle('display: none;');
			return false; 
		}
		
		$('formTable').setStyle('display: block;');
		
		var rowDisplay = "table-row";
		if(navigator && navigator.appName.indexOf("MSIE") > -1) {
			rowDisplay = "block";
		}
		
		var controls = DemoForm.controls[type - 1];
		for(var i=0; i<controls.length; i++) {
			$('formTable_' + controls[i]).style.display = "";
		}
	}
};

function initFList(){
	var list = document.getElementById("listcontainer");
	if (list) {
		doList(list);
	}
}

function doList(list){
	for(var i=0;i<list.childNodes.length;i++){

		if (list.childNodes[i].nodeName=="LI") {
			var isParent='f';
			for (var k=0; k < list.childNodes[i].childNodes.length;k++){

				if (list.childNodes[i].childNodes[k].nodeName=="UL"){
					isParent='t';
					var subList=list.childNodes[i].childNodes[k];
					doList(subList);

					list.childNodes[i].className = "parentNodeClosed";

					list.childNodes[i].onclick=function(event){
						showhide(this, event);
						return false;
					}

					list.childNodes[i].getElementsByTagName("ul")[0].style.display = "none";
				}
			}

			if (isParent=='f'){
				list.childNodes[i].className = "childNode";

				list.childNodes[i].onclick=function(event){
					if (!event) var event = window.event;
					event.cancelBubble = true;
					if (event.stopPropagation) event.stopPropagation();
					return false;
				}
			}
		}
	}
}

function showhide(el, evt){
	if (!evt) var evt = window.event;
	evt.cancelBubble = true;
	if (evt.stopPropagation) evt.stopPropagation();
				
	var visible = el.getElementsByTagName("ul")[0].style.display=="block";
	el.getElementsByTagName("ul")[0].style.display=(visible)?"none":"block";
	el.className=(visible)?"parentNodeClosed":"parentNodeOpen";
	return false;
}

var Header = {
	opacity: 0.95,
	fadeLen: 0.5,
	curr: 0,
	max: 4,
	interval: 0,
	mutex: false,
	select:function(n, auto) {
		if(Header.mutex || Header.curr == n) { return false; }
		Header.mutex = true;
		
		if(!auto && Header.interval > 0) { 
			clearInterval(Header.interval);
			Header.interval = 0;
		}
		
		var currbg = $("bg" + Header.curr);
		var currcap = $("cap" + Header.curr);
		var bg = $("bg" + n);
		var cap = $("cap" + n);
		
		bg.setStyle({zIndex: 1});
		
		currcap.removeClassName("cap_sel");
		cap.addClassName("cap_sel");
		
		currbg.fade({
			duration: Header.fadeLen, 
			from: 1, 
			to: 0,
			afterFinish: function() {
				currbg.setStyle({zIndex: 0, opacity: 1, display: 'block'});
				bg.setStyle({zIndex: 2});
				Header.curr = n;
				Header.mutex = false;
			}
		});
	},
	start:function() {
		Header.interval = setInterval(
			function() {
				Header.select((Header.curr + 1) % Header.max, true);
			},
			10000
		);
	}
};

Event.observe(
	window, 
	"load", 
	function() {
		var notice = $('notice');
		if(notice) {
			setTimeout(
				function() {
					Effect.Fade(notice);
				},
				10000
			);
		}
		
		Slide.toggle();
		initFList();
		
		var videos = document.getElementsByClassName("video");
		for(var i=0; i<videos.length; i++) {
			flowplayer(
				videos[i].id, 
				"/flowplayer-3.1.1.swf",
				{clip:{
					autoPlay: false,
					autoBuffering: true
				}}
			);
		}
		
		$('cap0').setOpacity(Header.opacity);
		$('cap1').setOpacity(Header.opacity);
		$('cap2').setOpacity(Header.opacity);
		$('cap3').setOpacity(Header.opacity);
		Header.start();
		
	}
);
