/* AJAX ERROR */
$(document).ajaxError(function (request,settings,e) {
    alert('Error requesting URL: '+e.url);
});
/* URL ROUTER */
var Router = function (route,params) {
    //parametre
    if (typeof(params) == 'object') {
        var p = '';
        $.each(params,function (name,value) {
            if (p != '') {
                p += '&';
            }
            p += escape(name)+'='+escape(value);
        });
        return Router(route)+'?'+p;
    }
    else {
        return '/' + route;
    }
};
Router.route = function (route,params) {
    var url = Router(route,params);
    location.href = url;
}

/* Plugin na input hint */
jQuery.fn.inputHint = function () {
    this.each(function () {
        var self = $(this);
        if (self.is('input[type=text]')) {
            jQuery.inputHintShow(self);
            self.focus(function () {
                jQuery.inputHintHide(this);
            }).blur(function () {
                jQuery.inputHintShow(this);
            }).closest('form').submit(function () {
                jQuery.inputHintHide(self);
                return true;
            });
        }
    });
    return this;
};
jQuery.inputHintShow = function (inpt) {
    inpt = jQuery(inpt);
    if (inpt.val() == inpt.attr('title') || inpt.val() == '') {
        inpt.addClass('hint').val(inpt.attr('title'));
    }
}
jQuery.inputHintHide = function (inpt) {
    inpt = jQuery(inpt);
    inpt.removeClass('hint');
    if (inpt.val() == inpt.attr('title')) {
        inpt.val('');
    }
}

$(function(){
    //hinty vo formularoch
    $('input.hint').inputHint();
	
    //horne menu
    $('#top-menu li').hover(function () {
        $(this).addClass('hover');
    }, function () {
        $(this).removeClass('hover');
    });
    //kvoli ipadu spravime horne polozky menu neklikatelne
    $('#top-menu a.top-expandable').click(function () {
        $(this).closest('li').toggleClass('clicked');
        return false;
    });
    
    //zrusime css nastavenia, ktore su potrebne pre polospravne fungovanie stranky bez javascriptu
    if ($('#bgbox, #bgboxFull').length) {
        $('#textbox, #bottom-panel').css({
            'position': 'absolute',
            'min-width': '960px'
        });
    }
    
    //zvacsenie / zmensenie obsahu
    $textboxPanel = $('#textbox-folded-panel');
    if ($textboxPanel.length) {
        var $textboxContent = $('#textbox-folded-content');
        var $textboxText = $textboxPanel.find('div.content-text');
        var $textboxTextPanel = $textboxPanel.find('div.content-panel');
        var $textboxOpener = $textboxPanel.find('a.opener');
        function textboxOpen() {
            $textboxTextPanel.show();
            $textboxOpener.removeClass('open').addClass('close');
            //animacia rozbalenia
            $textboxPanel.animate({
                width: 600
            }, {
                duration: 500,
                complete: function () {
                    var availH = $(window).height() - $('#nav-panel').height() - $('#bottom-panel').height() - 28 - $textboxPanel.data('content-h-delta');
                    var h = $textboxPanel.data('folded-h') + $textboxText.outerHeight() + 24; //24 - margin textboxu
                    //odpocitame paddingy
                    if (availH < h) {
                        boxH = availH;
                        $textboxTextPanel.height(boxH - $textboxPanel.data('folded-h') - $textboxPanel.data('content-h-delta') / 2);
                        $textboxTextPanel.css('overflow', 'auto');
                    } else {
                        boxH = h;
                        $textboxTextPanel.height('auto');
                        $textboxTextPanel.css('overflow', 'show');
                    }
                    $textboxContent.animate({
                        height: boxH
                    }, {
                        duration: 500
                    });
                }
            });
        }
        function textboxClose() {
            $textboxOpener.removeClass('close').addClass('open');
            //animacia zbalenia
            $textboxContent.animate({
                height: $textboxPanel.data('folded-h')
            }, {
                duration: 500,
                complete: function () {
                    $textboxTextPanel.hide();
                    $textboxTextPanel.height('auto');
                    $textboxPanel.animate({
                        width: $textboxPanel.data('folded-w')
                    }, {
                        duration: 500
                    });
                }
            });
        }
        $textboxOpener.click(function () {
            if ($(this).is('.open')) {
                //nastavime do data aktualnu sirku a vysku, aby sme vedeli, kam sa vratit
                if (!$textboxPanel.data('folded-w')) {
                    $textboxContent.height($textboxContent.height());
                    $textboxPanel.data('folded-w', $textboxPanel.width());
                    $textboxPanel.data('folded-h', $textboxContent.height());
                    $textboxPanel.data('content-h-delta', $textboxContent.outerHeight() - $textboxPanel.data('folded-h'));
                }
                textboxOpen();
            } else {
                textboxClose();
            }
        });
    }
    
    //shortcuts animation
    $shortcuts = $('#shortcuts li');
    if ($shortcuts.length) {
        $shortcuts.css('cursor', 'pointer').click(function () {
            $(this).find('a').trigger('click');
        });
        $shortcuts.on('mouseenter.shortcut', function () {
            var self = $(this);
            if (self.is('.active')) return;
            var a = self.find('a');
            self.addClass('hover');
            self.find('.icon').fadeIn();
            //ulozime si povodnu polohu
            if (!a.data('toppos')) {
                a.data('toppos', a.css('top'));
            }
            a.stop().animate({
                top: 0
            },{
                duration: 300
            });
        }).on('mouseleave.shortcut', function () {
            var self = $(this);
            if (self.is('.active')) return;
            var a = self.find('a'); 
            a.stop(true, true).animate({
                top: a.data('toppos')
            },{
                duration: 300,
                complete: function () {
                    self.removeClass('hover');
                    self.find('.icon').fadeOut();
                }
            });
        });
    }
    
    //popup titles
    //nastavime title do data a zrusime title, aby sme zabranili browseru zobrazovanie titlu
    $('#textbox a.switch, #shortcuts li').each(function () {
        $(this).data('title', this.title);
        this.removeAttribute('title');
    });
    $('#textbox a.switch, #shortcuts li').hover(function () {
        //create hint (existuje len jeden univerzalny)
        $hint = $('#mousehint');
        if (!$hint.length) {
            $hint = $('<div id="mousehint"><div class="text"></div><div class="arrow"></div></div>');
            $hint.appendTo('body');
        }
        $hint.show();
        //nastavime text
        $hint.find('div.text').text($(this).data('title'));
        //zobrazime hint na spravnej pozicii
        var $element = $(this);
        var position = $element.offset();
        $hint.css({
                top: position.top - $hint.outerHeight() - 14,
                left: position.left + $element.outerWidth() / 2 - $hint.outerWidth() / 2
        });
    }, function () {
        $('#mousehint').hide();
    });
});
