/* 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('');
    }
}
jQuery.fn.menuFadeEffect = function ( option ) {
    this.each(function () {
        var self = $(this);
        if (self.is('ul')) {
            
            var options = {
                sWidth   : (option.startWidth !== undefined)   ? option.startWidth   : '100'
               ,eWidth   : (option.endWidth !== undefined)     ? option.endWidth     : '100'
               ,sHeight  : (option.startHeight !== undefined)  ? option.startHeight  : '100'
               ,eHeight  : (option.endHeight !== undefined)    ? option.endHeight    : '100'
               ,sOpacity : (option.startOpacity !== undefined) ? option.startOpacity : 0
               ,eOpacity : (option.endOpacity !== undefined)   ? option.endOpacity   : 1
               ,speed    : (option.speed !== undefined)        ? option.speed        : 250
            };
            
            self.find('li').hover(
                function () {
                    var li = $(this);
                    if(li.has('div').length){
                        $.div = li.find('div').css({
                            opacity : options.sOpacity
                           ,width  : Math.round(li.width() * (options.sWidth / 100)) + 'px'
                           ,height  : Math.round(li.height() * (options.sHeight / 100)) + 'px'
                        });
                    } else {
                        $.div = $('<div></div>').html(li.html()).css({
                            opacity : options.sOpacity
                           ,width   : Math.round(li.width() * (options.sWidth / 100)) + 'px'
                           ,height  : Math.round(li.height() * (options.sHeight / 100)) + 'px' 
                        });
                        li.append($.div);
                    }
                    $.div.stop().animate({
                            opacity : options.eOpacity
                           ,width   : Math.round(li.width() * (options.eWidth / 100)) + 'px'
                           ,height  : Math.round(li.height() * (options.eHeight / 100)) + 'px'
                        }
                        , options.speed
                    ); 
                }
               ,function () {
                    var li = $(this);
                    $.div.stop().animate({
                            opacity : options.sOpacity
                           ,width   : Math.round(li.width() * (options.sWidth / 100)) + 'px'
                           ,height  : Math.round(li.height() * (options.sHeight / 100)) + 'px'
                        }
                        , options.speed
                        , function(){ $(this).remove() }
                    );
                }
            );
        }
    });
    return this;
}
jQuery.fn.scrollNews = function () {
    var list = $(this).find('dl.wn-data');
    var last = list.getLastVisibleNews();
    if(last){
        $('span.scrollDown',this).click(function () {
            var scrollNext = true;    
            if(list.position().top == 0){
                $('dd:eq('+last.idx+')',list).addClass('active');
                if(!(scrollNext = ((last.hidden < 15) ? true : false))){
                    list.animate({'top': '-='+last.hidden},600);
                }
            }
            if($('dd.active',list).nextAll('dd').length && scrollNext){  
                $('dd.active',list).removeClass('active').nextAll('dd:first').addClass('active');
                var offset = $('dd.active',list).outerHeight() + $('dd.active',list).prev('dt').outerHeight();
                offset += (list.position().top == 0) ? last.hidden : 0;
                list.animate({'top': '-='+offset},1000);
            }
        });
        $('span.scrollUp',this).click(function () {
            var scrollNext = true;  
            if($('dd:eq('+(parseInt(last.idx) +1)+')',list).is('.active') && list.position().top < 0){
                list.animate({'top': '+='+last.hidden},600);
                scrollNext = (last.hidden < 15) ? true : false;
            }                     
            if(list.position().top != 0 && $('dd.active',list).prevAll('dd:gt(1)').length && scrollNext){
                var offset = $('dd.active',list).outerHeight() + $('dd.active',list).prev('dt').outerHeight();
                $('dd.active',list).removeClass('active').prevAll('dd:first').addClass('active');
                list.animate({'top': '+='+offset},1000);
            }
        });
    }
    
}
jQuery.fn.getLastVisibleNews = function () {
    list = $(this);
    var last = null;
    var i = 0;
    var height = 0;
    for(i=0;i < $("dd",list).length;i++){
        height += $("dd:eq("+i+")",list).outerHeight();
        height += $("dt:eq("+i+")",list).outerHeight();
        if(height > list.closest('div.wn-con').height()){
            last = i;
            break;
        }
    }
    return (last != null) ? {"idx": i, "hidden": height - list.closest('div.wn-con').height()} : null;
}

$(function(){
    //hinty vo formularoch
    $('input.hint').inputHint();
    
    $('#top ul.btns > li').hover(
        function(){ $(this).addClass($(this).attr('id') + '-hover'); }
       ,function(){ $(this).removeClass($(this).attr('id') + '-hover'); }
    );
    
    $('#content ul.tabs > li').hover(
        function(){ 
            if(!$(this).hasClass('active')){
                $(this).addClass('hover'); 
            }
        }
       ,function(){ $(this).removeClass('hover');}
    );
    
    $("#menu li").hover(function () {
        $(this).addClass('hover');
    },function () {
       $(this).removeClass('hover'); 
    });
    
    //scrollovanie noviniek
    $('div.wn').scrollNews();

});
