/*
  
  
-------------------------------------------------------------------*/


function StyleSwitcher( className, defaultId ){
	if(!className) return;
	this.className = className;
	this.defaultId = defaultId || '';
	this.stylesheets = [];
	this.cookieName = 'style-' + className;
	this.init();
}

StyleSwitcher.prototype = {

	getActiveStyleSheetId: function(){
		var currentId = "";
		for( var i=0; i<this.stylesheets.length; i++ ){
			if( !this.stylesheets[i].disabled ){
				currentId = this.stylesheets[i].getAttribute("id");
				break;
			}
		}
		return currentId;
	},

	setActiveStyleSheet: function(id){
		for( var i=0; i<this.stylesheets.length; i++ ){
			if( this.stylesheets[i].getAttribute("id") == id ){
				this.stylesheets[i].disabled = true;
				this.stylesheets[i].disabled = false;
				this.createCookie( this.cookieName, id, 365 );
			}
			else{
				this.stylesheets[i].disabled = true;
			}
		}
	},


	createCookie: function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},


	readCookie: function(name) {
		var nameEQ = 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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},


	init: function(){
		var links = document.getElementsByTagName("LINK");
		if( !links ) return;

		for( var i=0; i<links.length; i++ ){
			if( links[i].getAttribute("rel") && links[i].getAttribute("rel").indexOf("stylesheet") != -1 ) {
				if( u.hasClassName(links[i], this.className) && links[i].getAttribute("id") ){
					this.stylesheets.push(links[i]);
				}
			}
		}

		var c = this.readCookie(this.cookieName);
		var id = c? c : (this.defaultId? this.defaultId : null );
		if(id) this.setActiveStyleSheet(id);

		// event
		var _this = this;
		EventManager.addEvent(window, 'unload', function(){
			_this.createCookie( _this.cookieName, _this.getActiveStyleSheetId(), 365 )
		}, false);
	}
}




