/* HomeGoods ~ JAVASCRIPT Dynamic Scrolling Box

	Author: Joel Nagy | Scroller
	Last Modified: November 16th, 2009 10:22:54 PM EST

	Dependency: prototype 1.7 and scriptaculous 1.8 
*/

// NAMESPACE
if (Scroller == null) // check for existence of namespace
var Scroller = {};

Scroller.Speed = 0;
Scroller.Width = 0;
Scroller.ItemWidth = 0;
Scroller.ShellWidth = 0;
Scroller.Count = 0;
Scroller.index = null;
Scroller.pauseEvents = true;
Scroller.timer = null;
Scroller.TimeToScroll = 7000; // 6 seconds
Scroller.AutoScroll = true;
Scroller.XML = '';
Scroller.delay = 0;
Scroller.PXtoScroll = 0;
Scroller.ViewPerBlock = 0;
Scroller.ViewPerScroll = 0;
Scroller.pos = 0;

Scroller.NUM_RX = new RegExp(/^\d+$/);

Scroller.isString = function(o) {
 			return (typeof o).toLowerCase() == 'string';
}

function prependChild(parent, node) {
    if (parent.firstChild) {
        parent.insertBefore(node, parent.firstChild);
    } else {
        parent.appendChild(node);
    }
}


Scroller.goLeft = function() {
	try {	
		if (Scroller.pauseEvents == true) return;
			else Scroller.pauseEvents = true;

		var $shell = $('ScrollShell');
		var $first = $('ScrollContainerFirst');
		var $kids = $first.childElements();

		var x = parseInt(Scroller.index.value);
		var pos = parseInt($first.style.left);

		if (x == 0) {
			Scroller.index.value = 0;
			var kid = '';
			for (var i = $kids.length-Scroller.ViewPerBlock, l = $kids.length; i < l; i++) {
				kid = $kids[i].getAttribute('id').replace(/ScrollerBlock_/, '');
				$first.insertBefore($('ScrollerBlock_'+ kid), $kids[0]);
			}
			$first.style.left = (-1 * Scroller.PXtoScroll) +'px';
		} else {
			Scroller.index.value = x-1;
		}

		new Scroller.scrollFX('ScrollContainerFirst', Scroller.PXtoScroll,
			function () {
				Scroller.autoScrollActivate();					
			});
		return;
	} catch (e)  { }
}

Scroller.goRight = function() {
	try {
		if (Scroller.pauseEvents == true) return;
			else Scroller.pauseEvents = true;

		var $first = $('ScrollContainerFirst');
		var $kids = $first.childElements();

		new Scroller.scrollFX('ScrollContainerFirst', (-1*Scroller.PXtoScroll),
			function () {
				Scroller.autoScrollActivate();

				var x = parseInt(Scroller.index.value);	
				if (Scroller.ViewPerBlock == x+1) {
					Scroller.index.value = 0;
					var kid = '';
					for (var i = 0, l = Scroller.ViewPerBlock; i < l && i < Scroller.Count; i++) {
						kid = $kids[i].getAttribute('id').replace(/ScrollerBlock_/, '');
						$first.appendChild($('ScrollerBlock_'+ kid));
					}
					$first.style.left = '0px';
				} else {
					if (x == Scroller.Count-1) {
						Scroller.index.value = 0;
					} else
						Scroller.index.value = x+1;
				}							
			});
		return;
	} catch (e)  { }
}

Scroller.dbg = function(s) {
	//s = $('scroller_dbg').innerHTML +' $$ '+ s;
	$('scroller_dbg').innerHTML = s; 
}

Scroller.scrollFX = function(a, x, finish) {
	try {
		finish = finish == null? function () { }: finish;
		new Effect.MoveBy(a, 0, x, {duration: Scroller.Speed,  transition: Effect.Transitions.sinoidal, queue: 'end', afterFinish: finish});
	} catch (e)  { }
}

Scroller.autoScroll = function() {
	try {
		if (Scroller.pauseEvents == true) return;

		Scroller.goRight();
		if (Scroller.AutoScroll)
			Scroller.timer = setTimeout('Scroller.autoScroll();', Scroller.TimeToScroll);
	} catch (e)  { }
}

Scroller.autoScrollActivate = function() {	
	try {
		Scroller.pauseEvents = false;
		clearTimeout(Scroller.timer);
		Scroller.timer = null;
		if (Scroller.AutoScroll)
			Scroller.timer = setTimeout('Scroller.autoScroll();', Scroller.TimeToScroll);
	} catch (e)  { }
}

Scroller.pause = function() {
	try {
		Scroller.pauseEvents = true;
		clearTimeout(Scroller.timer);
		Scroller.timer = null;
	} catch (e)  { }
}

