//Javascript function library put together by posiden5665
//Creator of YouDot (http://youdot.s1.jcink.com)
//For support please visit http://forum.jcink.com
//Feel free to redistribute and edit all you want just as long as you redistribute it as open-source and free to the public


//Function for getElementsByClassName for user use. The other functions are not part of what is intended to be used by people
//If an argument besides the class name is not going to be defined use "null" in its place just to be on the safe side though it should still work without adding "null"
function getElementsByClassName(classN, parentObj, tagType){
	if(typeof tagType == "undefined" && tagType == "" && tagType == null){
		if(!parentObj || parentObj == null){
			return getElementsByClassN(classN, null);
		}else{
			return getElementsByClassN(classN, parentObj);
		}
	}else{
		if(!parentObj || parentObj == null){
			var elemArr = new Array();
			for(var v = 0; v < document.getElementsByTagName(tagType).length; v++){
				if(document.getElementsByTagName(tagType)[v].className == classN){
					elemArr[elemArr.length]=document.getElementsByTagName(tagType)[v];
				}
			}
			return elemArr;
		}else{
			var elemArr = new Array();
			for(var v = 0; v < parentObj.getElementsByTagName(tagType).length; v++){
				if(parentObj.getElementsByTagName(tagType)[v].className == classN){
					elemArr[elemArr.length]=parentObj.getElementsByTagName(tagType)[v];
				}
			}
			return elemArr;
		}
	}
}
//Functions for getElementsByClassName
function getElementsByClassN(classN, parentObj){
elementsArray = new Array();
if(parentObj == null){
	for(var i=0; i<document.childNodes.length; i++){
		if(document.childNodes[i].nodeType != 3){
		returnV = accessChild(document.childNodes[i], classN)
			for(var p = 0; p < returnV.length; p++){
					elementsArray[elementsArray.length] = returnV[p];
			}
		}
	}
}else{
	for(var i=0; i<parentObj.childNodes.length; i++){
		if(parentObj.childNodes[i].nodeType != 3){
		returnV = accessChild(parentObj.childNodes[i], classN)
			for(var p = 0; p < returnV.length; p++){
					elementsArray[elementsArray.length] = returnV[p];
			}
		}
	}
}
return elementsArray;
}
function accessChild(parentEl, lookingFor){
var elms = new Array();
	for(var x = 0; x<parentEl.childNodes.length; x++){
		if(parentEl.childNodes[x].nodeType == 1){
			if(parentEl.childNodes[x].className == lookingFor){	
				elms[elms.length] = parentEl.childNodes[x];
			}
			if(parentEl.childNodes[x].childNodes.length != 0){
				returnVal = accessChild(parentEl.childNodes[x], lookingFor);
				for(var q = 0; q < returnVal.length; q++){
					elms[elms.length] = returnVal[q];
				}
			}
		}
	}
return elms;
}
//End of functions for getElementsByClassName


//Function to get the objects X and Y relative to the page whether scrolled or not.
function getPageXY(elem){
var left = 0;
var top = 0;
do{
left = left + elem.offsetLeft
top = top + elem.offsetTop
}while(elem = elem.offsetParent)

return new Array(left, top);
}

//Coordinate class that has a built in get difference in positioning feature.
function coordinatesClass(e){
	this.xPosition = getPageMouseX(e);
	this.yPosition = getPageMouseY(e);
	this.difX = null;
	this.difY = null;
	this.setDifference = function (ev){
		this.difX = getPageMouseX(ev) - this.xPosition;
		this.difY = getPageMouseY(ev) - this.yPosition;
		this.xPosition = this.xPosition + this.difX;
		this.yPosition = this.yPosition + this.difY;
	}
}
//Set global variable to be accessed by start and 
var _coordMovement = null;


//Functions used to get mouse position relative to page. Can be used by user as well as be used by the Coordinates Objects
function getPageMouseX(e){
	if(e.pageX != null){
		return e.pageX;
	}else{
		return document.documentElement.scrollLeft + e.clientX;
	}
}
function getPageMouseY(e){
	if(e.pageY != null){
		return e.pageY;
	}else{
		return document.documentElement.scrollTop + e.clientY;
	}
}

//Functions to be used by the user to create a coordinate object and get the difference.
//movementDifference function returns X and Y values in an array respectively
//You may also access the difference in X and Y values by using _coordMovment.difX and _coordMovement.difY
function startMovement(e){
	_coordMovement = new coordinatesClass(e);
}
function movementDifference(e){
	_coordMovement.setDifference(e);
	return new Array(_coordMovement.difX, _coordMovement.difY);
}
function stopMovement(){
	_coordMovement = null;
	moving = false;
}

//Expanding and shrinking elements functions

//Change the width of an element by specified amount
//Can be called by user, it is also used by the changeWidthHeightRec function when iterating
function changeWidth(elem, amount){
	var chars = "";
	var originalAmt = parseInt(elem.style.width.match(/^[0-9]+/));
	if(elem.style.width.match(/^[0-9]+$/) != null){
		chars = "px";
	}else if(elem.style.width.match(/\%/) != null){
		chars = "%";
	}else{
		chars = elem.style.width.match(/[a-zA-Z]+/);
	}
	elem.style.width = originalAmt + amount + chars;
}

//Change the height of an element by specified amount
//Can be called by user, it is also used by the changeWidthHeightRec function when iterating
function changeHeight(elem, amount){
	var chars = "";
	var originalAmt = parseInt(elem.style.height.match(/^[0-9]+/));
	if(elem.style.height.match(/^[0-9]+$/) != null){
		chars = "px";
	}else if(elem.style.height.match(/\%/) != null){
		chars = "%";
	}else{
		chars = elem.style.height.match(/[a-zA-Z]+/);
	}
	elem.style.height = originalAmt + amount + chars;
}

