/* 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.imgFadeEffect = function () {
    this.each(function () {
        var self = $(this);
        if (self.is('div') && self.find('img').length > 1){
            setTimeout(function(){ jQuery.imgFadeEffectAnimate(self , 0); },3000);
        }
    });
    return this;
}
jQuery.imgFadeEffectAnimate = function (obj , idx) {
    obj = jQuery(obj);
    if (obj.is('div')) {
        var active = obj.find('img:eq('+ idx +')');
        active
        .fadeOut(700,function(){
            $(this).removeClass('active');
        });
        
        idx = (idx + 1 < obj.find('img').length) ? idx + 1 : 0;
        var next = obj.find('img:eq(' + idx + ')');
        next
        .fadeIn(700,function(){
            $(this).addClass('active');
            
        });
        
        setTimeout(function(){ jQuery.imgFadeEffectAnimate(obj , idx); },4000);
    }
}

$(function(){
    //hinty vo formularoch
    $('input.hint').inputHint();
	
   	$('#sidebarControl, #sidebarTitle').click(function () {
        var sControl = $('#sidebarControl');
        var sControlSpan = sControl.find('span');
        var sContent = $('#sidebarContent');
        if (sContent.is(':hidden')) {
            sControl.show();
            sContent.slideDown(1000,function(){
                sControlSpan.removeClass('sDown').addClass('sUp').html('schovať');
            });
        } else {
            sContent.slideUp(1000,function(){
                sControlSpan.removeClass('sUp').addClass('sDown').html('zobraziť');
            });
        }
    });
    
    //sidepanely
    var sidebarContentHeight = 800;
    (function () {
        var sale = $('#sidebarContent');
        var saleAnim = $('#sidebarAnimation');
        var saleAnimContent = saleAnim.find('.content');
        
        sale.height(sidebarContentHeight - 20);
        var saleAnimHeight = sidebarContentHeight - 10;
        saleAnim.height(saleAnimHeight).css({
            overflow: 'hidden',
            position: 'relative'
        });
        
        if(saleAnimContent.find('.content-item').length > 3){
            var saleAnimContentHeight = saleAnimContent.height();
            if (saleAnimHeight < saleAnimContentHeight) {
                saleAnimContent.css({
                    position: 'absolute',
                    top: '0px',
                    left: '0px',
                    height: saleAnimContentHeight,
                    width: saleAnimContent.width(),
                });
                var saleAnimItemHeight = saleAnimContent.find('.content-item:eq(0)').outerHeight();
                var saleAnimTop = 0;
                var saleAnimEnabled = true;
                function saleAnimMove() {
                    if (saleAnimEnabled) {
                        saleAnimTop -= 1;
                        saleAnimContent.css('top', saleAnimTop + 'px');
                        
                        if (saleAnimContentHeight + saleAnimTop < saleAnimHeight) {
                            var item1 = saleAnimContent.find('.content-item:eq(0)');
                            saleAnimContent.append(item1);
                            saleAnimTop += item1.outerHeight();
                            saleAnimContent.css('top', saleAnimTop + 'px');
                        }
                    }
                    window.setTimeout(saleAnimMove, 50);
                }
                saleAnim.hover(function () {
                    saleAnimEnabled = false;
                }, function () {
                    saleAnimEnabled = true;
                });
                window.setTimeout(saleAnimMove, 2000);
            }
        }
    })()
    
    var pageWidth = $('body').width();
    var pageHeight = ($('body').height() > $(window).height()) ? $('body').height() : $(window).height();
	
    var reports = $('#reports');
    if(reports.length == 1){
    	    
        var reportsOverlay = $('<div id="reportsOverlay"></div>')
        .css({
            'position' : 'absolute'
           ,'width' : pageWidth + 'px'
           ,'height' : pageHeight + 'px'
           ,'left' : '0px'
           ,'top' : '0px'
           ,'background' : '#000'
           ,'z-index' : 100
           ,'opacity' : 0.6
        })
        .click(function(){
            reports.hide();
            reportsOverlay.hide();
        });
        
        var width = 400;
        reports
        .append('<div class="reportPopupClose"><button class="button">ZAVRIEŤ</button></div>')
        .addClass('reportPopup')
        .css({
            'position' : 'absolute'
           ,'top' : $(window).scrollTop() + 150 + 'px'
           ,'left' : ((pageWidth - width) / 2) + 'px'
           ,'z-index' : 100
           ,'border-radius' : '10px'
           ,'-moz-border-radius' : '10px'
           ,'background' : '#FFF'
           ,'padding' : '10px'
           ,'width' : width + 'px'
        })
        .show();
        
        $('#reports button')
        .click(function(){
            reports.hide();
            reportsOverlay.hide();
        });
        
        $('body').append(reportsOverlay);
        $('body').append(reports);
    }
    
    $("#contentimage").imgFadeEffect();
});
