/* Version 2009.05.31 rel0.01  */
/* $Id: scrolling.js 75 2009-05-31 07:12:13Z debizoh $
	* Copyright (C) 2009  Pumpkin Net  All rights reserved.
	* Pumpkin Net <http://www.pumpkinnet.to/>
*/


var ObjectLength = 3;

var AryItemsID = new Array();
var AryObj = new Array();
AryObj[0] = new Array();
AryObj[1] = new Array();
AryObj[2] = new Array();


// Marquee用Sales Comments
var AryObjMq = new Array();
AryObjMq[0] = new Array();
AryObjMq[1] = new Array();
AryObjMq[2] = new Array();

var IntScrollWaitTime = 20;

var AryMarq = new Array();
var ObjMouse;

var MaxVisible = 4;
var Offset = 5;
var BoxWidth = 116;	//marginを除いた数ではない...
var ParseScrollSpeed = 28;
var ScrollSpeed = 2;

var TimerID;
var IntChildNodesFix = 0;

var DebugClear = 0;
var TimerStart = 0;
MovePoint = new Array(0,0,0);
MoveState = new Array(0,0,0);
MoveCounter = new Array(1,1,1);
MoveFlag = new Array(0,0,0);
PointerCurrent = new Array(-1,-1,-1);

//ミリ秒
var MoveStopTime = new Number(300);
var MoveInterval = new Number(40);
var ConstMoveInterval = new Number(40);	//強制スクロールインターバル

// 要素分の配列

//WaitMovie = new Array(0,0,0);

WaitMovie = new Array(0,0,0);
//WaitMovie[0] = 0;

var VarTimerPosition = '';
var Timer_LeftTimer;
var Timer_LeftMouseTimer;
var Timer_LeftChildStart = 0;

var Timer_RightTimer;
var Timer_RightMouseTimer;
var Timer_RightChildStart = 0;

var VarIntMove = 0;

var ObjMarq = new Array();
var AryLeftScr = new Array(0,0,0);
var AryRightScr = new Array(0,0,0);
var BoolStartRight = 0;

var LeftTimerCounter = 0;
var RightTimerCounter = 0;

var TestTimerID;

function FncGetBrowser()
{
	return navigator.userAgent;

}

function FncSetAutoScroll(align,flg)
{
	if (align == "L")
	{
		if (flg == 1)
		{
			LeftTimerCounter = 0;
			Timer_LeftTimer = setInterval('FncParseScrollLeftChild()',ConstMoveInterval);
		}
		else
		{
			clearInterval(Timer_LeftTimer);
		}
		return;
	}
	else if (align == "R")
	{
		if (flg == 1)
		{
			RightTimerCounter = 0;
			Timer_RightTimer = setInterval('FncParseScrollRightChild()',ConstMoveInterval);
		}
		else
		{
			clearInterval(Timer_RightTimer);
		}
		return;

	}
}



function FncParseScrollRightStop(VarPointer,obj)
{
	AryRightScr[VarPointer - 1] = 0;
}

function FncParseScrollRight(VarPointer,obj)
{
	var NowPosition;
	var TMP;
	var Movie;

	Movie = VarPointer -1;
	if ( AryObj[Movie].length <= MaxVisible)
	{
		return;
	}

	if (VarTimerPosition != '')
	{
		AryRightScr[VarPointer - 1] = 1;
		return;
	}

	WaitMovie[Movie] = 1;

	VarTimerPosition = VarPointer;

	ObjMouse = obj;
	AryRightScr[VarPointer - 1] = 1;
	FncSetAutoScroll("R",1);
}

