// lil_Toady's awesome military scroller class

Scroller = new function ()
{
	this.Area = null;
	this.Wrapper = null;
	this.Pane = null;
	this.Limit = 0;

	this.Timer = null;
	this.Speed = 0;
	this.Dimensions = [
		x = 0,
		y = 0,
		width = 0,
		height = 0
	];
	this.Buttons = [
		state = 0,
		left = null,
		right = null
	];
	this.Cursor = [
		x = 0,
		y = 0
	];

	this.Preview = null;
	this.PreviewIMG = null;

	
	if ( document.layers ) document.captureEvents ( Event.MOUSEMOVE );

	document.onmousemove = function ( e )
	{
		if (!e) var e = window.event;
		Scroller.Cursor.x = !isNaN(e.pageX) ? e.pageX : e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		Scroller.Cursor.y = !isNaN(e.pageX) ? e.pageY : e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	}

	this.Init = function ( area, wrapper, pane, left, right )
	{
		this.Area = document.getElementById ( area );
		this.Wrapper = document.getElementById ( wrapper );
		this.Pane = document.getElementById ( pane );

		this.Limit = this.Pane.offsetWidth - this.Wrapper.offsetWidth - 40; // deal with margin
	
		// get wrapper dimensions
		var temp = this.Wrapper;
		this.Dimensions.x = 0;
		this.Dimensions.y = 0;
		while ( temp != null )
		{
			this.Dimensions.x += temp.offsetLeft;
			this.Dimensions.y += temp.offsetTop;
			temp = temp.offsetParent;
		}

		this.Dimensions.width = this.Dimensions.x + this.Wrapper.offsetWidth;
		this.Dimensions.height = this.Dimensions.y + this.Wrapper.offsetHeight;

		this.Buttons.state = 0;
		if ( left && right )
		{
			this.Buttons.left = document.getElementById ( left );
			this.Buttons.right = document.getElementById ( right );
		}

		this.createPreview ();
	}


	this.Start = function ( element )
	{
		if ( ( element == this.Area ) && ( !this.Timer ) )
		{
			this.Timer = setInterval ( this.Pulse, 50 );
		}
	}
	
	this.Pulse = function ()
	{
		var center = ( Scroller.Dimensions.x + Scroller.Dimensions.width ) / 2;
		Scroller.Speed = ( ( Scroller.Cursor.x / center ) - 1 ) * -50;

		var x = parseInt ( Scroller.Pane.style.left ) || 0;
		x += Scroller.Speed;

		if ( x > 0 )
		{
			x = 0;
		}
		else if ( x < -Scroller.Limit )
		{
			x = -Scroller.Limit;
		}
		Scroller.Pane.style.left = x + 'px';
		Scroller.Check ();
	}

	this.Stop = function ( element )
	{
		/*
		var temp = element.offsetParent;
		while ( temp != null )
		{
			if ( temp == this.Area )
			{
				return false;
			}
			temp = temp.offsetParent;
		}
		*/
		if ( element == this.Area )
		{
			clearInterval ( this.Timer );
			this.Timer = null;
			this.Speed = 0;
		}
		this.Check ();
	}

	this.Check = function ()
	{
		// Only gonna work with jQuery
		if ( this.Buttons.left && this.Buttons.right )
		{
			var state = 0;
			if ( this.Speed > 0 ) state = 1;
			else if ( this.Speed < 0 ) state = 2;
	
			if ( state != this.Buttons.state )
			{
				if ( this.Buttons.state == 1 ) $(this.Buttons.left).children().children(".down").stop().fadeTo("fast", 0.0);
				else if ( this.Buttons.state == 2 ) $(this.Buttons.right).children().children(".down").stop().fadeTo("fast", 0.0);

				if ( state == 1 ) $(this.Buttons.left).children().children(".down").stop().fadeTo("fast", 1.0);
				else if ( state == 2 ) $(this.Buttons.right).children().children(".down").stop().fadeTo("fast", 1.0);
	
				this.Buttons.state = state;
			}
		}
	}

	this.createPreview = function ()
	{
		var div = document.createElement ( 'div' );
		div.setAttribute ( 'class', 'potd-preview' );
		div.setAttribute ( 'className', 'potd-preview' );
		div.style.visibility = 'hidden';
		div.onclick = Scroller.closePreview;
		document.body.appendChild ( div );

		var img = document.createElement ( 'img' );
		img.setAttribute ( 'src', '' );
		img.setAttribute ( 'alt', '' );
		img.setAttribute ( 'width', '70%' );
		img.setAttribute ( 'class', 'potd-img' );
		img.setAttribute ( 'className', 'potd-img' );
		img.style.visibility = 'hidden';

		document.body.appendChild ( img );

		this.Preview = div;
		this.PreviewIMG = img;
	}

	this.openPreview = function ( url, name )
	{
		this.Preview.style.visibility = 'visible';
		this.PreviewIMG.style.visibility = 'visible';
		this.PreviewIMG.src = url;
		this.PreviewIMG.setAttribute ( 'src', url );
		this.PreviewIMG.setAttribute ( 'alt', name );
	}

	this.closePreview = function ()
	{
		this.Preview.style.visibility = 'hidden';
		this.PreviewIMG.style.visibility = 'hidden';
	}
}
