/******************************************************************************
	初期設定
******************************************************************************/

var gsMasterUrl = "http://yumeneko.sega.jp/support/";
//var gsMasterUrl = "http://ftp.wais-racco.co.jp/~yumeneko/";
var gsTickerSwf = gsMasterUrl+"cat_bl.swf";
//var gsTickerSwf = "./cat_bl.swf";
var gsFloatSwf = gsMasterUrl+'cat_soto.swf';
//var gsFloatSwf = "./cat_soto.swf";

var gsFloatIdPrefix = "CatFloat";
//var gsTickerId = "catmovie";
var gsTickerId = "CatTicker";
var giFloatHeight = 300;
var giFloatWidth = 300;
var giFloatLeft = 0;

var gsTickerTop = 0;
var gsTickerLeft = 0;

var gsLoopc = 0;
var gsAction = 0;

// フロートTickerを表示する
var gbShowFlg = 0;
var giFloatNum = 0;


var iLayerTop;
var iLayerLeft;
var oLayer;
var xdir;
var myinterval;

// MacIEチェックを行う
bMacIeFlg = CheckMacIe();

// Tikerを作成する
MakeTicker();

// onLoad時に実行する
AddLoadFunction( GetTopandLeft, 0 );

/********************************************************************************
	関数名		AddLoadFunction
	機能		BodyのLoadイベント時に実行する関数を追加する
	
	入力		fFunction		関数名（「"」で囲まずに指定する）
				sArg			関数の引数（"AAA,BBB"というように、カンマ区切りの文字列）
	出力		なし
	
	使い方		この関数は、<body onLoad="">が出現する後に実行すること。
				同様に、この関数が実行されたあとに、window.onload=function();が設定されると
				それまでの関数は無効となる。
				
				引数を使用する場合は、カンマ区切りの文字列を渡す。
				実際の関数内で、カンマ区切りの文字を配列にsplitして使う。
********************************************************************************/
function AddLoadFunction(fFunction, sArg){
	var fBaseOnload = window.onload;
	
	// 既存のonLoadイベントが存在しない場合
	if(typeof window.onload != 'function'){
		// 追加された関数をonLoadイベントとする
		window.onload = function(){
			// 新しい関数を実行する
			fFunction(sArg);
		}
	// 既存のonLoadイベントが存在する場合
	}else{
		window.onload = function(){
			// 元の関数を実行する
			fBaseOnload();
			// 新しい関数を実行する
			fFunction(sArg);
		}
	}
}

　var agt = navigator.userAgent.toLowerCase();
　var is_ie　 = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
　var is_gecko = (agt.indexOf('gecko') != -1);
　var is_opera = (agt.indexOf("opera") != -1);

//要素のスタイル属性を取得する関数
function getElementStyle(targetElm,IEStyleProp,CSSStyleProp) {
var elem = targetElm;
	if (elem.currentStyle) {
		return elem.currentStyle[IEStyleProp];
	} else if (window.getComputedStyle) {
		var compStyle = window.getComputedStyle(elem,"");
		return compStyle.getPropertyValue(CSSStyleProp);
	}
}

function getPosition(that) {
var targetEle = that;　　　//thatは位置を取得したい要素Object
var pos = new function(){ this.x = 0; this.y = 0; }
	while( targetEle ){
		pos.x += targetEle.offsetLeft; 
		pos.y += targetEle.offsetTop; 
		targetEle = targetEle.offsetParent;
//IEの補正：上記計算で無視されてしまう各親要素のborder幅を加算
		if ((targetEle) && (is_ie)) {
			pos.x += (parseInt(getElementStyle(targetEle,"borderLeftWidth","border-left-width")) || 0);
			pos.y += (parseInt(getElementStyle(targetEle,"borderTopWidth","border-top-width")) || 0);
		}
	}
//geckoの補正：カウントしないbody部border幅をマイナスしてしまうので２倍して加算
	if (is_gecko) {
//以下の部分でbody部を取得し、borderの減算を補正する。
		var bd = document.getElementsByTagName("BODY")[0];　　//body部を取得
		pos.x += 2*(parseInt(getElementStyle(bd,"borderLeftWidth","border-left-width")) || 0);
		pos.y += 2*(parseInt(getElementStyle(bd,"borderTopWidth","border-top-width")) || 0);
	}
	return pos;
}

function GetTopandLeft( sarg )
{
	gsTickerTop = getPosition( "CatTicker" ).y;
	gsTickerLeft = getPosition( "CatTicker" ).x;
}


