﻿function getTextExtractor() {
    return (function() {
        var patternLetters = /[áéíóöőúüűÁÉÍÓÖŐÚÜŰ]/g;
        var lookupLetters = {
            "á": "a", "é": "e", "í": "i",
            "ó": "o", "ö": "o", "ő": "o",
            "ú": "u", "ü": "u", "ű": "u",
            "Á": "A", "É": "E", "Í": "I",
            "Ó": "O", "Ö": "O", "Ő": "O",
            "Ú": "U", "Ü": "U", "Ű": "U"
        };
        var letterTranslator = function(match) { 
            return lookupLetters[match] || match;
        }

        return function(node) {
            var text = $.trim($(node).text());
            
            return text.replace(patternLetters, letterTranslator);
        }
    })();
}

/* Setup Nivo Slider */
function initSlider() {
    $(".nivoSlider").nivoSlider({
        effect: "fold", //Specify sets like: 'fold,fade,sliceDown,random'
        slices: 10,
        animSpeed: 300,
        pauseTime: 5000,
        startSlide: 0, //Set starting Slide (0 index)
        directionNav: false, //Next & Prev
        directionNavHide: true, //Only show on hover
        controlNav: true, //1,2,3...
        keyboardNav: false, //Use left & right arrows
        pauseOnHover: true, //Stop animation while hovering
        manualAdvance: false, //Force manual transitions
        captionOpacity: 1, //Universal caption opacity
        beforeChange: function(){},
        afterChange: function(){},
        slideshowEnd: function(){} //Triggers after all slides have been shown
    });
}

function hideSlider() {
    $(document).ready(function () {
        $(".nivoSlider").hide();
        $(".main").css('margin-top', 0);
    });
}

function initMatcher() {
    /* Macther loader */
    $("div.content.matches img.loader").hide();
    
    /* Matcher ajax stepping */
    $("#matcher button.prev").live("click", function () {
        $("div.matches img.loader").stop(true, true).animate({opacity: "show"}, "fast");
        
        var param = "matcher.php?stepping=prev&id=" + $("#matcher").attr("class");
        
        $("div.content.matches").load(param, function () {
            $("img.loader", this).stop(true, true).animate({opacity: "hide"}, "slow");
        });
    });
    
    $("#matcher button.next").live("click", function () {
        $("div.matches img.loader").stop(true, true).animate({opacity: "show"}, "fast");
        
        var param = "matcher.php?stepping=next&id=" + $("#matcher").attr("class");
        
        $("div.content.matches").load(param, function () {
            $("img.loader", this).stop(true, true).animate({opacity: "hide"}, "slow");
        });
    });
}

/* Set up matches title display - if too long, then replace a marquee */
function handleTooLongMatchInfo() {
    $("#matcher li.title").each(function () {
        if ($("span", this).width() > $(this).width()) {
            $("span", this).replaceWith($('<marquee scrollamount="1" behavior="alternate" />').text($("span", this).text()));
        }
    });
}

function showSelectedPlayer(pid) {
    $(document).ready(function () {
        if (pid != null) {
            $("#p" + pid + " .playerName").trigger("click");
            $('html,body').animate({scrollTop: $("#p" + pid).offset().top},'slow');
        }
    });
}

