/*** styleswitcher class** @author		Frank Schnappenberger* @copyright	triple-i new media system design* @version		1.0.0*//*** constructor** @param	integer		base font size in percent* @param	array		html code array (disBigger, disSmaller, smaller, bigger, settings, start, close)*/function StyleSwitcher(baseSize, htmlCode){	this.mDom			= (document.getElementById);	this.mBaseSize		= baseSize;	this.mBody			= null;	this.mFontSize		= 0;	this.mIncrement		= 10;	this.mBiggerBound	= 500;	this.mSmallerBound	= 50;	this.mHtml			= htmlCode;	this.mLifetime		= 365;	this.mStyleswitcher	= null;	this.mScaleElements	= new Array();	this.mOptWidth		= '60em';		/**	* init class	*/	this.Init = function()	{		if(!this.mDom) return false;				this.mStyleswitcher	= document.getElementById('styleswitcher');		this.mBody			= document.getElementsByTagName('body');				var check_elements	= new Array();				for(var i=0;i<this.mScaleElements.length;i++){			check_elements[i]	= document.getElementById(this.mScaleElements[i]);						if(check_elements[i]){				check_elements[i].style.maxWidth	= '98%';			}		}	}		/**	* scaling body of page or not?	*	* @param	boolean		recursion	*/	this.Scale = function(recurse)	{		if(!this.mDom) return false;				var check_elements	= new Array();				for(var i=0;i<this.mScaleElements.length;i++){			check_elements[i]	= document.getElementById(this.mScaleElements[i]);		}				if(check_elements[0]){			// horizontal scroll			if(check_elements[0].offsetWidth+10>document.body.clientWidth){							for(var i=0;i<this.mScaleElements.length;i++){					check_elements[i].style.width	= (document.body.clientWidth-10)+'px';				}								if(check_elements[0].offsetWidth+10>document.body.clientWidth){					this.Scale(false);				}			}else{				for(var i=0;i<this.mScaleElements.length;i++){					check_elements[i].style.width	= this.mOptWidth;				}								if(!recurse){					this.Scale(true);				}			}		}	}		/**	* set font size	*	* @param	integer		zoom (- smaller, + larger)	* @return	boolean		always false	*/	this.SetFontSize = function(zoom)	{		if(!this.mDom) return false;		var font_size	= this.mBody[0].style.fontSize;				if(this.mFontSize==0){			this.mFontSize	= this.mBaseSize;		}				if(this.mFontSize<=this.mBiggerBound && this.mFontSize>=this.mSmallerBound){			this.mFontSize					= (this.mFontSize+zoom*this.mIncrement);			this.mBody[0].style.fontSize	= this.mFontSize+'%';			//this.Scale(false);		}				if(this.mStyleSwitcher){			this.mStyleswitcher.innerHTML	= this.GetStyleSwitcher();		}				this.ToggleBackground();	}		this.ToggleBackground = function()	{		var p = document.getElementsByTagName('p');					for(var i=0;i<p.length;i++){			if(this.mBaseSize!=this.mFontSize){				p[i].style.backgroundImage = 'none';			}else{				p[i].style.backgroundImage = '';			}		}	}		/**	* set font size absolute	*	* @param	integer		font size in percent	* @return	boolean		always false	*/	this.SetAbsFontSize = function(fontSize)	{		if(!this.mDom) return false;				// take style sheets font size		if(fontSize==0){			this.mFontSize					= 0			this.mBody[0].style.fontSize	= '';		}else{			this.mFontSize					= Math.round(fontSize);			this.mBody[0].style.fontSize	= this.mFontSize+'%';		}				//this.Scale(true);				if(this.mStyleSwitcher){			this.mStyleswitcher.innerHTML	= this.GetStyleSwitcher();		}				this.ToggleBackground();	}		/**	* get styleswitcher code	*	* @return	string		code of styleswitcher	*/	this.GetStyleSwitcher = function()	{		if(!this.mDom) return false;				if(this.mFontSize>=this.mBiggerBound && false){			return this.mHtml['disBigger']+this.mHtml['smaller']+this.mHtml['settings'];		}else if(this.mFontSize<=this.mSmallerBound && this.mFontSize!=0 && false){			return this.mHtml['bigger']+this.mHtml['disSmaller']+this.mHtml['settings'];		}else{			return this.mHtml['settings']+this.mHtml['bigger']+this.mHtml['smaller'];		}	}		/**	* cookie helper to write cookie	*	* @param	string		name of cookie	* @param	string		value for cookie	*/	this.CreateCookie = function(name,value)	{		var date = new Date();		date.setTime(date.getTime()+(this.mLifetime*24*60*60*1000));					var expires = "; expires="+date.toGMTString();				// set cookie domain wide		document.cookie = name+"="+value+expires+"; path=/";	}		/**	* read cookie and return value	*/	this.ReadCookie = function(name)	{		var name_eq	= name + "=";		var ca 		= document.cookie.split(';');				for(var i=0; i<ca.length; i++){			var c 	= ca[i];						while (c.charAt(0)==' '){				c = c.substring(1,c.length);			}						if (c.indexOf(name_eq)==0){				return c.substring(name_eq.length,c.length);			}		}				return '';	}		/**	* set default stylesheet	*	* @return	boolean		always false	*/	this.SetPreferredStyleSheet = function()	{		if(!this.mDom) return false;				var a;				// iterate all link tags		for(var i=0; (a=document.getElementsByTagName("link")[i]); i++){					// is this link a style def?			if(a.getAttribute("rel").indexOf("style")!=-1 && a.getAttribute("title")){							// disable it				a.disabled = true;								// default style?				if(a.getAttribute("rel").indexOf("alt")==-1){					a.disabled = false;					this.SetAbsFontSize(0);				}			}		}	}		/**	* get default style title	*	* @return	string		title of active stylesheet	*/	this.GetPreferredStyleSheet = function()	{		if(!this.mDom) return false;				var a;				// iterate all link tags		for(var i=0; (a=document.getElementsByTagName("link")[i]); i++){					// search first non-alternative style with title			if(a.getAttribute("rel").indexOf("style")!=-1 && a.getAttribute("rel").indexOf("alt")==-1 && a.getAttribute("title")){				return a.getAttribute("title");			}		}				return '';	}		/**	* set active stylesheet by title	*	* @param	string		title of new stylesheet	* @return	boolean		always false	*/	this.SetActiveStyleSheet = function(title)	{		if(!this.mDom) return false;		if(!title) return false;				var a;				// iterate all link tags		for(var i=0; (a=document.getElementsByTagName("link")[i]); i++){					// is this link a style def?			if(a.getAttribute("rel").indexOf("style")!=-1 && a.getAttribute("title")){							// disable it				a.disabled = true;								// this is the right style? enable it				if(a.getAttribute("title")==title){					a.disabled = false;				}			}		}	}		/**	* get active stylesheet title	*	* @return	string		title of active stylesheet	*/	this.GetActiveStyleSheet = function()	{		if(!this.mDom) return false;				var a;				// iterate all link tags		for(var i=0; (a=document.getElementsByTagName("link")[i]); i++){					// is this the active style?			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled){				return a.getAttribute("title");			}		}				return '';	}}/*** set onload event*/InitStyle = function(e){	if(!document.getElementById) return true;	gStyleSwitcher.Init();		var cookie		= gStyleSwitcher.ReadCookie("fontsize");	var font_size	= cookie!='' ? cookie : gStyleSwitcher.mBaseSize;	gStyleSwitcher.SetAbsFontSize(font_size);		var cookie 		= gStyleSwitcher.ReadCookie("style");	var title 		= cookie!='' ? cookie : gStyleSwitcher.GetPreferredStyleSheet();	gStyleSwitcher.SetActiveStyleSheet(title);}/*** set onunload event*/window.onunload = function(e){	if(!document.getElementById) return true;		gStyleSwitcher.CreateCookie("fontsize", gStyleSwitcher.mFontSize);		var title 	= gStyleSwitcher.GetActiveStyleSheet();	gStyleSwitcher.CreateCookie("style", title);}
