function initMenu() {
  $('#menuCat ul').hide();
  $('#menuCat ul:first').show();
  $('#menuCat li span').click(
    function(e) {
        
      var contentDiv = $(this).next("ul");
      var checkElement = $(this).next();
      var relId = $(this).attr("rel");

        $.ajax({
            url: '/products/index/load-Sub-Categories/cat/'+relId+'',
            beforeSend: function(){
                $('#ajaxLoad'+relId).show();
            },
            success: function(data) {
              $(contentDiv).html(data);
              $('#ajaxLoad'+relId).fadeOut();
              if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
                return false;
                }
              if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
                $('#menuCat ul:visible').slideUp('normal');
                checkElement.slideDown('normal');
                return false;
                }
            }
        });
        
      }
    );
  }

$(document).ready(function() {

    // Init categories menu.
    initMenu();

    $("#chat_int").click(
        function() {
            $("#chat_list").load('/chat');
            $("#chat_list_box").slideToggle("slow");
        }
        );
    $("#chat_titulo").click(
        function() {
            $("#chat_list").load('/chat');
            $("#chat_list_box").slideToggle("slow");
        }
        );
        
    $('#jsddm > li').bind('mouseover', jsddm_open);
    $('#jsddm > li').bind('mouseout',  jsddm_timer);

    $(".kactoos-seguro").fancybox({
        'titlePosition': 'inside',
        'transitionOut': 'none',
        'padding': 0,
        'margin': 0,
        'centerOnScroll': 'TRUE',
        'scrolling':   'no'
    });
    
    // Testimonials Videos
    $(".videoTestimonio").fancybox({
        'padding'       : 10,
        'type':         'swf',
        'centerOnScroll': 'TRUE'
    });

    // Load Ajax
    $(".load_ajax").fancybox({
        ajax : {
            type	: "POST"
        }
    });

    $(".fancyframe").fancybox({
        'type': 'iframe',
        'scrolling': 'no',
        'height': '300'
    });


    $(".box").fancybox({
        'centerOnScroll': 'TRUE'
    });


    // Autocomplete for the search
    $("#searchBox").autocomplete("/products/index/search-products-autocomplete", {
        width: 240,
        max: 5,
        highlight: false,
        scroll: true,
        selectFirst: false,
        scrollHeight: 300,
        formatItem: function(data, i, n, value) {
            //return "<img src='/images/products/" + value.split("¬")[0] + "' width='20' alt=''  /><span>" + value.split("¬")[1] + "</span>";
            return "<a href='" + value.split("¬")[2] + "' ><img src='/libraries/thumb/?src=/images/products/" + value.split("¬")[0] + "&w=20' width='20' alt='Product'  /><span>" + value.split("¬")[1] + "</span></a>";
        },
        formatResult: function(data, value) {
            return value.split("¬")[1];
        }
    }).result( function(event, item ) {
        alert( item.split("¬")[2] );
           /*location.href = item.split("¬")[2];*/
        }) ;


    $(".fancy").tooltip({
        track: true,
        delay: 0,
        showURL: false,
        fixPNG: true,
        showBody: " - ",
        extraClass: "pretty fancy",
        top: -15,
        left: 5
    });

    $(".fancy2").tooltip({
        track: true,
        delay: 0,
        showURL: false,
        fixPNG: true,
        showBody: " - ",
        extraClass: "pretty2 fancy",
        top: -15,
        left: 5
    });


    // Validate checkbox registration_terms
    $('.val_check').click(function()
    {
        var registration_terms = $('#registration_terms').is(':checked');

        if (registration_terms == true)
        {
            $('#btnSave').attr('disabled', false);
            $('#btnSaveWeb').attr('disabled', false);
            $('#btnSave').fadeIn();
            $('#btnSaveWeb').fadeIn();
            $('#joinFlecha').hide();
        }
        else
        {
            //$('#btnSave').hide();
            //$('#btnSaveWeb').hide();
            $('#joinFlecha').fadeIn();
        }
    });


    //Desactivate the button JOIN in add.phtml when the user has click
    $('#btnSaveWeb').click(function() 
    {
        var registration_terms = $('#registration_terms').is(':checked');

        if (registration_terms == true)
        {
            var product_id = $(this).attr('rel');
            $(this).attr('disabled', 'disabled');
            //$(this).removeClass('orangeButtonBig');
            //$(this).addClass('grayButtonBig');
            $('#formJoin').submit();
            //window.location.href = '/products/index/join/id/'+product_id+'/';
        }
        else
        {
            $(this).attr('disabled', false);
            $(this).attr('disabled', false);
            $('#joinFlecha').hide();
            $('#joinFlecha').fadeIn();
        }

    });

    $('#errorsDiv').fadeIn('slow');

    $('.closefancy').click(function()
    {
        $.fancybox.close();
    });


    $('#newsForm').ajaxForm(function() {
                subscribeNewsLetter();
            });

    $('#newsFormSp').ajaxForm(function() {
                subscribeNewsLetterSp();
            });

    $(".emailNewsLetter").labelify({ text: function(input) { return "Ingresa tu email"; } });
});


