// commonFunctions 2010.10.17

// Attention: for correct function of this little library a DOCTYPE is required !
// but nowadays, HTML without doctype is absolutly crap ;-)

window.CF = new commonFunctions();

function commonFunctions()
{
	var that = this

	this.testString = "yodl!";

	this.isIE = navigator.userAgent.match(/MSIE\s([^;]*)/);
	this.isOpera = navigator.userAgent.match(/Opera[\s\/]([^\s]*)/);

	/* message alert */
	commonFunctions.prototype.msgAlert = function(msg)
		{
		var that = this;
		this.createPopup(msg)
		this.popupDiv.style.cssText = "background-color: #EEEEEE; padding: 5px; position: absolute; left: 0px; bottom: 0px; z-index: 100000;"
		this.popupDiv.onclick = function(){if(that.popupDiv) {document.body.removeChild(that.popupDiv); that.popupDiv = null;}}
		}

	/* dom element properties */
	commonFunctions.prototype.getDomElement = function(e)
		{
		if(typeof e == "object" && e) { return e; }
		else if(typeof e == "string" && document.getElementById(e)) {return document.getElementById(e); }
		else { return null; } // wrong elementtype
		}

	commonFunctions.prototype.getDomElementWidth = function(e)
		{
		return this.getDomElement(e).offsetWidth;
		}

	commonFunctions.prototype.getDomElementHeight = function(e)
		{
		return this.getDomElement(e).offsetHeight;
		}

	commonFunctions.prototype.getDomElementX = function(e)
		{
		return this.getDomElement(e).offsetLeft;
		}

	commonFunctions.prototype.getDomElementY = function(e)
		{
		return this.getDomElement(e).offsetTop;
		}


	/* manipulate display & visibility */
	commonFunctions.prototype.displayElement = function(e, hide)
		{
		if(hide == null) { this.getDomElement(e).style.display = "block"; }
		else { this.getDomElement(e).style.display = "none"; }
		}

	commonFunctions.prototype.toggleDisplayElement = function(e)
		{
		var styleValue = "none";
		if(this.getDomElement(e).style.display == styleValue) { styleValue = "block"; }
		this.getDomElement(e).style.display = styleValue;
		}

	commonFunctions.prototype.visibilityElement = function(e, hide)
		{
		if(hide == null) { this.getDomElement(e).style.visibility = "visible"; }
		else { this.getDomElement(e).style.visibility = "hidden"; }
		}

	commonFunctions.prototype.toggleVisibilityElement = function(e)
		{
		var styleValue = "hidden";
		if(this.getDomElement(e).style.visibility == styleValue) { styleValue = "visible"; }
		this.getDomElement(e).style.visibility = styleValue;
		}

	
	commonFunctions.prototype.setStyle = function(e, stylename, value)
		{
		eval('this.getDomElement(e).style.' + stylename + '="' + value + '";')
		}



	/* window.properties */
	commonFunctions.prototype.getWindowInnerWidth = function()
		{
		var wiw = 0;
		if (document.documentElement && document.documentElement.clientWidth)
			{ wiw = document.documentElement.clientWidth; }
		else if (document.body)
			{ wiw = document.body.clientWidth; }
		// window.innerWidth is not realy needed, becourse FF2&3, Safari5 and Opera10 
		// return correct values without scrollbars (-> real viewPort) with documentElement
		//if (window.innerWidth) { wiw = Math.min(wiw, window.innerWidth); }
		return wiw;
		}

	commonFunctions.prototype.getWindowInnerHeight = function()
		{
		var wih = 0;
		if (document.documentElement && document.documentElement.clientHeight)
			{ wih = document.documentElement.clientHeight; }
		else if (document.body)
			{ wih = document.body.clientHeight; }
		
		// window.innerHeight is not realy needed, becourse FF2&3, Safari5 and Opera10 
		// return correct values without scrollbars (-> real viewPort) with documentElement
		//if (window.innerHeight) { wih = Math.min(wih, window.innerHeight); }

		return wih;
		}

	commonFunctions.prototype.getDocumentWidth = function()
		{
		var dw = 0;
		if(document.documentElement && document.documentElement.scrollWidth)
			{ dw = document.documentElement.scrollWidth }
		else if(document.body && document.body.scrollWidth)
			{ dw = document.body.scrollWidth; }
		// document.width is not realy needed, becourse FF2&3, Safari5 and Opera10 
		// return correct values for complete document width with documentElement
		// if(document.width) { dw = Math.max(dw, document.width); }

		return dw;
		}

	commonFunctions.prototype.getDocumentHeight = function()
		{
		var dh = 0;
		if(document.documentElement && document.documentElement.scrollHeight)
			{ dh = document.documentElement.scrollHeight }
		else if(document.body && document.body.scrollHeight)
			{ dh = document.body.scrollHeight; }

		// document.height is not realy needed, becourse FF2&3, Safari5 and Opera10 
		// return correct values for complete document.height with documentElement
		// if(document.height) { dh = Math.max(dh, document.height); }

		return dh;
		}

	commonFunctions.prototype.getScrollX = function()
		{
		var sx = 0;
		if (window.pageXOffset)
			{ sx = window.pageXOffset; }
		else if (document.documentElement && document.documentElement.scrollLeft)
			{ sx = document.documentElement.scrollLeft; }
		else if(document.body)
			{ sx = document.body.scrollLeft; }
		return sx;		
		}

	commonFunctions.prototype.getScrollY = function()
		{
		var sy = 0;
		if (window.pageYOffset)
			{ sy = window.pageYOffset; }
		else if (document.documentElement && document.documentElement.scrollTop)
			{ sy = document.documentElement.scrollTop; }
		else if(document.body)
			{ sy = document.body.scrollTop; }
		return sy;		
		}


	/* manipulate Class Names */
	commonFunctions.prototype.testClassName = function(e, clsNm)
		{
		var oclsNm = this.getDomElement(e).className;
		var re = new RegExp(clsNm);
		if(oclsNm.match(re)) { return true }
		else { return false }
		}

	commonFunctions.prototype.addClassName = function(e, cnm)
		{
		var ocnm = this.getDomElement(e).className;
		if(this.testClassName(e, cnm)) { return false }
		if(ocnm && ocnm != "") {cnm = ocnm + " " + cnm}
		this.getDomElement(e).className = cnm;
		}

	commonFunctions.prototype.removeClassName = function(e, cnm)
		{
		var ocnm = this.getDomElement(e).className;
		var re = new RegExp(cnm);
		if(this.testClassName(e, cnm))
			{
			ocnm = ocnm.replace(re, '');
			ocnm = ocnm.replace(/^ +| +$/, '');
			this.getDomElement(e).className = ocnm;
			}
		}
	
	commonFunctions.prototype.replaceClassName = function(e, ocnm, ncnm)
		{
		if(this.testClassName(e, ocnm))
			{ 
			this.removeClassName(e, ocnm);
			this.addClassName(e, ncnm);
			}
		}
	
	commonFunctions.prototype.toggleClassName = function(e, cnm1, cnm2)
		{
		var	clrCnm = cnm1;
		var	setCnm = cnm2;
		if(this.testClassName(e, cnm2))
			{
			clrCnm = cnm2;
			setCnm = cnm1;
			}
		this.replaceClassName(e, clrCnm, setCnm);
		}


	/* popup layers */
	commonFunctions.prototype.createDeflector = function(classname, zidx, id)
		{
		if(this.deflectorDiv) {document.body.removeChild(this.deflectorDiv); this.deflectorDiv = null;}
		this.deflectorDiv = document.createElement('div');
		if(classname) {this.deflectorDiv.className = classname;}
		if(zidx) {this.deflectorDiv.style.zIndex = zidx;}
		if(id) {this.deflectorDiv.id = id;}
		document.body.appendChild(this.deflectorDiv);
		this.deflectorDiv.innerHTML = "&nbsp;";
		this.deflectorDiv.style.position = "absolute";
		this.deflectorDiv.style.top = "0px";		
		this.deflectorDiv.style.left = "0px";		
		this.deflectorDiv.style.display = "block";
		
		//this.deflectorDiv.style.height = Math.max( this.getWindowInnerHeight(), this.getDocumentHeight() ) + "px";
		//if(this.isIE) { this.deflectorDiv.style.width = Math.max(this.getWindowInnerWidth(), this.getDocumentWidth()) + "px"; }
		//else { this.deflectorDiv.style.width = "100%"; }
		//this.deflectorDiv.style.width = Math.max(this.getWindowInnerWidth(), this.getDocumentWidth()) + "px";
		
		this.deflectorDiv.style.width = Math.max(this.getWindowInnerWidth(), this.getDocumentWidth()) + "px";
		this.deflectorDiv.style.height = Math.max(this.getWindowInnerHeight(), this.getDocumentHeight()) + "px";
		
		
		/* give it a default lookalike */
		if(!classname && !id)
			{
			this.deflectorDiv.style.backgroundColor = "#AAAAAA";
			this.deflectorDiv.style.opacity = "0.5";
			this.deflectorDiv.style.filter = "alpha(opacity=50)";
			}
		}

	commonFunctions.prototype.createPopup = function(innerhtml, classname, zidx, id)
		{
		if(this.popupDiv) {document.body.removeChild(this.popupDiv); this.popupDiv = null;}
		this.popupDiv = document.createElement('div');
		if(classname) {this.popupDiv.className = classname;}
		if(zidx) {this.popupDiv.style.zIndex = zidx;}
		if(id) {this.popupDiv.id = id;}
		document.body.appendChild(this.popupDiv);
		this.popupDiv.innerHTML = innerhtml;
		this.popupDiv.style.display = "block";
		this.popupDiv.style.position = "absolute";
		this.popupDiv.style.top = "50%";		
		this.popupDiv.style.left = "50%";
		var xpos = Math.max(0, this.getWindowInnerWidth()/2 - this.getDomElementWidth(this.popupDiv)/2 + this.getScrollX());
		var ypos = Math.max(0, this.getWindowInnerHeight()/2 - this.getDomElementHeight(this.popupDiv)/2 + this.getScrollY());
		this.popupDiv.style.left = xpos + "px";
		this.popupDiv.style.top = ypos + "px";
		}

	commonFunctions.prototype.openDefPopup = function(srcelem, zidx, clsnmpop, clsnmdef, idpop, iddef)
		{
		var zindex = 1000;			if(zidx) { zindex = zidx }
		var popupClassName = "";	if(clsnmpop) { popupClassName = clsnmpop; }
		var defClassName = "";		if(clsnmdef) { defClassName = clsnmdef; }
		var popupID = "";			if(idpop) { popupID = idpop; }
		var defID = "";				if(iddef) { defID = iddef; }
		this.createDeflector(defClassName, zindex,  defID);
		this.createPopup(this.getDomElement(srcelem).innerHTML, popupClassName, zindex+1,  popupID);
		var that = this;
		this.deflectorDiv.onclick = function(){that.closeDefPopup()}
		}

	commonFunctions.prototype.closeDefPopup = function()
		{
		if(this.popupDiv) {document.body.removeChild(this.popupDiv); this.popupDiv = null;}
		if(this.deflectorDiv) {document.body.removeChild(this.deflectorDiv); this.deflectorDiv = null;}
		}


	/* AJAX stuff */
	commonFunctions.prototype.ajaxLoader = function(url, e, cff, extf)
		{
		var that = this;
		this.ajaxLoaderRequest;
		this.ajaxLoaderURL = url;
		this.ajaxLoaderDestination = this.getDomElement(e);
		try
			{ this.ajaxLoaderRequest = new XMLHttpRequest(); } // Opera 8.0+, Firefox, Safari, IE7+
		catch (e) 
			{
			try 
				{ this.ajaxLoaderRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e) 
				{ 
				try 
					{ this.ajaxLoaderRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
				catch (e)
					{ return false; } // Browser not AJAX compatible
				}
			}
		
		this.ajaxLoaderRequest.onreadystatechange = function()
			{
			if(that.ajaxLoaderRequest.readyState == 4 && that.ajaxLoaderRequest.status == 200 ) 
				{
				that.ajaxLoaderDestination.innerHTML = that.ajaxLoaderRequest.responseText;
				that.ajaxLoaderFollower();
				}
			if(that.ajaxLoaderRequest.readyState == 4 && that.ajaxLoaderRequest.status != 200 ) 
				{
				that.ajaxLoaderDestination.innerHTML = "<span style='color:#FF0000; font-weight: bold;'>AJAX ERROR!</span>";
				that.ajaxLoaderFollower();
				}

			}

		this.ajaxLoaderRequest.open("GET", this.ajaxLoaderURL, true);
		this.ajaxLoaderRequest.send(null); 
	
	
		this.ajaxLoaderFollower = function()
			{
			if(cff && cff!='') { eval("this."+cff+";") }
			if(extf && extf!='') { eval(extf+";") }
			}
		}


	/* Dumb: ajaxloader without any statuscheck, use this for loading local files */
	commonFunctions.prototype.dumbAjaxLoader = function(url, e, cff, extf)
		{
		var that = this;
		this.ajaxLoaderRequest;
		this.ajaxLoaderURL = url;
		this.ajaxLoaderDestination = this.getDomElement(e);
		try
			{ this.ajaxLoaderRequest = new XMLHttpRequest(); } // Opera 8.0+, Firefox, Safari, IE7+
		catch (e) 
			{
			try 
				{ this.ajaxLoaderRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e) 
				{ 
				try 
					{ this.ajaxLoaderRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
				catch (e)
					{ return false; } // Browser not AJAX compatible
				}
			}
		
		this.ajaxLoaderRequest.onreadystatechange = function()
			{
			if(that.ajaxLoaderRequest.readyState == 4 ) 
				{
				that.ajaxLoaderDestination.innerHTML = that.ajaxLoaderRequest.responseText;
				that.ajaxLoaderFollower();
				}
			}
	
		this.ajaxLoaderRequest.open("GET", this.ajaxLoaderURL, true);
		this.ajaxLoaderRequest.send(null); 
	
	
		this.ajaxLoaderFollower = function()
			{
			if(cff && cff!='') { eval("this."+cff+";") }
			if(extf && extf!='') { eval(extf+";") }
			}
		}
	
	
	
	
	
	
	
	
	
	/* animated changes */
	/* use like this:  */
	commonFunctions.prototype.animate = function(ane, ans, ansobj)
		{
		new function(e, s, sobj)
				{
					var that = this;
					
					this.animateElem = e;
					this.animateStyle = s;
					this.animateStyleFrom = sobj.from;
					this.animateStyleTo = sobj.to;
					this.animateStyleUnit = sobj.unit;
					this.animateStyleTime = sobj.time;
					this.animateFollowFunc = sobj.follower;
					
					this.animateStyleDiff = this.animateStyleTo - this.animateStyleFrom;
					this.animateStartTime = new Date();
					this.animateTimer = setInterval( function() { that.doAnimation.call(that); }, 5 );
			
					
					this.doAnimation = function()
						{
						var that = this;
						this.animateNow = new Date();
						this.animateDiffTime = this.animateNow - this.animateStartTime;
						
						if (this.animateDiffTime > this.animateStyleTime)
							{
							this.doChangeStyle(this.animateStyleTo);
							if (this.animateFollowFunc)
								{ this.animateFollowFunc.call(this); }
							clearInterval(this.animateTimer);
							return;
							}
			
						this.percentage = (Math.floor((this.animateDiffTime / this.animateStyleTime) * 100) / 100);
						this.val = (this.animateStyleDiff * this.percentage) + this.animateStyleFrom;
						this.doChangeStyle(this.val);
						}	
			
					this.doChangeStyle = function(val)
						{
						switch (this.animateStyle) 
							{
							case 'opacity':
							this.animateElem.style[this.animateStyle] = val;
							this.animateElem.style.filter = 'alpha(opacity=' + val * 100 + ')';
							break;
							
							default:
							this.animateElem.style[this.animateStyle] = val + this.animateStyleUnit;
							break;
							}
						}

				}(this.getDomElement(ane), ans, ansobj)
		}




	/* add events to elements, too be MUCH improved !!! */

	/* use like this: CF.addEvent(window, 'load', myfuntion); */
	commonFunctions.prototype.addEvent = function(e, eventType, fn2do)
		{ 
		if (this.getDomElement(e).addEventListener) { this.getDomElement(e).addEventListener(eventType, fn2do, false) }
		else if (this.getDomElement(e).attachEvent) { this.getDomElement(e).attachEvent("on"+eventType, fn2do); }
		else { return false } 
		}
	
	/*
	add events functions width param, use like this: 
	CF.addEventPar(window, 'load', 'dosomething(a, b, c)');
	*/
	commonFunctions.prototype.addEventPar = function(e, eventType, fnName)
		{ 
		var that = this;
		if (this.getDomElement(e).addEventListener)
			{ this.getDomElement(e).addEventListener(eventType, function() { var f = eval(fnName + ";"); }, false); return true; }
		else if (this.getDomElement(e).attachEvent)
			{ 
			if(fnName.match('this')) 
				{
				if(!this.getDomElement(e).id) { this.getDomElement(e).id = this.getDomElement(e).uniqueID }
				var replacethis = this.getDomElement(e).id;
				fnName = fnName.replace('this', '"' + replacethis + '"') 
				}
			fn = function() { var f = eval(fnName + ";"); }
			this.getDomElement(e).attachEvent("on"+eventType, fn); 
			}
		else
			{ return false } 
		}

	return true
}





