//jQuery.noConflict();


fnCheckGender = function() {

    var fn = Functional;
    var generateGender = function(g) {
        var fn = "a -> jQuery(this).attr('type')=='" + g + "' ? this : null";
        return fn.lambda();
    }


    var isM = generateGender("M");
    var isF = generateGender("F");
    var isChecked = "a -> (this.checked==true) ? this : null".lambda();
    var getValue = "this.value".lambda()

    jQuery("div.check_gender").each(function() {
        var iCnt = 0;
        var chk = jQuery("input[type='checkbox']", this);
        chk.each(function() {
            jQuery(this).bind("click",
            function() {

                if (jQuery(chk).map(isChecked).size() == 0) {
                    this.checked = true;
                    return;
                }

                var fnMap = [];
                jQuery(chk).map(isChecked).each(function() {
                    if (this.value == "isM") fnMap.push(isM);
                    if (this.value == "isF") fnMap.push(isF);
                });


                jQuery("tr#expand_content").remove();
                jQuery('td span.open_button').removeClass('close_button');
                jQuery(".expandible_content tr[type]").hide().map(fn.or.apply(null, fnMap)).show();
            });
            iCnt++;
        });
    });

};




prepareOnLoad = function() {

    jQuery('.expandible_content tr:has(span.ended)').click(function() {


        /*
        if ($(this).attr("rel").split("-")[1]!="") {
           document.location="risultati.shtml?" + $(this).attr("rel").split("-")[0];
           return;
        }
*/

        var event_final = $(this).attr("final");
        var event_type = $(this).attr("data");


        jQuery('div#load_content').slideUp(1500);
        jQuery('td span.open_button').removeClass('close_button');
        jQuery('td span.open_button', this).addClass('close_button');

        jQuery('tr#expand_content').remove();
        jQuery(this).after('<tr id="expand_content" class="' + jQuery(this).attr('class') + '"><td style="padding:0" colspan="10"><div id="load_content"></div></td></tr>');

        var rel = jQuery(this).attr("rel").split("-");


        if (rel[1] == "") {
            rel[1] = rel[0];
        }


        jQuery.ajax({
            url: 'include/shared/mondiali/2009/calendar/result/' + rel[0] + '.shtml',
            dataType: 'html',
            success: function(data) {
                data = data.replace(/src="\(none\)/g, "src=\"" + base_href);
                data = data.replace(/href="\(none\)/gi, "href=\"" + base_href_link);

                jQuery('div#load_content').html((function() {

                    var sel = "div.result_container[rel='" + rel[1] + "'] div.result_content table:first";
                    var chkQualificato = jQuery(sel, data).find("tr").map(function() {
                        return (jQuery.trim(jQuery("td:eq(6)", this).html()) == "Qualificato") ? this: null
                    }).size() > 0 ? true: false;


                    if (event_final == "" && event_type != 'match' && chkQualificato == true) {


                        return jQuery(sel, data).find("tr").map(function() {
                            return (jQuery.trim(jQuery("td:eq(6)", this).html()) != "Qualificato") ? this: null
                        }).each(function() {
                            jQuery(this).css("display", "none");
                        }).end().end();

                    } else {


                        return jQuery(sel, data).find("tr").map(function(i) {
                            var img = $("img:first", this)[0].src;
                            var rg = /italia/gi;
                            return (rg.test(img) || i <= 2) ? null: this;
                        }).each(function() {
                            $(this).css("display", "none");
                        }).end().end();

                    }

                })());


                jQuery("div#load_content")
                .find("img")
                .map(function() {
                    return (/blank/gi).test(this.src) == true ? this: null
                })
                .remove()
                .end()
                .end()
                .find("td.result_detail a")
                .each(function() {
                
                  var href=$(this).attr('href').split("?");
                  href=href[0] + "?" + rel[0] + "#" + rel[1];
                  $(this).attr('href',href);
                });


                jQuery('div#load_content').slideDown(1500);

            }
        });

    });

    jQuery('#tabs').tabs();
    jQuery('#tabs-2').tabs();

    jQuery("img").map(function() {
        return (/blank/gi).test(this.src) == true ? this: null
    }).remove();


    jQuery(".stabs").bind("click click_handler",
    function() {
        var el = jQuery(this);

        var type = (el.attr("type") == "") ? "tabs": el.attr("type");

        el.parent().find(".tabs").removeClass(type + "_on").addClass(type + "_off");
        el.addClass(type + "_on");

        var subClass = el.find("span:first").attr("data");
        var lnk = el.find("span:first").attr("rel");

        jQuery("." + subClass).hide();
        jQuery("#" + lnk).show();



        jQuery("body").trigger("tabs/selected", {
            tab: el
        })

    });


    jQuery('tr.ico_container th span').each(function(i) {
        jQuery(this).attr('title', jQuery(this).html());
    });


    jQuery("div.cls_maglie").each(function() {
        var div_maglia = jQuery(this);

        jQuery("table.maglia", div_maglia).map(function(n, i) {
            return n > 0 ? this: null;
        }).hide();

        jQuery("span.maglia", div_maglia).each(function() {
            var maglia = jQuery(this);

            maglia.parents("li:first").bind("click",
            function() {
                jQuery("table.maglia", div_maglia).hide();
                jQuery("table.maglia", div_maglia).map(function() {
                    return jQuery(this).attr("rel") == maglia.attr("rel") ? this: null;
                }).show();
            });
        });
        jQuery("span.maglia:first", div_maglia).parents("li:first").triggerHandler("click");

    });

};

