/*
  2008-04-09
  
-------------------------------------------------------------------*/


// function StyleSwitcher( className, defaultLinkId, cookieNamePrefix ){

function StyleSwitcher( className ){

	if(!className) return;
	this.className = className;

	var param = arguments[1];
	// this.defaultLinkId = defaultLinkId || '';
	this.defaultLinkId    = ( param && typeof param["defaultLinkId" ]   != "undefined" )? param["defaultLinkId"]    : '';
	this.cookieNamePrefix = ( param && typeof param["cookieNamePrefix"] != "undefined" )? param["cookieNamePrefix"] : '';
	this.cookieDomainName = ( param && typeof param["cookieDomainName"] != "undefined" )? param["cookieDomainName"] : '';

	this.stylesheets = [];
	this.cookieName = this.cookieNamePrefix + className;
	this.activeStyleSheetId = "";
	this.init();
}


StyleSwitcher.prototype = {

	getActiveStyleSheetId: function(){
		return this.activeStyleSheetId;
	},

	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.cookieDomainName );
				this.stylesheets[i].disabled = true;
				this.stylesheets[i].disabled = false;
				this.activeStyleSheetId = id;
			}
			else{
				this.stylesheets[i].disabled = true;
			}
		}
	},


	createCookie: function(name,value,days,domain) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else expires = "";
		var domainName = "";
		if (domain) domainName = "; domain=" + domain;
		document.cookie = name+"="+value+expires+"; path=/"+domainName;
	},


	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;
		var _this = this;
		var tmpNotAlternateId = "";
		var preferredStyleSheetId = "";

		for( var i=0; i<links.length; i++ ){
			var rel = links[i].getAttribute("rel");
			var id  = links[i].getAttribute("id");
			if( rel && rel.match(/stylesheet/i) && id ){
				if( u.hasClassName(links[i], this.className) ){

					this.stylesheets.push(links[i]);

					if( !rel.match(/alternate/i) ){
						preferredStyleSheetId = links[i].getAttribute("id")
					}
					else if( rel.match(/alternate/i) && links[i].disabled == false ){ // for IE
						links[i].disabled = true;
					}
				}
			}
		}

		var cookieVal = this.readCookie(this.cookieName);
		this.activeStyleSheetId = cookieVal?
									cookieVal:
										this.defaultLinkId?
											this.defaultLinkId:
												preferredStyleSheetId?
													preferredStyleSheetId:
														null;

		if( this.activeStyleSheetId )
			this.setActiveStyleSheet( this.activeStyleSheetId );
		else
			return;

		// event
		EventManager.addEvent(window, 'unload', function(){
			var id = _this.getActiveStyleSheetId();
			if(id) _this.createCookie( _this.cookieName, id, 365, _this.cookieDomainName );
		}, 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 ){
		var p = {};
		if( this.defaultLinkId ) p.defaultLinkId = this.defaultLinkId;
		if( this.cookieNamePrefix ) p.cookieNamePrefix = this.cookieNamePrefix;
		if( this.cookieDomainName ) p.cookieDomainName = this.cookieDomainName;

		this.switcher = new StyleSwitcher( this.className, p );
		if( this.displayBlockId )
			this.display( this.displayBlockId );
	}
}


FontSizeSelector.prototype = {

	init: function( roiObject ){
		var activeStyleSheetId = this.switcher.getActiveStyleSheetId();
		if(!activeStyleSheetId) return;

		var _this = this;
		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 );
					} );
				}
			}
		} );
		this.setButtonTitle();
	},

	setButtonTitle: function(){
		for( var i=0,len=this.buttons.length; i<len; i++ ){
			var btnParam = this.buttons[i];
			if( btnParam.buttonId && btnParam.label && btnParam.linkId ){
				var btn = document.getElementById(btnParam.buttonId);
				var type = ( btnParam.linkId == this.switcher.getActiveStyleSheetId() )? "current" : "default";// current=selected=active
				btn.title = btn.alt = this.createButtonTitle( btnParam.label, type );
			}
		}
	},

	createButtonTitle: function( label, type ){
		var reg = /%%label%%/i;
		var t = this.buttonTitle;
		if(t && t[type]){
			return t[type].replace( reg, label );
		}
		return label;
	},

	display: function( id ){
		if( id )
			document.write('<style type="text/css" media="screen,projection,tv">#'+ id +' {display: block !important;}</style>');
	},

	setFontSize: function( size ){
		var _this = this;
		u.foreach( this.buttons, function( btn ){
			if( u.$( btn.buttonId ) ){
				var el = u.$( btn.buttonId );
				if( btn.linkId == size ){
					_this.switcher.setActiveStyleSheet( btn.linkId );
					if(_this.roi) _this.roi.activate( u.$(btn.buttonId) );
				}
				else {
					if(_this.roi) _this.roi.deactivate( u.$(btn.buttonId) );
				}
			}
		} );
		this.setButtonTitle();
	}

};




