function availableOptions(mid) {
	// url,title,w,h,tb,stb,l,mb,sb,rs,x,y
	openWin('/store/compare.php?oid='+mid+'&type=custom','model_comp',600,600,0,0,0,0,1,1);
}
function compareNow(type) {
	var oid="",ct=0;
	$('#productsList :input:checkbox[@checked]').each(function() {
		oid += (oid!=""?",":"")+$(this).val();
		ct++;
	});
	if (ct<2) {
		alert('Please select at least 2 products to compare.');
	} else {
		// open window with comparison
		// url,title,w,h,tb,stb,l,mb,sb,rs,x,y
		openWin("/store/compare.php?type="+type+"&oid="+oid,"ProductComparison",600,600,0,0,0,0,1,1);
	}
}
function getQuote(midOverride) {
	var params = new Object;
	midOverride = parseInt(midOverride); // make sure it stays an int
	
	params['width'] = $("#b_width").val();
	params['height'] = params['length'] = $("#b_height").val();
	params['mcid'] = $("#displayedModelCategoryIds").val(); // grab the list of all model cats that are seen on the page.

	//if (params['width'] > 0 && params['length'] > 0) {
		if (midOverride == 0) {
			// We're getting all models at once, so save off the dimensions to the server.
			// For the most part, we'll ALWAYS get the quotes like this (all at at time).
			// The only time when we get them one at a time is for handling errors.
			$.ajax({
				url:'/request.php?method=savequickquotedimensions',data:params,type:'POST',dataType:'xml',timeout:15000,
				error: function(robj, etype, eobj) {
					//alert("An error occurred while attempting to save the quote dimensions.");
					saveQuickQuoteDimensions_Callback(params);
				},
				success: function(xml) {
					//alert("saved quote dimensions to session.");
					saveQuickQuoteDimensions_Callback(params);
				}
			});
		} else {
			// Asking for a single model, so just request it and be done.
			getQuoteForModel(midOverride, params['width'], params['length']);
		}
	//}
}
function saveQuickQuoteDimensions_Callback(params) {
	$('#displayedModelsList :input[@type=hidden]').each(function() {	
		getQuoteForModel($(this).val(), params['width'], params['length']); // Loop over each model and request the price.
	});
}
function getQuoteForModel(modelId, w, l) {
	var params = new Object;
	params['width'] = w;
	params['length'] = l;
	params['usedefaults'] = 1;
	params['extra'] = params['mid'] = modelId;
	params['vendorid'] = VENDOR_ID;
	params['accountid'] = ACCOUNT_ID;
	params['storeid'] = STORE_ID;
	
	// Put a spinner at each place where the price (heh, or error) will eventually show up.
	$('#qq_price'+modelId).html('<img src="/images/icons/kit_FFFFF_FFFFF.gif" border="0" />'); // <img src="/images/configurator/indicator_FF0000.gif" border="0" />');
	$('#qq_dims'+modelId).html('For a '+w+'" x '+l+'" blind');
	
	// Save the dimensions to the server.
	$.ajax({
		url:'/request.php?method=getprice',data:params,type:'POST',dataType:'xml',timeout:10000,
		error: function(robj, etype, eobj) {
			//var msg = "An error occurred while attempting to retrieve the price quote.<br />";
			//for (x in eobj) msg+= x + ': ' + eobj[x]+'<br>';
			var msg = '(<a href="#" onclick="getQuote('+modelId+');return false;">refresh</a>)';
			$('#qq_price'+modelId).html(msg).attr("className", "store-price-error");
		},
		success: function(xml) {
			var errorCode = $('errorcode',xml).text();
			var modelId = $('extra',xml).text();
			var content = 'N/A';
			var returnPrice = '';
			var showsavings = parseInt($('ShowSavings',xml).text());

			if (errorCode <= 0) {
				retailPrice = $('retailprice',xml).text();
				noPromoPrice = $('nopromoprice',xml).text();
				if (retailPrice.length && retailPrice != "0.00") {
					content = '$'+retailPrice;
					if (showsavings) {
						// Show the non-promo price if it's different.
						if (noPromoPrice.length && noPromoPrice != "0.00" && retailPrice!=noPromoPrice) {
							content += "&nbsp;<del>$"+noPromoPrice+"</del>";
						}
					}
				}
				if (showsavings) {
					// Loop over all of the discounts found and list them out below the free shipping link.
					$(xml).find('PriceDiscounts Retail Discount').each(function() {
						$("#freeshipping"+modelId).after("<li>"+$(this).attr("display")+" (Save $"+$(this).attr("savings")+")</li>");
					});
				}
			}
			$('#qq_price'+modelId).html(content);
		}
	});
}

/*********************************************************************
 * No onMouseOut event if the mouse pointer hovers a child element 
 * *** Please do not remove this header. ***
 * This code is working on my IE7, IE6, FireFox, Opera and Safari
 * 
 * Usage: 
 * <div onMouseOut="fixOnMouseOut(this, event);"> 
 *		So many childs 
 *	</div>
 *
 * @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com		
**/
function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}
function fixOnMouseOut(element, event, JavaScript_code) {
	var current_mouse_target = null;
	if( event.toElement ) {				
		current_mouse_target 			 = event.toElement;
	} else if( event.relatedTarget ) {				
		current_mouse_target 			 = event.relatedTarget;
	}
	if(!is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
		closeSelectMenu('pulldown-per-page');
	}
}


// Force the quotes to be immediately invoked when the DOM is ready.
$(document).ready(function() { 
	getQuote(0); 
});
