(function($) {
    $.fn.thumbPopup = function(options)
    {
        //Combine the passed in options with the default settings
        settings = jQuery.extend({
            popupId: "thumbPopup",
            popupCSS: {'border': '1px solid #000066', 'background': '#FFFFFF', 'padding':'5px 5px 1px 5px'},
            imgSmallFlag: "_t",
            imgLargeFlag: "_l",
            cursorTopOffset: 1,
            cursorLeftOffset: 1,
            loadingHtml: "..."
        }, options);

        //Create our popup element
        popup =
        $("<div />")
        .css(settings.popupCSS)
        .attr("id", settings.popupId)
        .css("position", "absolute")
        .appendTo("body").hide();

        //Attach hover events that manage the popup
        $(this)
        .hover(setPopup)
        .mousemove(updatePopupPosition)
        .mouseout(hidePopup);

        function setPopup(event)
        {
            var fullImgURL = $(this).attr("src").replace(settings.imgSmallFlag, settings.imgLargeFlag);

            $(this).data("hovered", true);

            //Load full image in popup
            $("<img />")
            .bind("load", {thumbImage: this}, function(event)
            {
                //Only display the larger image if the thumbnail is still being hovered
                if ($(event.data.thumbImage).data("hovered") == true) {
                    $(popup).empty().append(this);
                    updatePopupPosition(event);
                    $(popup).show();
                }
                $(event.data.thumbImage).data("cached", true);
            })
            .attr("src", fullImgURL);

            //If no image has been loaded yet then place a loading message
            if ($(this).data("cached") != true) {
                $(popup).append($(settings.loadingHtml));
                $(popup).show();
            }

            updatePopupPosition(event);            
        }

        function updatePopupPosition(event)
        {
            var windowSize = getWindowSize();
            var popupSize = getPopupSize();
            if (windowSize.width + windowSize.scrollLeft < event.pageX + popupSize.width + settings.cursorLeftOffset){
                $(popup).css("left", event.pageX - popupSize.width - settings.cursorLeftOffset);
            } else {
                $(popup).css("left", event.pageX + settings.cursorLeftOffset);
            }
            if (windowSize.height + windowSize.scrollTop < event.pageY + popupSize.height + settings.cursorTopOffset){
                $(popup).css("top", event.pageY - popupSize.height - settings.cursorTopOffset);
            } else {
                $(popup).css("top", event.pageY + settings.cursorTopOffset);
            }
        }

        function hidePopup(event)
        {
            $(this).data("hovered", false);
            $(popup).empty().hide();
        }

        function getWindowSize() {
            return {
                scrollLeft: $(window).scrollLeft(),
                scrollTop: $(window).scrollTop(),
                width: $(window).width(),
                height: $(window).height()
            };
        }

        function getPopupSize() {
            return {
                width: $(popup).width(),
                height: $(popup).height()
            };
        }

        //Return original selection for chaining
        return this;
    };
})(jQuery);
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;
                }
            }
        });
    });
};

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();
};

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;

};

$(document).ready(function() {    
    $('#buscador').submit(function() {             
        var string = $('#termino').val();    
        var cadena = removeAccents(string);        
        document.location = 'http://www.electroalmacen.es/catalogo/'+cadena;        
        return false;
    });
    $('.menu').initMenu();
    $('.watermarked').watermark('watermark');           
});
