﻿/**
 * Vars
 */
var Topo = {};

/**
 * Init
 */
Topo.init = function () {
	$(".menu-topo-opcoes-itm").click(Topo.menuParentClick);
	
	// Função criada para trocar o texto do campo busca que esta localizado no topo do site.
	$("#txt_topo_busca, .busca-text")
	.blur(function(){
		if ($(this).val() == "")
		{
			$(this).val(this.defaultValue).css("color","#999")
		}
	})
	.focus(function(){
		if ($(this).val() == this.defaultValue)	{
			$(this).val("").css("color","#666");
		}
	})

	$("#id_c_t, .busca-select").jdrop();
	
	$("#btn_topo_buscar, .busca-send").click(function() {
		var txt = $(this).parents('.busca-area:first').find('.busca-text').val(),
		cat = $(this).parents('.busca-area:first').find('.busca-select').val(),
		loc = "lista_produto.php?is=1&tx_busca="+txt;
		if (cat != undefined) loc += '&id_c='+cat;
		if (txt == 'Digite o que procura') {
			alert("Digite sua busca!");	
			$(this).parents('.busca-area:first').find('.busca-text').focus();
			return false;
		}
		location.href = loc;
	});
}
$(Topo.init);

Topo.menuParentClick = function (e) {
	var _this = $(this),
	cod = _this.attr("rel");
	
	var id_anterior_sel = $(".cx_opcoes_menu:visible").attr("id");
	if (id_anterior_sel != null) {
		$("#" + id_anterior_sel).slideUp(500);
		var cod_cat_anterior_sel = parseInt(id_anterior_sel.substr(14,1));
		$("#img_menu_cat_0"+cod_cat_anterior_sel).attr("src","img/menu/desativado/img_menu_cat_0"+cod_cat_anterior_sel+".gif");
	}
	
	if (cod_cat_anterior_sel != cod) {
		var _boxArea = $("#box_menu_cat_0" + cod),
		_listaArea = _boxArea.find('.menu-opcoes-lista');
		_this.find('img').attr("src","img/menu/ativado/img_menu_cat_0" + cod + ".gif");
		var url = (typeof(BASE_URL) != 'undefined') ? BASE_URL+"/" : '';
		url += 'e-commerce/actions/topo.php?act=' + cod;
		
		obtemDados();
	}
	
	// Obtem Dados do Banco
	function obtemDados () {
		if (_listaArea.html() == '') {
			$.getJSON(
				url,
				function (json) {
					var list_html = '';
					
					$.map(json, function (citem) {
						if (citem.cclear_i == true) list_html += '<div class="op_menu_cat_col">';
						
						list_html += "<li>\n";
							list_html += '<a href="' + citem.clink + '">';
								list_html += citem.descricao;
							list_html += "</a>\n";
						list_html += "</li>";

						if (citem.cclear_f == true) list_html += '</div>';
					});

					_listaArea.html(list_html);
					_boxArea.hide().slideDown(500);
				}
			);
		} else {
			_boxArea.hide().slideDown(500);
		}
	}
	
	e.preventDefault();
}
