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.createCookie( this.cookieName, id, 365 );
				if( this.stylesheets[i].getAttribute("rel") == "alternate stylesheet" ){
					this.stylesheets[i].setAttribute("rel", "stylesheet");
				}
				
				this.stylesheets[i].disabled = true;
				this.stylesheets[i].disabled = false;
			}
			else{
				if( this.stylesheets[i].getAttribute("rel") == "stylesheet" )
					this.stylesheets[i].setAttribute("rel", "alternate stylesheet")

				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);
	}
}


function FontSizeSelector( cssClassName, buttons, dispBlockId ){
	this.switcher = new StyleSwitcher( cssClassName, 'fontsize-medium' );
	this.buttons = buttons;
	this.blockId = dispBlockId || '';
	this.counter = 0; //addition for eneos
	if( this.blockId )
		this.display();
}

FontSizeSelector.prototype = {

	init: function(){},

	display: function(){
		if( this.blockId )
			document.write('<style type="text/css" media="screen,projection,tv">#'+ this.blockId +' {display: block !important;}</style>');
	},

	setActiveButton: function( roiObject ){
		var activeStyleSheetId = this.switcher.getActiveStyleSheetId();
		var _this = this;
		var i = 0;
		u.foreach( this.buttons, function( btn ){
			if( btn.linkId == activeStyleSheetId ){
				_this.setFontSize( btn.size );
				roiObject.activate( roiObject.buttons[i], roiObject.aSuffix );
			}
			i++;
		} );
	},

	setFontSize: function( size ){
		var _this = this;
		//alert(document.getElementById('iFR1').contentWindow.fsSelector);
		u.foreach( this.buttons, function( btn ){
			if( u.$( btn.buttonId ) ){
				var el = u.$( btn.buttonId );
				if( btn.size == size ){
					if(_this.counter){
						_this.switcher.setActiveStyleSheet( btn.linkId );
						if(document.getElementById('iFR1'))document.getElementById('iFR1').contentWindow.fsSelector.switcher.setActiveStyleSheet( btn.linkId );
						if(document.getElementById('iFR2'))document.getElementById('iFR2').contentWindow.fsSelector.switcher.setActiveStyleSheet( btn.linkId );
					}
					_this.counter++;
					if( btn.label )
						el.alt = el.title = btn.label;
				}
				else {
					if( btn.label )
						el.alt = el.title = btn.label;
				}
			}
		} );
	}
};