function FncParseScrollRightChild()
{

	var CheckVal = 0;
	var Movie;
	var i;
	for (i = 0; i < AryRightScr.length; i++)
	{
		if ( AryRightScr[i] == 1)
		{
			CheckVal = 1;
			break;
		}
	}
	if (VarTimerPosition == '' || VarTimerPosition == 0)
	{
		return;
	}

	/*
	RightTimerCounter = RightTimerCounter + 1;
	if (RightTimerCounter <= 1)
	{
		return;
	}
	*/

	Movie = VarTimerPosition - 1;
	WaitMovie[Movie] = 1;
	MoveCounter[Movie] = 0;

	if (BoolStartRight == 0)
	{
		BoolStartRight = 1;
		TMP = AryObj[Movie].pop();
		AryObj[Movie].unshift(TMP);
		AryObj[Movie][0].childNodes[0 + IntChildNodesFix].style.left =  (BoxWidth * -1) - (Offset * 2)  + "px";
	}

	if (MovePoint[Movie] + ParseScrollSpeed > BoxWidth + Offset + 2)
	{
		MovePoint[Movie] = BoxWidth + Offset + 2;
	}
	else
	{
		MovePoint[Movie] = MovePoint[Movie] + ParseScrollSpeed;
	}

	if (PointerCurrent[Movie] != - 1)
	{
		if (!AryObj[Movie])
		{
			window.alert("Movie:" + Movie);
		}

		if ( (AryObj[Movie].length - 1) == PointerCurrent[Movie] )
		{
			PointerCurrent[Movie] = 0;
		}
		else
		{
			PointerCurrent[Movie]++;
		}
	}

	FncClearM_B(Movie);
	SetMarquee(Movie + 1, - 1);

	for (i = 0; i < AryObj[Movie].length; i++)
	{
		WaitMovie[Movie] = 1;
		MoveCounter[Movie] = 1;

		VarIntMove = 1;
		var Nowvalue = AryObj[Movie][i].childNodes[0 + IntChildNodesFix].style.left;

		if (i != 0)
		{
			//1 * 116 + 5 + 
			AryObj[Movie][i].childNodes[0 + IntChildNodesFix].style.left = ((i - 1) * BoxWidth) + ( (i - 1) * Offset) + MovePoint[Movie] + "px";
		}
		else
		{
			AryObj[Movie][i].childNodes[0 + IntChildNodesFix].style.left = (BoxWidth * -1) - (Offset * -1)  + MovePoint[Movie] - (Offset * 2) + "px";
		
		}
		//AryObj[Movie][i].childNodes[0 + IntChildNodesFix].style.left = parseInt(Nowvalue) + MovePoint[Movie] + "px";

	}

	if (parseInt(MovePoint[Movie]) > BoxWidth - 1)
	{
		MovePoint[Movie] = 0;
		MoveState[Movie] = 0;

		FncSetM_B(AryObj[Movie][1]);

		AryPointer = AryObj[Movie][1].id.split('.');
		IntPointer = AryPointer[1];

		SetMarquee(Movie + 1,parseInt(IntPointer) );
		PointerCurrent[Movie] = 1;
		BoolStartRight = 0;
		if (AryRightScr[Movie] == 0)
		{
			VarTimerPosition = '';
			WaitMovie[Movie] = 0;
			MoveCounter[Movie] = 1;
			MoveState[Movie] = 1;
			FncSetAutoScroll("R",0);
		}
	}

}

function FncParseScrollLeftStop(VarPointer,obj)
{
	AryLeftScr[VarPointer - 1] = 0;
}

function FncParseScrollLeft(VarPointer,obj)
{
	var NowPosition;
	var TMP;
	var Movie;
	Movie = VarPointer -1;

	if ( AryObj[Movie].length <= MaxVisible)
	{
		return;
	}
	if (VarTimerPosition != '')
	{
		AryLeftScr[VarPointer - 1] = 1;
		return;
	}

	WaitMovie[Movie] = 1;

	VarTimerPosition = VarPointer;

	ObjMouse = obj;
	AryLeftScr[VarPointer - 1] = 1;
	FncSetAutoScroll("L",1);

}