function init() {
    /* Set up slider */
    initSlider();
    
    /* Newsletter registration form */
    var nlhintname = "Név";
    var nlhintemail = "Email";
    
    $(".header .info #mce-NAME").val(nlhintname);
    
    $(".header .info #mce-NAME").focus(function () {
        if ($(this).val() == nlhintname)
            $(this).val("");
    }).blur(function () {
        if ($(this).val() == "")
            $(this).val(nlhintname);
    });
    
    $(".header .info #mce-EMAIL").val(nlhintemail);
    
    $(".header .info #mce-EMAIL").focus(function () {
        if ($(this).val() == nlhintemail)
            $(this).val("");
    }).blur(function () {
        if ($(this).val() == "")
            $(this).val(nlhintemail);
    });
    
    /* Dropdown menu */
    var config = {
        sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
        interval: 200, // number = milliseconds for onMouseOver polling interval    
        over: function () { // function = onMouseOver callback (REQUIRED)    
            $("ul", this).animate({height: 'show'}, 'fast', function () {
                if (!$(this).parent().hasClass("hover")) {
                    $(this).hide();
                }
            });
        }, 
        timeout: 1, // number = milliseconds delay before onMouseOut    
        out: function() { // function = onMouseOut callback (REQUIRED)    
            $("ul", this).hide();
        }
    };

    /* Dropdown menu (cont I.) */
    $(".header .menu .sub").hoverIntent(config);        

    /* Dropdown menu (cont II.) */
    $(".header .menu .sub").hover(function() {
        $(this).addClass("hover");
    },function() {
        $(this).removeClass("hover");
    });
    
    /* Switch team */
    $(".dropdown.team a").each(function () {
        if ($(this).attr("class") != sessionteam) {
            $(this).click(function () {
                $.post("teamswitch.php", {"team" : $(this).attr("class")}, function () {
                    location.reload();
                });
            });
        } else {
            $(this).addClass("active").click(function () {
                $(".dropdown.team a").not(".active").fadeToggle("fast");
            });
        }
    });
    $(".dropdown.team").animate({opacity: "show"}, "slow");
    
    /* Hide all items of dropdown lists except active */
    $(".dropdown a").not(".active").hide();
    
    /* Box toggle */
    $("div.dropdown").click(function() {
        $(".content", $(this).parent()).slideToggle("fast");
    });
    
    /* Hide article content, except the first */
    $("div.box div.content > ul > li div.econtent").hide();
    $("div.box div.content > ul > li:first-child div.econtent").show();
    
    $("div.box div.content > ul > li div.eauthor a.button").hide();
    $("div.box div.content > ul > li:first-child div.eauthor a.button").show();
    
    /* Article box toggle and only one seeing */
    $("div.main div.box div.content div.etitle").click(function() {
        if ($("div.econtent", $(this).parent()).is(":hidden")) {
            $("div.econtent", $(this).parent().parent()).hide();
            $("div.econtent", $(this).parent()).animate({opacity: "show"}, "fast");
        }
    });
    
    /* Only one details button seeing on the article box */
    $("div.main div.box div.content > ul > li").hover(function () {
        if ($("div.eauthor a.button", $(this)).is(":hidden")) {
            $("div.eauthor a.button", $(this).parent()).hide();
            $("div.eauthor a.button", $(this)).animate({opacity: "show"}, "fast");
        }
    });
    
    /* Hide player content, except the first */
    $("div.box div.content > ul > li .playerDetails").hide();
    $("div.box div.content > ul > li:first-child .playerName").hide();
    $("div.box div.content > ul > li:first-child .playerDetails").show();
    
    /* Player box toggle and only one seeing */
    $("div.main div.box div.content .playerName").click(function() {
        if ($(".playerDetails", $(this).parent()).is(":hidden")) {
            $(".playerDetails", $(this).parent().parent()).hide();
            $(".playerDetails", $(this).parent()).animate({opacity: "show"}, "fast");
            $(".playerName", $(this).parent().parent()).show();
            $(this).hide();
        }
    });
    
    /* Social icon hover */
    $(".icon a").hover(function () {
        var src = $("img", this).attr("src").replace(".png", "_over.png");
        $("img", this).attr("src", src);
    }, function () {
        var src = $("img", this).attr("src").replace("_over.png", ".png");
        $("img", this).attr("src", src);
    });
    
    /* Replaces hash mark to void function */
    $('a[href="#"]').click(function(event){
        event.preventDefault();
    });

    /* Fancybox for single item */
    $(".fancybox.simple").fancybox({
        'overlayColor': '#444'
    });
    
    /* Fancybox for single item */
    $(".fancybox.iframe").fancybox({
        "width": "80%",
        "height": "80%",
        "overlayShow": false
    });

    /* Fancybox for multiple items */
    $(".fancybox.multi").fancybox({
        'overlayColor': '#444',
        "transitionIn": "elastic",
        "transitionOut": "elastic",
        "speedIn": 600,
        "speedOut": 200,
        "hideOnContentClick": true
    });

    /* Fancybox popup start on page load */
    $(".fancybox.simple.starter").trigger("click");
    
    /* Statistic table sorter */
    $("table.stat").tablesorter({
        sortList: [[4,1]],
        textExtraction: getTextExtractor()
    });
    
    /* Statistic table sorter */
    $("table.matches").tablesorter({
        sortList: [[0,0]],
        textExtraction: getTextExtractor()
    });
}
