/**
 * @author Vizua
 */
$(document).ready(function(){

    waiting = new Image();
    waiting.src = base + "img/waiting.gif";
	
	var season = $('ul#leagues select');
	season.change(function() {
		target = base + 'home/load_season/'+$(this).val()
		window.location.href = target;
	});
    
    $.address.externalChange(function(event){
        if (event.value != "/" && event.value != "") {
            if (event.value.search('index') != -1) {
                var address = event.value.replace('/league/index', '');
                $('#content').load(base + 'league/index/' + address);
				$('title').html ('MÚMF - Mestská únia malého futbalu Banská Bystrica - ligy');
            }
            if (event.value.search('table') != -1) {
                var address = event.value.replace('/league/table', '');
                $('#content').load(base + 'league/table/' + address);
				$('title').html ('MÚMF - Mestská únia malého futbalu Banská Bystrica - ligy');
            }
            if (event.value.search('teams') != -1) {
                var address = event.value.replace('/league/teams', '');
                $('#content').load(base + 'league/teams/' + address);
				$('title').html ('MÚMF - Mestská únia malého futbalu Banská Bystrica - ligy');
            }
            if (event.value.search('statistics') != -1) {
                var address = event.value.replace('/league/statistics', '');
                $('#content').load(base + 'league/statistics/' + address);
				$('title').html ('MÚMF - Mestská únia malého futbalu Banská Bystrica - ligy');
            }
			if (event.value.search('results') != -1) {
                var address = event.value.replace('/league/results', '');
                $('#content').load(base + 'league/results/' + address);
				$('title').html ('MÚMF - Mestská únia malého futbalu Banská Bystrica - ligy');
            }
        }
    });
    
    $('marquee').marquee('marquee').mouseover(function(){
        $(this).trigger('stop');
    }).mouseout(function(){
        $(this).trigger('start');
    });
    
    $('ul#nav').superfish({
        delay: 100, // one second delay on mouseout 
        speed: 'fast',
        dropShadows: false // faster animation speed 
    });
    
    $('ul#footnav').superfish({
        delay: 100, // one second delay on mouseout 
        speed: 'fast',
        dropShadows: false, // faster animation speed 
        autoArrows: false
    });
    
    var icons = {
        header: "icon_league_off",
        headerSelected: "icon_league_on"
    };
    
    $('#leagues').accordion({
        header: 'li.active a',
        active: false,
        collapsible: true
    });
    
    $('a.league').click(function(){
        $('li.active a img.arrow').attr('src', base + 'img/arrow_league_off.png');
        $('li.active').removeClass('opened');
        $('li.active').addClass('closed');
        var img = $(this).find('img.arrow');
        if ($(this).hasClass('ui-state-active')) {
            //arrow
            $(this).parent().removeClass('closed');
            $(this).parent().addClass('opened');
            img.attr('src', base + 'img/arrow_league_on.png');
            //load
            var idLeague = $(this).attr('id');
            $('#content').html('<div id="loader"><img src="' + base + 'img/waiting.gif" /></div>');
            $('#content').load(base + 'league/index/' + idLeague);
            $.address.value('/league/index/' + idLeague);
			$('title').html ('MÚMF - Mestská únia malého futbalu Banská Bystrica - ligy');
        }
        else {
            $(this).parent().removeClass('opened');
            $(this).parent().addClass('closed');
        }
    });
    
    $('ul.submenu li.table').click(function(){
        var idLeague = $(this).parent().attr('id');
        $('#content').html('<div id="loader"><img src="' + base + 'img/waiting.gif" /></div>');
        $('#content').load(base + 'league/table/' + idLeague);
        $.address.value('/league/table/' + idLeague);
		$('title').html ('MÚMF - Mestská únia malého futbalu Banská Bystrica - ligy');
        return false;
    });
    
    $('ul.submenu li.teams').click(function(){
        var idLeague = $(this).parent().attr('id');
        $('#content').html('<div id="loader"><img src="' + base + 'img/waiting.gif" /></div>');
        $('#content').load(base + 'league/teams/' + idLeague);
        $.address.value('/league/teams/' + idLeague);
		$('title').html ('MÚMF - Mestská únia malého futbalu Banská Bystrica - ligy');
        return false;
    });
    
    $('ul.submenu li.results').click(function(){
        var idLeague = $(this).parent().attr('id');
        $('#content').html('<div id="loader"><img src="' + base + 'img/waiting.gif" /></div>');
        $('#content').load(base + 'league/results/' + idLeague);
        $.address.value('/league/results/' + idLeague);
		$('title').html ('MÚMF - Mestská únia malého futbalu Banská Bystrica - ligy');
        return false;
    });
    
    $('ul.submenu li.statistics').click(function(){
        var idLeague = $(this).parent().attr('id');
        $('#content').html('<div id="loader"><img src="' + base + 'img/waiting.gif" /></div>');
        $('#content').load(base + 'league/statistics/' + idLeague);
        $.address.value('/league/statistics/' + idLeague);
		$('title').html ('MÚMF - Mestská únia malého futbalu Banská Bystrica - ligy');
        return false;
    });
    
    $('ul.submenu li:even').css('background-color', '#2f2f2f');
    $('ul.submenu li:even').hover(function(){
        $(this).css('background-color', '#707070');
    }, function(){
        $(this).css('background-color', '#2f2f2f');
    });
    
    $('#header').click(function(){
        window.location = base + 'home';
    });
    
    
    
    
    $("#pollAjaxLoader").hide(); //hide the ajax loader
    $("#pollMessage").hide(); //hide the ajax loader
    $("#pollSubmit").click(function(){
        var pollAnswerVal = $('input:radio[name=pollAnswerID]:checked').val();//Getting the value of a selected radio element.
        if ($('input:radio[name=pollAnswerID]:checked').length) {
            $("#pollAjaxLoader").show(); //show the ajax loader
            $.ajax({
                type: "POST",
                url: "home/vote",
                data: {
                    pollAnswerID: pollAnswerVal,
                    action: "vote"
                },
                success: function(theResponse){
                    //the functions.php returns a response like "1|13|#ffcc00-2|32|#00ff00-3|18|#cc0000-63" which the first number is the answerID, second is the points it has and third is the color for that answer's graph. The last number is the sum of all points for easilt calculating percentages.
                    if (theResponse == "voted") {
                        $("#pollAjaxLoader").hide(); //hide the ajax loader
                        $("#pollMessage").html("Už ste hlasovali.").fadeTo("slow", 1);
                    }
                    else {
                        var numberOfAnswers = (theResponse).split("-").length - 2;//calculate the number of answers
                        var splittedResponse = (theResponse).split("-");
                        var pollAnswerTotalPoints = splittedResponse[numberOfAnswers + 1];
                        
                        for (i = 0; i <= numberOfAnswers; i++) {
                            var splittedAnswer = (splittedResponse[i]).split("|");
                            var pollAnswerID = (splittedAnswer[0]);
                            var pollAnswerPoints = (splittedAnswer[1]);
                            var pollAnswerColor = (splittedAnswer[2]);
                            var pollPercentage = (100 * pollAnswerPoints / pollAnswerTotalPoints);
                            $(".pollChart" + pollAnswerID).css("background-color", pollAnswerColor);
                            $(".pollChart" + pollAnswerID).animate({
                                width: pollPercentage + "%"
                            });
                            var hlas = '';
                            switch (pollAnswerPoints) {
                                case '1':
                                    hlas = 'hlas';
                                    break;
                                case '2':
                                    hlas = 'hlasy';
                                    break;
                                case '3':
                                    hlas = 'hlasy';
                                    break;
                                case '4':
                                    hlas = 'hlasy';
                                    break;
                                default:
                                    hlas = 'hlasov';
                            }
                            
                            $("#pollAnswer" + pollAnswerID).html(" (" + Math.round(pollPercentage) + "% - " + pollAnswerPoints + " " + hlas + ")");
                            $("#pollRadioButton" + pollAnswerID).attr("disabled", "disabled"); //disable the radio buttons
                        }
                        $("#pollAjaxLoader").hide(); //hide the ajax loader again
                        $("#pollSubmit").attr("disabled", "disabled"); //disable the submit button
                    }
                }
            });
            return false;
        }
        else {
            $("#pollMessage").html("Vyberte jednu možnosť.").fadeTo("slow", 1, function(){
                setTimeout(function(){
                    $("#pollMessage").fadeOut("slow");
                }, 3000);
            });
            return false;
        }
        
    });
    
    theResponse = pollResults;
    var numberOfAnswers = (theResponse).split("-").length - 2;//calculate the number of answers
    var splittedResponse = (theResponse).split("-");
    var pollAnswerTotalPoints = splittedResponse[numberOfAnswers + 1];
    
    for (i = 0; i <= numberOfAnswers; i++) {
        var splittedAnswer = (splittedResponse[i]).split("|");
        var pollAnswerID = (splittedAnswer[0]);
        var pollAnswerPoints = (splittedAnswer[1]);
        var pollAnswerColor = (splittedAnswer[2]);
        var pollPercentage = (100 * pollAnswerPoints / pollAnswerTotalPoints);
        $(".pollChart" + pollAnswerID).css("background-color", pollAnswerColor);
        $(".pollChart" + pollAnswerID).animate({
            width: pollPercentage + "%"
        });
        var hlas = '';
        switch (pollAnswerPoints) {
            case '1':
                hlas = 'hlas';
                break;
            case '2':
                hlas = 'hlasy';
                break;
            case '3':
                hlas = 'hlasy';
                break;
            case '4':
                hlas = 'hlasy';
                break;
            default:
                hlas = 'hlasov';
        }
        
        $("#pollAnswer" + pollAnswerID).html(" (" + Math.round(pollPercentage) + "% - " + pollAnswerPoints + " " + hlas + ")");
    }
    $("#pollAjaxLoader").hide(); //hide the ajax loader again
});

