jQuery.fn.initMenu = function() {
    return this.each(function() {
        $('.menuitem', this).hide();
        $('li.expand > .menuitem', this).show();
        $('li.expand > .menuitem', this).prev().addClass('active');
        $('li a', this).click(function(e) {
            e.stopImmediatePropagation();
            var $this = $(this);
            var theElement = $this.next();
            var parent = $this.parent().parent();
            if (parent.hasClass('noaccordion')) {
                if (theElement[0] === undefined) {
                    window.location.href = this.href;
                }
                theElement.slideToggle('normal', function() {
                    var slideThis = $(this);
                    if (slideThis.is(':visible')) {
                        slideThis.prev().addClass('active');
                    }
                    else {
                        slideThis.prev().removeClass('active');
                    }
                });
                return false;
            }
            else if ( theElement.hasClass('menuitem') ) {
                if (theElement.is(':visible')) {
                    if (parent.hasClass('collapsible')) {
                        $('.menuitem:visible', parent).slideUp('normal', function() {
                            $this.prev().removeClass('active');                        
                        });
                        return false;
                    }
                    return false;
                }
                else {
                    $('.menuitem:visible', parent).slideUp('normal', function() {
                        $this.prev().removeClass('active');
                    });
                    $('ul.menu ul.menuitem li:has(ul)').addClass('parentMenu');  // show li with child in different colour
                    theElement.slideDown('normal', function() {
                        $this.prev().addClass('active');
                    });
                    return false;
                }
            }
        });
    });
};

function removeAccents(string) {

    var value = string.replace(/^\s+|\s+$/g,"").toLowerCase();;

    value = value.replace(new RegExp("\\s+", 'g'),"-");
    value = value.replace(new RegExp("[àáâãäå]", 'g'),"a");
    value = value.replace(new RegExp("æ", 'g'),"ae");
    value = value.replace(new RegExp("ç", 'g'),"c");
    value = value.replace(new RegExp("[èéêë]", 'g'),"e");
    value = value.replace(new RegExp("[ìíîï]", 'g'),"i");
    value = value.replace(new RegExp("ñ", 'g'),"n");                            
    value = value.replace(new RegExp("[òóôõö]", 'g'),"o");
    value = value.replace(new RegExp("œ", 'g'),"oe");
    value = value.replace(new RegExp("[ùúûü]", 'g'),"u");
    value = value.replace(new RegExp("[ýÿ]", 'g'),"y");
    value = value.replace(new RegExp("\\W", 'g'),"-");

    return value;

};

jQuery.fn.watermark = function(c, t) {
    var e = function(e) {
        var i = $(this);
        if (!i.val()) {
            var w = t || i.attr('title'), $c = $($("<div />").append(i.clone()).html().replace(/type=\"?password\"?/, 'type="text"')).val(w).addClass(c);
            i.replaceWith($c);
            $c.focus(function() {
                $c.replaceWith(i); setTimeout(function() {i.focus();}, 1);
            })
            .change(function(e) {
                i.val($c.val()); $c.val(w); i.val() && $c.replaceWith(i);
            })
            .closest('form').submit(function() {
                $c.replaceWith(i);
            });
        }
    };
    return $(this).live('blur change', e).change();
};
    
$(document).ready(function() {    
    $('.watermarked').watermark('watermark');   
    $('.menu').initMenu();
    $('#buscador').submit(function() {             
        var string = $('#termino').val();    
        var cadena = removeAccents(string);        
        document.location = 'http://www.labotigamusical.com/catalogo/'+cadena;        
        return false;
    });
});
