﻿function initPage() {
  $j.blockUI.defaults.overlayCSS.width = '609px';
  $j.blockUI.defaults.overlayCSS.margin = '0 0 0 1px';
  $j.blockUI.defaults.elementMessageCSS.border = '1px solid #6596BE';
  
  onSortClick();
	onPageLinkClick();
	BindMakeSelect();
}

/* Ajax methods */
function ajaxReplace(href, containerId, isAjaxParamName, onsuccess){
	isAjaxParamName = isAjaxParamName ? isAjaxParamName : "isajaxrequest";
	/* Add parameter to href */
	var indexOf = href.indexOf("?");
	if(indexOf > -1){
		href = href + "&";
	}
	else{
		href = href + "?";
	}
	href = href + isAjaxParamName + "=true",
	
	$j.ajax({
		url: href,
		dataType: "html",
		success: function(response){
			$j(containerId).html(response);
			if (typeof onsuccess == "function"){
				onsuccess();
			}
		}
	});
}

function onPageLinkClick(){

	// Top Pager
	$j("div#ctl00_MainContentPlaceHolder_hlpCarResults div.page-links a").click(function(){
	  $j("div#SearchResults").block("<div id=\"MiniAjaxLoader\"><div class=\"content\"><p><img src=\"/_Client/Images/Global/ajax-loader.gif\" alt=\"loading...\" title=\"loading...\" /></p><p>One moment please</p></div>");
		ajaxReplace(this.href, "#SearchResults", "isajaxrequest", initPage);
		return false;
    $j("div#SearchResults").unblock();
	});
	
	// Bottom Pager
	$j("div#ctl00_MainContentPlaceHolder_hlpCarResultsBottom div.page-links a").click(function(){
  	$j("div#SearchResults").block("<div id=\"MiniAjaxLoader\"><div class=\"content\"><p><img src=\"/_Client/Images/Global/ajax-loader.gif\" alt=\"loading...\" title=\"loading...\" /></p><p>One moment please</p></div>");
		ajaxReplace(this.href, "#SearchResults", "isajaxrequest", initPage);
		return false;
		$j("div#SearchResults").unblock();
	});

}

function onSortClick(){

	$j("a#ctl00_MainContentPlaceHolder_hypSort").click(function(){		   
	  $j("div#SearchResults").block("<div id=\"MiniAjaxLoader\"><div class=\"content\"><p><img src=\"/_Client/Images/Global/ajax-loader.gif\" alt=\"loading...\" title=\"loading...\" /></p><p>One moment please</p></div>");
		ajaxReplace(this.href, "#SearchResults", "isajaxrequest", initPage);
		return false;
		$j("div#SearchResults").unblock();
	});
	
}

function BindMakeSelect()
{
  $j("select.make").change(function ()
	{
    if($j(this).val() != '')
    {
      $j("div.model-container").html("<div id=\"AjaxLoader\">Please wait, loading models...</div>");
		  //perform the ajax request
			ajaxAide.raisePageCallBack(
			  "aspnetForm", 
				"RenderModels", 
				"selectedmakeid="+ $j(this).val(), 
				function(response){
				  //write our result to the containg div.
					$j("div.model-container").html(response.value);
			}); 
		}
	}); 
}

function Search()
{
	$j("div#SearchResults").block("<div id=\"MiniAjaxLoader\"><div class=\"content\"><p><img src=\"/_Client/Images/Global/ajax-loader.gif\" alt=\"loading...\" title=\"loading...\" /></p><p>One moment please</p></div>");
  
  ajaxAide.raisePageCallBack(
    "aspnetForm", 
		"RenderSearch",
		"selectedmakeid=" + $j("select.make").val() + "&selectedmodelid=" + $j("select.model").val() + "&minprice=" + $j("select.minprice").val() + "&maxprice=" + $j("select.maxprice").val(), 
		function(response){
		  //write our result to the containg div.
			//$j("div.model-container").html(response.value);
			// Replace the search grid
			ajaxReplace(document.location.href, "#SearchResults", "isajaxrequest", initPage);
			return false;
			$j("div#SearchResults").unblock();
	  }); 
}

function UrlSearch()
{
  // Get the selected manufacturer name and code
  var ddlMake = document.getElementById("ctl00_CarFilter_ddlManufacturer"); 
  var strMake = ddlMake.options[ddlMake.selectedIndex].text;
  var makeCode = ddlMake.options[ddlMake.selectedIndex].value;

  // Get the selected range name and code
  var ddlRange = document.getElementById("ctl00_CarFilter_ddlModel"); 
  var strRange = ddlRange.options[ddlRange.selectedIndex].text;
  var rangeCode = ddlRange.options[ddlRange.selectedIndex].value;
  
  if (rangeCode > 0)
  {
    window.location.href = "http://" + document.domain + "/used-cars/range/" + rangeCode + "/used-" + strMake + "-" + strRange + ".htm";
  }
  else if (makeCode > 0)
  {
    window.location.href = "http://" + document.domain + "/used-cars/make/" + makeCode + "/used-" + strMake + ".htm";
  }
  else
  {
    window.location.href = "http://" + document.domain + "/used-cars/stock.htm";
  } 
}

function addQueryStringParameter(href, parameterName, parameterValue){	
	var indexOf = href.indexOf("?");
	if(indexOf > -1){
		href = href + "&";
	}
	else{
		href = href + "?";
	}
	href = href + parameterName + "=" + parameterValue;
	return href;
}

function ShowMore()
{
  $j("div.show-more").click(function () {
    var height = {}; height['max-height'] = '3000px';
    $j("ul#AvailableCars").css(height);
    $j(this).html("<a id=\"lnkShowMore\" onclick=\"javascript:ShowLess(); return false;\" href=\"#\">Show Less...</a>");
  });   
}

function ShowLess()
{
  $j("div.show-more").click(function () {
    var height = {}; height['max-height'] = '73px';
    $j("ul#AvailableCars").css(height);
    $j(this).html("<a id=\"lnkShowMore\" onclick=\"javascript:ShowMore(); return false;\" href=\"#\">Show More...</a>");
  });   
}