var errorTag = "";
var errorText = "";
var saveListArray = new Array();
var noOfCookies = 0;
var recentListArray = new Array();
var recentListNoOfCookies = 0;


function saveList(vehId, thumb, manu, model, price){ // SAVE LIST 
	if (navigator.cookieEnabled == 0) {
  		alert("You need to enable cookies to use this option");
	} else {
		if (dupListCheck(vehId, 'saveList')){
			alert("This vehicle is already on your showroom list");
		} else {
			for(i=0; i<=7; i++){
				tempVal = getCookie('saveList' + i);
				if (tempVal != null){
					saveListArray[i] = getCookie('saveList' + i);
				}
			}
			noOfCookies = saveListArray.length;
			if (noOfCookies > 7){
				alert("You have reached the maximum.\n You must remove an item before adding a new one.");
			} else {
				if (noOfCookies == 0){
					//document.getElementById("myShowroomListContainer").style.display = "";
				}
				tempVal = vehId + "~" + thumb + "~" + manu + "~" + model + "~" + price;
				tempName = "saveList" + noOfCookies;
				setCookie(tempName, tempVal, 5);
				//alert(manu + "<br />" + model + "<br />has been added you your save list");
				saveListRefresh(noOfCookies);
			}
		}
	}
}

function recentList(vehId, thumb, manu, model, price){ // RECENT LIST
	var cookieCount = 0;
	if (dupListCheck(vehId, 'recentList')){
		// Vehicle already saved on recent list
		// Do nothing
	} else {
		for(i=0; i<=7; i++){
			tempVal = getCookie('recentList' + i);
			if (tempVal != null){
				recentListArray[i] = getCookie('recentList' + i);
				cookieCount = cookieCount +1;
			}
		}
		tempVal = vehId + "~" + thumb + "~" + manu + "~" + model + "~" + formatCurrency(price);
		setCookie('recentList0', tempVal, 5);
		if(cookieCount > 0){
			for(i=0; i<=(cookieCount -1) ; i++){
				setCookie('recentList' + (i + 1), recentListArray[i], 5)
			}
		}
		recentListNoOfCookies = recentListArray.length;
		recentListRefresh(recentListNoOfCookies);
	}
}


function saveListClear(){ // Clear all items off the Interest / Showroom list
	var answer = confirm ("Are you sure you wish to remove all items from your save list?")
	if (answer){
		for(i=0; i<=7; i++){
			deleteCookie('saveList' + i);
		}
		saveListArray.length = 0;
		$('#theSaveList').fadeOut('slow', function(){
			for(i=0; i<=8; i++){
				$('#srCell'+i).empty();
			}
			$('#theSaveList').removeAttr('style');
		});
		
		$('#clearShowroom').html('');
		$('#comparisonLink').html('');
		$('#showroomText').html("0 vehicles");
		$('#saveTotal').text("(0)");
		$('#saveTotal').attr("title", "You have 0 vehicles saved on you showroom list");
		
		savedAllRestore();
		refreshSaveRecentBody();
	}
}



function saveListRemove(x){ // Vehicle removal function
	deleteCookie('saveList' + x);
	for(i=0; i<=7; i++){
		tempVal = getCookie('saveList' + i);
		if (tempVal != null){
			saveListArray[i] = tempVal;
		} else {
			saveListArray[i] = null;
		}
	}
	count = 0;
	for(i=0; i<=7; i++){
		deleteCookie('saveList' + i);
	}
	for(i=0; i<=7; i++){
		tempVal = saveListArray[i];
		if (tempVal != null){
			setCookie("saveList" + count, tempVal, 5);
			count = count + 1;
		}
	}
	if(count == 0){
		saveListArray.length = 0;
		for(i=0; i<=8; i++){
			$('#srCell'+i).html('');
		}
		$('#clearShowroom').html('');
		$('#showroomText').html("0 vehicles");
		$('#saveTotal').text("(0)");
		$('#saveTotal').attr("title", "You have 0 vehicles saved on you showroom list");
		$('#comparisonLink').html('');
		scanForSavedRestore();
		refreshSaveRecentBody();
	} else {
		saveListArray.length = count;
		saveListRefresh(count-1, 'noFade');
	}
}

