/* 
/////////////////////////////////////////////////////////////////////////////////////
/// CODE FOR DATE AND TIME
/////////////////////////////////////////////////////////////////////////////////////
*/
d = new Date();
day = d.getDate();
month = d.getMonth()+1;
year = d.getFullYear();
full = month + "." + day + "." + year;

/*
/////////////////////////////////////////////////////////////////////////////////////
CLCP v2.1 Clear Links to Current Page
Jonathan Snook
This code is offered unto the public domain
http://www.snook.ca/jonathan/
/////////////////////////////////////////////////////////////////////////////////////
*/

function clearCurrentLink(){
	var a = document.getElementsByTagName("A");
	var aText = "";
    for(var i=0;i<a.length;i++) {
		aText += a[i].parentNode.nodeName.toLowerCase() + "\n";
		if(a[i].href == window.location.href.split("#")[0]) {
            a[i].parentNode.className += " on";
			a[i].className = "on"; //removeNode(a[i]);
		}
		// USE THIS IF YOU HAVE A SPECIFIC PAGE OR IF THE LINK IS IN A SPECIFIC ELEMENT
		//if(a[i].href.indexOf('/news-getposts/') > 0 && window.location.href.indexOf('/category/news/') > 0 && a[i].parentNode.nodeName.toLowerCase() == "div" && a[i].parentNode.parentNode.nodeName.toLowerCase() == "li") {
		if(a[i].href.indexOf('/news-getposts/') > 0 && window.location.href.indexOf('/category/news/') > 0 && a[i].parentNode.nodeName.toLowerCase() == "li") {
			a[i].parentNode.className += " on";
			a[i].className = "on";
		}
	}	
}

function removeNode(n){
    if(n.hasChildNodes())
        for(var i=0;i<n.childNodes.length;i++)
            n.parentNode.insertBefore(n.childNodes[i].cloneNode(true),n);
    n.parentNode.removeChild(n);
}

/*
/////////////////////////////////////////////////////////////////////////////////////
/// FORM FIELD AREA
/////////////////////////////////////////////////////////////////////////////////////
*/

// Javascript that sets the on/off function for all form elements
function classIt(ele) {
	ele.className = "off";
	ele.onfocus = function(){ this.className = "on" }
	ele.onblur = function() { this.className = "off" }
}

//Javascript that limits the number of characters in the message field
function limitText(limitField,limitNum,eleID) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		if (document.getElementById(eleID)) {
			document.getElementById(eleID).innerHTML = limitNum - limitField.value.length;
		}
	}
}

function styleFields() {
	var inputs = document.getElementsByTagName("input");
	for (i=0;i<inputs.length;i++) {
		if (!inputs[i].getAttributeNode("type")) {
			classIt(inputs[i]);
		} else if (inputs[i].getAttributeNode("type").value == "text" || inputs[i].getAttributeNode("type").value == "password") {
			classIt(inputs[i]);
		}
	}
	var textareas = document.getElementsByTagName("textarea");
	for (i=0;i<textareas.length;i++) {
		classIt(textareas[i]);
		/*
		//THIS IS FOR SETTING THE LIMITS OF A FIELD TO A CERTAIN AMOUNT - CALLS limitText()
		if (textareas[i].getAttributeNode("name").value == "fieldName") {
			var charCount = 250;
			var msgElementID = "text-replace";
			textareas[i].onkeydown = function() { limitText(this.form.fieldName,charCount,msgElementID);  }
			textareas[i].onkeyup = function() { limitText(this.form.fieldName,charCount,msgElementID); }
		}
		*/
	}
	var selects = document.getElementsByTagName("select");
	for (i=0;i<selects.length;i++) {
		classIt(selects[i]);
	}
}

/*
/////////////////////////////////////////////////////////////////////////////////////
/// CSS NAV JAVASCRIPT FOR IE < v7
/////////////////////////////////////////////////////////////////////////////////////
*/

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			if (this.className.indexOf(" nohover") == -1) {
				this.className+=" sfhover";
			}
			/*if (this.className.indexOf("submenu") != -1) {
				this.className+=" sfarrow";
			}*/
			//document.getElementById("content-testimonials").innerHTML = this.className;
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			//this.className=this.className.replace(new RegExp(" sfarrow\\b"), "");
			//document.getElementById("content-testimonials").innerHTML = this.className;
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/*
/////////////////////////////////////////////////////////////////////////////////////
ESSENTIAL FUNCTIONS
Reference Article:
Dustin Diaz:
http://www.dustindiaz.com/top-ten-javascript/
/////////////////////////////////////////////////////////////////////////////////////
*/

/* addEvent: simplified event attachment */
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

/* window 'load' attachment */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* grab Elements from the DOM by className */
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;
}

/* toggle an element's display */
function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

/* insert an element after a particular node */
function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

/* Array prototype, matches value in array: returns bool */
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

/* get, set, and delete cookies */
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
	
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}
	
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* quick getElement reference */
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

/*
/////////////////////////////////////////////////////////////////////////////////////
/// WINDOW ONLOAD FUNCTIONS
/////////////////////////////////////////////////////////////////////////////////////
*/

window.onload = function() {
	styleFields();
	clearCurrentLink();
	if (document.getElementsByTagName("body")[0].className == "homepage") {
		startit();
	}
	if (document.getElementsByTagName("body")[0].className == "subpage") {
		
	}
}
