// namespace
window.LOV = window.LOV || {};
LOV.Coral = LOV.Coral || {};
LOV.Coral.Produto = LOV.Coral.Produto || {};

// shortcut
var coral = LOV.Coral;

coral.Produto.firstTime = false;

coral.Produto.init = function() {
	$('#surface').bind('change', 'surface', coral.Produto.nextStep);
	$('#tipo').bind('change', 'tipo', coral.Produto.nextStep);
	$('#cor').bind('change', 'cor', coral.Produto.nextStep);

	var ctp = new ColorToolTip();
	ctp.setQuestion('Gostou dessa cor?');
	ctp.setToolTip('cToolTip');
	ctp.init('ctt1', 'ctt2', 'ctt3', 'ctt4', 'ctt5', 'ctt6');
	lastSensationCode = lastSensationCode || 22;
	lastColorAnd = lastColorAnd || '';
};
$().ready(coral.Produto.init);

coral.Produto.nextStep = function(e) {
	var id = e.data;
	var idx = ($(e.target).is('select')) ? e.target.options[e.target.selectedIndex].value : $('input[@name=\'' + e.target.id + '\']:checked').val();
	var depth = 0;
	var target = null;
	var searchBy = null;

	switch (id) {
		case 'surface':
			coral.Produto.reset('surface');
			if (e.target.id == 'surface') {
				depth = 1;
				$('#surfaceCtd1').html('');
				$('#surfaceCtd2').html('');
				$('#surfaceCtd3').html('');
			} else if ($(e.target)[0] && $(e.target)[0].parentNode && $(e.target)[0].parentNode.parentNode && $(e.target)[0].parentNode.parentNode.id == 'surfaceCtd1') {
				depth = 2;
				$('#surfaceCtd2').html('');
				$('#surfaceCtd3').html('');
			} else if ($(e.target)[0] && $(e.target)[0].parentNode && $(e.target)[0].parentNode.parentNode && $(e.target)[0].parentNode.parentNode.id == 'surfaceCtd2') {
				depth = 3;
				$('#surfaceCtd3').html('');
			}
		break;

		case 'tipo':
			depth = 1;
			coral.Produto.reset('tipo');
			$('#tipoCtd1').html('');
		break;

		case 'cor':
			depth = 1;
			coral.Produto.reset('cor');
		break;
	}

	target = coral.Produto[id][idx];
	if (target) {
		var str = [];
		for (var i = 0, l = target.fields.length; i < l; i++) {
			var fld = target.fields[i];

			if (fld.type == 'radio') {
				if (i == 0) {
					str.push('<label>');
					str.push('<strong>' + target.label + '</strong><br />');
					str.push('</label>');
					str.push('<div class="label">');
				}
				str.push('<input type="radio" id="' + fld.id + '" name="' + fld.id + '" value="' + fld.value + '" /><span>' + fld.label + '</span>');
				if (i == l - 1) { str.push('</div>'); }
			} else if (fld.type == 'select') {
				if (i == 0) {
					str.push('<label>');
					str.push('<select id="' + fld.id + '" name="' + fld.id + '">');
				}
				for (var j = 0, m = fld.options.length; j < m; j++) {
					var opt = fld.options[j];
					str.push('<option value="' + opt.value + '">' + opt.label + '</option>');
				}
				if (i == l - 1) {
					str.push('</select>');
					str.push('</label>');
				}
			} else if (fld.type == 'p') {
				if (i == 0) { str.push('<p>'); }
				str.push(fld.value);
				if (i == l - 1) { str.push('</p>'); }
			}
		}
		$('#' + id + 'Ctd' + depth).html(str.join(''));

		if (fld.type == 'radio') {
			$('input[@name=\'' + fld.id + '\']').bind('click', id, coral.Produto.nextStep);
		} else {
			$('select[@name=\'' + fld.id + '\']').bind('change', id, coral.Produto.nextStep);
		}

		// <SWFAddress control>
		if (coral.Produto.firstTime) {
			switch (depth) {
				case 1:
					if (coral.Produto.swfAddressPath.length < 4) { searchBy = id; }
					else {
						var tmpFld = $('#' + id + 'Ctd' + depth + ' input[@value=' + coral.Produto.swfAddressPath[3] + ']')[0];
						if (tmpFld) {
							tmpFld.checked = true;
							tmpFld.click();
						}
					}
				break;

				case 2:
					if (coral.Produto.swfAddressPath.length == 5) {
						var tmpFld = $('#' + id + 'Ctd' + depth + ' select')[0];
						if (tmpFld) {
							$(tmpFld).val(coral.Produto.swfAddressPath[4]);
						}
					}
					searchBy = id;
				break;
			}
		}
		// </SWFAddress control>
	}

	if (id == 'cor') { searchBy = 'cor'; }

	if (coral.Produto.firstTime) {
		
		if (searchBy == 'surface') {
			coral.Produto.firstTime = false;
			searchBySurface(true);
		} else if (searchBy == 'tipo') {
			coral.Produto.firstTime = false;
			searchByType(true);
		} else if (searchBy == 'cor') {
			searchByColor(true);
		} else {
			coral.Produto.firstTime = false;
		}
	}
}

