/*
|  ユーティリティ関数群  common.js (2004/01/04)
|  Copyright (C) 2003 nero
|  URL: http://www2.u-netsurf.ne.jp/~alt/
*/

var d = document;

/*-----------------------------------------------
			フレーム強制解除
-------------------------------------------------*/
if (self != top) top.location.href = self.location.href;


/*-----------------------------------------------
			汎用 ブラウザ判別
-------------------------------------------------*/
function getUA()
{
	this.str = navigator.userAgent;
	this.win = (this.str.indexOf("Win") > -1);
	this.mac = (this.str.indexOf("Mac") > -1);
	this.OS  = ((this.win && 'win') || (this.mac && 'mac') || void(0));

	if(d.all)
	{
		if(d.bgColor)
		{
			if(d.implementation
				&& d.implementation.hasFeature('HTML','1.0'))
					this.type = "ie6";
			else	this.type = "ie5";
		}
		else if(this.str.match(/Opera/i))
			this.type = (d.createTextNode)?	"op7": "op6";
	}
	else if(d.getElementById)
	{
		if(this.mac && this.str.match(/Safari/i)) this.type = "safari";
		else if(this.str.match(/Gecko/i)) this.type = "gecko";
		else if(this.str.match(/KHTML/i)) this.type = "khtml";
	}
	else if(d.layers) this.type = "nn4";
	else this.type = void(0);

	switch(this.type)
	{
		case 'ie6':		this.ie6	= true;	break;
		case 'ie5':		this.ie5	= true;	break;
		case 'gecko':	this.gecko	= true;	break;
		case 'op7':		this.op7	= true;	break;
		case 'op6':		this.op6	= true;	break;
		case 'safari':	this.safari	= true;	break;
		case 'khtml':	this.khtml	= true;	break;
		case 'nn4':		this.nn4	= true;	break;
	}

	this.good = (this.ie6 || this.gecko || this.op7 || false);
}


/*-----------------------------------------------
	汎用 Cookie  domain[optional], path[optional]
		.get[key] または .get.key
		.set(key, data, limit[optional])
		.del(key)
-------------------------------------------------*/
function CookieManager(domain, path)
{
	domain	= (domain)?	(';domain=' + domain): '';
	path	= (path)?	(';path=' + path): ';path=/';

	this.get = new Array();
	var rawData = (d.cookie)? d.cookie.split(';'): new Array();
	for(var	i =	0; i < rawData.length; i++)
	{
		if(rawData[i].split('=').length < 2) continue;
		this.get[rawData[i].split('=')[0].match(/\S.*\S/)]
			= unescape(rawData[i].split('=')[1].match(/\S.*\S/));
	}

	this.set = function(key, data, limit)
	{
		this.get[key] = data;
		limit = (limit)? limit: 30; // default
		var date = '';
		today = new Date();
		today.setTime(today.getTime() + 1000*60*60*24*limit);
		date = ';expires=' + today.toGMTString();
		d.cookie = key + '=' + escape(data) + date + domain + path;
	}

	this.del = function(key)
	{
		d.cookie = key +'=dummy;expires=Fri,31-Dec-1970 00:00:01;'
				 + domain + path;
		this.get[key] = '';
	}
}

/*-----------------------------------------------
	外部スタイルシート制御
	.titles
		外部 CSS のタイトルを格納した配列
		デフォルトシート, 代替シート, 無効 の順
	.defaultNo
		デフォルトシートの番号
	.defaultTitle
		デフォルトシートのタイトル
	.set(title)
		指定タイトルのスタイルに切り替える
	.disable
		外部スタイルシート全無効
-------------------------------------------------*/
function linkedCSS()
{
	var ss = d.styleSheets;

	this.titles = new Array();
	for(var i=0, len=ss.length; i<len; i++)
	{
		var t = ss.item(i).title;
		if(!ss.item(i).disabled && t)
		{
			this.defaultNo = i;
			this.defaultTitle = t;
			this.titles.unshift(t);
		}
		else
			this.titles[i] = (t)? t: '';
	}
	this.titles.push("スタイル無効");

	this.set = function(title)
	{
		var title_match = false;

		for(var i=0, len=ss.length; i<len; i++)
		{
			var cntTitle = ss.item(i).title;

			if(cntTitle==title)
			{
				ss.item(i).disabled = false;
				title_match = true;
			}
			else if(!cntTitle)	ss.item(i).disabled = false;
			else				ss.item(i).disabled = true;
		}

		// デフォルトシートを適用
		if(!title_match && this.defaultTitle)
			ss.item(this.defaultNo).disabled = false;
	}

	this.disable = function ()
	{
		for(var i=0, len=ss.length; i<len; i++)
			ss.item(i).disabled	 = true;
	}
}

/*-----------------------------------------------
			リンク要素の内容を取得
-------------------------------------------------*/
function getLink()
{
	var link = d.getElementsByTagName("link");
	var tmp = new Array(2);
	var tmplength = 0;

	for(var itm, rel, i=0, len=link.length; i<len; i++)
	{
		itm = link.item(i);
		rel = itm.getAttribute("rel");

		if(rel.match(/\s*next\s*/))
			this.next = setType(itm, "次ページ");
		else if(rel.match(/\s*(prev|previous)\s*/))
			this.prev = setType(itm,"前ページ");
		else if(rel.match(/\s*(start|parent|index|contents)\s*/))
			this.cover = setType(itm, "表紙");
		else if(rel.match(/\s*help\s*/))
			this.help = setType(itm, "ヘルプ");
		else if(rel.match(/\s*search\s*/))
			this.search = setType(itm, "検索");
	}
	this.length = tmplength;

	function setType(obj, defaultTitle)
	{
		tmp.title = obj.getAttribute("title") || defaultTitle;
		tmp.href  = obj.getAttribute("href");
		tmplength++;
		return tmp;
	}
}

