function defineSizeStyle(width) {
    
    width = parseInt(width);
    if (width < 1160) {
	$("#size-css").attr("href", basePath+"/css/mediumSize.css");
	initiateJscrollPane();
    } else {
	$("#size-css").attr("href", basePath+"/css/defaultSize.css");
	initiateJscrollPane();
    }
    
}

// Vertikalni centrovani obsahu
function setPosition() {
    var documentHeight = $(document).height();

    var mainHeight = $('#main').outerHeight(true);

    var top = Math.ceil((documentHeight - mainHeight) / 2);

    $('#main').css('top', top);
}

function resetMenus($submenus) {
    $submenus.parent('li').find('a:last').not('.hovered').parent('li').children('ul').each(function(){
        width = $(this).outerWidth(true);
        $(this).animate({
            right: -width
        }, 500, 'easeOutCirc');
    });
}

function initiateJscrollPane() {
    $('.scroll').jScrollPane({
        showArrows: false,
        verticalDragMinHeight: 20,
        verticalDragMaxHeight: 20
    });
}

$(document).ready(function(){
    var homepage = true;
    $('ul#menu>li>a').each(function(){
        if ($(this).hasClass('current')) {
            homepage = false;
        }
    });
    if (homepage == true) {
        $('ul#submenu2').parent().find('a:last').addClass('hovered');
        $('ul#submenu2').animate({
            right: 74
        }, 500, 'easeOutCirc');
    }
    
    defineSizeStyle($(window).width());
    // Custom scrollbar
    if (!$.browser.msie) {
        initiateJscrollPane();
    } else {
        if ($.browser.version > 7) {
            initiateJscrollPane();
        }
    }    
    
    setPosition();

    // Menu
    var $submenus = $('#menu li ul');
    var $menuWithSubmenu = $('#menu li').has('ul');
    var $menuWithoutSubmenu = $('#menu>li:not(:has(ul))');

    $submenus.each(function(){
        width = $(this).outerWidth(true);
        $(this).css('right', -width);
    });


    $menuWithSubmenu.find('a:last').click(function(event){
        event.preventDefault();
    });


    
    function refreshRefNav(i, total) {
	if (i==1) $('.nav a.prev').hide();
	else $('.nav a.prev').show();
	if (i==total) $('.nav a.next').hide();
	else $('.nav a.next').show();
    }
    
    $('.nav a').live('click', function(event){
	    event.preventDefault();
	    var countItems = $('.items .item').size();
	    var current = $('#currentItem').html();
	    var isCurrent = false;
	    var navClass = $(this).attr('class');
	    
	    var i = 1;
	    $('.items .item').each(function(){
		if (isCurrent==true && navClass=='next') {
		    // next
		    $('#currentItem').html($(this).html());
		    refreshRefNav(i, countItems);
		    return false;
		}
		if ($(this).html()==current) isCurrent = true;
		if (isCurrent==true && navClass=='prev') {
		    // prev
		    $('#currentItem').html($(this).prev().html());
		    refreshRefNav(i-1, countItems);
		    return false;
		}
		

		i++;
	    });
	
    });
	$menuWithoutSubmenu.click(function(){
		$submenus.each(function(){
			width = $(this).outerWidth(true);
			$(this).animate({
				right: -width
			}, 500, 'easeOutCirc');
		});
		$('ul li a').each(function() {
				if ($(this).hasClass('hovered')) {
					$(this).removeClass('hovered');
				}
		});
    });
    
    $menuWithSubmenu.click(function(){
        $thisLink = $('a:last', this);
        if ($thisLink.hasClass('hovered')) {
            $thisLink.removeClass('hovered');
            resetMenus($submenus);
        } else {
            $(this).parent().find('a').removeClass('hovered');
            $thisLink.addClass('hovered');
            resetMenus($submenus);
            $(this).find('ul').animate({
                right: $(this).find('a').outerWidth(true) - 40
            }, 500, 'easeOutCirc');
        }
    });

    $menuWithSubmenu.find('ul li a').click(function(event){
        event.stopPropagation();
    });
    
    
     
    
    
    
    // Ajaxove odkazy
    var $spinner = $('<img>', {
        src: basePath + '/img/ajax-loader.gif',
        css: {
            position: 'absolute',
            top: '30%',
            left: '50%'
        }
    });

    
    $spinner.ajaxStart(function(){
	$('#snippet--contentSnippet').fadeTo('fast', 0.2);
        $(this).appendTo('#content').hide().fadeTo('1000', 1);
    }).ajaxStop(function(){
        $(this).fadeOut('1000');
        $('#snippet--contentSnippet').fadeTo('fast', 1);
        // Custom scrollbar
        if (!$.browser.msie) {
            initiateJscrollPane();
        } else {
            if ($.browser.version > 7) {
                initiateJscrollPane();
            }
        }

    });
        

});



$(window).resize(function() {
    setPosition();
    defineSizeStyle($(this).width());
});





