$(document).ready(function() {
    $('.logos_representadas').cycle({
        fx:      'scrollRight',
        timeout: 3000,
        speed:   400,
        pause:   1
	});

    setMenuClick();
    setMenuCategoriaClick();
//    initMenu();
      initCatMenu();

    $('#formulario').ajaxForm({
        beforeSubmit: preSubmit,
        success: postSubmit
    });
});

function CargarContenido(link, dataType){
    var url = '../views/'+link;
    //alert(url);
    var $contenido = $('.main_content');
    $contenido.fadeOut('slow',function(){
    $('#left_menu_productos').hide();
    $.get(url,
    function(data) {
        $('.main').html(data, function(){
            $contenido.fadeIn('slow');
            setProductoClick();
            initCatMenu();
        });
        StopLoading();

    },
    dataType
    );
    });
   StartLoading();

}

function CargarProducto(link, dataType){
    //  alert(link);
    var $contenido = $('.producto');
        $contenido.fadeOut('slow',function(){
        $.get('../views/'+link,
        function(data) {
            $('.derecha_subcategoria').html(data, function(){
                $contenido.fadeIn('slow');
            });
            StopLoading();
        },
        dataType
        );
    });
   StartLoading();
}

function CargarCategoria(link, dataType){
    var url = '../views/'+link;
    //alert(url);
    var $contenido = $('.main_content');
    $contenido.fadeOut('slow',function(){
    $.get(url,
    function(data) {
        $('.main').html(data, function(){
            $contenido.fadeIn('slow');
            setProductoClick();
            initCatMenu();
        });
        StopLoading();
    },
    dataType
    );
    });
   StartLoading();
}

function StartLoading(){
//    var width = $(window).width();
//    var left = (width/2)-15;
//    $('#loading').css('left',left);
    $('#loading').fadeTo("fast",0.2);
    //alert('')
}
function StopLoading(){
    $('#loading').fadeOut("fast");
}

function setMenuClick(){
  $('a[name=menu]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var link = $(this).attr('rev');

        //Cargar contenido
        CargarContenido(link,'html');
    });
}

function setProductoClick(){
    $('a[name=producto]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var link = $(this).attr('href');

        //Cargar contenido
        CargarProducto(link,'html');
    });
}

function setMenuCategoriaClick(){
  $('a[name=menu_categoria]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var link = $(this).attr('rev');

        //Cargar contenido
        CargarCategoria(link,'html');
    });
}

function initMenu() {
  $('#menu ul').hide();
  //$('#menu ul:first').show();
  $('#menu li a').click(
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible')) && (checkElement.attr('class') == 'menu_ul')){
        //$('#menu ul:visible').slideUp('normal');
        return false;
        }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))&& (checkElement.attr('class') == 'menu_ul')) {
        $('#menu ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
        }
      }
    );
}

function initCatMenu() {
  $('#cat_menu ul').hide();
  //$('#menu ul:first').show();
  $('#cat_menu li a').click(
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        //$('#menu ul:visible').slideUp('normal');
        return false;
        }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#cat_menu ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
        }
      }
    );
  }

  function abrir_menu(id_categoria, id_subcategoria){
      $(document).ready(function() {
          $('#menu_productos').next().slideDown('normal');
          $('#cat_'+id_categoria).next().slideDown('normal');
          $('#subcat_'+id_subcategoria).next().slideDown('normal');
      });
  }


  function preSubmit(){
    StartLoading();
    if (typeof enPreSubmit === 'function')
        enPreSubmit();
    return true;
}

function postSubmit(responseText, statusText){
    $(".main").html("");
    $(".main").append(responseText);
    StopLoading();
    $('#formulario').ajaxForm({
        beforeSubmit: preSubmit,
        success: postSubmit
    });
    if (typeof enPostSubmit === 'function')
        enPostSubmit();
}

function agregarAlCarrito(id, nombre){
    if($('#carrito_vacio').length>0){
        $('#carrito_vacio').remove();
    }

    if (!$('#id_producto_'+id).length>0) {
        $('#productos_en_carrito').append('<div id = "id_producto_'+id+'">-&nbsp;<a href="productos.php?id_producto='+id+'">'+nombre+'</a>&nbsp;&nbsp; <a href="#" onclick="javascript:quitarDelCarrito(\''+id+'\');">(quitar)</a></div>');
        $.get('../gadgets/carrito.php',{
            accion: 'agregar', id: id, nombre: nombre
        });
    }
}

function quitarDelCarrito(id){
    $(('#id_producto_'+id)).remove();
    $.get('../gadgets/carrito.php',{
            accion: 'quitar', id: id
        });
    if ($('#productos_en_carrito div').size()==0){
        $('#productos_en_carrito').html('<span id="carrito_vacio"> <i> Seleccione los productos<br> que desee y envíe su <br>solicitud de cotización</i></span>');
    }
}

function vaciarCarrito(){
    $('#productos_en_carrito').html('<span id="carrito_vacio"> <i>Seleccione los productos <br>que desee y envíe su <br>solicitud de cotización</i></span>');
    $.get('../gadgets/carrito.php',{
            accion: 'vaciar'
        });
}
