/*--------------------------------------
	javascript sytlesheet manager
	version 2007-01-28 (same as 2006-10-24)
	toDos
		2006-10-24
			メニューを表示させない設定、この情報をCookieに登録
			Cookie適用期間を適宜更新させたい
		2007-01-28
			どっかで色々エラートラップが必要だと思うが大丈夫か？
--------------------------------------*/

//----- configurable variables -----
var noStyle = {
	"id": "style_none",
	"title": "no style"
}

var cookieName = "siteStyle";
var styleBoxAppendTargetId = "head-tools";
var selectBoxId = "css_select";
//----------------------------------



var calledStyle;

var linktags = document.getElementsByTagName("link");
var styles = new Array();

for (var styleIndex=0; styleIndex<linktags.length; styleIndex++) {
	if (linktags[styleIndex].rel.match(/stylesheet/)) {
		styles.push( { "id": linktags[styleIndex].id, "title": linktags[styleIndex].title } );
		
		if (!linktags[styleIndex].rel.match(/alternate/)) {
			calledStyle = linktags[styleIndex].id;
		}
	}
}

if (noStyle.id && noStyle.title) {
	styles.push(noStyle);
}

if (CookieManager.getCookie(cookieName)) {
	calledStyle = CookieManager.getCookie(cookieName);
	changeStyle(calledStyle);
}

var privfunc = window.onload;
window.onload = function() {
	if (privfunc) { privfunc(); }
	createStyleSelectBox();
}


/*------------------------------------------------------------------------------
	関数の定義
		changeStyle() : スタイルを指定されたものに変更
		ifStyleExist() : 指定idのstylesheetが存在するかどうかのチェック
		createStyleSelectBox() : スタイル選択ボックスの生成
------------------------------------------------------------------------------*/

// 関数 changeStyle : スタイルを指定されたものに変更

function changeStyle(calledStyleId) {
	if (!ifStyleExist(calledStyleId)) { return 0; }
	
	for (var styleIndex = 0; styleIndex < styles.length; styleIndex++) {
		// 全シートを一度オフに
		var presentStyle = document.getElementById(styles[styleIndex]["id"]);
		if (presentStyle) {
			presentStyle.rel = "alternate stylesheet";
			presentStyle.disabled = true;
		}
	}
	
	if (calledStyleId == noStyle.id) return;
	
	// 指定されたシートをオン
	var styleTarget = document.getElementById(calledStyleId)
	styleTarget.disabled = false;
	styleTarget.rel = "stylesheet";
}

// スタイルシートのプレビュー（HTML内のセレクトボックスで実行）
// select box 内なら changeStyle(this.item(this.selectedIndex).value);


// 関数 ifStyleExist : stylesheetが存在するかどうかのチェック

function ifStyleExist(checkId) {
	for (var styleIndex=0; styleIndex<styles.length; styleIndex++) {
		if (styles[styleIndex].id == checkId) {
			return true;
		}
	}
	
	return false;
}

// 関数 createStyleSelectBox : スタイルを選択するselect boxを出力

function createStyleSelectBox() {
	if (!document.getElementById(styleBoxAppendTargetId)) { return 0; }
	var box;
	
	box = document.createElement("div");
	box.id = "css_changer";
	
	var p = document.createElement("p");
	p.appendChild(document.createTextNode("style : "));
	
	var select = document.createElement("select");
	select.id = selectBoxId;
	
	for (var styleIndex = 0; styleIndex < styles.length; styleIndex++) {
		var option = document.createElement("option");
		option.value = styles[styleIndex].id;
		option.appendChild(document.createTextNode(styles[styleIndex].title));
		if (styles[styleIndex].id == calledStyle) {
			option.selected = true;
		}
		select.appendChild(option);
	}
	
	select.onchange = function() {
		changeStyle(this.item(this.selectedIndex).value);
		CookieManager.setCookie(
			cookieName,
			document.getElementById(selectBoxId).item(document.getElementById(selectBoxId).selectedIndex).value
		);
	}
	
	p.appendChild(select);
	box.appendChild(p);
	
	var appendTarget = document.getElementById(styleBoxAppendTargetId);
	appendTarget.appendChild(box);
}



