/**
* Do not remove
*
* CSS Changer
* @author BF Engineering Finland, Jaakko Suutarla
* @email info@bf-engineering.fi
* @web www.bf-engineering.fi
* @version 1.3
* @comments/changes Hmm some changes to speed up... 
* @uses Prototype JavaScript framework, version 1.4.0
*	@(c) 2005 Sam Stephenson <sam@conio.net>
* 	@Licence Prototype is freely distributable under the terms of an MIT-style license.
*	For details, see the Prototype web site: http://prototype.conio.net/
*
* Do not remove
*/


var cssChange = Class.create();
cssChange.prototype = {
	initialize: function(){
		/**************				    	     ****
		*****			CONF		************/
		
		/* ÄLÄ KOSKE MUUALLE KUIN TÄHÄN OSAAN TAI NO KOSKE VAAN :) */

		/* <link tagilla on attribuutti id jonka arvo on style eli tällä sidotaan se että muutetaan tämän <link tagin arvoa href */
		this.cssnode = $('style');
		
		/* Toiminnan suorittava elementin tagi mallia class="change_css"  eli sisältöön luodaan vaan vaikka <a class="change_css"></a>*/
		this.ClassName='change_css';
		
		/* Tyylitiedostojen määrittely ja napin määrittely*/ 
		/* Tähän annatta tyylitiedoston polun mallia css/style.css */

		/* Tyylitiedosto numero yksi */
		this.style1 = 'fileadmin/template/css/style-handheld.css';

		/* Tyylitiedoston kutsun tekevän napin teksti */
		this.buttontext1 = 'Computer edition';

		/* Tyylitiedosto numero kaksi */
		this.style2 = 'fileadmin/template/css/style-screen.css';

		/* Tyylitiedoston kutsun tekevän napin teksti */
		this.buttontext2 = 'Mobile edition';

		/**************				   	    ****
		*****			CONF		************/
		
	},

	BindElementbyClass: function(classname) {
		var classNodes = document.all ? document.all : document.getElementsByTagName('*');
		for (i=0; i<classNodes.length; i++){
			if (classNodes[i].classname == classname || classNodes[i].className == classname){
				classNodes[i].onclick = this.bindedSetStyle.bind(this);
			}
		}
	},
	bindedSetStyle: function(){
		if(this.cookiesAlright === true)
		{		
			this.getCurrent();
	                this.setCurrentCookies(this.href,this.buttontext);
        	        this.cssnode.setAttribute('href',this.href);
                	this.setTextControllers(this.buttontext);
		}
	},
	setTextControllers: function(text){
		var inc=0;
		var classNodes=document.all ? document.all : document.getElementsByTagName('*');
		for (i=0; i<classNodes.length; i++){
			if (classNodes[i].className=='change_css'){
				classNodes[i].innerHTML = text;
			}
		}
	},
	cookieSetStyle: function(){
		if(this.getCookie('href') == undefined){
			this.href = this.style2;
			this.buttontext = this.buttontext2;
			this.cssnode.setAttribute('href',this.href);
			this.setTextControllers(this.buttontext);
			return 1;
		}
		else{
			this.cssnode.setAttribute('href',this.getCookie('href'));
			this.setTextControllers(this.getCookie('buttontext'));
			return 2;
		}
	},
	getCurrent: function(){
		cur = this.cssnode.getAttribute('href');
		switch(cur){
			case this.style1:
				this.href = this.style2;
				this.buttontext = this.buttontext2;
				break;
			case this.style2:
				this.href = this.style1;
				this.buttontext = this.buttontext1;
				break;
			default:
				this.href = this.style2;
				this.buttontext = this.buttontext2;
				this.cssnode.setAttribute('href',this.href);
				this.button.innerHTML = this.buttontext;
				break;
		}
		
	},

	setCookie: function (name, value, expires, path, domain, secure) {
    		document.cookie= name + "=" + escape(value) +
        	((expires) ? "; expires=" + expires.toGMTString() : "") +
        	((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
        	((secure) ? "; secure" : "");

		if(document.cookie.length > 0)
			return(1);
		else
			return(2);
	},

	setCurrentCookies: function(ref,text){
		this.setCookie('href',ref);
		this.setCookie('buttontext',text);

	},

	testCookie: function(){
		return(this.setCookie('test','1234'));
	},

	getCookie: function (name) {
   		var dc = document.cookie;
		var prefix = name + "=";
   		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) {
        		begin = dc.indexOf(prefix);
        		if (begin != 0) return null;
    			} else {	begin += 2;	}
    		var end = document.cookie.indexOf(";", begin);
   		if (end == -1) {
        		end = dc.length;
    		}
   		 return unescape(dc.substring(begin + prefix.length, end));
	},
	
	DuckAndHide: function() {
		Element.remove(this.button);
	}
	
}

var css = null;
function init(){
        css = new cssChange();
        if(css.testCookie() == 1){
                css.cookieSetStyle();
                css.cookiesAlright = true;
        } else {
                css.DuckAndHide();
		css = null;
        }
}

init();

function BindButtons(){
	if(css != null){
		css.cookieSetStyle()
        	css.BindElementbyClass(css.ClassName);
	}
}

Event.observe(window, 'load', BindButtons, false);