function FncParseScrollLeftChild()
{
	var CheckVal = 0;
	var Movie;
	var i;

	for (i = 0; i < AryLeftScr.length; i++)
	{
		if ( AryLeftScr[i] == 1)
		{
			CheckVal = 1;
			break;
		}
	}

	if (VarTimerPosition == '' || VarTimerPosition == 0)
	{
		return;
	}
	/*
	LeftTimerCounter = LeftTimerCounter + 1;
	if (LeftTimerCounter <= 1)
	{
		return;
	}
	*/
	Movie = VarTimerPosition - 1;

	WaitMovie[Movie] = 1;
	MoveCounter[Movie] = 0;

	if (MovePoint[Movie] + ParseScrollSpeed > BoxWidth)
	{
		MovePoint[Movie] = BoxWidth;
	}
	else
	{
		MovePoint[Movie] = MovePoint[Movie] + ParseScrollSpeed;
	}

	FncClearM_B(Movie);
	//window.alert("mv:" + Movie);
	SetMarquee(Movie + 1, - 1);

	for (i = 0; i < AryObj[Movie].length; i++)
	{
		WaitMovie[Movie] = 1;
		MoveCounter[Movie] = 1;
		MoveState[Movie] = 1;

		AryObj[Movie][i].childNodes[0 + IntChildNodesFix].style.left = (i * BoxWidth) + (i * Offset) - MovePoint[Movie] + "px";
	}

	if (parseInt(MovePoint[Movie]) >= BoxWidth - 1)
	{
		MovePoint[Movie] = 0;
		MoveState[Movie] = 0;

		TMP = AryObj[Movie].shift();
		AryObj[Movie].push(TMP);

		FncSetM_B(AryObj[Movie][1]);

		AryPointer = AryObj[Movie][1].id.split('.');
		IntPointer = AryPointer[1];

		SetMarquee(Movie + 1,parseInt(IntPointer) );

		PointerCurrent[Movie] = 1;

		if (AryLeftScr[Movie] == 0)
		{
			VarTimerPosition = '';
			WaitMovie[Movie] = 0;
			MoveCounter[Movie] = 1;
			MoveState[Movie] = 1;
			FncSetAutoScroll("L",0);
		}
	}

}

function FncChangeSwitchLeft(obj,flag,VarPointer)
{
	if (flag == "on")
	{
		obj.src = '/img2/search/switch-left-over.png';
		WaitMovie[VarPointer - 1] = 1;
		MoveState[VarPointer - 1 ] = 1;
	}
	else
	{
		obj.src= '/img2/search/switch-left.png';
		WaitMovie[VarPointer - 1] = 0;
		MoveCounter[VarPointer - 1] = 1;
		MoveState[VarPointer - 1 ] = 1;
		AryLeftScr[VarPointer - 1] = 0;
	}
}

function FncChangeSwitchRight(obj,flag,VarPointer)
{
	if (flag == "on")
	{
		obj.src = '/img2/search/switch-right-over.png';
		WaitMovie[VarPointer - 1] = 1;
		MoveState[VarPointer - 1] = 1;
	}
	else
	{
		obj.src = '/img2/search/switch-right.png';
		WaitMovie[VarPointer - 1] = 0;
		MoveCounter[VarPointer - 1] = 1;
		MoveState[VarPointer - 1] = 1;
		AryRightScr[VarPointer - 1] = 0;
	}
}

function FncChangeBG(obj,flag,VarPointer)
{
	var AryPointer = new Array();
	var IntPointer;

	var CurrentPosition;
	
	if (flag == "on")
	{
		for (i = 0; i < AryObj[VarPointer - 1].length; i++)
		{
			AryObj[VarPointer - 1][i].childNodes[0 + IntChildNodesFix].style.backgroundImage = 'url(/img2/search/bg.gif)';
		}

		obj.style.backgroundImage = 'url(/img2/search/bg-over.gif)';
		WaitMovie[VarPointer - 1] = 1;
		MoveCounter[VarPointer - 1] = 1;

		var ParentObj = obj.parentNode;
		
		for (r = 0; r < ObjectLength; r++)
		{
			var IntPos;
			IntPos = r + 1;
			if (ParentObj.id.indexOf("scroll" + IntPos + ".") != -1)
			{
				AryPointer = ParentObj.id.split('.');
				CurrentPosition = AryPointer[1];
				SetMarquee(VarPointer,CurrentPosition);
			}
		}
		PointerCurrent[VarPointer - 1] = - 1;

	}
	else
	{
		obj.style.backgroundImage = 'url(/img2/search/bg.gif)';
		WaitMovie[VarPointer - 1] = 0;

		for (i = 0; i < AryObj[VarPointer - 1].length; i++)
		{
			AryObj[VarPointer - 1][i].childNodes[0 + IntChildNodesFix].style.backgroundImage = 'url(/img2/search/bg.gif)';
		}
		SetMarquee(VarPointer,-1);
		PointerCurrent[VarPointer - 1] = - 1;
	}
}