function UDmove( x )
{
	iLayerTop = parseInt( iLayerTop ) + parseInt( x );
	oLayer.style.top = iLayerTop;
}
function LRmove( x )
{
	iLayerLeft = parseInt( iLayerLeft ) + parseInt( x );
	oLayer.style.left = iLayerLeft;
}
function Getdir()
{
	return xdir;
}

// calling function
function startfirstwalk()
{
	gsAction = 1;
	myinterval = setInterval( "timerCallback()", 10 );
}
function startdownmove()
{
	gsAction = 2;
	gsLoopc = 0;
}
function startshowpop()
{
	gsAction = 0;
}
function startupmove()
{
	gsAction = 3;
	gsLoopc = 0;
}
function startsecondwalk()
{
	gsAction = 4;
}
function stopmoving()
{
	gsAction = 0;
	clearInterval( myinterval )
}

function timerCallback( )
{
	switch( gsAction ){
		case 0: // nothing
			break;
		case 1: // first walk
			iLayerLeft = parseInt( iLayerLeft ) + parseInt( xdir );
			oLayer.style.left = iLayerLeft + 'px';
			break;
		case 2: // down walk
			if( gsLoopc >= 10 ){
				iLayerTop = parseInt( iLayerTop ) - -1;
				oLayer.style.top = iLayerTop + 'px';
				gsLoopc = 0;
			}
			break;
		case 3 : // up walk
			if( gsLoopc >= 10 ){
				iLayerTop = parseInt( iLayerTop ) - 1;
				oLayer.style.top = iLayerTop + 'px';
				gsLoopc = 0;
			}
			break;
		case 4 : // second walk
			iLayerLeft = parseInt( iLayerLeft ) - parseInt( xdir );
			oLayer.style.left = iLayerLeft + 'px';
			break;
	}
/*
	if(( gsLoopc > 10 ) && ( gsLoopc < 50 )){
		iLayerTop = parseInt( iLayerTop ) + 1;
		oLayer.style.top = iLayerTop;
	} else if( gsLoopc < 100 ){
		iLayerLeft = parseInt( iLayerLeft ) + parseInt( xdir );
		oLayer.style.left = iLayerLeft;
	} else if(( gsLoopc > 150 ) && ( gsLoopc < 200 )){
		iLayerLeft = parseInt( iLayerLeft ) - parseInt( xdir );
		oLayer.style.left = iLayerLeft;
	} else if( gsLoopc < 240 ){
		iLayerTop = parseInt( iLayerTop ) - 1;
		oLayer.style.top = iLayerTop;
	}
*/
	gsLoopc = parseInt( gsLoopc ) + 1;
//	document.TESTF.TESTT.value = gsLoopc;
}

/******************************************************************************
	関数名		MakeTicker
	機能		ティッカーを作成する
	
	入力		なし
	出力		document.writeによるHTML出力
******************************************************************************/
function MakeTicker(){
// 引数をチェック
	var d = document;
	var scripts;
	var pt, sk;
	pt = 1;
	sk = 1;
	if( d.getElementsByTagName ) scripts = d.getElementsByTagName( "script" );
	else if( d.scripts ) scripts = d.scripts;
	else if( d.all && d.all.tags ) scripts = d.all.tags( "script" );
	if( scripts ){
		var script = scripts[ scripts.length - 1 ];
		var src = script.src;
		var query = new Object();
		var pear, pears;
		if( src && src.indexOf( "?" ) != -1 ){
			src = src.substring( src.indexOf( "?" ) + 1 );
			pears = src.split( ";" );
			for( var i = 0; i < pears.length; i ++ ){
				pear = pears[ i ].split( "=" );
				query[ pear[ 0 ]] = pear[ 1 ];
			}
		}
		if( query[ "pt" ] ) pt = query[ "pt" ];
		if( query[ "sk" ] ) sk = query[ "sk" ];
	}
//
	var sTickerUrl = gsTickerSwf;
	
	var sHtml = "";
	sHtml += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="130" height="200" id="' + gsTickerId + '" align="middle" >';
	sHtml += '<param name="allowScriptAccess" value="always" />';
	sHtml += '<param name="movie" value="' + sTickerUrl + '?pt=' + pt + '&sk=' + sk + '&urlv=' + gsMasterUrl + '" />';
	sHtml += '<param name="quality" value="high" />';
//	sHtml += '<param name="bgcolor" value="#ffffff" />';
	sHtml += '<param name="wmode" value="transparent" />';
	sHtml += '<embed wmode="transparent" src="' + sTickerUrl + '?pt=' + pt + '&sk=' + sk + '&urlv=' + gsMasterUrl + '" quality="high" width="130" height="200" name="' + gsTickerId + '" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	sHtml += '</object>';
//	sHtml += '<FORM NAME=TESTF><INPUT TYPE=TEXT NAME=TESTT></FORM>'
	document.write(sHtml);
}

