var lastSensationCode = 2;
var lastFamilyCode;
var lastType;
var lastKey;
var lastColor;
var lastSelection;
var lastColorAnd;
var colorSearchType;

function searchBySurface(keepPage) {
	var page = 1;
	var x = $("#surface").val();

	if (keepPage) {
		if (coral && coral.Produto && coral.Produto.swfAddressPath) {
			page = coral.Produto.swfAddressPath[2] || page;
		}
	}
	hideCombinationsDiv();
	if (x > 0) {
		coral.Produto.setValue('surface');
		searchProducts("bySurface", x, page);
	}
}

function searchByType(keepPage) {
	var page = 1;
	var x = $("#tipo").val();

	if (keepPage) {
		if (coral && coral.Produto && coral.Produto.swfAddressPath) {
			page = coral.Produto.swfAddressPath[2] || page;
		}
	}
	hideCombinationsDiv();
	if (x > 0) {
		coral.Produto.setValue('type');
		searchProducts("byType", x, page);
	}
}

function searchByColor(keepPage) {
	var x = $("#cor").val();
	var elemento;

	if (x > 0) {
		coral.Produto.setValue('color');
		$('.navBarCores a').each(function(i) {
			$('.navBarCores a')[i].className = $('.navBarCores a')[i].className.split('_')[0];
		});
		elemento = $("#sensacao" + lastSensationCode)[0];
		if (elemento) { elemento.className = elemento.className.split('_')[0] + "_selecionado"; }
		loadColorPalete(lastSensationCode, x);
	} else {
		coral.Produto.firstTime = false;
	}
}

function searchProducts(type, key, page) {
	if (lastSelection == null) { lastSelection = $(".navBarCores .frescas_selecionado")[0]; }
	var wall = '';
	var surface = '';

	if (type == "bySurface") {
		surface = ($('#surfaceCtd1 input:checked').length && $($('#surfaceCtd1 input:checked')[0]).length && $($('#surfaceCtd1 input:checked')[0]).val() > 0) ? $($('#surfaceCtd1 input:checked')[0]).val() : '';
		wall = ($('#surfaceCtd2 select:first').length && $('#surfaceCtd2 select:first').val() > 0) ? $('#surfaceCtd2 select:first').val() : '';
	}

	if (key != "" && key != 0) {
		$.ajaxSetup({contentType: "application/x-www-form-urlencoded; charset=UTF-8"})
		$("#products").load(
			"/do/products/submitSearch",
			{searchType:type, searchKey:key, page:page, surface:surface, wall:wall},
			function() {
				lastType = type;
				lastKey = key;
				showProducts();
			}
		);
	}
}

function searchByColorType(x) {
	var elemento = $("#sensacao" + x)[0];
	if (lastSelection == null) { lastSelection = $(".navBarCores .frescas_selecionado")[0]; }

	loadColorPalete(x, lastFamilyCode);
	$('.navBarCores a').each(function(i) {
		$('.navBarCores a')[i].className = $('.navBarCores a')[i].className.split('_')[0];
	});
	elemento.className = elemento.className + "_selecionado";
	lastSelection = elemento;
}

function searchCombinationsByColor(x) {
	$("#searchColor").load(
		"/do/colors/submitSearch",
		{colorCode:x, familyCode:lastFamilyCode, sensationCode:lastSensationCode},
		function() {
			lastColor = x;
			coral.Produto.setValue('color', 1);
			showCombinations();
		}
	);
}

function searchBySurfaceAndColor() {
	var page = 1;
	var x = $("#surfaces").val();
	var color2 = $("#cor").val();

	if (coral && coral.Produto && coral.Produto.swfAddressPath) {
		page = coral.Produto.swfAddressPath[2] || page;
	}

	if (x) {
		searchByColorAnd("bySurface", x, page, color2);
	}
}

function searchByTypeAndColor() {
	var page = 1;
	var x = $("#types").val();
	var color2 = $("#cor").val();

	if (coral && coral.Produto && coral.Produto.swfAddressPath) {
		page = coral.Produto.swfAddressPath[2] || page;
	}

	if (x) {
		searchByColorAnd("byType", x, page, color2);
	}
}

function searchByColorAnd(type, key, page, color2) {
	coral.Produto.firstTime = false;
	colorSearchType = type;
	lastColor = color2;
	lastKey = key;
	if (key > 0) {
		$("#products").load(
			"/do/colors/searchProductsByColor",
			{colorCode:lastColor, searchType:type, searchKey:key, page:page, colorCode2:color2},
			function() {
				lastColorAnd = (type == 'bySurface') ? 'surface' : 'type';
				coral.Produto.setValue('color', 1);
				showProducts();
			}
		);
	}
}