coral.Produto.surface = {
	1: {
		label: 'Ambiente:',
		fields: [
			{
				id: 'wallAmbient',
				type: 'radio',
				value: '10',
				label: 'Interior'
			},
			{
				id: 'wallAmbient',
				type: 'radio',
				value: '11',
				label: 'Exterior'
			}
		]
	},
	10: {
		label: 'Tipo de Parede:',
		fields: [
			{
				id: 'wall',
				type: 'select',
				options: [
					{value: '0', label: 'Todos'},
					{value: '100', label: 'Preparação'},
					{value: '101', label: 'Acabamento'},
					{value: '102', label: 'Técnicas Especiais'}
				]
			}
		]
	},
	11: {
		label: 'Tipo de Parede:',
		fields: [
			{
				id: 'wall',
				type: 'select',
				options: [
					{value: '0', label: 'Todos'},
					{value: '103', label: 'Preparação'},
					{value: '104', label: 'Acabamento'},
					{value: '105', label: 'Técnicas Especiais'}
				]
			}
		]
	},
	2: {
		label: 'Ambiente:',
		fields: [
			{
				id: 'woodAmbientMain',
				type: 'radio',
				value: '12',
				label: 'Interior'
			},
			{
				id: 'woodAmbientMain',
				type: 'radio',
				value: '13',
				label: 'Exterior'
			}
		]
	},
	12: {
		label: 'Tipo de Madeira:',
		fields: [
			{
				id: 'woodInner',
				type: 'select',
				options: [
					{value: '0', label: 'Todos'},
					{value: '106', label: 'Pintura'},
					{value: '107', label: 'Envernizamento'},
					{value: '108', label: 'Técnicas Especiais'}
				]
			}
		]
	},
	_106: {
		label: 'Etapa:',
		fields: [
			{
				id: 'woodAmbientStep',
				type: 'radio',
				value: '200',
				label: 'Preparação'
			},
			{
				id: 'woodAmbientStep',
				type: 'radio',
				value: '201',
				label: 'Acabamento'
			}
		]
	},
	_107: {
		label: 'Etapa:',
		fields: [
			{
				id: 'woodAmbientStep',
				type: 'radio',
				value: '202',
				label: 'Preparação'
			},
			{
				id: 'woodAmbientStep',
				type: 'radio',
				value: '203',
				label: 'Acabamento'
			}
		]
	},
	13: {
		label: 'Tipo de Madeira:',
		fields: [
			{
				id: 'woodOuter',
				type: 'select',
				options: [
					{value: '0', label: 'Todos'},
					{value: '106', label: 'Pintura'},
					{value: '107', label: 'Envernizamento'},
					{value: '108', label: 'Técnicas Especiais'}
				]
			}
		]
	},
	_106: {
		label: 'Etapa:',
		fields: [
			{
				id: 'woodAmbientStep',
				type: 'radio',
				value: '204',
				label: 'Preparação'
			},
			{
				id: 'woodAmbientStep',
				type: 'radio',
				value: '205',
				label: 'Acabamento'
			}
		]
	},
	3: {
		label: 'Ambiente:',
		fields: [
			{
				id: 'metalAmbient',
				type: 'radio',
				value: '14',
				label: 'Interior'
			},
			{
				id: 'metalAmbient',
				type: 'radio',
				value: '15',
				label: 'Exterior'
			}
		]
	},
	14: {
		label: 'Tipo de Metal:',
		fields: [
			{
				id: 'metalInner',
				type: 'select',
				options: [
					{value: '0', label: 'Todos'},
					{value: '113', label: 'Alumínio'},
					{value: '112', label: 'Ferro'},
					{value: '114', label: 'Galvanizado'}
				]
			}
		]
	},
	_113: {
		label: 'Etapa:',
		fields: [
			{
				id: 'metalStep',
				type: 'radio',
				value: '208',
				label: 'Preparação'
			},
			{
				id: 'metalStep',
				type: 'radio',
				value: '209',
				label: 'Acabamento'
			}
		]
	},
	_112: {
		label: 'Etapa:',
		fields: [
			{
				id: 'metalStep',
				type: 'radio',
				value: '206',
				label: 'Preparação'
			},
			{
				id: 'metalStep',
				type: 'radio',
				value: '207',
				label: 'Acabamento'
			}
		]
	},
	_114: {
		label: 'Etapa:',
		fields: [
			{
				id: 'metalStep',
				type: 'radio',
				value: '210',
				label: 'Preparação'
			},
			{
				id: 'metalStep',
				type: 'radio',
				value: '211',
				label: 'Acabamento'
			}
		]
	},
	15: {
		label: 'Tipo de Metal:',
		fields: [
			{
				id: 'metalOuter',
				type: 'select',
				options: [
					{value: '0', label: 'Todos'},
					{value: '116', label: 'Alumínio'},
					{value: '115', label: 'Ferro'},
					{value: '117', label: 'Galvanizado'}
				]
			}
		]
	},
	_116: {
		label: 'Etapa:',
		fields: [
			{
				id: 'metalStep',
				type: 'radio',
				value: '214',
				label: 'Preparação'
			},
			{
				id: 'metalStep',
				type: 'radio',
				value: '215',
				label: 'Acabamento'
			}
		]
	},
	_115: {
		label: 'Etapa:',
		fields: [
			{
				id: 'metalStep',
				type: 'radio',
				value: '212',
				label: 'Preparação'
			},
			{
				id: 'metalStep',
				type: 'radio',
				value: '213',
				label: 'Acabamento'
			}
		]
	},
	_117: {
		label: 'Etapa:',
		fields: [
			{
				id: 'metalStep',
				type: 'radio',
				value: '216',
				label: 'Preparação'
			},
			{
				id: 'metalStep',
				type: 'radio',
				value: '217',
				label: 'Acabamento'
			}
		]
	},
	4: {
		label: 'Etapa:',
		fields: [
			{
				id: 'roofStep',
				type: 'radio',
				value: '16',
				label: 'Interior'
			},
			{
				id: 'roofStep',
				type: 'radio',
				value: '17',
				label: 'Exterior'
			}
		]
	}
};

