
var podManager = (function(jQuery) {
	
	if (!jQuery) {
		alert("jQuery is not loaded yet!");
		return null;
	}
	
	// Global dw private data goes here	

	/* 
	 * The problem is the resize (it's always basing it off of the imageWidth attribute
	 * which should only be used for the initial load
	 * It should also do a pass that normalizes it,
	 * 
	 */
	// dw scope public
	return {
		// variables for use with the arrows
		offset : 0,
		actualWidth : 0, 								// width of all the items plus the spacing between each one
		podsVisible : 2, 								// should never be lower than 2
		currentIndex : 0,
		nonPodWidth : -1, 							 	// start with it set to -1 so we know that it hasn't been set yet
		visibleWidth : 0, 								// pageWidth / podAreaWidth
		count : 0, 										// number of items
		podPadding : 0,
		selectors : {
			panel : "#panel",
			window: "#podsCntr > #clipped",
			images: ".podFrame .center img",
			imageContainers: ".center",
			pods: ".podFrame",
			leftButton: "#podControls .leftButton",
			rightButton: "#podControls .rightButton"
		},
		constants: {
			CAP_IMAGE_WIDTH : (2+2),					// width of the left and right end caps
			GAP : 9									// padding between pods
		},
		collections : {
			images : null,
			pods : null,
			widths : null,
			actualWidths: null
		},								// "Object" for storing collections so we can only do one call on jquery to ease on performance
		init : function () {
			
			// populate the collections
			podManager.collections.images = jQuery(podManager.selectors.images);
			podManager.collections.pods = jQuery(podManager.selectors.pods);
			podManager.collections.widths = new Array();
			podManager.collections.actualWidths = new Array();
			
			podManager.count = podManager.collections.images.length;
			
			// normalize the imageWidths
			podManager.normalizeImageWidths();
			
			// update values
			podManager.updateValues();
			
			// wireup buttons
			jQuery(podManager.selectors.leftButton).click(podManager.handleLeftClick);
			jQuery(podManager.selectors.rightButton).click(podManager.handleRightClick);
			jQuery(podManager.selectors.leftButton).hover (podManager.handleOn,podManager.handleOff);
			jQuery(podManager.selectors.rightButton).hover (podManager.handleOn,podManager.handleOff);
			
			
			// wireup pod click event
			podManager.collections.pods.click(function() {
				var url = jQuery(this).find("a").attr("href");
				var target= jQuery(this).find("a").attr("target");
				if (target == "_blank")
				{
					window.open(url);
					return false;
				}
				location.href = url;
				return false;
			});
			
			// wireup resize 
			jQuery(window).resize(podManager.resizePods);
			
			// and call the resize
			podManager.resizePods();
		},
		updateValues : function () {
			
			// first visibleWidth
			podManager.nonPodWidth = (podManager.nonPodWidth == -1) ? podManager.getNonPodWidth() : podManager.nonPodWidth; 
			var bodyMinWidth = jQuery("body").css("min-width");
			var minContentWidth = parseInt(bodyMinWidth.substring(0,bodyMinWidth.length-2));
			var normalizedWidth =  (minContentWidth > jQuery(window).width()) ? minContentWidth : jQuery(window).width();
			//var areaWidth = normalizedWidth - (leftRightMargin + leftColumn + PODoutsideTrim);
			podManager.visibleWidth = normalizedWidth - podManager.nonPodWidth;
			
			// now numThatFit
			podManager.podsVisible = -1;
			var viewablePodImageWidth = 0;
			var podImageWidth = 0;
			jQuery.each(podManager.collections.widths, function(index, item) {
				//alert(item);
				podImageWidth += index > 0 ? parseInt(item) + podManager.constants.GAP : parseInt(item);
				podImageWidth += podManager.constants.CAP_IMAGE_WIDTH;
				if(podManager.visibleWidth < podImageWidth && podManager.podsVisible == -1)
					{podManager.podsVisible = index;}
				else if(podManager.visibleWidth >= podImageWidth && index <3)
				{
					viewablePodImageWidth = podImageWidth;
				}
				podManager.podsVisible = (podManager.podsVisible == -1 && index == podManager.collections.widths.length - 1) ? podManager.collections.widths.length : podManager.podsVisible;
			
			});
			podManager.podsVisible = (podManager.podsVisible < 2) ? 2 : podManager.podsVisible;
			podManager.podsVisible = (podManager.podsVisible >= 3) ? 3 : podManager.podsVisible;
			podManager.podPadding = (podManager.visibleWidth - viewablePodImageWidth) / podManager.podsVisible;
			
			// now calculate the actual width  of all those gosh darn pods (collectively and separately)
			podManager.actualWidth = 0;
			podManager.collections.actualWidths = new Array();
			jQuery(podManager.selectors.pods).each(function(index){
				var width = index > 0 ? jQuery(this).width() + podManager.constants.GAP : jQuery(this).width();
				podManager.actualWidth += width;
				podManager.collections.actualWidths.push(width);
			});
			
			// setup the hover states
			if(podManager.currentIndex == podManager.count - podManager.podsVisible)
			{
				// we've gone as far to the right that we can go
				jQuery(podManager.selectors.rightButton).css({"opacity" : "0.6", "alpha" : "opacity(60)"})
			}
			else
			{
				jQuery(podManager.selectors.rightButton).css({"opacity" : "1.0", "alpha" : "opacity(100)"})
			}
			if(podManager.currentIndex == 0)
			{
				// we've gone as far to the right that we can go
				jQuery(podManager.selectors.leftButton).css({"opacity" : "0.6", "alpha" : "opacity(60)"})
			}
			else
			{
				jQuery(podManager.selectors.leftButton).css({"opacity" : "1.0", "alpha" : "opacity(100)"})
			}
			/* DeBug
			jQuery("span#key_1").html("Offset:");
			jQuery("span#value_1").html(podManager.offset);

			jQuery("span#key_2").html("Visible Pod Image Width:");
			jQuery("span#value_2").html(viewablePodImageWidth);
			jQuery("span#key_3").html("Page Width:");
			jQuery("span#value_3").html(podManager.visibleWidth);
			jQuery("span#key_4").html("Num That Fit:");
			jQuery("span#value_4").html(podManager.podsVisible);
			jQuery("span#key_5").html("Pod Index:");
			jQuery("span#value_5").html(podManager.currentIndex);
			jQuery("span#key_6").html("Pod Padding:");
			jQuery("span#value_6").html(podManager.podPadding);
			jQuery("span#key_7").html("Pod Count:");
			jQuery("span#value_7").html(podManager.count);
			*/
		},
		normalizeImageWidths : function () {
			// grab all the images
			// populate initial cut of widths
			
			podManager.collections.images.each(function(i) {
				podManager.collections.widths.push(jQuery(this).attr("imageWidth"));
			});
		},
		resizePods : function () {
			podManager.updateValues();
			
			// check index to see if they can fit
			if((podManager.count) - podManager.currentIndex < podManager.podsVisible)
			{
				
				podManager.currentIndex = podManager.count - podManager.podsVisible  >= podManager.currentIndex ? podManager.currentIndex : podManager.count - podManager.podsVisible;
			}
			jQuery(podManager.selectors.window).css("width", podManager.visibleWidth);
			jQuery.each(podManager.collections.images, function(index, item) {
				var obj = jQuery(this);
				var width = parseInt(obj.attr("imageWidth"));
				width += podManager.podPadding + podManager.constants.CAP_IMAGE_WIDTH;
				obj.parents(podManager.selectors.pods).css("width", width + "px");
				width -= podManager.constants.CAP_IMAGE_WIDTH;
				obj.parents(podManager.selectors.imageContainers).css("width", width+"px");
			});
			
			podManager.realignPods();
			jQuery(podManager.selectors.panel).css("left", podManager.offset+"px");
		},
		realignPods : function () {
		//	alert(podManager.currentIndex +" MODULUS "+ podManager.podsVisible +" = "+ podManager.currentIndex % podManager.podsVisible);
		//	var tinyOffset = 0;
		//	var remainder = podManager.currentIndex % podManager.podsVisible;
		//	tinyOffset = remainder == 0 ? podManager.constants.GAP : 
		//	podManager.offset = ((podManager.visibleWidth) * (podManager.currentIndex / podManager.podsVisible) * -1) - (podManager.constants.GAP);
		//	podManager.offset = ((podManager.visibleWidth) * (podManager.currentIndex / podManager.podsVisible) * -1) - (podManager.constants.GAP / (podManager.currentIndex % podManager.podsVisible));
			var newOffset = 0;
			jQuery(podManager.selectors.pods).each(function(index) {
				if(index < podManager.currentIndex)
				{
					if(jQuery(this).hasClass("first"))
					{
						newOffset -= jQuery(this).width() + podManager.constants.GAP;
					}
					else
					{	
						newOffset -= jQuery(this).width() + podManager.constants.GAP;//+.5;
					}
				}
			});
			podManager.offset = newOffset;
			podManager.offset = podManager.offset > 0 || podManager.offset == (podManager.constants.GAP*-1) ? 0 : podManager.offset;
		},
		getNonPodWidth : function () {
			var leftRightMargin= 102;
			var PODoutsideTrim =36;
			var leftColumn = jQuery("td.storefrontLeftCell").width();

			return (leftRightMargin + leftColumn + PODoutsideTrim);
		},
		handleLeftClick : function () {
			podManager.updateValues();
			
			if((podManager.offset * -1) <= 0)
				{podManager.offset = 0;podManager.currentIndex=0;}
			else
			{
				//podManager.offset += podManager.visibleWidth + podManager.constants.GAP; // the GAP is an IE7 Hack
				//podManager.currentIndex -= podManager.podsVisible;
				var object = jQuery(podManager.selectors.pods).get(podManager.currentIndex);
				//alert(jQuery(object).html());
				if(jQuery(object).hasClass("first"))
				{
					podManager.offset += jQuery(object).width() + podManager.constants.GAP;
				}
				else
				{	
					podManager.offset += jQuery(object).width() + podManager.constants.GAP;//+.5;
				}
				//podManager.offset += jQuery(object).width() + podManager.constants.GAP;
				podManager.currentIndex--;
				
			}
			if(podManager.offset >= 0)
			{
				podManager.offset = 0;
				podManager.currentIndex = 0;
			}
			podManager.updateValues();
			//podManager.realignPods();
			// slide animation
			jQuery(podManager.selectors.panel).animate({"left": podManager.offset+"px"}, 500);	
			//podManager.realignPods();
		},
		handleRightClick : function () {
			podManager.updateValues();
			if((podManager.offset * -1) + podManager.visibleWidth >= podManager.actualWidth)
			{
				podManager.offset = podManager.offset;
				podManager.currentIndex = podManager.count - podManager.podsVisible;
			}
			//else if((podManager.offset * -1)+(podManager.visibleWidth*2) > podManager.actualWidth)
			//{
				/*
				//alert(podManager.count);
				var end = podManager.count - podManager.podsVisible;
				var amountToSubtract = 0;
				for(var i=podManager.collections.actualWidths.length-1;i>=end;i--)
				{
				//	alert("i="+i+ " | "+ amountToSubtract+" + "+podManager.collections.actualWidths[i]);
					amountToSubtract += parseInt(podManager.collections.actualWidths[i]);
				}
				//alert(amountToSubtract);
				podManager.offset = -1* (podManager.actualWidth - amountToSubtract+9);
				podManager.currentIndex = end;
				*/
		//	}
			else if(podManager.currentIndex < podManager.count - podManager.podsVisible)
			{
				
				//podManager.offset -= podManager.visibleWidth + podManager.constants.GAP;
				//podManager.currentIndex += podManager.podsVisible;
				
				//alert("Number of pods: "+jQuery(podManager.selectors.pods).length);
				var object = jQuery(podManager.selectors.pods).get(podManager.currentIndex);
				if(jQuery(object).hasClass("first"))
				{
					podManager.offset -= jQuery(object).width() + podManager.constants.GAP;
				}
				else
				{	
					podManager.offset -= jQuery(object).width() + podManager.constants.GAP;//+.5;
				}
				podManager.currentIndex++;
			}
			
			podManager.updateValues();
			//podManager.realignPods();
			jQuery(podManager.selectors.panel).animate({"left" : podManager.offset+"px"}, 500);
			
			
		},
		handleOn : function() {
			var imageObj = jQuery(this).find("img");
			
			// grab the image url
			var url = imageObj.attr("src");
			var index = url.indexOf("_off.jpg");
			if(index != -1)
			{
				//alert(url.substr(0,index));
				imageObj.attr("src", url.substr(0,index)+ "_on.jpg");
			}
			
			
		},
		handleOff : function() {
			var imageObj = jQuery(this).find("img");
			
			// grab the image url
			var url = imageObj.attr("src");
			var index = url.indexOf("_on.jpg");
			if(index != -1)
			{
				//alert(url.substr(0,index));
				imageObj.attr("src", url.substr(0,index)+ "_off.jpg");
			}
		},
		/*
		 * calculates the width
		 */
		calcVisiblePodWidths : function () {
			
		}
	}
})(jQuery);