var lastHash = location.hash;
var pollInt;
var title = '';

var url = '';

function showPopup(url) {
    if (url != null) {
        //Google Analytics
        pageTracker._trackPageview(title);

        // dohvacanje i prikaz sadrzaja sa trazenog url-a
        $('#popupContent').load(url.replace('#!', ''), function() {
            $('#popup').show('clip', 300, function(){
                $('.scroll-vert').jScrollPane({"hideFocus": true, "autoReinitialise": true, "autoReinitialiseDelay": 750});
            });
        });
    }
}

function pollHash() {
    if (lastHash !== location.hash) {
        lastHash = location.hash;

        url = String(location.hash);
        url = url.slice(url.search('!') + 1, url.length);
        if (url != '') {
            title = $('a[href$="' + url + '"]').attr('rel');
            document.title = title;

            $('a[class$="' + 'selected' + '"]').removeClass('selected');
            $('a[href$="' + url + '"]').addClass('selected');

            showPopup(url);
        } else {
            $('#close').click();
        }
    }
}

$(document).ready(function() {
    lastHash = location.hash;
    pollInt = window.setInterval("pollHash()", 100);

    url = String(location.hash);

    // ie fix
    if (url == '#') {
        url = '';
    }

    if (url != '') {
        clearInterval(pollInt);
        lastHash = url;
        pollInt = window.setInterval("pollHash()", 100);

        title = $('a[href$="' + url + '"]').attr('rel');
        document.title = title;

        $('a[href$="' + url + '"]').addClass('selected');

        showPopup(url);
    }

    $('.nav').click(function () {
        clearInterval(pollInt);
        location.hash = this.hash;
        lastHash = this.hash;
        pollInt = window.setInterval("pollHash()", 100);

        url = this.hash;

        title = $('#' + this.id).attr('rel');
        document.title = title;

        $('a[class$="' + 'selected' + '"]').removeClass('selected');
        $('#' + this.id).addClass('selected');

        showPopup(url);
    });


    $('#close').click(function() {
        document.title = 'Mesnice Fiolić';
        clearInterval(pollInt);

        if ($.browser.msie) {
            location.hash = '#';
            lastHash = '#';
        } else {
            location.hash = '';
            lastHash = '';
        }
        pollInt = window.setInterval("pollHash()", 100);

        $('a[class$="' + 'selected' + '"]').removeClass('selected');

        $('.scroll-vert').jScrollPane();
        $('#popupContent').html('');
        $('#popup').hide('clip', 300);
    });
});
