function isdefined( variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}

//////////////////////////////////////////////////////////////
////////     panel slide shut	//////////////////////////////
//////////////////////////////////////////////////////////////
function closePanel(e) {
	if (isdefined('currentPanel') && currentPanel != "none"  && currentPanel != $("entry"+e)) {
		new Effect.Shrink(currentPanel, { duration: 0.15, direction:'top-right' } );
		Element.removeClassName(currentPanel.parentNode, "selectedRow");
	}
	currentPanel = "none";				
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////


function moveSummary() {
	Element.undoPositioned("orderSummary");
	orderSummaryY = Position.cumulativeOffset($("orderSummary"))[1]-Position.realOffset($("orderSummary"))[1];
	Element.makePositioned("orderSummary");
	if (orderSummaryY < 0) {
		new Effect.Move(
			"orderSummary", 
			{	y:(0-(orderSummaryY))+50, 
				mode:"absolute", 
				duration:0.25
			} 
		)
	}
}

Event.observe(window,'scroll',moveSummary);



//////////////////////////////////////////////////////////////
/////////  panel slide open           ////////////////////////
//////////////////////////////////////////////////////////////
function orderEntryPanel(e,i) {
	var coords = Position.positionedOffset(i);
	var moveToX = (coords[0]+(Element.getDimensions(i).width))-362;
	var moveToY = coords[1]-44;
	
	closePanel(e);
		
	new Effect.Move(
		$("entry"+e),
		{	x:moveToX, 
			y:moveToY,
			mode:"absolute", 
			duration:0,
			afterFinish: function() {
				new Effect.Grow($("entry"+e), { duration: 0.25, direction:'top-right' } );
				Element.addClassName($("entry"+e).parentNode, "selectedRow");
				//$("form"+e).quantity.focus();
			}
		}
	)
	currentPanel = $("entry"+e);
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////



function highlightSummary() {
	opt = {duration:1.5, startcolor:'#919734', endcolor:'#E7AD00' };
	new Effect.Highlight("orderSummary",opt);
}



//////////////////////////////////////////////////////////////
/////////  add to cart                ////////////////////////
//////////////////////////////////////////////////////////////
function addToCart(f,i,skuID,shapeID,isUpdatePending,mySkus) {
// f is the form that was submitted

	// defaults for arguments...
	isUpdatePending = isUpdatePending || false; 
	mySkus = mySkus || "";
	
	// check for multiple
	if((f.quantity.value%i)) {
		Element.addClassName("minOrder"+skuID,"error");
		if (isUpdatePending) {
			f.quantity.focus();
			return false;
		}
		f.quantity.value=(Math.round(f.quantity.value/i)*i);
		if (f.quantity.value==0) f.quantity.value = i;
		return false;
	}
	
	f.quantity.value = Math.abs(f.quantity.value);

	// display progress indicator in order summary
	if (!isUpdatePending) {
		if ($('orderSummaryContents')) {
			$('orderSummaryContents').innerHTML='<img src="image/loading_animation.gif" style="margin:21px 50px;" />';
		}

		if(f.quantity.value!=0) {
		// add/update item
			$('icon'+skuID).src = '_ccLib/image/shapes/CHEK-'+shapeID+'.png';
			$('addButton'+skuID).src="image/button-orderentry-updatequant.gif";
		} else {
		// removing item				
			$('icon'+skuID).src = '_ccLib/image/shapes/ICON-'+shapeID+'.png';
			$('addButton'+skuID).src="image/button-orderentry-addtoorder.gif";
		}
	} else {
		// show indicator next to line item
		Element.show($('indicator'+skuID));
	}


	Element.removeClassName("minOrder"+skuID,"error");

	closePanel(0);
	// set up the Ajax.Request options
	var opt = {
		method: 'post',
		postBody: Form.serialize(f),
		onSuccess: function() {
			if(isUpdatePending) {
				updatePendingOrderTable(shapeID,mySkus);
				Element.hide($('indicator'+skuID));
			} else {
				new Ajax.Updater('orderSummary','index.cfm/fuseaction/order.updateOrderSummary' );
				highlightSummary();
			}
		},
		// we'll use status code 900 to catch CF errors
		// in this case we post the actual form like we normally do (synchronously) so we can debug whatever the issue is.
		on900: function(t) {
			f.submit();
		},
		onFailure: function() {
			f.submit();
		}
	}
	
	new Ajax.Request('index.cfm', opt);
	
	return false;
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////



function updatePendingOrderTable(shapeID,mySkus) {
	
	new Ajax.Updater('orderSubtotal','index.cfm?fuseaction=order.updateOrderSubtotal');
	new Ajax.Request('index.cfm?fuseaction=order.unitprice&shapeID='+shapeID, { 
		
		onSuccess: function(t) {
			var newUnitPrice = t.responseText;
			var shapeTotalQuant = 0;
			var	nf = new NumberFormat(0);
			nf.setCurrency(true);
			mySkus.each(
				function(ii) {
					if (newUnitPrice != 0) {
						shapeTotalQuant=shapeTotalQuant+parseInt($('form'+ii).quantity.value);
						nf.setNumber(newUnitPrice*1000);
						$('unitprice'+ii).innerHTML = nf.toFormatted();
						nf.setNumber(newUnitPrice*$('form'+ii).quantity.value);
						$('extprice'+ii).innerHTML = nf.toFormatted();
					}
				}
			);

			nf.setNumber(newUnitPrice*1000);
			$('shapeUnitPrice'+shapeID).innerHTML = nf.toFormatted();
			nf.setNumber(newUnitPrice*shapeTotalQuant);
			$('shapeExtPrice'+shapeID).innerHTML = nf.toFormatted();
			
			nf.setCurrency(false);
			nf.setPlaces(0);
			nf.setNumber(shapeTotalQuant);
			$('shapeTotalQuant'+shapeID).innerHTML = nf.toFormatted();
		}
	
	});
	

}




//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
var HoverPreviews = {
	
	bind:function() {
		jQuery('.shapeIcon').bt({
		  width: '130px', 
		  fill: '#F7F7F7', 
		  strokeStyle: '#B7B7B7', 
		  spikeLength: 10, 
		  spikeGirth: 10, 
		  positions: ['top','bottom','right'],
		  ajaxPath:["jQuery(this).attr('data-thumbnail')"], 
		  ajaxCache:false,
		  offsetParent:jQuery(this).offsetParent(),
		  padding: 15, 
		  cornerRadius: 0, 
		  cssStyles: {
		    fontFamily: '"lucida grande",tahoma,verdana,arial,sans-serif', 
		    fontSize: '11px',
		    textAlign:'center',
		    minHeight:'100px',
		    minWidth:'100px'
		  }
		}).click(function(){ jQuery(this).btOff(); });
	}
	
}

jQuery(document).ready(function(){
	HoverPreviews.bind();
});