function SetMarquee(VarPointer,flag)
{
	var match = 0;
	var MaqPoint;

	if (flag == -1)
	{
		if (AryMarq[VarPointer - 1].childNodes.length != 0)
		{
			if (!!ObjMarq[VarPointer - 1])
			{
				if (ObjMarq[VarPointer - 1].isMarqueeing)
				{
					ObjMarq[VarPointer - 1].stop;
				}
			}

			AryMarq[VarPointer - 1].removeChild(AryMarq[VarPointer - 1].childNodes.item(0));
	
		}
		return;
	}
	else
	{
		if (AryMarq[VarPointer - 1].childNodes.length != 0)
		{
			if (!!ObjMarq[VarPointer - 1])
			{
				if (ObjMarq[VarPointer - 1].isMarqueeing)
				{
					ObjMarq[VarPointer - 1].stop;
				}
			}
			AryMarq[VarPointer - 1].removeChild(AryMarq[VarPointer - 1].childNodes.item(0));
		}

		//AryMarq[VarPointer - 1].childNodes.item(0).innerText = AryObjMq[VarPointer - 1][flag - 1].innerText;
		//AryMarq[VarPointer - 1].childNodes.item(0).textContent = AryObjMq[VarPointer - 1][flag - 1].textContent;
		//var oMarq = document.createElement('marquee');

		var oMarq = document.createElement('div');
		//oMarq.style.overflow = 'hidden';
		oMarq.style.whiteSpace = 'nowrap';

		var StrBrowser = FncGetBrowser();
		if (StrBrowser.indexOf("Firefox") != -1)
		{
			//if firefox
			oMarq.innerHTML = AryObjMq[VarPointer - 1][flag - 1].textContent;
		}
		else
		{
			oMarq.innerHTML = AryObjMq[VarPointer - 1][flag - 1].innerText;
		}
		AryMarq[VarPointer - 1].appendChild(oMarq);

		if (!!ObjMarq[VarPointer - 1])
		{
			ObjMarq[VarPointer - 1].renew(VarPointer - 1);
		}
		else
		{
			ObjMarq[VarPointer - 1] = new Marquee(VarPointer - 1);
		}
	}
	return;
}