/* 関数名 clearMovie : フローティングを消す */

function clearMovie(){
	// オブジェクトを削除する
	oLayer.innerHTML='';

	//戻す為のコール
	callJavascript();

	// フラグを変更する
	gbShowFlg = 0;
}
/* 関数名 showMovie : フローティングを出す */
function showMovie(){
	var iLayerWidth;
	var tickerobj;
	var d = document;
	if( d.getElementsById ) tickerobj = d.getElementsById( "CatTicker" );
	else if( d.CatTicker ) tickerobj = d.CatTicker;
	else if( d.all && d.all.tags ) tickerobj = d.all.tags( "CatTicker" );
	
	giFloatNum++;
	sId = gsFloatIdPrefix + giFloatNum;

	oLayer = document.createElement('div');
	oLayer.id = sId;

	// フローティングレイヤーのサイズを算出する
	iLayerWidth = GetWindowSize("width");
	iLayerHeight = giFloatHeight;

	// Y座標を取得する
	iLayerTop = GetMiddlePosition("top");
	iLayerLeft = GetMiddlePosition( "left" );
	if( parseInt( iLayerLeft ) > ( parseInt( iLayerWidth ) / 2 )){
		xdir = -1;
		gsDir = 2;
	} else {
		xdir = 1;
		gsDir = 1;
	}
//	document.TESTF.TESTT.value = Getdir();

	// フローティングレイヤーにスタイルを指定する
	oLayer.style.position = "absolute";
	oLayer.style.zIndex = 1000;
	oLayer.style.display = "block";
	oLayer.style.width = giFloatWidth + "px";
	oLayer.style.height = giFloatHeight + "px";
	oLayer.style.top = iLayerTop + "px";
	oLayer.style.left = iLayerLeft + "px";

	// SWFタグを作成する
	var sHtml = "";
	sHtml += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + giFloatWidth + '" height="' + giFloatHeight + '" id="CatFloat" align="middle">';
	sHtml += '<param name="allowScriptAccess" value="always" />';
	sHtml += '<param name="movie" value="' + gsFloatSwf + '?L_R=' + gsDir + '&urlv=' + gsMasterUrl + '" />';
	sHtml += '<param name="quality" value="high" />';
	sHtml += '<param name="wmode" value="transparent" />';
	sHtml += '<param name="salign" value="lt" />';
	sHtml += '<param name="SWLIVECONNECT" value="TRUE" />';
	sHtml += '<embed SWLIVECONNECT="TRUE" salign="lt" align="middle" src="' + gsFloatSwf + '?L_R=' + gsDir + '&urlv=' + gsMasterUrl + '" quality="high" wmode="transparent" width="' + iLayerWidth + '" height="' + iLayerHeight + '" name="CatFloat" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	sHtml += '</object>';
//	sHtml += "(Top=" + iLayerTop + "),(LEFT=" + iLayerLeft + ")<br>";
//	sHtml += "(TicY=" + getPosition( tickerobj ).y + "),(LEFT=" + getPosition( tickerobj ).x + ")<br>";
	// SWFを挿入する
	oLayer.innerHTML = sHtml;

	// オブジェクトを追加する
	document.getElementsByTagName('body')[0].appendChild(oLayer);

	// フラグを変更する
	gbShowFlg = 1;
}


