//------------------------------------------------------------------------------
// File: lib.js
// Desc: for IE6+，NN7+
//------------------------------------------------------------------------------




//==============================================================================
// Utility Functions
//==============================================================================


//------------------------------------------------------------------------------
// Name: trace()
// Desc:
//------------------------------------------------------------------------------
var g_trace_window
function trace( s )
{
	if( !g_trace_window || g_trace_window.closed ) {
		g_trace_window = window.open( "", "lib_js_trace", "scrollbars=1,resizable=1,top=0,left=0,width=150,height=100" )
		g_trace_window.document.writeln("<head><title>trace</title>")
		g_trace_window.document.writeln("<style type='text/css'>\n<!--")
		g_trace_window.document.writeln("	body { margin-top: 0px; margin-left: 0px; margin-right: 0px; margin-bottom: 0px; }")
		g_trace_window.document.writeln("	font { font-size:12px; line-height:12px; }")
		g_trace_window.document.writeln("-->\n</style>")
		g_trace_window.document.writeln("</head>")
		g_trace_window.document.write("<pre><font>")
	}
	var t = new String()
	for( var i=0; i<s.length; i++ ) {
		if( s.charAt(i) == "<" )		t += "&lt;"
		else if( s.charAt(i) == ">" )	t += "&gt;"
		else							t += s.charAt(i)
	}
	g_trace_window.document.writeln(t)
}




//------------------------------------------------------------------------------
// Name: preloadImages()
// Desc:
//------------------------------------------------------------------------------
function preloadImages()
{
	if( !document.userImages ) document.userImages = new Array()
	var j = document.userImages.length
	var a = preloadImages.arguments
	for( var i = 0; i < a.length; i++ ) {
		document.userImages[j] = new Image
		document.userImages[j++].src = a[i]
	}
}




//------------------------------------------------------------------------------
// Name: openBrWindow()
// Desc:
//------------------------------------------------------------------------------
function openBrWindow( theURL, winName, features )
{
	window.open( theURL, winName, features )
}




//------------------------------------------------------------------------------
// Name: jump()
// Desc:
//------------------------------------------------------------------------------
function jump( url )
{
	location.href = url
}




//------------------------------------------------------------------------------
// Name: getPlatform()
// Desc: プラットフォーム識別
//------------------------------------------------------------------------------
function getPlatform()
{
	if( navigator.platform.indexOf("Win") != -1 ) return "win"
	if( navigator.platform.indexOf("Mac") != -1 ) return "mac"
	return "unknown"
}

function win() { return getPlatform() == "win" }
function mac() { return getPlatform() == "mac" }




//------------------------------------------------------------------------------
// Name: getBrowser()
// Desc: ブラウザ識別
//------------------------------------------------------------------------------
function getBrowser()
{
	if( navigator.appName == "Microsoft Internet Explorer" ) return "IE"
	if( navigator.appName == "Netscape" )					 return "NN"
	return "unknown"
}




//------------------------------------------------------------------------------
// Name: getVer()
// Desc: ブラウザヴァージョン取得
//------------------------------------------------------------------------------
function getVer()
{
	if( getBrowser() == "IE" ) {
		var ver = navigator.appVersion.slice( navigator.appVersion.indexOf("MSIE") + 5, navigator.appVersion.length )
		return parseFloat( ver.slice(0,ver.indexOf(";")) )

	}
	else if( getBrowser() == "NN" ) {
		return ver = parseFloat( navigator.appVersion.slice(0,navigator.appVersion.indexOf(" ")) )
	}
}




//------------------------------------------------------------------------------
// Name: getInnerWidth()
// Desc: ブラウザの内幅を得る
//------------------------------------------------------------------------------
function getInnerWidth()
{
	// for IE
	if( document.all )
		return document.body.clientWidth	// IE only
	// for NN
	if( document.getElementById || document.layers )
		return window.innerWidth			// NN only
	return -1
}