coral.Produto.tipo = {
	40: {
		fields: [{
			type: 'p',
			value: 'Indicado especialmente para base ou acabamento em madeiras, metais, tijolos, telhas e cerâmicas não vitrificadas.'
		}]
	},
	10: {
		fields: [{
			type: 'p',
			value: 'Tintas de alto desempenho à base de água para aplicações variadas. Podem ser usadas em camadas espessas ou finas.'
		}]
	},
	60: {
		fields: [{
			type: 'p',
			value: 'Tintas à base de água e da resina acetato de polivinil, o PVA. Indicadas para paredes externas e internas de alvenaria.'
		}]
	},
	90: {
		fields: [{
			type: 'p',
			value: 'Para acabamento em madeira, concreto aparente, pedra mineira, tijolo e telhas. Pode ser transparente ou colorido.'
		}]
	},
	50: {
		fields: [{
			type: 'p',
			value: 'Substância que impede a passagem de fluidos, especialmente água. Para proteção e impermeabilização de superfícies.'
		}]
	},
	20: {
		fields: [{
			type: 'p',
			value: 'Produtos de preparação para a pintura. Fundos, massas e líquidos para uniformizar, nivelar e corrigir superfícies.'
		}]
	},
	80: {
		fields: [{
			type: 'p',
			value: 'Massas para criar efeitos decorativos - lisos, texturizados, riscados, granulados ou envelhecidos - em relevo.'
		}]
	},
	100: {
		fields: [{
			type: 'p',
			value: 'Esmaltes anticorrosivos e anti-ferrugem para aplicação em superfícies metálicas.'
		}]
	},
	70: {
		fields: [{
			type: 'p',
			value: 'Corantes para tinta látex PVA e acrílica, removedores de esmaltes e vernizes e diluidores de tintas e vernizes sintéticos.'
		}]
	}
};

coral.Produto.cor = {};

coral.Produto.swfAddressPath = [];

