var fetched_menu = new Array();
var cur_id = '';
var target_id = '';

$(document).ready(
    function() {
        anchorify();

        prepPostit();

        $('noscript').remove();

        useAnchor();
    }
);

function prepPostit() {
    $('#postit').prepend('<div id="bar"></div>').css('cursor', 'move').attr('title', 'Double-click to minimise/maximise').draggable().dblclick(function(event) {
        $('#postit > #img').toggle();
        $(this).toggleClass('open');
        $(this).toggleClass('closed');
        event.preventDefault();
    });

    $('#postit a').hover(
        function() {
            $(this).parent().css('background-image', 'url(\'/img/kosmapostithover.gif\')');
        },
        function() {
            $(this).parent().css('background-image', 'url(\'/img/kosmapostit.gif\')');
        }
    );
}

function getAnchor(uri) {
    if (!uri) {
        uri = document.location.toString();
    }
    if (uri.match('#')) {
        return '#' + uri.split('#')[1];
    } else {
        return '';
    }
}

function getHost(uri) {
    if (!uri) {
        uri = document.location.toString();
    }
    return uri.split('/')[2];
}

function useAnchor() {
    var anchor = getAnchor();
    if (anchor) {
        $('.sidebox a#' + anchor.split('/')[0]).click();
        $('.sidebox a#' + anchor.split('/')[0] + '-' + anchor.split('/')[1]).click();
        $('.sidebox a#' + anchor.split('/')[0] + '-' + anchor.split('/')[1] + '-' + anchor.split('/')[2]).click();
    }
}

function anchorify() {
    var anchors = $('a');

    anchors.each(
        function() {
            var href = $(this).attr('href');
            if (!href.match(/#/)) {
                href = href.replace(/^\//i, '/#');
                $(this).attr('href', href);
            }
        }
    );

    anchors.click(bindLinks);
}

function getParent(id) {
    var id_array = id.split('-').reverse();

    if (!id_array[1]) {
        return id_array[0];
    } else {
        return id_array[1];
    }
}

function matchPage() {
    if (!cur_id || !target_id) {
        return false;
    }

    var cur_parent = getParent(cur_id);
    var target_parent = getParent(target_id);

    if (target_parent != cur_parent) {
        return false;
    } else {
        return true;
    }
}

function bindLinks() {
    target_id = $(this).attr('id');

    if ($.inArray(target_id, fetched_menu) != -1) {
        $('#' + target_id + '-children').toggle();
    } else {
        var htmlpath = target_id.replace(/-/g, '/');
        $('*').addClass('wait');

        $.ajax({
            type: 'POST',
            url: '/' + htmlpath + '/',
            async: false,
            data: 'raw=1',
            success:
                function(data) {
                    if (data.match(/^<li/)) {
                        var thislink = $('.sidebox a#' + target_id);
                        thislink.parent().append('<ul id="' + target_id + '-children">' + data + '</ul>');
                        anchorify();
                        fetched_menu.push(target_id);
                    } else {
                        if (!matchPage() && (target_id != 'postitinfo')) {
                            $('.mainbox > *:not(#postit, #postit2)').remove();
                        }

                        var mainbox = $('.mainbox');
                        mainbox.prepend('<div id="test">' + data + '</div>');
                        var content_type = $('.mainbox #test div').attr('id');

                        if (content_type != 'postit') {
                            var content_obj = $('.mainbox > #' + content_type);

                            if (content_obj.size()) {
                                content_obj.remove();
                            }

                            mainbox.prepend($('.mainbox #test').html());
                        }

                        $('.mainbox > #test').remove();

                        $('#images').css('cursor', 'pointer').click(
                            function() {
                                $(this).remove();
                            }
                        );

                        if ($('.mainbox div:first').attr('id') == 'text') {
                            var text = '2';
                            var imgmap = '1';
                        } else {
                            var text = '1';
                            var imgmap = '2';
                        }

                        $('#postit2').css('cursor', 'move').attr('title', 'Double-click to close').draggable().dblclick(
                            function() {
                                $(this).remove();
                            }
                        );

                        $('#text').css('z-index', text).mouseover(
                            function() {
                                $(this).css('z-index', '2')
                                $('#map, #images').css('z-index', '1');
                            }
                        );

                        $('#map, #images').css('z-index', imgmap).mouseover(
                            function() {
                                $(this).css('z-index', '2')
                                $('#text').css('z-index', '1');
                            }
                        );

                        cur_id = target_id;
                    }
                },
            error:
                function() {
                    alert('Sorry, that page does not exist');
                }
            }
        );

        $('*').removeClass('wait');
    }
}