//==============================================================================
// CImage クラス
//==============================================================================


//------------------------------------------------------------------------------
// Name: imageChange()
// Desc:
// Params:	src: Image.src
//------------------------------------------------------------------------------
function imageChange( src )
{
	this.doc.images[this.id].src = src
}




//------------------------------------------------------------------------------
// Name: function CImage
// Desc: CImage クラス
// Params:	id:  画像ID．番号(数値)または識別子(文字列)をとる
//               識別子は<img>内のid属性の値．NNの47以下はname属性
//				 番号は親 document オブジェクト内でのインデクス
//          doc: images[] の親となる document オブジェクト
//				 未指定(引数１つ)時はデフォルト
//------------------------------------------------------------------------------
function CImage( id, doc )
{
	this.id  = id
	if( !doc )
		this.doc = document
	else
		this.doc = doc

	this.change = imageChange
}




//==============================================================================
// CLayer クラス
//==============================================================================

// << NOTICE >>
// NN47時，<DIV>タグのstyle属性のpositionを指定しておく必要あり．無いとエラー．

//------------------------------------------------------------------------------
// Name: layerGetObject()
// Desc:
//------------------------------------------------------------------------------
function layerGetObject()
{
	// for IE55,NN6
	if( document.getElementById )
		return this.doc.getElementById(this.id)
	// for IE
	if( document.all )
		return this.doc.all(this.id)
	// for NN47
	if( document.layers )
		return this.doc.layers[this.id]
}




//------------------------------------------------------------------------------
// Name: layerGetVisible()
// Desc: レイヤーの可視状態を返す
// Return:	visible - 可視
//			hidden  - 不可視
//			inherit - 上位レイヤーに従う
//------------------------------------------------------------------------------
function layerGetVisible()
{
	var oj = this.getObject()

	// for IE,NN6
	if( document.getElementById || document.all ) {
		if( oj.style.visibility == "" )		return "inherit"
		return oj.style.visibility
	}
	// for NN47
	if( document.layers ) {
		if( oj.visibility == "show" )		return "visible"
		if( oj.visibility == "hide" )		return "hidden"
		if( oj.visibility == "inherit" )	return "inherit"
	}
}




//------------------------------------------------------------------------------
// Name: layerShow()
// Desc: レイヤーを可視状態にする
//------------------------------------------------------------------------------
function layerShow()
{
	var oj = this.getObject()

	// for IE,NN6
	if( document.getElementById || document.all )
		oj.style.visibility = "visible"
	// for NN47
	else if( document.layers )
		oj.visibility = "show"
}




//------------------------------------------------------------------------------
// Name: layerHide()
// Desc: レイヤーを不可視状態にする
//------------------------------------------------------------------------------
function layerHide()
{
	var oj = this.getObject()

	// for IE,NN6
	if( document.getElementById || document.all )
		oj.style.visibility = "hidden"
	// for NN47
	else if( document.layers )
		oj.visibility = "hide"
}




//------------------------------------------------------------------------------
// Name: layerGetLeft()
// Desc: レイヤーの左座標を得る		※座標はposition属性に依存
//------------------------------------------------------------------------------
function layerGetLeft()
{
	var oj = this.getObject()

	// for IE55,NN6
	if( document.getElementById )
		return oj.style.left
	// for IE
	if( document.all )
		return oj.style.pixelLeft
	// for NN47
	if( document.layers )
		return oj.left
}




//------------------------------------------------------------------------------
// Name: layerGetTop()
// Desc: レイヤーの上座標を得る		※座標はposition属性に依存
//------------------------------------------------------------------------------
function layerGetTop()
{
	var oj = this.getObject()

	// for IE55,NN6
	if( document.getElementById )
		return oj.style.top
	// for IE
	if( document.all )
		return oj.style.pixelTop
	// for NN47
	if( document.layers )
		return oj.top
}