coral.Produto.setAddressPath = function(pPath) {
	var path = pPath;

	path = path.replace(/^\//, '');
	path = path.replace(/\/$/, '');
	coral.Produto.swfAddressPath = path.split('/');

	return path;
}

coral.Produto.swfAddress = function(e) {
	var path = e.path;

	path = path.replace(/^\//, '');
	path = path.replace(/\/$/, '');

	if (path == coral.Produto.swfAddressPath.join('/')) { return; }
	if (path.split('/').length < 2) {
		coral.Produto.swfAddressPath = [];
		coral.Produto.firstTime = false;
		coral.Produto.reset();
		hideCombinationsDiv();
		$("#products").hide();
		$("#colors").hide();
		return;
	}

	coral.Produto.firstTime = true;
	coral.Produto.setAddressPath(path);

	path = path.split('/');
	switch (path[0]) {
		case 'surface':
			$('#surface').val(path[1]);
			$('#surface').change();
		break;

		case 'type':
			$('#tipo').val(path[1]);
			$('#tipo').change();
		break;

		case 'color':
			lastFamilyCode = path[1] || '';
			lastSensationCode = path[3] || '';
			lastColor = path[4] || '';
			lastColorAnd = path[5] || '';

			$('#cor').val(path[1]);
			$('#cor').change();
		break;
	}
}

coral.Produto.getValue = function(pType, pLevel) {
	switch (pType) {
		case 'surface':
			if (pLevel == 1) {
				return $('#surface').val();
			}

			if (pLevel == 2) {
				return $($('#surfaceCtd1 input:checked')[0]).val();
			}

			if (pLevel == 3) {
				return $('#surfaceCtd2 select:first').val();
			}
		break;

		case 'type':
			if (pLevel == 1) {
				return $('#tipo').val();
			}
		break;

		case 'color':
			if (pLevel == 1) {
				return $('#cor').val();
			}
		break;
	}

	return false;
}

coral.Produto.setValue = function(pType) {
	var path = coral.Produto.swfAddressPath;
	var page = path[2] || 1;

	switch (pType) {
		case 'surface':
			if (coral.Produto.getValue('surface', 1) > 0 && coral.Produto.getValue('surface', 2) > 0 && coral.Produto.getValue('surface', 3) > 0) {
				SWFAddress.setValue(coral.Produto.setAddressPath('/surface/' + coral.Produto.getValue('surface', 1) + '/' + page + '/' + coral.Produto.getValue('surface', 2) + '/' + coral.Produto.getValue('surface', 3)));
			} else if (coral.Produto.getValue('surface', 1) > 0 && coral.Produto.getValue('surface', 2) > 0) {
				SWFAddress.setValue(coral.Produto.setAddressPath('/surface/' + coral.Produto.getValue('surface', 1) + '/' + page + '/' + coral.Produto.getValue('surface', 2)));
			} else if (coral.Produto.getValue('surface', 1) > 0) {
					SWFAddress.setValue(coral.Produto.setAddressPath('/surface/' + coral.Produto.getValue('surface', 1) + '/' + page));
			} else {
				SWFAddress.setValue(coral.Produto.setAddressPath(''));
			}
		break;

		case 'type':
			if (coral.Produto.getValue('type', 1) > 0) {
				SWFAddress.setValue(coral.Produto.setAddressPath('/type/' + coral.Produto.getValue('type', 1) + '/' + page));
			} else {
				SWFAddress.setValue(coral.Produto.setAddressPath(''));
			}
		break;

		case 'color':
			if (coral.Produto.getValue('color', 1) > 0) {
				var addr = '/color/' + coral.Produto.getValue('color', 1) + '/' + page;

				if (lastSensationCode) { addr += '/' + lastSensationCode; }
				if (lastColor) { addr += '/' + lastColor; }
				if (lastColorAnd) {
					if (lastColorAnd == 'surface') {
						addr += '/surface/' + (path[6] || $('#surfaces').val());
					} else if (lastColorAnd == 'type') {
						addr += '/type/' + (path[6] || $('#types').val());
					}
				}
				SWFAddress.setValue(coral.Produto.setAddressPath(addr));
			} else {
				SWFAddress.setValue(coral.Produto.setAddressPath(''));
			}
		break;
	}
}

coral.Produto.reset = function(pFilter) {
	if (pFilter != 'surface') {
		$('#surface').val(0);
		$('#surfaceCtd1').html('');
		$('#surfaceCtd2').html('');
		$('#surfaceCtd3').html('');
	}

	if (pFilter != 'tipo') {
			$('#tipo').val(0);
			$('#tipoCtd1').html('');
	}

	if (pFilter != 'cor') {
			$('#cor').val(0);
	}
}