var Marquee = function ( VarPointer, opt ){

	//AryMarq[VarPointer - 1].childNodes.item(0);
//    if( ! document.getElementById(id) ) throw 'invalid id. [' + id + ']';

    var option = opt || {};
	this.parentWidth = 479;
	this.objid = VarPointer;
    this.amount = option.amount || 20;	//speed
    this.delay  = option.delay  || 100;
    this.position = Number.POSITIVE_INFINITY; // means out of range.
    this._wrap();
    this.start();
	this.waittime = 0;
}
Marquee.prototype = {
    /* wrap child nodes */
    _wrap : function() {

		if ( AryMarq[this.objid].childNodes.length == 0)
		{
			return;
		}

        //var t = document.getElementById( this.id );
		var t = AryMarq[this.objid].childNodes.item(0);

		t.style.postion = 'absolute';
        this.minWidth = t.offsetWidth;
        t.style.position = 'relative';
        t.style.width = '100%'; // for ie6.

    },
	renew : function(VarPointer)
	{
		this.objid = VarPointer;

		if ( AryMarq[this.objid].childNodes.length != 0)
		{
			if (AryMarq[this.objid].childNodes.length == 1)
			{
				var t = AryMarq[this.objid].childNodes[0];
			}
			else
			{
				var t = AryMarq[this.objid].childNodes[0 + IntChildNodesFix];
			}
	        var TMP = t.style.left.replace("px","");
		    this.position = TMP;
			this.waittime = 0;
		    this._wrap();
	        //this.position = this._startPosition();
		    this.start();
		}
	},
    start : function() {
        this.stop();
        this._next();
    },
    _next : function() {

		if (AryMarq[this.objid].childNodes.length == 1)
		{
			var t = AryMarq[this.objid].childNodes[0];
		}
		else
		{
			var t = AryMarq[this.objid].childNodes[0 + IntChildNodesFix];
		}
		if ( AryMarq[this.objid].childNodes.length == 0)
		{
			return;
		}
		if (this.waittime < IntScrollWaitTime)
		{
			this.waittime++;
	        var self = this;
	        this.tid = window.setTimeout(
	            function(){ self._next(); },
	            this.delay
	        );
			return;
		}

        this.curWidth = Math.min(t.offsetWidth,t.parentNode.offsetWidth); // dirty. (for "overflow:hidden" parent)
        this.position = this.position - this.amount;
        if ( this._isOutOfRange() ) {
            this.position = this._startPosition();
        }

        //var w = t.firstChild;
		//window.alert("this:" +this.position);

        t.style.left = this.position + 'px';

        var self = this;
        this.tid = window.setTimeout(
            function(){ self._next(); },
            this.delay
        );
    },
    _startPosition : function() {
        return ( this.amount > 0 ) ?  this.curWidth
                                   : -this.minWidth;
    },
    _isOutOfRange : function() {
        return this.position < -this.minWidth || this.curWidth < this.position;
    },
    stop : function() {
        if ( this.tid ) window.clearTimeout( this.tid );
        this.tid = null;
    },
    isMarqueeing : function() {
        return ( this.tid ) ? true : false;
    }
}



function FncStartLoop() {

	var items;
	var AryScroll = new Array();
	var ScrollName = 0;

	//window.alert("len:" + AryID.length);

	FncCreateObj();

	for (Movie = 0; Movie < WaitMovie.length; Movie++)
	{
		MoveState[Movie] = 1;
		MoveCounter[Movie] = MoveStopTime / 1.2;
		if ( AryObj[Movie].length > 2)
		{
			FncSetM_B(AryObj[Movie][1]);
			SetMarquee(Movie + 1,1);
			PointerCurrent[Movie] = 1;
		}
	}
	SetGlobalMoveTimer(1);

}

function SetGlobalMoveTimer(flg)
{
	if (flg == 1)
	{
		TimerID = setInterval('mov()',MoveInterval);
	}
	else
	{
		clearInterval(TimerID);
	}
}

function obj(ElementID) {//IDから要素を取得する
	//window.alert("id:" +  document.getElementById(ElementID) );
	return document.getElementById(ElementID);
} 


function mov()
{//再読み込みで全体を動かす。

	var NowPosition;
	var TMP;

	AryObject = new Object;
	for (Movie = 0; Movie < WaitMovie.length; Movie++)
	{
		if ( AryObj[Movie].length <= MaxVisible)
		{
			continue;
		}

		if (parseInt(WaitMovie[Movie]) == 1)
		{
			continue;
		}

		if (parseInt(MoveState[Movie]) == 1)
		{
			if ( parseInt(MoveStopTime) >= parseInt(MoveCounter[Movie]) )
			{
				MoveCounter[Movie]++;
				continue;
			}
			MoveCounter[Movie] = 1;
			MoveState[Movie] = 0;

		}

		FncClearM_B(Movie);
		SetMarquee(Movie + 1, - 1);

		if (MovePoint[Movie] + ScrollSpeed > BoxWidth + Offset )
		{
			MovePoint[Movie] = BoxWidth + Offset;
		}
		else
		{
			MovePoint[Movie] = MovePoint[Movie] + ScrollSpeed;
		}

		for (i=0; i < AryObj[Movie].length; i++)
		{
			AryObj[Movie][i].childNodes[0 + IntChildNodesFix].style.left = (i * BoxWidth) + (i * Offset) - MovePoint[Movie] + "px";
		}

		if (parseInt(MovePoint[Movie]) > BoxWidth - 1)
		{
			MovePoint[Movie] = 0;
			MoveState[Movie] = 1;

			TMP = AryObj[Movie].shift();
			TMP = AryObj[Movie].push(TMP);

			FncSetM_B(AryObj[Movie][1]);

			AryPointer = AryObj[Movie][1].id.split('.');
			IntPointer = AryPointer[1];
	
			SetMarquee(Movie + 1,parseInt(IntPointer) );

			//SetMarquee(Movie + 1,2);
			PointerCurrent[Movie] = 1;

		}
	}
}

