/**
* ProductScroller: a jQuery Plugin to scroll products
* @author: Josh Johnson (@jnjosh)
* @version: 0.1
* @url: http://www.captiveaire.com
* @published: 01/19/2010
* @updated: 03/17/2010
*
* @notes: none.
*/
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
function guid() {
	function S4() {
	    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
	}
	return (S4() + S4() + S4());
}
if(typeof jQuery != 'undefined') {
	jQuery(function($) {
		$.fn.extend({
			ProductScroller: function(options, linkView) {
				var products = options;
				var $linkView = linkView, $productContainer;
				var divs = [];
				var lastIndex, firstIndex, linkViewIndex, currentIndex = 1, interval;
				var isScrolling = false;
				var singleScrollWidth = 217;
				
				/*
				 * Build Container View Item
				 */
				function divBuilder(i, prepending) {
					var id = guid();
					var $item = $("<div>").attr("class", "productbox").attr("id", id).attr("rel", i);
					$item.append($("<span>" + products[i].title + "</span>").attr("class", "productlabel"));
					$item.append($("<img>").attr("id", "productimage").attr("src", products[i].image));
					$item.append($("<span>" + products[i].desc + "</span>").attr("class", "desc"));
					$item.append($("<input>").val(products[i].url).attr("id", "boxurl").attr("type", "hidden"));
					$("#" + id).live("click", function() {
						location.href = $(this).find("input").val();
					});
					
					if (prepending) {
						divs.splice(0, 0, id);						
					} else {
						divs.push(id);
					}
					return $item;
				}
				
				/*
				 * Clear first container view item
				 */
				function clearFirst($context) {
					var id = divs[0],
						num = parseInt($("#" + id).attr("rel"));
					firstIndex = num === 0 ? num + 1 : num === (products.length - 1) ? 0 : num + 1;
					divs.remove(0);
					$("#" + id).remove();
				}
				
				/*
				 * Clear last container view item
				 */
				function clearLast($context) {
					var id = divs.pop(),
						num = parseInt($("#" + id).attr("rel"));
					lastIndex = num === (products.length - 1) ? num - 1 : num === 0 ? (products.length - 1) : num - 1;
					$("#" + id).remove();
				}
				
				/*
				 * Build Link View Item
				 */
				function linkViewItem(i) {
					var id = "linkViewItem" + i;
					var $linkItem = $("<a>").attr("id", id).addClass("linkViewItem").text(i+1);
					if (i === 0) {
						$linkItem.addClass("linkViewSelected");
					}
					//$linkItem.click(function() {
					//	// scroll
					//	scrollViewTo($("#productcontainer"), i+1);
					//});
					return $linkItem;
				}
				
				/*
				 * setup autorun interval	
				 */
				function setupInterval() {
					interval = setInterval(function() { $("#scrollRight").click();}, 8000);
				}
				
				/*
				 * Scroll ContainerView to index
				 */
				function scrollViewTo($container, index) {
					if (isScrolling === false) {
						if (index === currentIndex) return;
						
						// lock scrolling
						isScrolling = true;

						if (index === 0) {
							index = products.length;
							currentIndex = index+1;
						} else if (index === 1 && currentIndex===10) {
							currentIndex = 0;							
						}

						// calculate difference
						var diff = index - currentIndex,
							scrollDir = "+",
							oppScrollDir = "-",
							difference = Math.abs(diff),
							diffScrollWidth = singleScrollWidth * difference;
						
						if (diff < 0) {
							scrollDir = "-";
							oppScrollDir = "+";
						}
						
						// increment global index
						currentIndex = index;
							
						// prepend item 
						var k;
						if (scrollDir === "-") {
							//console.log("<");
							clearLast($container);
							$container.animate({left: '-=' + diffScrollWidth}, 0);
							k = (lastIndex + 1) === products.length ? 0 : lastIndex + 1;
							divBuilder(k, true).prependTo($container);
						} else {
							//console.log(">");
							clearFirst($container);
							$container.animate({left: '+=' + diffScrollWidth}, 0);
							k = (firstIndex - 1) === -1 ? (products.length - 1) : firstIndex - 1;
							divBuilder(k, false).appendTo($container);
						}
	
						// perform scroll		
						$container.animate({left: oppScrollDir + '=' + diffScrollWidth}, 250, function() {
							isScrolling = false; 
							// set up button links :: remove previous selection, add new selection
							$("a.linkViewSelected", $linkView).removeClass("linkViewSelected");
							$("#linkViewItem" + (index - 1)).addClass("linkViewSelected");
						});
						
						clearInterval(interval);
						setupInterval();
					}
				}
				
				// setup w/ jquery
				return this.each(
					function() {
						if($.fn.jquery < '1.2.6') {return;}
						var $self = $(this);
						
						// setup
						$self.addClass("productscroller");
						$self.append($("<img>").attr("id", "scrollLeft").attr("src", "images/CaptiveAire/design/homepage/left-test.png"));
						$self.append($("<img>").attr("id", "scrollRight").attr("src", "images/CaptiveAire/design/homepage/right-test.png"));
						$container = $("<div>").attr("id", "productcontainer");
						$self.append($container);

						// add children
						firstIndex = 0;
						for (var j = 0; j <= (products.length * 2)-1; j++) {
							var index = j >= products.length ? j - products.length : j;
							divBuilder(index).appendTo($container, false);
							linkViewItem(index).appendTo($linkView, false);
							if ($container.children().length == 10) break;
						}
						lastIndex = products.length - 1;
						$container.animate({left: '-=366'}, 0);
						
						// bindings
						$("#scrollLeft").live("click", function() {
							var scrollIndex = currentIndex - 1;
							if (scrollIndex > products.length) scrollIndex = 1;
							scrollViewTo($container, scrollIndex);
						});

						$("#scrollRight").live("click", function() {
							var scrollIndex = currentIndex + 1;
							if (scrollIndex > products.length) scrollIndex = 1;
							scrollViewTo($container, scrollIndex);
						});
						
						// setup autoscroll
						setupInterval();
					}
				);
			}
		});
	});
}