//
// javascript for displaying popup tips
//
    var HELP_TIP_SELECTOR           = 'a.button_tips';
    var HELP_TIP_NO_DIALOG_TOP_SELECTOR     = '.button_tips_no_dialog_top';
    var HELP_TIP_NO_DIALOG_RIGHT_SELECTOR   = '.button_tips_no_dialog_right';
    var HELP_TIP_NO_DIALOG_LEFT_SELECTOR    = '.button_tips_no_dialog_left';
    var HELP_DIALOG_SELECTOR        = 'div#help_dialog';
    var HELP_SCRIPT                 = '/platform/help.pl';

    $(document).ready(function(){
        help_init();
    });

    function help_init() {
        $(HELP_TIP_SELECTOR).bt({
            width           : 200,
            spikeLength     : 20,
            spikeGirth      : 10,
            cornerRadius    : 5,
            fill            : 'rgba(0,0,0,.9)',
            strokeWidth     : 2,
            strokeStyle     : '#CCCCCC',
            positions       : [ 'top', 'right', 'most' ],
            shrinkToFit     : true,
            cssStyles       : { color: '#FFFFFF', fontSize: '11px', padding: '5px', paddingLeft: '10px', paddingRight: '10px' },
            ajaxPath        : [ "$(this).attr('ajaxPath')" ],
            ajaxCache       : false,
            ajaxOpts        : { global: false }
         }).click( help_tips_show );

        $(HELP_TIP_NO_DIALOG_TOP_SELECTOR).bt({
            width           : '200px',
            spikeLength     : 20,
            spikeGirth      : 10,
            cornerRadius    : 5,
            fill            : 'rgba(0,0,0,.9)',
            strokeWidth     : 2,
            strokeStyle     : '#CCCCCC',
            positions       : [ 'top', 'right', 'most' ],
            shrinkToFit     : true,
            cssStyles       : { color: '#FFFFFF', fontSize: '11px', padding: '5px', paddingLeft: '10px', paddingRight: '10px' }
         });

        $(HELP_TIP_NO_DIALOG_LEFT_SELECTOR).bt({
            width           : '200px',
            spikeLength     : 20,
            spikeGirth      : 10,
            cornerRadius    : 5,
            fill            : 'rgba(0,0,0,.9)',
            strokeWidth     : 2,
            strokeStyle     : '#CCCCCC',
            positions       : [ 'left', 'top', 'most' ],
            shrinkToFit     : true,
            cssStyles       : { color: '#FFFFFF', fontSize: '11px', padding: '5px', paddingLeft: '10px', paddingRight: '10px' }
         });

        $(HELP_TIP_NO_DIALOG_RIGHT_SELECTOR).bt({
            width           : '200px',
            spikeLength     : 20,
            spikeGirth      : 10,
            cornerRadius    : 5,
            fill            : 'rgba(0,0,0,.9)',
            strokeWidth     : 2,
            strokeStyle     : '#CCCCCC',
            positions       : [ 'right', 'top', 'most' ],
            shrinkToFit     : true,
            cssStyles       : { color: '#FFFFFF', fontSize: '11px', padding: '5px', paddingLeft: '10px', paddingRight: '10px' }
         });
    }

    function help_tips_show() {
        var cgi_params = {
            f_name      : 'f_topic_view_json',
            topic_id    : jQuery(this).attr('topic_id')
            };

        jQuery.ajax({
            type        : 'POST',
            url         : HELP_SCRIPT,
            dataType    : 'json',
            data        : cgi_params,
            success     : function(data){
                                jQuery(HELP_DIALOG_SELECTOR).html(data.topic_detail);
                                
                                jQuery(HELP_DIALOG_SELECTOR).dialog({
                                    title       : 'Help: ' + data.topic_title,
                                    modal       : true,
                                    width       : 500,
                                    zIndex      : 9000,
                                    autoOpen    : false,
                                    close       : function(){ $(this).dialog('destroy'); }
                                });

                                // make sure our content isn't larger than the max allowed by the browser window
                                var max_modal_height = parseInt(jQuery(window).height() * .85);
                                var cur_modal_height = jQuery(HELP_DIALOG_SELECTOR).height();

                                var modal_height = 'auto';
                                if (cur_modal_height+100 > max_modal_height) {
                                    modal_height = max_modal_height;
                                }

                                jQuery(HELP_DIALOG_SELECTOR).dialog('option','height',modal_height);
                                jQuery(HELP_DIALOG_SELECTOR).dialog('open');
                            }
            });

        return true;
    }