//The following function should not be called by the user
//The correct call to this function would be to use the fucntion: changeWidthHeightRec so that it will call recursively by setting a timing interval this is just the work horse function
//_timerInt is used by changing changeWidthHeightRec as well as transparencyFading
var _timerInt = null
function changeWHR(elem, timing, widthAmount, heightAmount, widthGoal, heightGoal){
	var executeWidth = false;
	if(widthAmount != null && widthGoal != null){
		if((widthAmount > 0 && parseInt(elem.style.width.match(/^[0-9]+/)) <= widthGoal) || (widthAmount < 0 && parseInt(elem.style.width.match(/^[0-9]+/)) >= widthGoal)){
			executeWidth = true;
			changeWidth(elem, widthAmount);
		}
	}
	var executeHeight = false; 
	if(heightAmount != null && heightGoal != null){
		if((heightAmount > 0 && parseInt(elem.style.height.match(/^[0-9]+/)) <= heightGoal) || (heightAmount < 0 && parseInt(elem.style.height.match(/^[0-9]+/)) >= heightGoal)){
			executeHeight = true;
			changeHeight(elem, heightAmount);
		}
	}

	if(!executeHeight && !executeWidth){
		clearInterval(_timerInt)
		_timerInt = null;
	}
}


//Function to be called by the user to change a height and width recursively
//If no change to either a height or a width then just subsitute null for the arguments when calling the function.
function changeWidthHeightRec(elem, timing, widthAmount, heightAmount, widthGoal, heightGoal){
		var callFunc = function(){
			changeWHR(elem, timing, widthAmount, heightAmount, widthGoal, heightGoal);
		}
		_timerInt = setInterval(callFunc, timing);
}



//Transparency fading
//All arguments are required.
//Use negative amount to lower opacity
//fadeAmount and transparecyGoal should both be an integer between 1-100 (transparencyGoal may include 0)
function transparencyFading(elem, fadeAmount, transparencyGoal, timing){
	var reachedGoal = false;
	var callFunc = function(){
		var b = false;
		var getOpacity = null;
		//Check user agent to check for internet explorer
		if(navigator.userAgent.match(/MSIE/) != null){
			b = true;
			//alert(elem.filters.alpha.opacity == null)
			try{
				getOpacity = elem.filters.alpha.opacity;
			}catch(err){
				elem.style.filter = "alpha(opacity=100)";
				getOpacity = elem.filters.alpha.opacity;
			}
			getOpacity = (getOpacity == null || isNaN(getOpacity)) ? 100 : parseInt(getOpacity);
		}else{
			b = false;
			getOpacity = parseFloat(elem.style.opacity);
			getOpacity = (getOpacity == null || isNaN(getOpacity)) ? 1.0 : getOpacity;
		}
		if(b){
			getOpacity = getOpacity + fadeAmount;
			elem.style.filter = "alpha(opacity="+getOpacity+")";
			if((fadeAmount < 0 && getOpacity <= transparencyGoal) || (fadeAmount > 0 && getOpacity >= transparencyGoal)){
				reachedGoal = true;
			}
		}else{
			getOpacity = getOpacity + (fadeAmount/100);
			elem.style.opacity = getOpacity;
			if((fadeAmount < 0 && getOpacity <= transparencyGoal/100) || (fadeAmount > 0 && getOpacity >= transparencyGoal/100)){
				reachedGoal = true;
			}
		}
		if(reachedGoal){
			clearInterval(_timerInt);
			_timerInt = null;
		}
	}
	_timerInt = setInterval(callFunc, timing);
}



//Cookie Class
//_cookies.setCookie take 4 arguements: STRING name (name of the cookie), MIXED value (value of the cookie), INT expiration time (Number of seconds from the time when script is called on to add to eg. 86400 would add 1 day to whatever time it is when the script is loaded), STRING path (you may specify a path for the cookie to use)
//When using setCookie, name and value must be set. If the others are not being used you may put in null or leave them out. However if you want to put a path on without putting an expire you must put null in for expire. 
//The path sent in the setCookie call must contain all trailing /'s
//_cookies.readCookie takes 1 arguement: STRING name (the name of the cookie that is to be read)
var _cookies = new function(){
	this.setCookie = function(name, value, expire, path){
		var createCookieString = name + "=" + value;
		if(!expire || expire == null || expire == 0){
			expire = "; expires=" + 0 + "; ";
		}else{
			expire = (expire * 1000) + new Date().getTime();
			d = new Date();
			d.setTime(expire);
			expire = "; expires=" + d.toUTCString() + "; "; 
		}
		createCookieString += expire;
		if(!path || path == null){
			path = "path=/";
		}else{
			path = "path="+path;
		}
		createCookieString += path;
		document.cookie =  createCookieString;
	}
	this.readCookie = function(name){
		name = name + "=";
		var cookieVal = false;
		var cookiesArray = document.cookie.split("; ");
		alert(cookiesArray.length);
		for(var cookieIndex = 0; cookieIndex < cookiesArray.length; cookieIndex++){
			alert(cookiesArray[cookieIndex].slice(0, name.length))
			if(cookiesArray[cookieIndex].slice(0, name.length) == name){
				alert(cookiesArray[cookieIndex].slice(name.length));
				cookieVal = cookiesArray[cookieIndex].slice(name.length);
				break;
			}
		}
		return cookieVal;
	}
}