Scroller.resume = function() {
	try {
		Scroller.pauseEvents = false;
		clearTimeout(Scroller.timer);
		Scroller.timer = null;
		if (Scroller.AutoScroll)
			Scroller.timer = setTimeout('Scroller.autoScroll();', Scroller.TimeToScroll);
	} catch (e)  { }
}

Scroller.parseInt = function(val) {
	var v = parseInt(val);
	if (v == null || !v || v.toString() == 'null' || v.toString() == 'undefined' || v.toString() == 'NaN')
		v = 0;
	return v;
}

Scroller.prep = function() {
	try {		
		$shell = $('ScrollShell');
		var X = $$('div.scrollItem');
		if (X.length > 0) {
			if (Scroller.Speed == 0)
				Scroller.Speed = 0.4;
			
			var w = Scroller.parseInt($(X[0]).getStyle('width')) + Scroller.parseInt($(X[0]).getStyle('margin-left')) + 			Scroller.parseInt($(X[0]).getStyle('margin-right'));

			Scroller.ShellWidth = parseInt($($shell).getStyle('width')); ///parseInt(Scroller.getStyle($shell, "width"));
	
			// How many Items will fit per block
			var c = Math.floor(Scroller.ShellWidth / w);
			if (c < 1) c = 1;
			
			if (Scroller.ViewPerBlock == 0)
				Scroller.ViewPerBlock = c;
			if (Scroller.ViewPerScroll == 0)
				Scroller.ViewPerScroll = c; 

			// Now insert Containers
			var $first = new Element('div', {id: 'ScrollContainerFirst', className: 'ScrollContainer'}); // className is required over 
			$shell.appendChild($first);

			var $d = null, l = Math.ceil(X.length / Scroller.ViewPerBlock);
			if (Scroller.Count == 0)
				Scroller.Count = l;
			else if (Scroller.Count < 0)
				Scroller.Count = X.length;

			for (var i = 0; i < Scroller.Count; i++) {
				$d = new Element('div', {id: 'ScrollerBlock_'+i, className: 'block'});
				$d.setAttribute('class', 'block'); // For IE 6 & 8
				$first.appendChild($d);
				for (var j = i*Scroller.ViewPerScroll, m = j+Scroller.ViewPerScroll; j < m; j++)
					if (X[j] != null)
						$d.appendChild(X[j]);
			}

			var block = $d;
			Scroller.Width = Scroller.parseInt($(block).getStyle('width')) + Scroller.parseInt($(block).getStyle('margin-left')) + Scroller.parseInt($(block).getStyle('margin-right'));
			Scroller.ItemWidth = Scroller.parseInt($(X[0]).getStyle('width'));

			if (Scroller.PXtoScroll == 0)
				Scroller.PXtoScroll = Scroller.Width;
			else if (Scroller.PXtoScroll < 0)
				Scroller.PXtoScroll = Scroller.ItemWidth;
// if Scroller.PxtoScroll == Scroller.Width, then Scroller.Count = Scroller.Width / Scroller.ItemWidth
// else if Scroller.PxtoScroll < Scroller.Width, then Scroller.Count = Scroller.Width / PScroller.xtoScroll;	

			// Prep positioning and width
			$first.style.position = 'absolute';
			$first.style.left = 0;
			$first.style.top = 0;
			$first.style.width = (Scroller.Count*Scroller.Width+100) +'px';

			// Prep Auto-Scroll
			if (Scroller.Count > 1 && Scroller.AutoScroll) {
				Scroller.timer = setTimeout('Scroller.autoScroll();', Scroller.TimeToScroll);
			} else
				Scroller.AutoScroll = false;
			Scroller.pauseEvents = false;

			// Prep Arrows
			if (Scroller.Count > 1 && $('LeftArrow')) {
				$('LeftArrow').style.display = 'block';
				$('RightArrow').style.display = 'block';
			}
		}
	} catch (e)  { }
}

Scroller.activate = function() {
	if (Scroller.XML != '') {
		// pull in XML for aditional scroll data and populate #ScrollShell
		new Ajax.Request(Scroller.XML, {
			method:'get',
			onSuccess: function(transport){
				var xml = transport.responseText;
				$('ScrollShell').innerHTML += xml;
				Scroller.prep();
			}
		  });
	} else
		Scroller.prep();
}

document.observe('dom:loaded', function() {
	try {
		if ($("scrollerIndex") != null) {
			Scroller.index = $("scrollerIndex");
			Scroller.index.value = 0;
		} else {
			Scroller.index = {};
			Scroller.index.value = 0;
		}

		if (Scroller.delay > 0)
			setTimeout("Scroller.delay = 0; Scroller.activate();", Scroller.delay);
		else
			Scroller.activate();

	} catch (e)  { }
}); //: dom:loaded