/**
 * @fileOverview *************************
 * 
 * @version 0.0.1_2009-06-26
 * @requires jquery.js
 */
/* ------------------------------------------------------------------------------ */


/* ============================================================================== */

/**
 * スタイルシートの切替処理。
 * @class スタイルシートの切替処理。
 * @constructor
 * @param {String} className link要素のclass属性値。
 * @param {String} defaultId デフォルトでアクティブに設定したいlink要素のid属性値。
 */
function StyleSwitcher( className, defaultId ){
	if(!className) return;

	/** link要素のclass属性値。
		@type String const @private */
	this.className = className;

	/** デフォルトでアクティブに設定したいlink要素のid属性値。
		cookieにidがあれば、そちらを優先。
		これって必要？
		@type String const @private */
	this.defaultId = defaultId || null;

	/** 切り替えるlink要素格納用配列。
		@type Array @private */
	this.stylesheets = [];

	/** クッキーに保存する際の名前のプレフィックス。
		@type String const @private */
	this.cookieName = 'style-' + className;

	/** クッキーの寿命設定。日数を指定。
		@type Number const @private*/
	this.cookieExpires = 365;

	/**  */
	this.init();
}

/* ----------------------------------------------------- */
/**
 * 初期化処理。
 * this.classNameを持つlink要素の列挙と、cookieの読み込み、イベントの設定
 * @function
 */
StyleSwitcher.prototype.init = function(){
	var links = document.getElementsByTagName("LINK");
	if( !links ) return;
	var _this = this;

	// 対象のlink要素を取得。
	for (var i=0; i<links.length; i++) {
		if (links[i].getAttribute("rel") && links[i].getAttribute("rel").match(/stylesheet/i)) {
			if ($(links[i]).hasClass(this.className) && links[i].getAttribute("id")) {
			//if( u.hasClassName(links[i], this.className) && links[i].getAttribute("id") ){
				this.stylesheets.push(links[i]);
			}
		}
	}

	// クッキーを読み込み、IDがセットされていればそのIDを持つスタイルシートをアクティブに。
	var c = this.readCookie(this.cookieName);
	var id = c? c : (this.defaultId? this.defaultId : null );
	if(id) this.setActiveStyleSheet(id);

	// イベントの設定。画面のunloadイベント時に、アクティブなスタイルシートのIDをクッキーに保存。
	$(window).unload(function() {
		var id = _this.getActiveStyleSheetId();
		if (id) {
			_this.createCookie(_this.cookieName, id, _this.cookieExpires);
		}
	});
}


/* ----------------------------------------------------- */
/**
 * 現在アクティブなスタイルシートのid属性値を取得
 * @function
 * @returns {String} アクティブなスタイルシートのid属性値。
 */
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;
}

/* ----------------------------------------------------- */
/**
 * 指定したスタイルシートをアクティブにする。
 * その他のスタイルシートはdisabled=trueに設定。
 * @function
 * @param {String} id アクティブにするスタイルシートのid属性値。
 */
StyleSwitcher.prototype.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;
		}
	}
}

/* ----------------------------------------------------- */
/**
 * クッキーの生成
 * @function
 * @param {String} name クッキーに設定するname
 * @param {String} value クッキーに設定するvalue
 * @param {Number} days クッキーの有効期限
 */
StyleSwitcher.prototype.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=/";
}

/* ----------------------------------------------------- */
/**
 * クッキーの読み込み。
 * @function
 * @param {String} name クッキーから読み込むname
 */
StyleSwitcher.prototype.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;
}



/* ============================================================================== */
/**
 * 文字サイズ切替ボタン。
 * @class 文字サイズ切替ボタン。
 * @constructor
 * @param {Object} param 設定
 */
function FontSizeSelector(options){

	this.className      = options.className;// || "fontsize";
	this.blockId        = options.blockId;// || "fontsize-selector";
	this.roi            = options.rollover;
	this.buttonsSetting = options.buttonsSetting;

	this.buttons;
	this.switcher = new StyleSwitcher(this.className);

	this.activeStyleSheetId = null;

	this.init();
}


/* ----------------------------------------------------- */
/**
 * 初期化処理。
 * @function
 */
FontSizeSelector.prototype.init = function(){
	var _this = this;

	// クッキーを読み込み値を取得。無ければalternateの有無をチェック。
	var val = this.switcher.readCookie(this.switcher.cookieName);
	if (val) {
		this.activeStyleSheetId = val;
	}
	else {
		var links = $("link");
		if (links.length) {
			links.each(function() {
				var link = $(this);
				var id =link.attr("id");
				var rel = link.attr("rel");
				var disabled = link.attr("disabled");

				if (rel && rel.match(/stylesheet/i) && id) {
					if (!rel.match(/alternate/i)) {
						_this.activeStyleSheetId = id;
					}
					else if (rel.match(/alternate/i) &&  disabled == false) {// for IE
						link.attr({"disabled": true});
					}
				}
			});
		}
	}
	if (!this.activeStyleSheetId) return;


	var sel = [];
	for (var size in this.buttonsSetting) {
		var o = this.buttonsSetting[size];
		sel.push("#" + o.buttonId);
	}
	var selector = sel.join(", ");

	this.buttons = $(selector);


	for (var size in this.buttonsSetting) {
		var o = this.buttonsSetting[size];
		var btn = $("#" + o.buttonId);
		if (btn.length) {
			if (o.styleId == this.activeStyleSheetId) {
				if (this.roi) {
					$.each(this.roi.buttons, function(index) {
						if (this.id == o.buttonId) {
							_this.roi.activate(this , _this.roi.aSuffix);
						}
					});
				}
				btn.attr({"alt": o.titleSelected, "title": o.titleSelected});
			}
			else {
				btn.attr({"alt": o.title, "title": o.title});
			}
		}
	}
}


/* ----------------------------------------------------- */
/**
 * 押された文字サイズボタンに応じてスタイルシートの変更、
 * ボタンのロールオーバーの設定変更。
 * @function
 * @param {String} size 各ボタンのbuttonsSetting[n].styleId値。
 */
FontSizeSelector.prototype.setFontSize = function(size){
	var _this = this;
//	if (this.activeStyleSheetId == "fontsize-"+size) return;

	this.buttons.each(function() {
		var tmp = $(this).attr("id").split("-");
		var btnSize = tmp[tmp.length-1];
		////var bs = _this.buttonsSetting[size];
		var bs = _this.buttonsSetting[btnSize];

		if (btnSize && btnSize.match(size)) {
			_this.switcher.setActiveStyleSheet(bs.styleId);
			$(this).attr({"alt": bs.titleSelected, "title": bs.titleSelected});
		}
		else {
			$(this).attr({"alt": bs.title, "title" : bs.title});
		}
	});
}