function rollOverLB ( strImgName )
{
    // Turn off all of the images
    $("#step1").attr("src" , $("#step1").attr("src").replace("_on", "_off") );
    $("#step1").attr("style" , "opacity:.5");
    $("#step2").attr("src" , $("#step2").attr("src").replace("_on", "_off") );
    $("#step2").attr("style" , "opacity:.5");
    $("#step3").attr("src" , $("#step3").attr("src").replace("_on", "_off") );
    $("#step3").attr("style" , "opacity:.5");

    // Turn on the current image
    strNewSRC = $("#" + strImgName).attr("src").replace("_off", "_on");
    $("#" + strImgName).attr("src" , strNewSRC );
    $("#" + strImgName).attr("style" , "opacity:1");

    // Tur on the text
    strTextId = strImgName.replace("step", "");

    // Turn off all of the methods, and do it with all the possible ways
    $(".lbIntroText").stop( true, true ).clearQueue().hide();

    $("#lbStep" + strTextId).stop().show();
}

/**
 * Sets a cookie for the overlay stuff, this uses the jquery cookie library :)
 */
function setCookie ( strCookieName, strCookieValue )
{
    var date = new Date();
    date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
    $.cookie( strCookieName , strCookieValue, {
        path: '/',
        expires: date
    });
}


// Categories Menu
var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

function jsddm_open()
{
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}

function jsddm_close()
{
    if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer()
{
    closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{
    if(closetimer)

    {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

document.onclick = jsddm_close;

/*chat user list*/
var refreshId = setInterval(
    function()
    {
        //if (windowFocus != false) {
        $("#chat_list").load('/chat');
    //}
    }, 70000);

// ------------------------------


function subscribeNewsLetter(){
            $.post("/users/index/subscribe-news", $("#newsForm").serialize(),

            function(html)
            {
                //alert("¡Producto Agregado a tu lista de deseos!");
                $.blockUI({
                    message: '<h2 style="font-size: 16px; color: #FFF">'+html+'</h2>',
                    css: {
                        border: 'none',
                        padding: '15px',
                        'z-index': '99999',
                        backgroundColor: '#000',
                        '-webkit-border-radius': '10px',
                        '-moz-border-radius': '10px',
                        opacity: .5,
                        color: '#fff'
                    }});
                setTimeout($.unblockUI, 3000);
            });
     }


function subscribeNewsLetterSp(){
            $.post("/users/index/subscribe-news", $("#newsFormSp").serialize(),

            function(html)
            {
                //alert("¡Producto Agregado a tu lista de deseos!");
                $.blockUI({
                    message: '<h2 style="font-size: 16px; color: #FFF">'+html+'</h2>',
                    css: {
                        border: 'none',
                        padding: '15px',
                        'z-index': '99999',
                        backgroundColor: '#000',
                        '-webkit-border-radius': '10px',
                        '-moz-border-radius': '10px',
                        opacity: .5,
                        color: '#fff'
                    }});
                setTimeout($.unblockUI, 3000);
            });
     }
	 
	 
function botlink(url) {
	
		$.post(url, "",
		
            function(html)
            {
                //alert("¡Producto Agregado a tu lista de deseos!");
                $.blockUI({
                    message: '<h2 style="font-size: 16px; color: #FFF">'+html+'</h2>',
                    css: {
                        border: 'none',
                        padding: '15px',
                        'z-index': '99999',
                        backgroundColor: '#000',
                        '-webkit-border-radius': '10px',
                        '-moz-border-radius': '10px',
                        opacity: .5,
                        color: '#fff'
                    }});
                setTimeout($.unblockUI, 3000);
			});

}



    function pulsar(e) {
        tecla = (document.all) ? e.keyCode :e.which;
        return (tecla!=13);
    }

    function superGroupImg(img) {
        $("#productImg").attr("src" , "/images/products/"+img );
    }