function displayInfo(topic) {
    $.ajax({type:'GET',
        url:'/info/' + topic,
        success:function(html) {
            $().data('info-topic', topic);
            var info = $().data('info-dialog');
            if (info) {
                info.closest('.ui-dialog').css('display', 'block');
                info.html(html);
            } else {
                var buttons = {};
                buttons.Close = function() {
                    $().data('info-dialog').closest('.ui-dialog').css('display', 'none');
                };
                buttons['Info Center'] = function() {
                  var topic = '/infocenter/topic/com.cpm.documentation.msc-' + $().data('info-topic') + '.html'
                  window.open(topic, 'info_center', 'height=600,width=1000,resizable=yes,scrollbars=yes').focus();
                };
                var options = {
                    title:'Tell me...',
                    width:500,
                    buttons:buttons,
                    beforeclose:function() {
                        $().data('info-dialog').closest('.ui-dialog').css('display', 'none');
                        return false;
                    }
                };
                info = $(html).dialog(options);
                $().data('info-dialog', info);
            }
        },
        error:function(xhr, status) {
            document.write(xhr.responseText);
            document.close();
        }
    });
}

function bindBaseLinks(context) {
    $('.storm-button', context).each(function() {
        $(this).addClass('ui-state-default ui-corner-all');
        $(this).hover(function() {
            $(this).addClass('ui-state-hover');
        }, function() {
            $(this).removeClass('ui-state-hover');
        }).focus(function() {
            $(this).addClass('ui-state-focus');
        }).blur(function() {
            $(this).removeClass('ui-state-focus');
        });
    });
    $('.info', context).each(function() {
        $(this).click(function() {
            displayInfo($(this).attr('href'));
            return false;
        });
    });
}

function reloadDocument() {
    document.cookie = 'storm-position=' + $(window).scrollTop() + 'x' + $(window).scrollLeft() + ';'
    window.location.href = window.location.href.split('#')[0]
}

$(function() {
    bindBaseLinks();
    $("#info-menu-toggle").click(function () {
        $("#info-menu").slideToggle();
        return false;
    });
    $.each(document.cookie.split('; '), function() {
        var parts = this.split('=');
        if (parts[0] == 'storm-position') {
            document.cookie = 'storm-position=; max-age=0'
            if (parts.length > 1) {
                parts = parts[1].split('x');
                if (parts.length > 1) {
                    $(window).scrollTop(parseInt(parts[0]));
                    $(window).scrollLeft(parseInt(parts[1]));
                }
            }
        }
    });
});

$.fn.toggleText = function(a, b) {
    return this.each(function () {
        $(this).text($(this).text() == a ? b : a);
    });
};
