var grabElement = function( _ele ) {
	var _retVal = null;
	if( typeof _ele == "string" ) {	
		_retVal = document.getElementById( _ele );
	} else {
		_retVal = _ele;		
	}
	return _retVal;
}
var gE = grabElement;

var gETAC = function( _params ) {
	if( arguments.length == 1 ) {
		_name = _params["tagName"];
		_className = _params["class"];
		_element = _params["ele"];
	} else if( arguments.length == 3 ){
		_name = arguments[0];
		_className = arguments[1];
		_element = arguments[2];
	} else {
		throw( "invalid number of arguments past" );
	}
	var _collectionWithClass = new Array();
	var _searchElement = grabElement( _element );
	var _collection = _searchElement.getElementsByTagName( _name );
	for( var _i = 0; _i < _collection.length; _i++ ) {
		if( hasClass( _collection[_i], _className)) {
			_collectionWithClass.push( _collection[_i] );
		}
	}
	return _collectionWithClass;
};

var getElementsByTagAndClass = gETAC;

/**
 * Array override
 **/
Array.prototype.indexOf = function( _findThisVar ) {
    for( var _i = 0; _i < this.length; _i++ ) {
        if( this[_i] == _findThisVar ) {
            return _i;
        }
    }
    return -1;
}

var hasIndexOf = function( _findThisVar ) {
	return( this.indexOf( _findThisVar ) > -1 );
}

Array.prototype.contains = hasIndexOf;
Array.prototype.hasIndexOf = hasIndexOf;

Array.prototype.each = function( callback ) { 
	for( var i = 0; i < this.length; i++ ) {
		callback( this[i] );
	}
};

/**
 * String Override
 */

String.prototype.hasIndexOf = function(theIndex) {
	var _retVal = true;
	if( this.indexOf(theIndex) == -1 ) {
		_retVal = false;
	}
	return _retVal;
}

String.prototype.format = function( str ) {
	
    if( arguments.length == 0 ) {
        return null;
    }
    
    for(var i = 1; i < arguments.length; i++ ) {
        var re = new RegExp( '\\{' + (i-1) + '\\}', 'gm' );
        str = str.replace( re, arguments[i] );
    }
    
    return str;
}

/**
 * Function Override
 */

Function.prototype.bind = function(obj) {
	var fn = this;
	return function () {
		var args = [this];
		for (var i = 0, ix = arguments.length; i < ix; i++) {
			args.push(arguments[i]);
		}
		return fn.apply(obj, args);
	};
}
/**
 * Class Manipulation
 */
var addClass = function( _params ) {
	
	// handle params
	if( arguments.length == 1 ) {
		_item = _params["ele"];
		_classToAdd = _params["class"];
	} else if( arguments.length == 2 ) {
		_item = arguments[0];
		_classToAdd = arguments[1];
	}	
	
	if( ! hasClass( { "ele": _item, "class" : _classToAdd } ) ) {
		_item.className += (" " + _classToAdd);
	}
}
var aC = addClass;

var removeClass = function( _params ) {
	
	// handle params
	if( arguments.length == 1 ) {
		_item = _params["ele"];
		_classToRemove = _params["class"];
	} else if( arguments.length == 2 ) {
		_item = arguments[0];
		_classToRemove = arguments[1];
	}	

	if( hasClass( _item, _classToRemove ) ) {
		var _theClass = _item.className;
		var _classes = _theClass.split(" ");
		for(var _i = 0; _i < _classes.length; _i++) {
			if( _classes[_i] == _classToRemove || _classes[_i] == " ") {
				_classes.splice( _i, 1 );
			}
		}
		_item.className = _classes.join(" ");
	}
}
var rC = removeClass;

function hasClass( _params ) {
	if( arguments.length == 1 ) {
		_item = _params["ele"];
		_classToSearch = _params["class"];
	} else if( arguments.length == 2 ) {
		_item = arguments[0];
		_classToSearch = arguments[1];
	}
	if( _item.className.hasIndexOf( _classToSearch ) ) {
		var _theClass = _item.className;
		var _classes = _theClass.split(" ");
		for(var _i = 0; _i < _classes.length; _i++) {
			if( _classes[_i] == _classToSearch) {
				return true;
			}
		}
	}
	return false;
}

function stripEmptyTextNodes( _element ) {
	for( var _i = 0; _i < _element.childNodes.length; _i++) {
		if( _element.childNodes[_i].nodeName == "#text" ) {
			_element.removeChild( _element.childNodes[_i] );
			_i--;
		}
	}
	return _element;
}


/**
 * Window Object
 */
var windowObject = {
	onload: [],
	loaded: function()
	{
		if( arguments.callee.done ) return;
		arguments.callee.done = true;
		for( _i = 0; _i < windowObject.onload.length; _i++) {
			windowObject.onload[_i]();
		}
	},
	addLoadFunction: function( fireThis )
	{
		for(var index = 0; index < arguments.length; index++) {
			this.onload.push(arguments[index]);
		}
		if (document.addEventListener) 
			document.addEventListener("DOMContentLoaded", windowObject.loaded, null);
		if (/KHTML|WebKit/i.test(navigator.userAgent))
		{ 
			var _timer = setInterval(function()
			{
				if (/loaded|complete/.test(document.readyState))
				{
					clearInterval(_timer);
					delete _timer;
					windowObject.loaded();
				}
			}, 10);
		}
		/*@cc_on @*/
		/*@if (@_win32)
		var proto = "src='javascript:void(0)'";
		if (location.protocol == "https:") proto = "src=//0";
		document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");
		var script = document.getElementById("__ie_onload");
		script.onreadystatechange = function() {
		    if (this.readyState == "complete") {
		        windowObject.loaded();
		    }
		};
		/*@end @*/
	   window.onload = windowObject.loaded;
	}
};

function getXPos(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function getYPos(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function getHeight(obj) {
	return obj.offsetHeight;
}

function getWidth(obj) {
	return obj.offsetWidth;
}

function getMouseCoords(e) {
  if( !e ) {
  	if( window.event ) { e = window.event; } else { return; }
  }
  if( typeof( e.pageX ) == 'number' ) {
    //most browsers
    var xcoord = e.pageX;
    var ycoord = e.pageY;
  } else if( typeof( e.clientX ) == 'number' ) {
    //Internet Explorer and older browsers
    //other browsers provide this, but follow the pageX/Y branch
    var xcoord = e.clientX;
    var ycoord = e.clientY;
    var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||
     ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ||
     ( navigator.vendor == 'KDE' )
    if( !badOldBrowser ) {
      if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //IE 4, 5 & 6 (in non-standards compliant mode)
        xcoord += document.body.scrollLeft;
        ycoord += document.body.scrollTop;
      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE 6 (in standards compliant mode)
        xcoord += document.documentElement.scrollLeft;
        ycoord += document.documentElement.scrollTop;
      }
    }
  } else {
    return;
  }
  return [xcoord, ycoord];
}


var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}


function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal (j);
			}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
		}
	return null;
}

function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
}
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "; path=/") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