function saveListCodeMeUp(x){ // SAVE LIST CODE ME UP
	var tempCookie = getCookie('saveList' + x);
	var tempArray = tempCookie.split("~");
	if (tempArray[4] == "0"){
		tempArray[4] = "Price unavailable"
	}
	returnCode = "<div class=\"listWrapper\"><div href=\"/vehicle-details/?VehicleId=" + tempArray[0] + "\" class=\"saveTableItemOverlay myPointer\" data-info=\"" + tempArray[2] + "<br />" + tempArray[3] + "<br />" + formatCurrency(tempArray[4]) + "<br />Click for full details\"></div>";
	returnCode = returnCode + "<img src=\"" + tempArray[1] + "\" border=\"0\" alt=\"" + tempArray[2] + " " + tempArray[3] + "\" class=\"listImage\" />";
	returnCode = returnCode + "<div class=\"checkbox\" id=\"\" title=\"Add this vehicle to your comparison selection\"><input type=\"checkbox\" id=\"\" name=\"listImages[]\" value=\"" + tempArray[0] + "\"></div>"
	returnCode = returnCode + "<div class=\"saveItemDelete\"><img src=\"/assets/images/mini-close.gif\" title=\"Delete this vehicle from your save list\" class=\"myPointer\" /></div>";
	returnCode = returnCode + "<div class=\"saveSumm\">" + tempArray[2] + "<br />" + tempArray[3] + "<br />" + formatCurrency(tempArray[4]) +"</div></div>";
	return returnCode;
}

function recentListCodeMeUp(x){ // RECENT LIST CODE ME UP
	var tempCookie = getCookie('recentList' + x);
	var tempArray = tempCookie.split("~");
	if (tempArray[4] == "0"){
		tempArray[4] = "Price unavailable"
	}
	returnCode = "<div class=\"listWrapper\"><div href=\"/vehicle-details/?VehicleId=" + tempArray[0] + "\" class=\"recentTableItemOverlay myPointer\" data-info=\"" + tempArray[2] + "<br />" + tempArray[3] + "<br />" + tempArray[4] + "<br />Click for full details\"></div>";
	returnCode = returnCode + "<img src=\"" + tempArray[1] + "\" border=\"0\" alt=\"" + tempArray[2] + " " + tempArray[3] + "\" class=\"recentListImage\" />";
	returnCode = returnCode + "<div class=\"saveSumm\">" + tempArray[2] + "<br />" + tempArray[3] + "<br />" + tempArray[4] +"</div></div>";
	return returnCode;
}

function saveListRefresh(x, y){ // SAVE LIST REFRESH
	for(i=0; i<=8; i++){
		$('#srCell'+i).html("");
	}
	for(i=0; i<=x; i++){
		if (i == x && y == null){
			$('#srCell'+(i+1)).css('display', 'none');
			$('#srCell'+(i+1)).html(saveListCodeMeUp(i)).fadeIn('slow');
		} else {
			$('#srCell'+(i+1)).html(saveListCodeMeUp(i));
		}
	}
	
	$('#saveTotal').text("(" + (x+1) + ")");
	tempWord = "vehicles";
	if(x == 0){
		tempWord = "vehicle";
	}
	$('#saveTotal').attr("title", "You have " + (x+1) + " " + tempWord + " saved on you showroom list");
	
	$('#clearShowroom').html('<a href=\"javascript:saveListClear();\">Clear All Items</a>');
	$('#comparisonLink').html('Use the tick boxes next to the vehicle images to select up to 3 vehicles you would like to compare side by side')
	tempWord = "vehicles";
	if ((x+1) == 1){
		tempWord = "vehicle";
	}
	$('#showroomText').html((x+1) + " " + tempWord);
	$(".checkbox").dgStyle();
	compareCheckboxes();
	deleteLinkApply();
	saveListLinkConstruct();
	scanForSaved();
	scanForSavedRestore();
	refreshSaveRecentBody();
}

