/* = CLEAR FIELD
-------------------------------------------------
	clears the value of any inpu field with the
	class name of clearDefault
-------------------------------------------------*/

function clickClear() {
	if(!document.getElementsByTagName) return false;
	
	var inputs = document.getElementsByTagName("INPUT");
	
	for ( var i = 0; i < inputs.length; i++)
	{
		if(inputs[i].className.match("clear-default"))
		{
			inputs[i].onclick = function() {
				if(this.value == this.defaultValue)
				{
					this.value = "";
				}
				return 0;
			};
			inputs[i].onfocus = function() {
				if(this.value == this.defaultValue)
				{
					this.value = "";
				}
				return 0;
			};
			inputs[i].onblur = function() {
				if(this.value == "")
				{
					this.value = this.defaultValue;
				}
				return 0;
			};
		}
	}
	return 0;
}

addLoadEvent(clickClear);

/* = GET ELEMENTS BY CLASS
-------------------------------------------------
	adds in some much needed functionality to the
	DOM. 
	
	searchClass: class name to search for
	node: (optional) node to search under
	tag: (optional) type of element to search for
-------------------------------------------------*/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
    	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

/* = ADD ON LOAD
-------------------------------------------------
	This adds functions to onload without
	stepping on each other.
-------------------------------------------------*/

function addLoadEvent(func) {
	var oldOnLoad = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldOnLoad();
			func();
		};
	}
}


function slideSwitch() {
	if ($.slideshowClicked == false) {
		var active = $('#home-header > div:visible');
		var activeIndex = active[0].id.substr(8,1);
	
		var next = '';
		if (active.next().attr('id') == 'home-header-thumbs' ) {
			swapContent(0);
		} else {
			swapContent(parseInt(activeIndex));
		}		
	};
}

function swapContent (index) {
	var currentVisible = $('#home-header > div:visible');
	if (currentVisible.attr('id') != 'feature-'+(index+1) ) {
		$('#feature-'+(index+1)).fadeIn('slow');
		currentVisible.fadeOut('slow');
		$('#home-header-thumbs li').removeClass("active");
		$('#link-'+(index+1)).addClass('active');
	}
}

function buttonsInit () {
	swapContent(0);
	$('#home-header-thumbs li:first').addClass('active');
	$('#home-header-thumbs').show();
	var featureBoxes = $('#home-header > div');
	var featureThumbs = $('#home-header-thumbs li');
	featureThumbs.each(function(index) {
		$(this).click(function(event) {
			event.preventDefault();
			swapContent(index);
			$.slideshowClicked = true;
		});
	});
	setInterval(slideSwitch, 6000);
}

$(document).ready(function() {
	clickClear();
	if ($('body').hasClass('home')) {
		buttonsInit();
		$.slideshowClicked = false;
	}
});

