var isShipOMeterActive = true;

var _lastSMValue = null;
function updateShipOMeter(o) {
		
		
		var siteContainer = $('div.bn_g_vertical');
		var cartContainer = $('div.shipMeter');
		if (!(siteContainer) && !(cartContainer)) return;
		if (siteContainer) {
			if ($('siteContainer').is (':hidden') || $(siteContainer).parents(':hidden').length > 0) return;
		}

		
		if(_lastSMValue && (o == _lastSMValue)) return;
		_lastSMValue = o;
		
		//same function for both cart and product pages	
		var hurdleAmount = 89.00;
		var minHurdle = 50.00;
		var s = $('div.shipMeter');
		if(s) s.html('');		
		var delta = (1.0 * o) - hurdleAmount;
		var somMarkup;
		var imgPath = "//aimg.disneystore.com/content/ds/themes/default11/images/shipometer/";
		
		if (isCart) {
		//define variables for cart version
		var cart = "Cart";
		var barWidth = 121;
		var handTop = 5;
		var divWidth = 357;
		var divHeight = 65;
		var progressPosition = "height: 14px; top: 34px; left: 170px;";
		var msgPosition = "width: 47px; text-align: left; right: 13px; top: 12px;";
		var handPosition = 153;
		$("#innerProgressBar").css("margin-top", "20px");
		$("div.shipMeter").css({ "position":"absolute", "right":"36px","margin-top":"10px" });
		}
		else {
		//define variables for site version
		setTimeout ( "$('#cartBox').slideUp();", 2000 );
		var cart = "";
		var barWidth = 131;
		var handTop = 54;
		var divWidth = 163;
		var divHeight = 137;
		var progressPosition = "height: 14px; top: 83px; left: 16px;";
		var msgPosition = "width: 163px; text-align: center; left: 0; top: 100px;";
		var handPosition = 0;
		}
		
		if(delta >= 0.0) {
		//cart amount is equal to or greater than the hurdle amount, show static End image. Div size set by image.
			somMarkup = "<div style='position: relative; width:"+divWidth+"; height:"+divHeight+";'><a href='javascript:popup(\"//aimg.disneystore.com/content/ds/popups/0120.html\")'><img id='somBox1' src='" + imgPath + "somEnd"  + cart +  ".png' style='width:"+divWidth+"; height:"+divHeight+";'/></a></div>";		
		} 
		
		else if((1.0 * o) < minHurdle) {	
		//cart amount is less than minimum required to show meter, show static Start image. Div size set by image.
			somMarkup = "<div style='position: relative; width:"+divWidth+"; height:"+divHeight+";'><a href='javascript:popup(\"//aimg.disneystore.com/content/ds/popups/0120.html\")'><img id='somBox2'  src='" + imgPath + "somStart"  + cart +  ".png' style='width:"+divWidth+"; height:"+divHeight+";'/></a></div>";
		}
		
		else {
			var barPosition=parseInt(barWidth*(o/hurdleAmount));
			var msg = "Only $" + Math.abs(delta).toFixed(2) + " away!";
			if (barPosition < 25) barPosition=25; //cheat so the hand is visible in the block.
			handPosition += barPosition-30;
			somMarkup = '<div style="z-index:0; position: relative; color: black; font-weight: Bold; margin-bottom: 5px; width:'+divWidth+'; height:'+divHeight+';"><img id="somBox3" src="' + imgPath + 'somBack' + cart + 
			'.png" style=" width:'+divWidth+'; height:'+divHeight+';"/><div style="position: absolute; left: 0; top: 0; z-index:6;"><a href="javascript:popup(\'//aimg.disneystore.com/content/ds/popups/0120.html\')"><img style="width: ' 
			+ divWidth + 'px; height: ' + divHeight + 'px;" src="' + imgPath + 'trans.gif"/></a></div><div style="position: absolute; z-index:2; top: ' 
			+ handTop + 'px; left: ' + handPosition + 'px;"><img id="somHand" src="' + imgPath + 'somHand.png" style="width: 49px; height: 41px;"/></div><div style="background-image:url(' + imgPath + 'progressBar'  
			+ cart +  '.gif); position: absolute; z-index:1; ' + progressPosition + ' width: ' 
			+ barPosition + 'px; overflow: hidden;"></div><div style="position: absolute; z-index:1; ' 
			+ msgPosition + '">' + msg + '</div></div>';
	
		}
		
		if(isCart) {
			var container = cartContainer;
			if(container) {
				container.html(somMarkup);
			}
		} else { 		
			somMarkup = '<div class="shipMeter" style="margin-bottom: 5px;">' + somMarkup + '</div>';
			var container = siteContainer;
			if(container) {
				container.prepend(somMarkup);
			}
		}
		if (document.getElementById("somHand")) fnLoadPng(document.getElementById("somHand"));
		if (document.getElementById("somBox1")) fnLoadPng(document.getElementById("somBox1"));
		if (document.getElementById("somBox2")) fnLoadPng(document.getElementById("somBox2"));
		if (document.getElementById("somBox3")) fnLoadPng(document.getElementById("somBox3"));
	}