//------------------------------------------------------------------------------
// Name: layerTransfer()
// Desc: レイヤーを移動		※座標はposition属性に依存
//------------------------------------------------------------------------------
function layerTransfer( x, y )
{
	var oj = this.getObject()

	// for IE55,NN6
	if( document.getElementById ) {
		oj.style.left = x
		oj.style.top  = y
	}
	// for IE
	else if( document.all ) {
		oj.style.pixelLeft = x
		oj.style.pixelTop  = y
	}
	// for NN47
	else if( document.layers )
		oj.moveTo( x, y )
}




//------------------------------------------------------------------------------
// Name: layerClip()
// Desc: レイヤーをクリップ		※position:relative時無効（NN47を除く）
//------------------------------------------------------------------------------
function layerClip( clipLeft, clipTop, clipRight, clipBottom )
{
	var oj = this.getObject()

	// for IE,NN6
	if( document.getElementById || document.all ) {
		oj.style.clip = "rect(" + clipTop + "," + clipRight + "," + clipBottom +
						"," + clipLeft + ")"
	}
	// for NN47
	else if( document.layers ) {
		with( oj.clip ) {
			top = clipTop; right = clipRight; bottom = clipBottom; left = clipLeft
		}
	}
}




//------------------------------------------------------------------------------
// Name: layerWrite()
// Desc: レイヤー内のＨＴＭＬを書き換える
//------------------------------------------------------------------------------
function layerWrite( html )
{
	var oj = this.getObject()

	// for IE,NN6
	if( document.getElementById )
		oj.innerHTML = html
	// for NN47
	else if( document.layers ) {
		// position:relative時，描画がひっかかる
		with( oj.document ) {
			open()
			write(html)
			close()
		}
	}
}




//------------------------------------------------------------------------------
// Name: layerChangeImage()
// Desc: レイヤー内の画像を変更
// Params:	src:
//          name: 画像ID．NN47を考慮し，識別子(文字列)のみを用いる
//				  未指定時は "img"+this.id
//------------------------------------------------------------------------------
function layerChangeImage( src, name )
{
	var oj = this.getObject()
	if( !name )
		name = "img" + this.id
	var image = new CImage( name, oj.document )
	image.change( src )
}




//------------------------------------------------------------------------------
// Name: function CLayer
// Desc: CLayer クラス
// Params:	id:  レイヤーID．識別子(文字列)．<div>内のid属性の値
//          doc: 親となる document オブジェクト
//				 未指定(引数１つ)時はデフォルト
//------------------------------------------------------------------------------
function CLayer( id, doc )
{
	this.id  = id
	if( !doc )
		this.doc = document
	else
		this.doc = doc

	this.getObject	= layerGetObject
	this.getVisible	= layerGetVisible
	this.show		= layerShow
	this.hide		= layerHide
	this.getLeft	= layerGetLeft
	this.getTop		= layerGetTop
	this.trn		= layerTransfer
	this.clip		= layerClip
	this.write		= layerWrite
	this.chgImage	= layerChangeImage
}




//==============================================================================
// CCookie クラス
//==============================================================================


//------------------------------------------------------------------------------
// Name: cookieClear()
// Desc: 要素の初期化
//------------------------------------------------------------------------------
function cookieClear()
{
	this.data = new Array()
}




//------------------------------------------------------------------------------
// Name: cookieGet()
// Desc: 要素の取得
//------------------------------------------------------------------------------
function cookieGet( index )
{
	return this.data[index]
}




//------------------------------------------------------------------------------
// Name: cookieAdd()
// Desc: 要素の追加
//------------------------------------------------------------------------------
function cookieAdd( value )
{
	this.data[this.data.length] = value
}




//------------------------------------------------------------------------------
// Name: cookieSet()
// Desc: 要素のセット
//------------------------------------------------------------------------------
function cookieSet( index, value )
{
	this.data[index] = value
}




