
$(function() {
	var brandSliderOptions = {
		btnPreviousSelector: "#brand-slider-previous",
		btnNextSelector: "#brand-slider-next",
		itemMargin: 20,
		slideSpeed: "fast"
	};
	
	$("#brand-slider ul").each(function() {
		var thisLocal = this;
		var theImages = $("img", this);
		var theItems = $("li", this);
		
		// Clean up some styles
		$(thisLocal).css({
			"padding": "0",
			"margin": "0",
			"list-style-type": "none",
			"position": "relative"
		});
		
		theImages.css({
			"display": "block",
			"border-width": 0,
			"position": "absolute",
			"top": 0
		});
		
		theItems.css({
			"display": "inline"
			//"float": "left",
			//"height": $(thisLocal).parent().height() + "px",
			//"margin-right": brandSliderOptions.itemMargin + "px"
		});
		
		var numItems = theItems.length;
		//alert("numItems = " + numItems);
		
		// Calculate total width of all images + inner margins
		var totalWidth = 0;
		theImages.each(function() {
			totalWidth += $(this).width();
			//$(this).parent().css("width", $(this).width());
		});
		if (numItems > 0) {
			totalWidth += (numItems - 1) * brandSliderOptions.itemMargin;
		}
		//alert("totalWidth = " + totalWidth);
		
		// Compute an array of offsets to "center" the UL at each given image
		var offsets = [];
		//var xOffsetCurrent = $(thisLocal).width() / 2; // Start at center of the UL
		var xOffsetCurrent1 = 0; // Start at center of the UL
		var xOffsetCurrent2 = $(thisLocal).width() / 2; // Start at center of the UL
		theImages.each(function() {
			// Position the image within the UL
			$(this).css("left", xOffsetCurrent1);
			
			var thisItemWidth = $(this).width();
			
			xOffsetCurrent1 += thisItemWidth + brandSliderOptions.itemMargin;
			
			xOffsetCurrent2 -= thisItemWidth / 2; // move left half an item's width
			offsets.push(xOffsetCurrent2);
			xOffsetCurrent2 -= thisItemWidth / 2; // move left the other half
			xOffsetCurrent2 -= brandSliderOptions.itemMargin; // move left a margin
		});
		//alert(offsets);
		
		// Make the UL wide enough to hold it all
		$(thisLocal).css("width", totalWidth + 100 + "px");
		
		var currentItem = -1;
		var goToItem = -1;
		var locked = false;
		
		if (numItems > 0) {
			//currentItem = 0;
			
			currentItem = Math.floor(numItems / 2);
			
			// Start at the currentItem
			$(thisLocal).css("margin-left", offsets[currentItem] + "px");
			
			// Set up next/prev buttons
			if (brandSliderOptions.btnPreviousSelector) {
				$(brandSliderOptions.btnPreviousSelector).click(function() {
					//modified so user can't go below item 2					
					//if (!locked && currentItem > 0) {
					if (!locked && currentItem > 2) {
						// Lock the slider
						locked = true;
						
						// Set the next item
						goToItem = currentItem - 1;
						
						// Begin tween
						$(thisLocal).animate({
							"margin-left": offsets[goToItem] + "px"
						}, brandSliderOptions.slideSpeed, function() {
							// Unlock the slider
							locked = false;
							
							currentItem = goToItem;
						});
					}
					return false;
				});
			}
			
			if (brandSliderOptions.btnNextSelector) {
				$(brandSliderOptions.btnNextSelector).click(function() {					//modified so user can't go above max-2 item
					//if (!locked && currentItem < numItems - 1) {
					if (!locked && currentItem < numItems - 3) {
						// Lock the slider
						locked = true;
						
						// Set the next item
						goToItem = currentItem + 1;
						
						// Begin tween
						$(thisLocal).animate({
							"margin-left": offsets[goToItem] + "px"
						}, brandSliderOptions.slideSpeed, function() {
							// Unlock the slider
							locked = false;
							
							currentItem = goToItem;
						});
					}
					return false;
				});
			}
		}
	});
});