/********************************************************************************
	関数名		GetMiddlePosition
	機能		現在のスクロール位置でのミドルポジションを取得する
	
	入力		sMode			top / left
	出力		iPositionX		X座標
				iPositionY		Y座標
	
	処理概要	1. Windowサイズを取得する
				2. スクロール位置を取得する
				3. ミドルポジションを算出する
				4. ミドルポジションを返して終了する
********************************************************************************/
function GetMiddlePosition(sMode){

	var retcode;
	var tickerobj;
	var d = document;
	if( d.getElementsById ) tickerobj = d.getElementsById( "CatTicker" );
	else if( d.CatTicker ) tickerobj = d.CatTicker;
	else if( d.all && d.all.tags ) tickerobj = d.all.tags( "CatTicker" );
	
	var b = 30 - 20;
//	if( GetNavigatorName() == "Explorer" ){
//		b = 30 - 20 ;
//	}
	gsTickerTop = getPosition( tickerobj ).y;
	gsTickerLeft = getPosition( tickerobj ).x;
	if( sMode == "top" ){
		if( gsTickerTop ){
			retcode = parseInt( gsTickerTop ) - parseInt( b );
		} else {
			retcode = 0; // document.write( "Cannot getID<br>" );
		}
	} else {
		if( gsTickerLeft ){
			retcode = parseInt( gsTickerLeft ) - 85;
		} else {
			retcode = 0;
		}
	}
	return retcode;
}

/********************************************************************************
	関数名		GetScrollPosition
	機能		スクロール位置を算出する
	
	入力		sMode		top / left
	出力		iPosition
********************************************************************************/
function GetScrollPosition(sMode){

	var iScrollPosition;

	if(sMode == "left"){
		if(self.pageXOffset){
			iScrollPosition = self.pageXOffset;
		// Explorer 6 Strict
		}else if(document.documentElement && document.documentElement.scrollLeft){
			iScrollPosition = document.documentElement.scrollLeft;
		// all other Explorers
		}else if(document.body){
			iScrollPosition = document.body.scrollLeft;
		}
	} else {

		if(self.pageYOffset){
			iScrollPosition = self.pageYOffset;
		// Explorer 6 Strict
		}else if(document.documentElement && document.documentElement.scrollTop){
			iScrollPosition = document.documentElement.scrollTop;
		// all other Explorers
		}else if(document.body){
			iScrollPosition = document.body.scrollTop;
		}
	}
	
	return iScrollPosition;
}

/********************************************************************************
	関数名		GetWindowSize
	機能		ウィンドウサイズを取得する
	
	入力		sMoe		"width" | "height"
*********************************************************************************/
function GetWindowSize(sMode){
	var iWindowWidth, iWindowHeight;
	
	// all except Explorer
	if(self.innerHeight){
		iWindowWidth = self.innerWidth;
		iWindowHeight = self.innerHeight;
	// Explorer 6 Strict Mode
	}else if(document.documentElement && document.documentElement.clientHeight){
		iWindowWidth = document.documentElement.clientWidth;
		iWindowHeight = document.documentElement.clientHeight;
	// other Explorers
	}else if(document.body){
		iWindowWidth = document.body.clientWidth;
		iWindowHeight = document.body.clientHeight;
	}
	
	if(sMode == "width"){
		return (iWindowWidth);
	} else {
		return (iWindowHeight);
	}
}
	

/********************************************************************************
	関数名		CheckMacIe
	機能		MacIEであることをチェックする
********************************************************************************/
function CheckMacIe(){
	var sOs = GetOsName();
	var sNavigator = GetNavigatorName();
	
	if((sOs == "MacOSX" || sOs == "MacOS") && sNavigator == "Explorer"){
		return true;
	} else {
		return false;
	}
}

	
/********************************************************************************
	関数名		GetOsName
	機能		OS情報を取得する
	
	入力		なし
	出力		sOsData		Os情報 ("MacOSX","Windows")
********************************************************************************/
function GetOsName()
{
	var uAgent  = navigator.userAgent.toUpperCase();
	if (uAgent.indexOf("MAC OS X") >= 0) return "MacOSX";
	if (uAgent.indexOf("MAC") >= 0) return "MacOS";
	if (uAgent.indexOf("WIN") >= 0) return "Windows";
	if (uAgent.indexOf("X11") >= 0) return "UNIX";
	return "";
}
function GetNavigatorName(){
	if(navigator.IBM_HPR) return "HomepageReader";
	
	aName  = navigator.userAgent.toUpperCase();
	if (aName.indexOf("SAFARI") >= 0) return "Safari";
	if (aName.indexOf("CHIMERA") >= 0) return "Camino";
	if (aName.indexOf("OPERA") >= 0) return "Opera";
	
	aName = navigator.appName.toUpperCase();
	if (aName.indexOf("NETSCAPE") >= 0)  return "Netscape";
	if (aName.indexOf("MICROSOFT") >= 0) return "Explorer";
	return "";
}
//////////////////////////////////////////////
//FLASHの内部関数をコールします。
/////////////////////////////////////////////
function callJavascript(){
   window.document.CatTicker.SetVariable("name", "1");
}