function recentListRefresh(x, y){ // RECENT LIST REFRESH
	for(i=0; i<=7; i++){
		$('#rCell'+i).html("");
	}
	for(i=0; i<=x; i++){
		if (i == x && y == null){
			$('#rCell'+(i+1)).css('display', 'none');
			$('#rCell'+(i+1)).html(recentListCodeMeUp(i)).fadeIn('slow');
		} else {
			$('#rCell'+(i+1)).html(recentListCodeMeUp(i));
		}
	}
	recentListLinkConstruct();
}

function compareURLConstruct(){
	var val = [];
	$('.checkbox input:checked').each(function(i){
		val[i] = $(this).val();
	});
	if (val.length == 1){
		var returnVal = "?vehicleId0=" + val[0]
	}
	if (val.length == 2){
		var returnVal = "?vehicleId0=" + val[0] + "&vehicleId1=" + val[1]
	}
	if (val.length == 3){
		var returnVal = "?vehicleId0=" + val[0] + "&vehicleId1=" + val[1] + "&vehicleId2=" + val[2];
	}
	
	return returnVal;
}

function deleteLinkApply(){
	var val = [];
	$('.saveItemDelete').each(function(i){
		$(this).click(function(){
			saveListRemove(i);
		});
	});
}

function compareCheckboxes(){
	var compareURL = "";
	$('.saveListContainer .checkbox').click(function(){
		var numberChecked = $('.saveListContainer .checkbox input:checked').length;
		if(numberChecked == 0){
			$('#comparisonLink').fadeOut('fast', function(){
				$('#comparisonLink').html('Use the tick boxes next to the vehicle images to select up to 3 vehicles you would like to compare side by side');
				$('#comparisonLink').fadeIn('fast', function(){refreshSaveRecentBody()});
			});
		}
		if(numberChecked == 1){
			$('#comparisonLink').fadeOut('fast', function(){
				$('#comparisonLink').html('You will need to add at least one more vehicle to be able to compare side by side');
				$('#comparisonLink').fadeIn('fast', function(){refreshSaveRecentBody()});
			});
			$('#comparisonLink').html('You will need to add at least one more vehicle to be able to compare side by side');
		}
		if(numberChecked == 2){
			$('#comparisonLink').fadeOut('fast', function(){
				$('#comparisonLink').html('<a href=\"/vehicle-compare/' + compareURLConstruct() + '\" >Compare</a>');
				comparisonLinkWireUp();
				$('#comparisonLink').fadeIn('fast', function(){refreshSaveRecentBody()});
			});
			$('.saveListContainer .checkbox input:not(:checked)').parent().show();
		}
		if(numberChecked == 3){
			$('#comparisonLink').fadeOut('fast', function(){
				$('#comparisonLink').html('You have now reached the maximum vehicles you can compare at one time.<br /><a href=\"/vehicle-compare/' + compareURLConstruct() + '\" >Compare</a>');
				comparisonLinkWireUp();
				$('#comparisonLink').fadeIn('fast', function(){refreshSaveRecentBody()});
			});
			$('.saveListContainer .checkbox input:not(:checked)').parent().hide();
		}
	});
}

function saveListLinkConstruct(){ // SAVE LIST LINK BINDING
	$('.saveTableItemOverlay').click(function(){
		returnPageLnkWireUp()
		window.location = $(this).attr('href');
	});
	/*$('.saveTableItemOverlay').hover(function(){
		$('#saveListHoverInfo').html('<span class="hidden">' + $(this).attr('data-info') + '</span>');
		$('#saveListHoverInfo span').fadeIn('fast');
	}, function(){
		$('#saveListHoverInfo span').fadeOut('fast');
	});*/
}

function recentListLinkConstruct(){ // RECENT LIST LINK BINDING
	$('.recentTableItemOverlay').click(function(){
		returnPageLnkWireUp()
		window.location = $(this).attr('href');
	});
	/*$('.recentTableItemOverlay').hover(function(){
		$('#recentListHoverInfo').html('<span class="hidden">' + $(this).attr('data-info') + '</span>');
		$('#recentListHoverInfo span').fadeIn('fast');
	}, function(){
		$('#recentListHoverInfo span').fadeOut('fast');
	});*/
}