//------------------------------------------------------------------------------
// Name: cookieLength()
// Desc: 要素数の取得
//------------------------------------------------------------------------------
function cookieLength()
{
	return this.data.length
}




//------------------------------------------------------------------------------
// Name: cookieRead()
// Desc: クッキーデータの読み込み
//------------------------------------------------------------------------------
function cookieRead()
{
	var pt1, pt2

	// クッキー取得
	var str = unescape( document.cookie )

	// name名のデータ文字列を抽出
	var pt1 = 0
	while( true ) {
		pt1 = str.indexOf( this.tag+"=", pt1 )
		if( pt1 == -1 )
			return false
		if( pt1 == 0 || str.charAt(pt1-1) == " ")
			break
		pt1++
	}
	var pt2 = str.indexOf( ";", pt1 )
	if( pt2 != -1 )
		str = str.slice( pt1, pt2 )
	else
		str = str.slice( pt1, str.length )

	// 配列を新たに割り当て
	if( this.data.length != 0 )
		this.data = new Array()

	// データの分割
	pt1 = str.indexOf( "|", 0 )
	if( pt1 == -1 )
		return 1
	while( 1 ) {
		pt2 = str.indexOf( "|", pt1 + 1 )
		if( pt2 == -1 )
			break
		else {
			this.add( str.slice( pt1 + 1, pt2 ) )
			pt1 = pt2
		}
	}

	return true
}




//------------------------------------------------------------------------------
// Name: cookieWrite()
// Desc: クッキーデータの書き出し
//------------------------------------------------------------------------------
function cookieWrite()
{
	// データの準備
	var str = escape( this.tag ) + "=|"
	for( var i = 0; i < this.data.length; i++ )
		str += escape( this.data[i] ) + "|"
	str += ";"

	// 有効期限
	if( this.limit ) {
		str += "expires=" + this.limit.toGMTString() + ";"
	}

	// データ書き出し
	document.cookie = str
}




//------------------------------------------------------------------------------
// Name: cookieSetLimit()
// Desc: クッキーデータの有効期限をセット（現在からの相対日数で指定）
//------------------------------------------------------------------------------
function cookieSetLimit( y, m, d )
{
	var now = new Date()

	// 月
	m += now.getMonth()
	if( m >= 12 ) {
		y++;
		m %= 12
	}
	// 年
	y += now.getFullYear()
	// 日
	d += now.getDate()
	while( true ) {
		var f = false
		if( m == 1 ) {	// 2月
			// 閏年
			if( !(y % 4) && (y % 100) )
				if( d >= 30 ) {
					d %= 29; m++; f=true
				}
			else
				if( d >= 29 ) {
					d %= 28; m++; f=true
				}
		}
		else if( (m == 3) || (m == 5) || (m == 8) || (m == 10) ) {	// 4,6,9,11
			if( d >= 31 ) {
				d %= 30; m++; f=true
			}
		}
		else {
			if( d >= 32 ) {
				d %= 31; m++; f=true
			}
		}

		if( m >= 12 ) {
			m %= 12; y++
		}

		if( !f )
			break
	}

	this.limit	= new Date()
	this.limit.setDate( d )
	this.limit.setMonth( m )
	this.limit.setFullYear( y )
}




//------------------------------------------------------------------------------
// Name: function CCookie
// Desc: CCookie クラス
//------------------------------------------------------------------------------
function CCookie( tag )
{
	this.tag	= tag
	this.data	= new Array()	// データ配列
	this.limit	= 0 			// 有効期限（セットされない場合，ブラウザを閉じるまで）

	this.clear	= cookieClear
	this.get	= cookieGet
	this.add	= cookieAdd
	this.set	= cookieSet
	this.length	= cookieLength
	this.read	= cookieRead
	this.write	= cookieWrite
	this.setLimit	= cookieSetLimit

	if( !tag ) {
		alert( "no tag" )
		return
	}
}




