/**
 * 
 * Basic popup functions - loading, show, hide.
 * 
 * @require prototype.js
 * @require scriptaculous/effects.js
 */

/*
    Create popup and bind functions to events
 */
function run_popup(obj, block_id, func_show, func_hide, options)
{
    if (options == undefined) options = {};
    if (options.timeout == undefined) options.timeout = 1000;
    if (options.offsetx == undefined) options.offsetx = 0;
    if (options.offsety == undefined) options.offsety = 20;
    
    if (!func_show)
        func_show = "show_popup('"+block_id+"');";
    if (!func_hide)
        func_hide = "hide_popup('"+block_id+"');";

    var pos = Position.cumulativeOffset(obj);
    
    if ($(block_id).style.display == 'none')
    {
        $(block_id).style.left = pos[0] + options.offsetx + 'px';
        $(block_id).style.top  = pos[1] + options.offsety + 'px';
    }

    $(block_id).onmouseout = function(){
        $(block_id).timer = window.setTimeout(func_hide, options.timeout);
    };

    $(block_id).onmouseover= function(){
        if ($(block_id).timer) 
            window.clearTimeout($(block_id).timer);
    };

    obj.onmouseout   = function(){
        if (obj.timer) 
            window.clearTimeout(obj.timer); 
        $(block_id).timer = window.setTimeout(func_hide, options.timeout);
    };

    if ($(block_id).timer) window.clearTimeout($(block_id).timer);
    obj.timer = window.setTimeout(func_show, options.timeout);
}

/*
    Simple function to show popup
 */ 
function show_popup(id)
{

    if ($(id).style.display == 'none')
    {
        new Effect.toggle(id);
    }
    $(id).isLoad = true;
}

/*
    Simple function to hide popup
 */ 
function hide_popup(id)
{
    if ($(id).style.display != 'none')
    {
        new Effect.toggle(id);
    }
}

/*
    Hide popup and mark it not loaded
 */
function reload_popup(id)
{
    $(id).isLoad = false;
    hide_popup_toggle(id);
}
