/*
  2008-04-04
  
-------------------------------------------------------------------*/


// 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.stylesheets = [];
	this.cookieName = this.cookieNamePrefix + className;
	this.activeStyleSheetId = "";
	this.init();
}


StyleSwitcher.prototype = {

	getActiveStyleSheetId: function(){
		return this.activeStyleSheetId;
	},

	setActiveStyleSheet: function(id){
		for( var i=0,len=this.stylesheets.length; i<len; i++ ){
			if( this.stylesheets[i].getAttribute("id") == id ){
				this.createCookie( this.cookieName, id, 365 );
				this.stylesheets[i].disabled = true;
				this.stylesheets[i].disabled = false;
				this.activeStyleSheetId = id;
			}
			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,len=ca.length; i<len; 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;
	},


	hasClassName: function(el, className){
		if( typeof el == 'string' ) el = document.getElementById(el);
		if(!el.className) return false;
		return new RegExp("(^|\\s)" + className + "(\\s|$)" ).test( el.className );
	},


	init: function(){
		var links = document.getElementsByTagName("link");
		if( !links ) return;
		var _this = this;
		var tmpNotAlternateId = "";
		var preferredStyleSheetId = "";

		for( var i=0,len=links.length; i<len; i++ ){
			var rel = links[i].getAttribute("rel");
			var id  = links[i].getAttribute("id");
			if( rel && rel.match(/stylesheet/i) && id ){
				if( this.hasClassName(links[i], this.className) ){

					// 切り替え対象のスタイルシートを格納
					this.stylesheets.push(links[i]);

					if( !rel.match(/alternate/i) ){
						// 優先スタイルシートのID属性値を格納
						preferredStyleSheetId = links[i].getAttribute("id")
					}
					else if( rel.match(/alternate/i) && links[i].disabled == false ){ // for IE
						// IEでは代替スタイルシートであってもdisabled=falseなので、一旦trueに設定。必要かどうか要検討。
						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 );
		}, false);
	}
}



/*
  
  
-------------------------------------------------------------------*/

function FontSizeSelector( param ){
	this.className = "";
	this.buttons   = [];
	this.buttonTitle = {};
	this.displayBlockId = "";
	this.roi = null;

	for( var i in param ){
		this[i] = param[i];
	}

}


FontSizeSelector.prototype = {

	init: function( roiObject ){
		if( this.className && this.buttons.length ){
			var p = {};
			// 初回表示にしたいスタイルシートのID属性値を指定。オプション。通常は優先スタイルシートを表示させるべき。
			if( this.defaultLinkId ) p.defaultLinkId = this.defaultLinkId;
			// 同一ドメイン内で、別々のcookieを保持したい場合は指定。通常は未指定。
			if( this.cookieNamePrefix ) p.cookieNamePrefix = this.cookieNamePrefix;
	
			this.switcher = new StyleSwitcher( this.className, p );
			if( this.displayBlockId )
				this.display( this.displayBlockId );
		}
		var activeStyleSheetId = this.switcher.getActiveStyleSheetId();
		if(!activeStyleSheetId) return;

		var _this = this;
		this.roi = roiObject;

		for( var i=0,len=this.buttons.length; i<len; i++ ){
			var btn = this.buttons[i];
			var el = document.getElementById(btn.buttonId);
			if( btn.linkId == activeStyleSheetId ){
				if( _this.roi ){
					for( var j=0,len=_this.roi.buttons.length; j<len; j++ ){
						var  fsbtn = _this.roi.buttons[j];
						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
				if(btn) {
					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;
		for( var i=0,len=this.buttons.length; i<len; i++ ){
			var btn = this.buttons[i];
			var el = document.getElementById(btn.buttonId);
			if( el ){
				if( btn.linkId == size ){
					_this.switcher.setActiveStyleSheet( btn.linkId );
					if(_this.roi) _this.roi.activate( el );
				}
				else {
					if(_this.roi) _this.roi.deactivate( el );
				}
			}
		}
		this.setButtonTitle();
	}

};




/*
  
  
-------------------------------------------------------------------*/

var EventManager = {};

EventManager.list = [];

EventManager.removeEvent = function( obj, event, listener, useCapture ){
	useCapture = useCapture || false;
	if(obj.removeEventListener){
		obj.removeEventListener( event, listener, useCapture );
	} else if(obj.detachEvent){
		obj.detachEvent( "on"+event, listener );
	} else {
		//delete obj['on'+event];
	}
};

EventManager.addEvent = function( obj, event, listener, useCapture ){
	//EventManager.list.push(arguments);
	EventManager.list[EventManager.list.length] = arguments;
	useCapture = useCapture || false;
	if(obj.addEventListener){
		obj.addEventListener( event, listener, useCapture );
	} else if(obj.attachEvent){
		obj.attachEvent( "on"+event, listener );
	} else {
		var exists = obj['on'+event];
		obj['on'+event] = (exists)?
			function(){
				exists();
				listener();
			} : function() {
				listener();
			};
	}
};


EventManager.addLoadEvent = function( listener ){
	EventManager.addEvent( window, "load", listener, false );
};

EventManager.removeEventCache = function(){
	if(!EventManager.list) return;
	for( var i=0,len=EventManager.list.length; i<len; i++ ){
//		EventManager.removeEvent.apply( this, EventManager.list[i] );
		EventManager.removeEvent( EventManager.list[i][0], EventManager.list[i][1], EventManager.list[i][2], EventManager.list[i][3] );
	}
}


EventManager.addEvent(window, 'unload', EventManager.removeEventCache, false);