function updateCombination(x) {
	searchCombinationsByColor(x);
}

function updateCombinations(x, family, sensation) {
	lastFamilyCode = family;
	lastSensationCode = sensation;
	searchCombinationsByColor(x);
}

function loadColorPalete(x, y) {
	$("#colorsPalete").load(
		"/do/colors/search",
		{sensationCode:x, familyCode:y},
		function() {
			lastSensationCode = x;
			lastFamilyCode = y;
			coral.Produto.setValue('color', 1);
			showColors();
		}
	);
}

function showColors() {
	$("#products").hide();
	hideCombinationsDiv();
	$("#colors").show();

	if (coral.Produto.firstTime) {
		if (coral && coral.Produto && coral.Produto.swfAddressPath && coral.Produto.swfAddressPath[4]) {
			searchCombinationsByColor(coral.Produto.swfAddressPath[4]);
		} else {
			coral.Produto.firstTime = false;
		}
	}
}

function showProducts() {
	$("#colors").hide();
	$("#products").show();
}

function showCombinations() {
	var path = [];

	$("#products").hide();
	$("#colors").hide();
	showCombinationsDiv();

	if (coral.Produto.firstTime) {
		if (coral && coral.Produto && coral.Produto.swfAddressPath && coral.Produto.swfAddressPath[5]) {
			path = coral.Produto.swfAddressPath;
			if (path[5] == 'surface') {
				$('#surfaces').val(path[6]);
				searchBySurfaceAndColor();
			} else if (path[5] == 'type') {
				$('#types').val(path[6]);
				searchByTypeAndColor();
			} else {
				coral.Produto.firstTime = false;
			}
		} else {
			coral.Produto.firstTime = false;
		}
	}
}

function hideCombinationsDiv() {
	$("#searchColor").hide();
	$("#searchOrder").hide();
	$('#surfaces').val(0);
	$('#types').val(0);
}

function showCombinationsDiv() {
	$("#searchColor").show();
	$("#searchOrder").show();
}

function showProductsPage(x) {
	var path = [];

	if (coral && coral.Produto && coral.Produto.swfAddressPath) { path = coral.Produto.swfAddressPath; }
	path[2] = x;
	SWFAddress.setValue('/' + path.join('/'));

	searchProducts(lastType, lastKey, x);
}

function showProductsPage2(x) {
	var path = [];

	if (coral && coral.Produto && coral.Produto.swfAddressPath) { path = coral.Produto.swfAddressPath; }
	path[2] = x;
	SWFAddress.setValue('/' + path.join('/'));
	searchByColorAnd(colorSearchType, lastKey, x, lastColor);
}

function searchByName() {
	hideCombinationsDiv();
	var x = $("#searchTxt").val();
	searchProducts("byName", x, 1);
}

function searchByNameHeader(productName){
	if(productName != "null"){
		searchProducts("byName", productName, 1);
	}
}

function addToBasket(x) {
	$.ajax({
		url: "/do/basket/add",
		data: "productCode=" + x,
		success: function(response) {
			//window.alert(response);
		}
	});
}

function findColorOfProduct(x) {
	$.ajax({
		url: "/do/products/findReadyColorsByProduct",
		data: "productCode=" + x,
		success: function(response) {
			$("#colorsOfThisProduct").html(response);
		}
	});
}

function detailSuperficie(productName) {
	var prod = productName.split(".")[0];
	window.location.href = "/guiadePintura/preparacao/" + prod + ".shtml";
}

function createProductComboBox(line) {
	clearProduct();
	clearColor();
	$("#productCombobox").load("/do/products/submitProductType", {lineCode: line});
}

function clearProduct() {
	$("#productCombobox").html("<select id=\"product\"><option value=\"\">Selecione um produto</option></select>");
}

function clearColor() {
	$("#colorCombobox").html("<select id=\"color\"><option value=\"\">Selecione uma cor</option></select>");
}

function createColorCombobox(x,y) {
	clearColor();
	$("#colorCombobox").load("/do/products/submitProduct", {lineCode: x, productCode: y});
}

function loadPDF(x, y) {
	$("#buttonOk").load("/do/products/submitSearchFispq", {productCode: x, colorCode: y});
}
