/* HomeGoods ~ JAVASCRIPT Dynamic Scrolling Box

	Author: Joel Nagy | Scroller
	Last Modified: April 10th, 2009 12:12:12 PM EST

	Dependency: prototype 1.7 and scriptaculous 1.8 
*/

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

Scroller.Width = 0;
Scroller.ShellWidth = 0;
Scroller.Count = 0;
Scroller.MaxSmoothScroll = 3; // How far is too far to slide from one end to the other
Scroller.index = null;
Scroller.pauseEvents = true;
Scroller.timer = null;
Scroller.TimeToScroll = 7000; // 6 seconds
Scroller.autoScroll = true;
Scroller.XML = '';
Scroller.delay = 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');

		if (Scroller.index.value == 1) { // start
			var $b = $('ScrollerBlock_'+ (Scroller.Count-1));
			$x = $b.cloneNode(true);
			$x.setAttribute('id', 'ScrollerBlock_X');
			prependChild($first, $x);
			$first.style.width = (Scroller.Width*(Scroller.Count+1)+100)+'px';
			$first.style.left = (-1*Scroller.Width)+'px';

			new Scroller.scrollFX('ScrollContainerFirst', Scroller.Width,
				function () {
					Scroller.index.value = Scroller.Count;
					$first.style.left = (-1*Scroller.Width*(Scroller.Count-1))+'px';
					$first.removeChild($('ScrollerBlock_X'));
					$first.style.width = (Scroller.Width*(Scroller.Count)+100)+'px';
					Scroller.autoScrollActivate();	
				});		
		} else { // others
			new Scroller.scrollFX('ScrollContainerFirst', Scroller.Width,
				function () {
					Scroller.index.value = parseInt(Scroller.index.value) -1;
					Scroller.autoScrollActivate();
				});
		}
	} catch (e)  { }
}

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

		var $shell = $('ScrollShell');
		var $first = $('ScrollContainerFirst');

		if (Scroller.index.value == Scroller.Count) { // end
			// At the end so we need to prep the container to hold the content in order for the scroll to the begining
			// dulicate block1 -> scroll -> shift to true beginging and rm dupliacted block1
			var $b = $('ScrollerBlock_0');
			$x = $b.cloneNode(true);
			$x.setAttribute('id', 'ScrollerBlock_X');
			$first.appendChild($x);
			$first.style.width = (Scroller.Width*(Scroller.Count+1)+100)+'px';

			new Scroller.scrollFX('ScrollContainerFirst', (-1*Scroller.Width),
				function () {
					Scroller.index.value = 1;
					$first.style.left = 0;
					$first.removeChild($('ScrollerBlock_X'));
					$first.style.width = (Scroller.Width*(Scroller.Count)+100)+'px';
					Scroller.autoScrollActivate();
				});			
		} else { // others
			new Scroller.scrollFX('ScrollContainerFirst', (-1*Scroller.Width),
				function () {
					Scroller.index.value = parseInt(Scroller.index.value) +1;
					Scroller.autoScrollActivate();
				});
		}
	} 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: 0.4,  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) {
			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;

			// 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 / c);

			for (var i = 0; i < l; 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*c, m = j+c; j < m; j++)
					if (X[j] != null)
						$d.appendChild(X[j]);
			}

			var block = $d;
			Scroller.Count = l;
			Scroller.Width = Scroller.parseInt($(block).getStyle('width')) + Scroller.parseInt($(block).getStyle('margin-left')) + Scroller.parseInt($(block).getStyle('margin-right'));

			// 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 = 1;
		} else {
			Scroller.index = {};
			Scroller.index.value = 1;
		}

		if (Scroller.delay > 0)
			setTimeout("Scroller.delay = 0; Scroller.activate();", Scroller.delay);
		else
			Scroller.activate();
	} catch (e)  { }
}); //: dom:loaded