if (typeof QuadrigaCars === 'undefined') {
	QuadrigaCars = {}
}

function setDefaultEcoCarConfig() {
	QuadrigaCars.bespokeEcoCarConfigurator = {
		inStock: false,
		basePrice: 0,
		priceTotal: 0,
		pack360Price: 0,
		optionsPrice: 0,
		quadrigaOptionsPrice: 0,
		otherOptionsPrice: 0,
		term: 0,
		articleId: 0,
		baseOptionId: 0,
		saveOptions: {}
	}
}

$(document).ready(
	function() {
		setDefaultEcoCarConfig();
		
		QuadrigaCars.startMode = $('#start-mode')[0].value;
		
		switch ($('#start-mode')[0].value) {
			case 'eco-cars':
				QuadrigaCars.userConfigMode = parseInt($("#user-config-mode")[0].value);
				
				if (QuadrigaCars.userConfigMode) {
					restoreEcoCarsConfig($("#user-config")[0].innerHTML);
				}
				
				QuadrigaCars.startMode = $('#start-mode')[0].value;
				
				showEcoCarsInfo();
				break;
				
			case 'your-quadriga':
				QuadrigaCars.userConfigMode = parseInt($("#user-config-mode")[0].value);
				
				if (QuadrigaCars.userConfigMode) {
					restoreEcoCarsConfig($("#user-config")[0].innerHTML, false);
				}
				
				QuadrigaCars.startMode = $('#start-mode')[0].value;
				break;
		}
	}
);

var ecoCarSelectorText;

var isRestoreUserConfig = false;
function restoreEcoCarsConfig(config, isShowCarInfo) {
	eval('userConfig = ' + config);
	
	QuadrigaCars = userConfig.quadrigaCars;
	
	// Default value for isShowCarInfo equal TRUE
	if (typeof isShowCarInfo === 'undefined') {
		isShowCarInfo = true;
	}
	
	if (QuadrigaCars.bespokeEcoCarConfigurator && QuadrigaCars.bespokeEcoCarConfigurator.baseOptionId !== 0) {
		//QuadrigaCars.userConfigMode = false;
		isRestoreUserConfig = true;
		
		ShowConfiguratorByArticle(QuadrigaCars.bespokeEcoCarConfigurator.articleId, QuadrigaCars.bespokeEcoCarConfigurator.baseOptionId, QuadrigaCars.bespokeEcoCarConfigurator.term, isShowCarInfo);
		
		$('#configurator')[0].style.display = '';
		
		//QuadrigaCars.userConfigMode = true;
		isRestoreUserConfig = false;
	} else {
		setDefaultEcoCarConfig();
	}
}

function showEcoCarsInfo() {
	$('#rightpanel').html($('#ecocarsinfo').html());

	$("h3").hover(
		function(){
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");
		}
	);

	$("div.sliding-content-container > h3").click(
		function() {
		        var activeContainer = $("div.sliding-content-container").filter(".sliding-content-container-active");
		        var thisContainer = $(this).parent();

			// if clicked container is not active, expand it
			if (!thisContainer.hasClass("sliding-content-container-active"))	
			{
	       			thisContainer.find("span.intro").hide();
				thisContainer.children("div.full-content").slideDown("slow");
				thisContainer.addClass("sliding-content-container-active");			
			}	

			// collapse active container
			activeContainer.children("div.full-content").slideUp("slow");
			activeContainer.removeClass("sliding-content-container-active");
			activeContainer.find("span.intro").show();

			return false;
		});
	
	hideCarBackground();
}

function ShowEcoChoice(choice) {
	switch (choice) {
		case '0':
			QuadrigaCars.selectedEcoCarType = '0';
			
			$("#ecocarselector")[0].value = 0;
			
			QuadrigaCars.selectedEcoCarName = '';
			QuadrigaCars.selectedEcoCarType == '';
			
			showEcoCarsInfo();
			
			var supersizeElements = $('.supersize');
			
			for (var i = 0; i < supersizeElements.length; i++) {
			    if ($('#start-mode')[0].value === 'eco-cars') {
					supersizeElements[i].style.background = 'none';
				} else {
					supersizeElements[i].style.background = '#f0f0f0';
				}
			}
			
			$("#configurator")[0].style.display = 'none';
			
			$("#eco-car-container")[0].style.display = 'none';
			
			break;
			
		case 'none':
			QuadrigaCars.selectedEcoCarType = 'none';
			
			QuadrigaCars.selectedEcoCarName = 'No, thanks';
			QuadrigaCars.selectedEcoCarType == 'none';
			
			var choiceInputs = document.getElementsByName('EcoToGoChoice')
			
			for (var i = 0; i < choiceInputs.length; i++) {
				choiceInputs[i].checked = false;
			}
			
			showEcoCarsInfo();
			
			var supersizeElements = $('.supersize');
			
			for (var i = 0; i < supersizeElements.length; i++) {
				if ($('#start-mode')[0].value === 'eco-cars') {
					supersizeElements[i].style.background = 'none';
				} else {
					supersizeElements[i].style.background = '#f0f0f0';
				}
			}
			
			$("#configurator")[0].style.display = 'none';
			
			break;
		
		default: // Bespoke Eco Cars:
			QuadrigaCars.selectedEcoCarTitle = $('#eco-cars-title-' + choice)[0].innerHTML;
			QuadrigaCars.selectedEcoCarType = 'bespoke';
			
			$("#configurator")[0].style.display = '';
			
			bespokenow = true;
			
			ShowConfiguratorByArticle(choice, null, null);
	}
}

