//  2009-12-04  EF      Workaround: add callback function registration to be executed when modal box is shown.
//                      This is necessary because of the use of the element.remove().appendTo() technique that has the
//                      collateral effect of removing all jQuery events attached to the element.
//  2009-12-04  EF      Use element.appendTo() without need to remove() first. Previous workaround becomes unnecessary.
var _modalTipsOnShowCallback = new Object();

function RegisterModalTipOnShowEvent(element, callbackFunction) {
    var jElement;
    if (typeof(element) == "string" && element.length > 0 && element.charAt(0) != "#") {
        //  Element ID
        jElement = $("#" + element);
    }
    else {
        //  Element
        jElement = $(element);
    }
    
    if (!jElement.hasClass(".modal_tip")) {
        //  Function is called by an element contained in the modal_tip window, search for the parent
        jElement = jElement.parent(".modal_tip");
    }
    
    if (jElement.length > 0) {
        if (!jElement.attr("id") || jElement.attr("id") == "") {
            throw "Spartan modal tips. RegisterModalTipOnShowEvent(). element must have an id";
        }

        _modalTipsOnShowCallback[jElement.attr("id")] = callbackFunction;
    }

}

// Clasificados.pr ui
$(function() {
    // Spartan Modal tips

    //  Store current selected box in order to move it outside it's container and restore it later (due to style inheritance issues)
    var searchBoxParentContainer = null;
    var activeSearchBox = null;

    $('ul.menu li').children().click(function(event) { event.stopPropagation(); });

    //  2009-09-22  EF      Use classes instead of names. Change behaviour not to manipulate html but to mov DIV position.
    $('ul.menu li').click(function() {
        HideSearchBoxes();

        activeSearchBox = $(this).find('.modal_tip');
        if (activeSearchBox != null) {
            if (activeSearchBox.find('.form_fields').length == 0) {
                //  Element does not have a search box ==> navigate directly to sub title page link (subportada)
                var navigationLink = $(this).find('a.modal_tip_sub_title_page');

                if (navigationLink.length > 0) {
                    document.location.href = $(this).find('a.modal_tip_sub_title_page').get(0).href;
                }
            }
            else {
                searchBoxParentContainer = activeSearchBox.parent();

                var offset = $(this).offset();
                var modalbox = activeSearchBox;
                //modalbox.css({ 'top': '0px', 'left': '0px' });

                //modalbox.remove().appendTo(".ic_inner_content_without_banners");
                modalbox.appendTo(".ic_inner_content_without_banners");

                if (modalbox.length > 0 && modalbox.attr("id") && modalbox.attr("id") != '' &&
                        _modalTipsOnShowCallback[modalbox.attr("id")]) {
                    _modalTipsOnShowCallback[modalbox.attr("id")]();
                }

                modalbox.css({ 'top': (offset.top + 37) + 'px', 'left': offset.left + 'px' });
                //modalbox.find('#modal_tip_content').html($(this).find('.modal').html());
                modalbox.show();
                modalbox.slideDown('fast');
            }
        }

    });

    function HideSearchBoxes() {
        if (activeSearchBox) {
            activeSearchBox.appendTo(searchBoxParentContainer);
            activeSearchBox = null;
            searchBoxParentContainer = null;
        }

        $('.modal_tip').fadeOut('fast');
    }

    $('.close_modal_tip').click( function(event) { event.preventDefault(); event.stopPropagation(); HideSearchBoxes(); });

    //    $('ul.menu li').click(function() {
    //        var modalcontent = $(this).find('.modal').html();
    //        if (modalcontent != null) {
    //            var offset = $(this).offset();
    //            var modalbox = $('#modal_tip');
    //            modalbox.css({ 'top': (offset.top + 37) + 'px', 'left': offset.left + 'px' });
    //            modalbox.find('#modal_tip_content').html($(this).find('.modal').html());
    //            modalbox.slideDown('fast');
    //        }
    //    });
    //    $('#close_modal_tip').click(function() { $('#modal_tip').fadeOut('fast'); });

    // Search Box
    var searchString = 'Escriba aqui su busqueda / Escriba aqui el clasicode';
    $('#big_input').bind('click', function() { if ($(this).val() == searchString) $(this).val(''); }).bind('blur', function() { if ($(this).val().length == 0) $(this).val(searchString); });

});
