/**
* 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('page','siteadjust');
	this.mOptWidth		= '64em';
	
	/**
	* 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	= '99%';
			}
		}
	}
	
	/**
	* 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();
	}
	
	/**
	* 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();
	}
	
	/**
	* get styleswitcher code
	*
	* @return	string		code of styleswitcher
	*/
	this.GetStyleSwitcher = function()
	{
		if(!this.mDom) return false;
		
		return this.mHtml['bigger']+this.mHtml['resetsize']+this.mHtml['smaller']+this.mHtml['settings'];
	}
	
	/**
	* 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
	*/
	function GetPreferredStyleSheet()
	{
		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
*/
window.onload = 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);
}

var gSwitcherHtml				= new Array();
gSwitcherHtml['open']			= '<h4>Schriftgr&ouml;&szlig;e</h4><ul>';
gSwitcherHtml['close']			= '</ul>';
gSwitcherHtml['bigger']			= '<li><a href="#" onclick="gStyleSwitcher.SetFontSize(1)" title="Schrift vergr&ouml;&szlig;ern"><img src="/pics/styleswitcher/font_bigger.gif" alt="Schrift vergr&ouml;&szlig;ern" width="15" height="15" /></a><span class="nodisplay">.</span></li>'+"\n";
gSwitcherHtml['resetsize']		= '<li><a href="#" onclick="gStyleSwitcher.SetAbsFontSize(75)" title="Schriftgr&ouml;&szlig;e zur&uuml;cksetzen"><img src="/pics/styleswitcher/font_reset.gif" alt="Schriftgr&ouml;&szlig;e zur&uuml;cksetzen" width="15" height="15" /></a><span class="nodisplay">.</span></li>'+"\n";
gSwitcherHtml['smaller']		= '<li><a href="#" onclick="gStyleSwitcher.SetFontSize(-1)" title="Schrift verkleinern"><img src="/pics/styleswitcher/font_smaller.gif" alt="Schrift vergr&ouml;&szlig;ern" width="15" height="15" /></a><span class="nodisplay">.</span></li>'+"\n";
gSwitcherHtml['settings']		= '</ul><h4>Hintergrund- und Vordergrundfarben</h4><ul><li><a href="#" onclick="gStyleSwitcher.SetActiveStyleSheet(\'Schwarz auf wei&szlig;\')" title="Schwarz auf wei&szlig;"><img src="/pics/styleswitcher/color_high.gif" alt="Schwarz auf wei&szlig;" width="15" height="15" /></a><span class="nodisplay">.</span></li>'+"\n";
gSwitcherHtml['settings']		+= '<li><a href="#" onclick="gStyleSwitcher.SetPreferredStyleSheet()" title="Standardfarben wieder herstellen"><img src="/pics/styleswitcher/color_std.gif" alt="Standardfarben" width="15" height="15" /></a><span class="nodisplay">.</span></li>'+"\n";
gSwitcherHtml['settings']		+= '<li><a href="#" onclick="gStyleSwitcher.SetActiveStyleSheet(\'Gelb auf blau\')" title="Gelb auf blau"><img src="/pics/styleswitcher/color_invert.gif" alt="Gelb auf blau" width="15" height="15" /></a><span class="nodisplay">.</span></li>'+"\n";

if(document.getElementById){
	var gStyleSwitcher		= new StyleSwitcher(75,gSwitcherHtml);
}
