$(document).ready(function () {
	
	if(getUrlVars()['SortBy'] != null){
		var sortBy = getUrlVars()['SortBy'].replace(':', '');
	} else {
		sortBy = "PriceAsc";
	}
	
	$('#' + sortBy).parent().addClass('sortHilight');
		
	
	$('.pageSize select[name=pageSize]').val($('form[name=frmSearch] input[name=PageSize]').val());
	
	$('.pageSize select[name=pageSize]').change(function(){
		pageSize = $('.pageSize select[name=pageSize] option:selected').val();
		//currentURL = $(location).attr('href');
		currentURL = location.href;
		pageSizePos = currentURL.search('&PageSize=');
		if(pageSizePos == -1){
			newURL = currentURL + "?Page=1&PageSize=" + pageSize + "&ManufacturerId=-1";
		} else {
			firstPart = currentURL.slice(0,pageSizePos);
			tempSecondPart = currentURL.slice(pageSizePos+10);
			restPos = tempSecondPart.search('&');
			secondPart = tempSecondPart.slice(restPos);
			newURL = firstPart + "&PageSize=" + pageSize + secondPart;
		}
		setCookie('pageSize', pageSize, 50);
		window.location = newURL;
	});
	
	$('.linkMe').click(function(){
		var pageURL = window.location.href;
		setCookie('listPageReturnURL', pageURL, 2);
		window.location = $(this).attr('href');
	});
	
	$('.financeTbl td, .financeTbl th').click(function(){
		if($(this).children().attr('nodeName') != "IMG"){
			var pageURL = window.location.href;
			setCookie('listPageReturnURL', pageURL, 2);
			setCookie('openTab', 5, 1);
			window.location = $(this).parent().parent().parent().attr('href');
		}
	});
	
	$('.viewVehicleDetails').click(function(){
		var pageURL = window.location.href;
		setCookie('listPageReturnURL', pageURL, 2);
	});
	
	$('.vehicleImage').error(function(){
 		$(this).attr('src', '/images/no-image.png');
	});
	
	$("#myFinance select[name='financeTerm']").val($("#incomingFigs input[name='financeTerm']").val());
	$("#myFinance input[name='Deposit']").val($("#incomingFigs input[name='financeDeposit']").val());
	$("#myFinance select[name='AnnualMileage']").val($("#incomingFigs input[name='financeAnnualMileage']").val());
	
	$('#financeUpdate').click(function(){
		
		term = $("select[name='financeTerm']").val();
		deposit = $("input[name='Deposit']").val();
		mileage = $("select[name='AnnualMileage']").val();
		
		setCookie('termCookie', term, 5);
		setCookie('depositCookie', deposit, 5);
		setCookie('mileageCookie', mileage, 5);
		
		window.location = $("#myFinance input[name='currentURL']").val();
		
	});

});
