var CostingBox = {
	addCostLine: function(upsellType,upsellTitle,upsellPrice,total,display_free)
	{
		var item = '';
		
		if (upsellPrice instanceof Date || (/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/).test(upsellPrice))
		{
			if (upsellPrice instanceof Date === false)
			{
				var date_parts = upsellPrice.split('-');
				upsellPrice = new Date(date_parts[0], parseInt(date_parts[1], 10) - 1, date_parts[2]);
				delete date_parts;
			}
			
			item = dateFormat(upsellPrice, dateFormat.masks.mediumDate);
		}
		else if (isNaN(parseFloat(upsellPrice)))
		{
			item = upsellPrice;
		}
		else if (display_free === false)
		{
			// This Currently Returns Either a Number with a Dollar Sign
			item = '$ ' + upsellPrice;
		}
		else
		{
			// This Currently Returns Either a Number with a Dollar Sign or FREE
			item = KAANGO.util.monetize(upsellPrice);
		}

		if (upsellTitle != null)
		{
			if (total === true)
			{
				list = $.create('ul', {'class': 'costing-box-line-item-total'})
							.append($.create('li', {'class': 'costing-box-price-total'}, item))
							.append($.create('li', {'class': 'costing-box-item-total'}, upsellTitle + ':'));
			}
			else
			{
				list = $.create('ul', {'class': 'costing-box-line-item'})
							.append($.create('li', {'class': 'costing-box-price', 'id': upsellTitle.replace(/[\W^-]+/, "")}, item))
							.append($.create('li', {'class': 'costing-box-item'}, upsellTitle + ':'));
			}
			
			$('#' + upsellType).append($.create('li').append(list));
		}
	},

	addCostSection: function (id)
	{
		$('#costingBox').append($.create('li').append($.create('ul',{'id':id})));
	},
	
	highlight: function ()
	{
		$("#pricing_box_full .summary_content_box").effect("highlight", {}, 750);
	},

	buildFromOrder: function(order)
	{
		this.startAlteration();
		this.clearAll();
		
		for (var order_key in order)
		{
			var order_section = order[order_key];
			
			this.addCostSection(order_key);
			
			for (var sec_key in order_section)
			{
				var total_line = (order_section[sec_key]['bold'] == 1) ? true : false, 
					d_free_line = (order_section[sec_key]['d_free'] == 1) ? true : false;
				
				this.addCostLine(order_key, order_section[sec_key]['name'], order_section[sec_key]['value'], total_line, d_free_line);
			}
		}
		
		delete order_section;
		
		this.endAlteration();
		$('#sidebarHolder').height($('#sidebar').offset().top+$('#sidebar').height()-$('#main-body').offset().top);
	},

	clearAll: function()
	{
		/* clear costingBox for rebuilding. */
		$('#costingBox').empty();
	},

	startAlteration: function()
	{
		$('#costingBox').hide();
		$('#costingUpdate').show();
	},

	endAlteration: function()
	{
		$('#costingBox').show();
		$('#costingUpdate').hide();
	},

	initAnimation: function() {
		var m = 1,
			sidebar = $('#sidebar'),
			sidebarHolder = $.create('div',{'id':'sidebarHolder','style':'height:'+sidebar.height()+'px;visibility:hidden'}).insertBefore(sidebar),
			mainBody = $("#main-body"),
			timeOut;
		function setToFixed(mainOffsetTop,scrollTop,h1,h2) {
			if ($.browser.msie && $.browser.version <= 6 )
			{
				sidebarHolder.height(h2);
				sidebar.css({'position':'absolute','bottom':'','margin-left':'607px'});
				sidebar[0].style.setExpression('top','((document.documentElement || document.body).scrollTop-'+mainOffsetTop+')+"px"');
			}
			else if ($.browser.mozilla)
			{
				var top = scrollTop+h1 > mainOffsetTop + h2 ? h2 - h1 : scrollTop-mainOffsetTop;
				top = top < 0 ? mainOffsetTop : top;
				sidebar.css({'position':'absolute','top':(m==3?mainOffsetTop+h2-h1-mainOffsetTop:'0px'),'bottom':'','margin-left':'607px'})
					.stop()
					.animate({'top':top},{queue:true, duration:500, easing:'swing', complete:function() {
						sidebarHolder.height(mainBody.height());
					}});
			}
			else
			{
				sidebarHolder.height(h2);
				sidebar.css({'position':'fixed','top':'0px','bottom':'','margin-left':'607px'});
			}
			m = 2;
		}
		function setToStatic() {
			var h1 = sidebar.height(),
				h2 = $('#main-body').height();
			sidebarHolder.height(h1>h2?h1:h2);
			sidebar.css({'position':'absolute','top':'0px','bottom':'','margin-left':'607px'});
			if ($.browser.msie && $.browser.version <= 6 )
			{
				sidebar[0].style.removeExpression('top');
			}
			m = 1;
		}
		
		function checkMozilla(css, fn)
		{
			if ($.browser.mozilla)
			{
				clearTimeout(timeOut);
				timeOut = setTimeout(function() {
					sidebar.stop()
						.animate(css,{duration:500, easing:'swing', complete:function() {
							fn();
						}});
					timeOut = undefined; 
				},300);
			}
			else
			{
				fn();
			}
		}
		if ($.browser.msie && $.browser.version <= 6 )
		{
			$('body').css({'background-image':'url("/resources/img/spacer.gif")','background-repeat':'no-repeat','background-attachment':'fixed'});
		}
		var animate = function(){
			var mainOffsetTop = mainBody.offset().top;
			var scrollTop = $(window).scrollTop();
			var h1 = sidebar.height();
			var h2 = mainBody.height();
			if (h1 < h2)
			{
				switch(m)
				{
					case 2: // middle
						if (mainOffsetTop > scrollTop-2)
						{
							checkMozilla({'top':'0px'},setToStatic);
						}
						else
						{
							if (scrollTop+h1 > mainOffsetTop + h2 && typeof timeOut == 'undefined')
							{
								function setToBottom() {
									sidebarHolder.height(mainBody.height());
									sidebar.css({'position':'absolute','top':'','bottom':'10px','margin-left':'607px'});
									if ($.browser.msie && $.browser.version <= 6 )
									{
										sidebar.css({'left':'612px'});
										sidebar[0].style.removeExpression('top');
									}
									m = 3;
								}
								checkMozilla({'top':h2-h1},setToBottom);
							}
							else if ($.browser.mozilla)
							{
								clearTimeout(timeOut);
								timeOut = setTimeout(function() {
									var top = scrollTop+h1 > mainOffsetTop + h2 ? h2 - h1 : scrollTop-mainOffsetTop;
									if (top != 0)
									{
										sidebar.stop()
											.animate({'top':top},{queue:true, duration:500, easing:'swing', complete:function() {
												sidebarHolder.height(h2);
											}});
										timeOut = undefined;
									}
								},300);
							}
						}
						break;
					case 1: // top
						if (mainOffsetTop < scrollTop)
						{
							setToFixed(mainOffsetTop,scrollTop,h1,h2);
						}
						break;
					case 3: // bottom
						if (mainOffsetTop + h2 - h1 > scrollTop)
						{
							setToFixed(mainOffsetTop,scrollTop,h1,h2);
						}
						break;
				}
			}
			else
			{
				checkMozilla({'top':'0px'},setToStatic);
			}
		};
		setToStatic();
		$(window).bind('scroll',animate);
		
		// Necessary to prevent animate() being called due to JQuery scroll
		var animateTimeout;
		return function() {clearTimeout(animateTimeout); animateTimeout = undefined; animateTimeout = setTimeout(animate,300);}
	}
};