function FncSetM_B(obj)
{
	obj.childNodes[0 + IntChildNodesFix].style.backgroundImage = 'url(/img2/search/bg-over.gif)';
}

function FncClearM_B(VarPointer)
{
	for (i = 0; i < PointerCurrent.length; i++)
	{
		if ( PointerCurrent[i] != - 1)
		{
			if (!AryObj[VarPointer])
			{
				window.alert("Pointer:" + VarPointer);
			}

			AryObj[VarPointer][PointerCurrent[i]].childNodes[0 + IntChildNodesFix].style.backgroundImage = 'url(/img2/search/bg.gif)';

		}
	}
}

function FncCreateObj()
{
	var AryDivs = document.getElementsByTagName("div");
	var AryPointer = new Array();
	var IntPointer;
	var IntPos;

	for (r = 0; r < ObjectLength; r++)
	{
		for (i = 0; i < AryDivs.length; i++)
		{
			IntPos = r + 1;
			if (AryDivs[i].id.indexOf("scroll" + IntPos  + ".") != -1)
			{
				AryDivs[i].style.display = 'block';
	
				AryPointer = AryDivs[i].id.split('.');
				IntPointer = AryPointer[1];
	
				AryObj[r][IntPointer - 1] = AryDivs[i];
			}
			else if (AryDivs[i].id.indexOf("marquee.") != -1)
			{
				AryPointer = AryDivs[i].id.split('.');
				IntPointer = AryPointer[1];
	
				AryMarq[IntPointer - 1] = AryDivs[i];
			}
	
			else if (AryDivs[i].id.indexOf("salescomments" + IntPos + ".") != -1)
			{
				AryPointer = AryDivs[i].id.split('.');
				IntPointer = AryPointer[1];
				AryObjMq[r][IntPointer - 1] = AryDivs[i];
			}
		}
	}
	FncCheckNodes();
	FncDisplay();

}

function FncCheckNodes()
{
	var Intlen;
	/* IE以外でのChild Nodes補正処理 */
	for (r = 0; r < ObjectLength; r++)
	{
		for (i=0; i < AryObj[r].length; i++)
		{
			IntLen = AryObj[r][i].childNodes.length;
			if (IntLen != 1)
			{
				IntChildNodesFix = 1;
			}
			break;
		}
	}

}

function FncRenewObj()
{
	var AryDivs = document.getElementsByTagName("div");
	var AryPointer = new Array();
	var IntPointer;

	for (i=0; i < AryDivs.length; i++)
	{
		if (AryDivs[i].id.indexOf("scroll1.") != -1)
		{
			AryPointer = AryDivs[i].id.split('.');
			IntPointer = AryPointer[1];

			AryObj[0][AryObj[0].length - 1] = AryDivs[i];


		}
	}
}


function FncDisplay()
{
	for (r = 0; r < ObjectLength; r++)
	{
		for (i=0; i < AryObj[r].length; i++)
		{
			AryObj[r][i].childNodes[0 + IntChildNodesFix ].style.left = i * BoxWidth + (i * Offset) + "px";
		}
	}


}