function scanForSaved(){
	$('.saveTableItemOverlay').each(function(i){
		var tempVal = $(this).attr('href').replace('/vehicle-details/?VehicleId=', '');
		$('.callToActionLink .saveVehicle').each(function(x){
			if($(this).attr('data-vehicleid') == tempVal){
				$(this).siblings('.savedVehicle').removeClass('hardHidden');
				$(this).addClass('hardHidden');
				$(this).parent().parent().addClass('callToActionCellDisabled');
				$(this).parent().parent().removeClass('callToActionCellActive');
			}
		});
	});
}

function scanForSavedRestore(){
	$('.callToActionLink .savedVehicle').each(function(i){
		var tempVal = $(this).attr('data-vehicleid');
		var idFound = 'false';
		$('.saveTableItemOverlay').each(function(x){
			var tempValSecond = $(this).attr('href').replace('/vehicle-details/?VehicleId=', '');
			if(tempVal == tempValSecond){
				idFound = 'true';
			}
		});
		if(idFound == 'false'){
			$(this).siblings('.saveVehicle').removeClass('hardHidden');
			$(this).parent().parent().removeClass('callToActionCellDisabled');
			$(this).parent().parent().addClass('callToActionCellActive');
			$(this).addClass('hardHidden');
		}
	});
}

function savedAllRestore(){
	$('.callToActionLink .savedVehicle').addClass('hardHidden');
	$('.callToActionLink .saveVehicle').removeClass('hardHidden');
	$('.callToActionLink .saveVehicle').parent().parent().removeClass('callToActionCellDisabled');
	$('.callToActionLink .saveVehicle').parent().parent().addClass('callToActionCellActive');
}

function saveRecentSwitchWireUp(){
	$('#saveHeaderContainer').click(function(){
		if($('#theSaveList').css('display') == 'none'){
			$(this).parent().removeClass('recentSaveList');
			$(this).parent().addClass('saveRecentList');
			$('#theRecentList').fadeOut('fast');
			
			$('.saveRecentBody').animate({
			height : $('#theSaveList').height()}, 'fast', function(){
				$('#theSaveList').fadeIn('fast');
			});
			
			setCookie('saveRecentList', 'saveList', 10);
		}
	});
	
	$('#recentHeaderContainer').click(function(){
		if($('#theRecentList').css('display') == 'none'){
			$(this).parent().removeClass('saveRecentList');
			$(this).parent().addClass('recentSaveList');
			
			$('#theSaveList').fadeOut('fast');
			$('.saveRecentBody').animate({
			height : $('#theRecentList').height()}, 'fast', function(){
				$('#theRecentList').fadeIn('fast');
			});
			
			setCookie('saveRecentList', 'recentList', 10);
		}
	});
}

function refreshSaveRecentBody(){
	if($('#theSaveList').css('display') != 'none'){
		var tempCount = 0;
		for(i=0; i<=7; i++){
				tempVal = getCookie('saveList' + i);
				if (tempVal != null){
					tempCount = tempCount +1;
				}
		}
		//alert(tempCount);
		if(tempCount == 0){
			$('.saveRecentBody').delay(600).animate({height : 20}, 'fast');
		} else {
			$('.saveRecentBody').animate({height : $('#theSaveList').height()}, 'fast');
		}
	}
}

function returnPageLnkWireUp(){
	
		testPage = window.location.href;
		if(testPage.indexOf('/stocklist/') != -1){
			setCookie('listPageReturnURL', testPage, 2);
		}
}

function comparisonLinkWireUp(){
	$('#comparisonLink a').click(function(){
		returnPageLnkWireUp();
	});
}

$(document).ready(function () {
	
	$('.saveVehicle').click(function(){
		var vehId = $(this).attr('data-vehicleId');
		var thumb = $(this).attr('data-thumbnail');
		var make = $(this).attr('data-make');
		var model = $(this).attr('data-model');
		var price = $(this).attr('data-price');
		saveList(vehId, thumb, make, model, price);
	});	
	
	$(".radio").dgStyle();
	$(".checkbox").dgStyle();
	compareCheckboxes();
	deleteLinkApply();
	saveListLinkConstruct();
	recentListLinkConstruct();
	scanForSaved();		
	saveRecentSwitchWireUp();	
});