function showContactForm() {
	var formInner = document.getElementById('contactform').innerHTML;
	
	$("#eco-car-container")[0].innerHTML = formInner + '<span id="is-show-contact-form" style="display: none;"></span>';
	$("#eco-car-container")[0].style.display = '';
	$("#configurator")[0].style.display = 'none';
}

function showForcedContactForm() {
	showContactForm();
	
	var emailInputs = $('.contact-form-email');
	
	for (var i = 0; i < emailInputs.length; i++) {
		emailInputs[i].value = $('#user-config-email')[0].value;
	}
	
	return;
}

function saveEcoCarsConfig() {
	if (!QuadrigaCars.bespokeEcoCarConfigurator.inStock) {
		saveBespokeEcoCarOptions();
	}
	
	var saveParams = {};
	
	saveParams.saveUserMode = true;
	saveParams.quadrigaCars = QuadrigaCars;
	saveParams.userConfig = $.toJSON(saveParams);
	
	saveParams.hash = $('#user-config-hash')[0].value;
	saveParams.isSendNotification = 0;
	
	$.post('include/save-user-cars.php', saveParams, function(data, textStatus) {
		if (textStatus !== 'success') {
			alert('There was error when save user cars.')
			return;
		}
		
		eval('var responseObject = ' + data);
		
		if (responseObject.success) {
			document.location = $('html head base')[0].href + 'your-quadriga.php#confirm';
		} else {
			alert('There was error when send request.')
		}
		
		return false;
	});
}

function ecoCarsProceedCallback() {
	// Track click in GA
	pageTracker._trackPageview('/electriccars-proceed.php');
	if (typeof(console) != 'undefined')
	{
		console.log('trackPageView(/electriccars-proceed.php)');
	}

	// Save config and go to Your Quadriga / Confirm
	switch (QuadrigaCars.startMode) {
		case 'eco-cars':
			if ($("#ecocarselector")[0].value === '0') {
				document.location = $('html head base')[0].href + 'your-quadriga.php#confirm';
			} else {
				saveEcoCarsConfig();
			}
			break;
			
		case 'your-quadriga':
			saveEcoCarsConfig();
			updatePrice();
			changeSelectStep(3);
			
			$('#rightpanel').html('');
			hideCarBackground();
			break;
	}
}

var ecoCarsToGoTerm = 'monthly';
function onEcoCarsToGoTermSelect(term) {
	var allInputs = document.getElementsByTagName('input');
	var oldInputChecks = []
	for (var i = 0; i < allInputs.length; i++) {
		if (allInputs[i].className.indexOf('eco-cars-choice-price ' + ecoCarsToGoTerm) !== -1) {
			oldInputChecks[oldInputChecks.length] = allInputs[i].checked;
		}
	}
	
	ecoCarsToGoTerm = term;
	
	var j = 0;
	for (var i = 0; i < allInputs.length; i++) {
		if (allInputs[i].className.indexOf('eco-cars-choice-price ' + ecoCarsToGoTerm) !== -1) {
			allInputs[i].checked = oldInputChecks[j];
			j++;
			
			if (j === oldInputChecks.length) {
				j = 0;
			}
		}
	}
	
	var allTRElements = document.getElementsByTagName('tr');
	for (var i = 0; i < allTRElements.length; i++) {
		if (allTRElements[i].className.indexOf('eco-car-row') !== -1) {
			if (allTRElements[i].className.indexOf(ecoCarsToGoTerm) == -1) {
				allTRElements[i].style.display = 'none';
			} else {
				allTRElements[i].style.display = '';
			}
		}
	}
	
	var allSpan = document.getElementsByTagName('span');
	for (var i = 0; i < allSpan.length; i++) {
		if (allSpan[i].className.indexOf('eco-cars-to-go-term') !== -1) {
			if (allSpan[i].className.indexOf(ecoCarsToGoTerm) == -1) {
				allSpan[i].style.backgroundColor = '#AA3C3C';
				allSpan[i].style.color = 'white';
			} else {
				allSpan[i].style.backgroundColor = 'white';
				allSpan[i].style.color = '#AA3C3C';
			}
		}
	}
	
	onEcoCarsToGoChecked();
}

function onEcoCarsToGoChecked() {
	var ecoCarsToGoTotalPrice = 0;
	var modelsSelected = '';
	var ecoCarsPriceSelected = document.getElementsByName("eco-cars-choice-price-" + ecoCarsToGoTerm);
	var ecoCarsNameSelected = document.getElementsByName("eco-cars-choice-name-" + ecoCarsToGoTerm);
	
	for (var i = 0; i < ecoCarsPriceSelected.length; i++) {
		if (ecoCarsPriceSelected[i].checked) {
			ecoCarsToGoTotalPrice += parseInt(ecoCarsPriceSelected[i].value);
			if (modelsSelected.length > 0) {
				modelsSelected += ', ' + ecoCarsNameSelected[i].value;
			} else {
				modelsSelected = ecoCarsNameSelected[i].value;
			}
		}
	}
	
	QuadrigaCars.ecoCarsToGoTotalPrice = ecoCarsToGoTotalPrice;
	document.getElementById("eco-cars-to-go-total-price").innerHTML = "&pound;" + ecoCarsToGoTotalPrice + ' p.m.';
	
	document.quadrigaoptions.EcoCarChosen.value = modelsSelected;
}