/*
  2008-02-13
  
-------------------------------------------------------------------*/


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");
				return currentId;
			}
		}
		return false;
	},

	setActiveStyleSheet: function(id){
		for( var i=0; i<this.stylesheets.length; i++ ){
			if( this.stylesheets[i].getAttribute("id") == id ){
				this.createCookie( this.cookieName, id, 365 );
				this.stylesheets[i].disabled = true;
				this.stylesheets[i].disabled = false;
			}
			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").match(/stylesheet/i) ) {
				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(){
			var id = _this.getActiveStyleSheetId();
			if(id)
				_this.createCookie( _this.cookieName, id, 365 );
			//else
			//	_this.createCookie( _this.cookieName, links[0].getAttribute("id"), 365 );
		}, false);
	}
}



/*
  
  
-------------------------------------------------------------------*/

function FontSizeSelector( param ){
	this.className = "";
	this.buttons   = [];
	this.buttonTitle = {};
	this.displayBlockId = "";
	this.roi = null;

	for( var i in param ){
		this[i] = param[i];
	}

	if( this.className && this.buttons.length ){
		this.switcher = new StyleSwitcher( this.className );
		if( this.displayBlockId )
			this.display( this.displayBlockId );
	}
}


FontSizeSelector.prototype = {

	init: function( roiObject ){
		var activeStyleSheetId = '';
		var val = this.switcher.readCookie(this.switcher.cookieName);
		if( val ) {
			activeStyleSheetId = val;
		}
		else{
			var links = document.getElementsByTagName("LINK");
			if( !links ) return;
			for( var i=0; i<links.length; i++ ){
				var rel = links[i].getAttribute("rel");
				if( rel && rel.match(/stylesheet/i) && links[i].getAttribute("id") ) {
					if( !rel.match(/alternate/i) ){
						activeStyleSheetId = links[i].getAttribute("id")
					}
					else if( rel.match(/alternate/i) && links[i].disabled == false ){ // for IE
						links[i].disabled = true;
					}
				}
			}
		}
		if(!activeStyleSheetId) return;

		var _this = this;
		var i = 0;
		this.roi = roiObject;
		u.foreach( this.buttons, function( btn ){
			var el = u.$(btn.buttonId);
			if( btn.linkId == activeStyleSheetId ){
				if( _this.roi ){
					u.foreach( _this.roi.buttons, function( fsbtn ){
						if( fsbtn.id == btn.buttonId )
							_this.roi.activate( fsbtn, _this.roi.aSuffix );
					} );
				}
				if( el && btn.label )
					el.alt = el.title = _this.getButtonTitle(  btn.label, "current" );
			}
			else {
				if( el && btn.label )
					el.alt = el.title = _this.getButtonTitle(  btn.label, "default" );
			}
			i++;
		} );
	},

	display: function( id ){
		if( id )
			document.write('<style type="text/css" media="screen,projection,tv">#'+ id +' {display: block !important;}</style>');
	},

	getButtonTitle: function( label, type ){
		var reg = /%%label%%/i;
		var t = this.buttonTitle;
		if(t && t[type]){
			return t[type].replace( reg, label );
		}
		return label;
	},

	setFontSize: function( size ){
		var _this = this;
		u.foreach( this.buttons, function( btn ){
			if( u.$( btn.buttonId ) ){
				var el = u.$( btn.buttonId );
				if( btn.size == size ){
					_this.switcher.setActiveStyleSheet( btn.linkId );
					if(_this.roi) _this.roi.activate( u.$(btn.buttonId) );
					if( btn.label )
						el.alt = el.title = _this.getButtonTitle(  btn.label, "current" );
				}
				else {
					if(_this.roi) _this.roi.deactivate( u.$(btn.buttonId) );
					if( btn.label )
						el.alt = el.title = _this.getButtonTitle(  btn.label, "default" );
				}
			}
		} );
	}

};



