jQuery(function() {

 // carregando
  var loader = jQuery('<div id="loader"><img src="img/load.gif" alt="carregando..." /></div>')
  	.css({position: "fixed", top: "50%", left: "50%", 'z-index': 20})
  	.appendTo("body")
  	.hide();

  $(document).ajaxStart(function() {
    	loader.show();
    }).ajaxStop(function() {
    	loader.hide();
    }).ajaxError(function(a, b, e) {
    	throw e;
    });


});

 function aviso(msg) {
  $('#aviso').html(msg);
  $('#aviso').slideDown('fast');
  setTimeout(function(){ $('#aviso').slideUp('fast') },8000);
 };

 function boxSelecao(acao,idproduto) {
    if ( typeof acao == 'undefined' ) { acao = ''; };
    if ( typeof idproduto == 'undefined' ) { idproduto = ''; };
    $.ajax({
      url: "_p_selecao.php?acao="+acao+"&idproduto="+idproduto+"&q="+$('#quantidade').val(),
      cache: false,
      dataType: "html",
      success: function(html){
        var ahtml = html.split("::",2);
        var msg = '';
        msg = ahtml[0];
        $('#carrinho_status').val(msg);
        if (ahtml[1] != "") { aviso(ahtml[1]); };
      }
    });
  };

$(function(){


  w = screen.availWidth;
  h = screen.availHeight;


  $("#bt_orcar").live('click',function(){
    boxSelecao('incluir',$(this).attr('vid'));
  });

  $("#esvaziar").live('click',function(){
    boxSelecao('esvaziar',0);
  });

  $(".p_s_remover").live('click',function(){
    boxSelecao('remover',$(this).attr('vid'));
    $(this).parent().parent().fadeOut();
  });

  $('#fornecedores').change(function(){
     window.location = "?menu=Produtos&submenu=Fornecedores&fornecedores="+$(this).val()+"&p=1";
  });

  $('#segmentos').change(function(){
     window.location = "?menu=Produtos&submenu=Segmentos&segmento="+$(this).val()+"&p=1";
  });

  $(".bt_video").live('click',function(){
    $("#cvideo").show();
    $.ajax({
      url: "videos.php?v="+$(this).attr('video'),
      cache: false,
      dataType: "html",
      success: function(html){
         $("#fvideo").html(html);
      }
    });

  });

   $("#c_pesquisa").focus(function(){
    if ($(this).val() == $(this).attr('nome')){
       $(this).val("");
     };
    this.select();
   });

   $("#c_pesquisa").blur(function(){
    if ($(this).val() == ''){
       $(this).val($(this).attr('nome'));
     };
   });

  $('.oculto').parent().click(function(){
    $(this).children(".oculto").toggle();
    $(this).toggleClass('fechado aberto');
  });

  $("#cvideo img").live('click',function(){
    $("#fvideo").html('');
    $("#cvideo").hide();
  });


  $(".i_menu").hover( function () {
    var vtamanho =  $(this).width()/2 -60;
    $(this).children(".submenu").css("margin-left",vtamanho);
    $(this).children(".submenu").show();
  }, function () {
    $(this).children(".submenu").hide();
  });

  $(".i_p_menu").hover( function () {
   // var vtamanho =  $(this).width()/2 -60;
   // $(this).children(".submenu").css("margin-left",vtamanho);
   var htamanho =  $(this).children(".p_submenu").height()/2 + 13;
   $(this).children(".p_submenu").css("margin-top",-htamanho);

    $(this).children(".p_submenu").show();
  }, function () {
    $(this).children(".p_submenu").hide();
  });


  $("#aviso").live('click',function(){
    $("#aviso").fadeOut();
  });





});
