/* 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('');
    }
}

//plugin na zebra tabulky a highlight tabulky
jQuery.fn.zebraTable = function () {
    this.each(function () {
        var self = $(this);
        if (self.is('table')) {
            $('tbody tr:even',self).removeClass('even').addClass('odd');
            $('tbody tr:odd',self).removeClass('odd').addClass('even');
        }
    });
    return this;
};
jQuery.fn.highlightTable = function () {
    this.each(function () {
        var self = $(this);
        if (self.is('table')) {
            $('tbody tr',self).unbind('mouseover.highlight').bind('mouseover.highlight',function () {
                $('>td',this).addClass('highlight');
            });
            $('tbody tr',self).unbind('mouseout.highlight').bind('mouseout.highlight',function () {
                $('>td',this).removeClass('highlight');
            });
        }
    });
    return this;
};

//pugin na popup okno
jQuery.fn.homepopup = function () {
    var popup = this.eq(0);
    //overlay
    var overlay = $('<div id="homepopupOverlay"></div>');
    var bodyW = $(document).width();
    var bodyH = $(document).height();
    overlay.width(bodyW);
    overlay.height(bodyH);
    popup.before(overlay);
    //animacia na popup
    var desiredHeight = popup.height();
    popup.find('> *').hide();
    popup.height(1).show();
    popup.css({
        top: (($(window).height() - desiredHeight) / 2 + $(document).scrollTop()) + 'px',
        left: (($(window).width() - popup.width()) / 2 + $(document).scrollLeft()) + 'px'
    });
    popup.animate({
        height: desiredHeight + 'px'
    },{
        duration: 500,
        complete: function () {
            $(this).find('> *').show();
        }
    });
    popup.find('.close').click(function () {
        popup.hide();
        overlay.remove();
        //nastavime cookies
        if ($('#homepopupNomore').is(':checked')) {
            $.get(Router('system/current-time/'),function (curtime) {
                jQuery.cookie('popup1', curtime, {
                    expires: 100,
                    path: '/'
                });
            })
        }
    });
    return this;
};

//plugin na cookies
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

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();
	$('#mapa').height($('#bottom-background').height() + 26);
	$("#animation").imgFadeEffect();
});
