if (!SOF) { var SOF = {}; }
if (!SOF.components) { SOF.components = {}; }


SOF.components.Conveyor = function() {

	return {
		initialize:function() {
			
			// testing purposes only
			//$('#conveyor #products div.item').each(function(i){ if (i > 2) $(this).remove(); });
			
			
			/**
			* Duplicate items on conveyor if total number of items does not
			* meet minimum requirement. Copies both HTML and events bound.
			*/
			var minItems = 8;
			var items = $('#conveyor #products div.item');

			if (items.length < minItems) {
				var diff = minItems-items.length;
				// Start at 1, item 0 will have a unique ID we don't want to copy
				for (var i = 1 ; i <= diff ; i++) {
					var item = $(items[i]);
					item.clone(true).appendTo('#conveyor #products .scrollableArea');
					// Update items object with cloned items
					items = $('#conveyor #products div.item');
				}
			}
			
			
			$("#conveyor #products").smoothDivScroll({
				scrollingHotSpotLeft: "div.scrollingHotSpotLeft", 
				scrollingHotSpotRight: "div.scrollingHotSpotRight",
				visibleHotSpots: "onstart", 
				autoScroll: "always", 
				autoScrollDirection: "endlessloopright",
				autoScrollStep: 1,
				autoScrollInterval: 35,
				startAtElementId: 'startHere'
			});
			
			
			$("#conveyor #conveyor-belt").smoothDivScroll({
				scrollingHotSpotLeft: "div.scrollingHotSpotLeft", 
				scrollingHotSpotRight: "div.scrollingHotSpotRight",
				visibleHotSpots: "onstart", 
				autoScroll: "always", 
				autoScrollDirection: "endlessloopright",
				autoScrollStep: 1,
				autoScrollInterval: 35
			});
			
			this.addEventListeners();
			
			// z-index re-order for IE
			if ($.browser.msie === true && $.browser.version != "8.0") {
				var item_length = $("#conveyor #products div.item").length;
				$("#conveyor #products div.item").each(function(i){
					$(this).css('z-index', item_length-i);
				});
			}
			
		},
		addEventListeners:function() {
			
			$(window).blur(function(){
				$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("stopAutoScroll");
			});
			
			$(window).focus(function(){
				$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("startAutoScroll");
			});
			
			$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("stopAutoScroll");
			
			// Start and stop conveyor on item rollover
			$("#conveyor .item").hover(
				function(){
					$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("stopAutoScroll");
				},
				function(){
					$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("startAutoScroll");
				}
			);
	
			// Show product details on price label rollover
			$('#conveyor .item .details').hover(
				function(e){
				
					// prevent duplicates in case dummy wasn't removed on mouse out
					$('#dummyDetails').remove(); 
					
					// clone the pricing details and place them outside of the conveyor belt
					var offset = $(this).offset();
					var cloned = $('p', this).clone(true);
					
					cloned.attr('id', 'dummyDetails');
					cloned.css('top', offset.top+80); // top + some extra padding
					cloned.css('left', offset.left);
					
					$('body').append(cloned);
					
				},
				function() {
					// remove dummy element
					$('#dummyDetails').remove();
				}
			);
			
			// hide the first few items on init
			$('#conveyor .item').each(function(i){
				if (i < 3) {
					//$(this).css('visibility', 'hidden');
				} else {
					//return false;
				}
			});
			
			// Add forward / rewind buttons
			$('#conveyor').append('<div class="arrow-left"><span></span></div>');
			$('#conveyor').append('<div class="arrow-right"><span></span></div>');
			
			
			$('#conveyor .arrow-right').hover(
				function(){
					$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("option","autoScrollStep",6);
					$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("option","autoScrollDirection","endlessloopright");
				},
				function(){
					$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("option","autoScrollStep",1);
					$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("option","autoScrollDirection","endlessloopright");
				}
			);
			
			$('#conveyor .arrow-left').hover(
				function(){
					$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("option","autoScrollStep",6);
					$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("option","autoScrollDirection","endlessloopleft");
				},
				function(){
					$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("option","autoScrollStep",1);
					$("#conveyor #products, #conveyor #conveyor-belt").smoothDivScroll("option","autoScrollDirection","endlessloopleft");
				}
			);
		}
		
	}; // end return

}();

