// carregamento automático de funções //
$(document).ready(function(){
    
    $("#boxFabricantes").hide();
    
    $("#boxMarca").click(function () {
     
        if($('#boxFabricantes').css('display') == 'none') {
          $("#boxMarca").hide();
          $("#boxFabricantes").show();
        }
        else {
          $("#boxFabricantes").hide();
          $("#boxMarca").show();
        }                       
          
          
    });
    
    
    // controle sobre os campos de busca no topo da página
    $("#campo_busca").click(function () {
      $("#campo_busca").val('');
    });
    $("#campo_busca").blur(function () {
      if($("#campo_busca").val() == '')
        $("#campo_busca").val('Palavra-Chave');
    });       
    
}); 

function login(url){

  $.ajax({
		type: "POST",
		url: url+"capa/ajax/login",
		data: 'email='+$("#login_email").val()+'&senha='+$('#login_senha').val(),
		success: function(retorno) {
           
		  if(retorno != 0 && retorno != "" && retorno != 'undefined') {
		                          
        $("#div_logoff").show(); 
        $("#div_login").hide();
        
        // botoes de cadastro e logoff
        $("#btn_logoff").show();
        $("#btn_cadastro").hide();                 
        
        var arrString = retorno.split("|");
        
        var nome  = arrString[0];
        var email = arrString[1];
        
        var stringHtml = "<div class='left m10 w200'><strong>Olá, "+nome+"</strong><br /><strong>E-mail: "+email+"</strong></div><div class='right m10 w100'><a href='"+url+"pedidos' class='right'>MEUS PEDIDOS</a><a href='"+url+"meus_dados' class='right'>MEUS DADOS</a></div>";
        $("#div_logoff").html(stringHtml);
        
        var caminho = window.location.pathname;
        if(caminho.search("login") != -1)
           window.location = url+"capa/";        
		  }
		  else
		      alert('Dados incorretos. Tente novamente.');
          //window.location = url+"login/";
    }
	});	
	
}  

function logoff(url){
    
  $.ajax({
		type: "POST",
		url: url+"capa/ajax/logoff",
		data: '',
		success: function(retorno) {
		    
        $("#div_logoff").hide(); 
        $("#div_login").show();
        
        // botoes de cadastro e logoff 
        $("#btn_cadastro").show();
        $("#btn_logoff").hide();  
      
    }
		
	});	
	
}


function submitenter(e, url){

var keycode;

if (window.event) keycode = window.event.keyCode;
else 
  if (e) keycode = e.which;
  else
     return true;

if (keycode == 13)
   {
   //alert(e);
   login(url);
   return false;
   }
else
   return true;
}


function pressionaEnter(e, url, funcao, val){
    
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else 
        if (e) keycode = e.which;
        else
            return true;

    if (keycode == 13){
      //alert(e);
      funcao(url, val);
      return false;
    }
    else
        return true;
}   



  
// FUNÇÕES ABAIXO PARA GALERIA DE FOTOS - JCAROUSEL //
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        auto: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });
});   
