
$(document).ready(function(){
	$("input:reset").button();
	
	//initial setup
	$(".select").each(function(){
		$(this).load(searchpage + "?grab=" + $(this).attr("name") + "&" + data, function(responseText){
			if($.trim(responseText)){
				$(this).show();
			}else{
				$(this).hide();
			}
		});
	});
	
	
	$.history.init(function(hash) {
		if(hash == "") {
            ajaxrequest(data, pluginpage);
        } else {
            ajaxrequest(hash, pluginpage);
        }
	});
	
	//$(".test").text(pluginpage + "?" + data);
	
	//$("#results").load(searchpage + "?grab=total&" + data);
	
	
	
	//on change
	$(".input").change(function(){
		
		mytitle = $("#vtype option:selected").text();
		//$("title").html( mytitle + " | " + dname );	//Bugs out in IE
		document.title = mytitle + " | " + dname;
		
		if($(this).attr("name")=="type"){
			if($(this).val()=="SEARCHNEW"){
				$("#miles").hide();
			}else{
				$("#miles").show();
			}
			$(".select").each(function(){
				$(this).val("");
				$(this).attr('selectedIndex', '-1').children("option:selected").removeAttr("selected");
				$(this).prepend("<option class='optload' value='' selected='selected'>Loading...</option>");
			});
		}
		
		if($(this).attr("name")=="make" || $(this).attr("name")=="category"){
			$(".select").each(function(){
				if($(this).attr("name")!="make" && $(this).attr("name")!="category"){
					$(this).val("");
					$(this).attr('selectedIndex', '-1').children("option:selected").removeAttr("selected");
					$(this).prepend("<option class='optload' value='' selected='selected'>Loading...</option>");
				}
			});
		}
		
		if($(this).attr("name")=="model"){
			$(".select").each(function(){
				if($(this).attr("name")=="bdate" || $(this).attr("name")=="edate" || $(this).attr("name")=="eprice" || $(this).attr("name")=="emileage"){
					$(this).val("");
					$(this).attr('selectedIndex', '-1').children("option:selected").removeAttr("selected");
					$(this).prepend("<option class='optload' value='' selected='selected'>Loading...</option>");
				}
			});
		}
		
		var datastr = "dealerid=" + dealerid;
		$("#search option:selected").each(function(){
			datastr += "&" + $(this).parent().attr("name") + "=" + escape($(this).val());
		});
		$("#search input:checked").each(function(){
			datastr += "&" + $(this).attr("name") + "=" + $(this).val();
		});
		if($("#search input.stockno").val()!=""){
			datastr += "&" + $("#search input.stockno").attr("name") + "+" + jQuery.trim($("#search input.stockno").val());
		}
		
		//$(".test").text(datastr);
		ajaxrequest(datastr, pluginpage);
		
		$(".select").each(function(){
			$(this).load(searchpage + "?grab=" + $(this).attr("name") + "&" + datastr, function(responseText){
				$(".optload").remove();
				if($.trim(responseText)){
					$(this).show();
				}else{
					$(this).hide();
				}
			});
		});
		
		//$("#results").load(searchpage + "?grab=total&" + datastr);
	});
	
	
	//reset
	$("input:reset").click(function(){
		$(".select").each(function(){
			$(this).load(searchpage + "?grab=" + $(this).attr("name") + "&" + data, function(responseText){
				if($.trim(responseText)){
					$(this).show();
				}else{
					$(this).hide();
				}
			});
		});
		$("#vtype option.current").attr('selected','selected');
		
		ajaxrequest(data, pluginpage);
		
		mytitle = $("#vtype option:selected").text();
		document.title = mytitle + " | " + dname;
		
		/*
		$(".select").each(function(){
			$(this).load(searchpage + "?grab=" + $(this).attr("name"));
			$(this).show();
		});
		$("#vtype option[value='']").attr('selected','selected');
		
		ajaxrequest("dealerid=" + dealerid, pluginpage);
		
		mytitle = $("#vtype option:selected").text();
		document.title = mytitle + " | " + dname;
		*/
		return false;
	});
	
	
	//make links ajax
	$(".pagedisplay a, a.viewdetails, #backbtn").live('click', function(){
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
			event.returnValue=false;	//IE not returning false fix
		}
		var splitResult = $(this).attr("href").split("?");	// IE 7 is a bitch and throws the domain name before relative link
		var mylink = splitResult[1];
		//$(".test").text(pluginpage + "?" + mylink);
		ajaxrequest(mylink, pluginpage);
		$(window).scrollTop(0);
		if($(this).attr("rel")){
			document.title = $(this).attr("rel") + " | " + dname;
		}
		return false;
	});
	
	$(".sizelist input").live('click', function(){
		var mylink = "sizelist=" + $(this).val() + $("#passvars").val();
		//$(".test").text(pluginpage + "?" + mylink);
		ajaxrequest(mylink, pluginpage);
	});
});

function ajaxrequest(datastr,pluginpage){
	//$(".test").text(datastr);
	
	var url = datastr;
	url = url.replace(/^.*#/, '');

	$(".content").hide();
	$.ajax({
		method: "get",
		url: pluginpage,
		data: datastr,
		beforeSend: function(){ $("#loading").show(); },
		complete: function(){ $("#loading").hide(); },
		success: function(html){
			$(".content").fadeIn('fast');
			$(".content").html(html);
			$.history.load(url);
		}
	});
}

