var submenu_timer = '';

$.ajaxProgress.setImage('/public/images/ajax-loader.gif');

$(function () {
    $('a.ext').attr('title', 'otvoriť v novom okne');
	$('a.ext').attr('target', '_blank');
	
	$('#mainmenu a').hover(function () {
        window.clearTimeout(submenu_timer);
        $('#submenu .submenu').hide();
        $('#submenu .subsubmenu').hide();
        var submenu_id = 'sbmn_' + $(this).attr('rel');
        $('#' + submenu_id).show();
    }, function () {
        showActiveMenu();
    });
    
    $('#submenu a').hover(function () {
        window.clearTimeout(submenu_timer);
        var subsubmenu_id = 'sbmn_' + $(this).attr('rel');
        $('#' + subsubmenu_id).show();
        $('.subsubmenu', $(this).parent().parent()).each(function (idx, el) {
            if ($(el).attr('id') != subsubmenu_id) {
                $(el).hide();
            }
        });
    }, function () {
        submenu_timer = window.setTimeout(function () {
            var subsubmenu_id = 'sbmn_' + $(this).attr('rel');
            $('#' + subsubmenu_id).hide();
        }, 500);
    });
    
    $('#submenu .subsubmenu a').hover(function () {
        window.clearTimeout(submenu_timer);
        //$(this).parent().parent().show();
    }, function () {
        submenu_timer = window.setTimeout(function () {
            $(this).parent().parent().hide();
            showActiveMenu();
        }, 500);
    });
    
    $('#submenu').hover(function () {
        window.clearTimeout(submenu_timer);
    }, function () {
        submenu_timer = window.setTimeout(function () {
            var subsubmenu_id = 'sbmn_' + $(this).attr('rel');
        $('#' + subsubmenu_id).hide();
        }, 500);
        showActiveMenu();
    });
});

// anketa
$(function () {
    $('div.poll a.poll-vote').click(function () {
        var vote = $(this);
        //var pollContainer = vote.parents('div.poll-container:eq(0)');
        var pollContainer = vote.parent().parent();
        var answer = vote.attr('rel');
        var pollId = pollContainer.attr('rel');
        
        // zakaze sa hlasovanie
        var disabler = $('<div></div>');
        disabler.width(pollContainer.width());
        disabler.height(pollContainer.height());
        disabler.addClass('poll-disabler');
        disabler.css({
            top: pollContainer.offset()['top'],
            left: pollContainer.offset()['left']
        });
        disabler.appendTo(pollContainer);
        $.getJSON('/ankety/vote/',{
            pollId: pollId,
            answer: answer
        }, function (voted) {
            if (voted.body) {
                for (var i = 0 ; i < voted.body.length ; i++) {
                    var pcn = 0;
                    if (i == voted.body.length - 1 && voted.total > 0) {
                        for (var j = 0 ; j < i ; j++) {
                            pcn += Math.round(voted.body[j] / voted.total * 100);
                        }
                        pcn = 100 - pcn;
                    } else if (voted.total > 0) {
                        pcn = Math.round(voted.body[i] / voted.total * 100);
                    }
                    $('div.answer:eq('+i+') div.pctg', pollContainer).css("width", pcn + '%');
                }
            }
            disabler.remove();
        });
    });
});

function showActiveMenu() {
    window.clearTimeout(submenu_timer);
    submenu_timer = window.setTimeout(function () {
        var active_id = '';
        $('#mainmenu li').each(function (idx, e) {
            if ($(e).is('.active')) {
                active_id = $('a', $(e)).attr('rel');
            }
        });
        $('#submenu .submenu').hide();
        if (active_id != '') {
            var submenu_id = 'sbmn_' + active_id;
            $('#' + submenu_id).show();
        }
    }, 500);
}

