$(document).ready(function() {
    $.history.init(loadContent, { unescape: ",/" });
    fleXenv.fleXcrollMain("article");
    
    function loadContent(hash) {
        if(hash != "") {
            $('#ajaj-loader').show();
            $('#ajax-container #ajax-content').hide();
            if ($('#ajax-content').length > 0) {
                $.ajax({
                url: hash,//$(this).attr('href'),
                data: {layout_ajax: true, compact: true},
                success: function(data) {
                    $('#ajax-content').html(data);
					$('#ajax-content').show();
                    galleryInit();
                    $('#ajaj-loader').hide();
					if (typeof onPositions == 'function') {
						onPositions();
					}
                    window.fleXenv.fleXcrollMain("article");
                    window.fleXenv.updateScrollBars();
                    window.fleXenv.scrollTo('ajax-content');

                }
            });
            } else {
                $.ajax({
                    url: hash,//$(this).attr('href'),
                    data: {layout_ajax: true},
                    success: function(data) {
                        $('#ajax-container').html(data);
                        galleryInit();
						if (typeof onPositions == 'function') {
							onPositions();
						}
                        window.fleXenv.fleXcrollMain("article");
                        window.fleXenv.updateScrollBars();
                        window.fleXenv.scrollTo('ajax-content');
                        $('#ajaj-loader').hide();
                    }
                });
            }
        }
    }
    $('#article-pager a.article-link').mousedown(function(e) {
        if (e.which != 1) return;
        //console.log($(this).parent().parent().addClass('sel'));
        e.preventDefault();
        var href = $(this).attr('href');
        href = href.replace(/^.*#/, '')
        _gaq.push(['_trackPageview', href]);
        href = href.replace(/^\/myslim-ze\//, '/');
        href = href.replace(/^\/media\//, '/');
        href = href.replace(/^\/moderator\//, '/');
        href = href.replace(/^\/spisovatel\//, '/');
        href = href.replace(/^\/media\//, '/');
        href = href.replace(/^\/video\//, '/');
        href = href.replace(/^\/galerie\//, '/');
        href = href.replace(/^\/odkazy\//, '/');
        href = href.replace(/^\/kontakt\//, '/');
        //console.log(href);
        $.history.load(href);
        $('#article-pager li').removeClass('sel');
        $(this).parent().parent().addClass('sel');
    });
    $('#article-pager a.article-link').click(function(e) {
        e.preventDefault();
    });
});
