/*

pulldown-menu class for IE5+ and Gecko-based Browsers

Author:
Sebastian Bechtold
sebastian_bechtold@web.de
http://www.rotblind.de

you may use this script for any private or commercial project
as long as you don't remove this note about its origin.

*/

var block_autohide = true;
var open_top_menu;


function Pulldownmenu(id)
{
    this.add_item = function add_item(url, caption, icon, target_frame, submenu_id)
    {
        this.items[this.items.length] = new Pdm_item(url, caption, icon, target_frame, submenu_id);
        this.draw();
    }


    this.close_submenu = function close_submenu()
    {
        if(this.open_child)
        {
            this.open_child.hide(true);
            delete(this.open_child);
        }
    }


    this.draw = function draw()
    {
        if(document.getElementById(this.id))
        {
            html_output = '<table cellspacing="0" cellpadding="2" style="color:#' + this.font_color + ';font:' + this.font + ';">';

            for(var i = 0; i < this.items.length; i++)
            {
                if(this.items[i].submenu_id)
                {
                    submenu_indicator = '<img src="' + URL_PDM_ICONS + 'open_submenu.gif" />';
                    onmouseover = this.id + '.open_submenu(' + this.items[i].submenu_id + ', document.getElementById(\'' + this.id + '\').offsetLeft + document.getElementById(\'' + this.id + '\').offsetWidth, document.getElementById(\'' + this.id + '\').offsetTop + document.getElementById(\'' + this.id + '\').firstChild.firstChild.firstChild.offsetHeight * ' + i + ')';
                }
                else
                {
                    submenu_indicator = '';
                    onmouseover = this.id + '.close_submenu()';
                }

                icon = this.items[i].icon || 'default.gif';
                if (this.items[i].checked) icon = 'checked.gif';

                onclick = '';
                target_frame = '';

                if (this.items[i].url)
                {
                    if (this.items[i].target_frame)
                    {
                       switch (this.items[i].target_frame) {
                       case '_blank': onclick = "window.open('" + this.items[i].url + "')";
                                      break;
                       case '_klein': onclick = "window.open('" + this.items[i].url + "','" +  this.items[i].caption + "','height=400,width=500,scrollbars,resizable=yes')";
                                      break;
                       default: onclick = this.items[i].target_frame + ".document.location.href='" + this.items[i].url  + "'";
                       }
                    }
                    else
                    {
                        onclick = "document.location.href='" + this.items[i].url  + "'";
                    }
                }

                caption = this.items[i].caption || this.items[i].url;

                html_output += '<tr onclick="' + onclick + '" onmouseover="this.style.background=\'#' + this.color_hover + '\';' + onmouseover + '" onmouseout="this.style.background=\'#' + this.color_panel + '\';">';

                if(!this.hide_icons)
                {
                    html_output += '<td style="background-color:#' + this.color_iconsbackground + ';padding-left:4px;padding-right:4px;"><img src="' + URL_PDM_ICONS + icon + '" /></td>'
                }
                else
                {
                    if(this.items[i].checked) caption = '<u>' + caption + '</u>';
                }

                html_output += '<td style="padding-left:6px;">' + caption + '</td><td>' + submenu_indicator + '</td></tr>';
            }

            html_output += '</table>';
            document.getElementById(this.id).innerHTML = html_output;
        }
    }


    this.hide = function hide(override)
    {
        if(!block_autohide || override)
        {
            if(document.getElementById(this.id))
            {
                this.close_submenu();

                document.getElementById(this.id).style.visibility = 'hidden';

                delete(this.parent);

                if(open_top_menu == this) window.clearInterval(this.hide_interval);
            }
        }
    }


    this.open_submenu = function open_submenu(submenu, x, y)
    {
        if(this.open_child) this.open_child.hide(true);
        this.open_child = submenu;
        submenu.parent = this;
        submenu.show(x,y);
    }


    this.show = function show(x, y)
    {
        if(!this.parent)
        {
            if(open_top_menu) open_top_menu.hide(true);

            open_top_menu = this;
            block_autohide = true;
            this.hide_interval = window.setInterval(this.id + '.hide();', this.auto_hide_delay);
        }

        if(!document.getElementById(this.id))
        {
            panel_html = '<div id="' + this.id + '" onmouseover="block_autohide=true" onmouseout="block_autohide=false" style="position:absolute;visibility:hidden;filter:progid:DXImageTransform.Microsoft.Shadow(color=#bbbbbb,direction=135,strength=4,enabled=' + this.IE_filter_shadow +');"></div>';

            if(document.getElementById('pdm_panels_container'))
            {
                document.getElementById('pdm_panels_container').innerHTML += panel_html;
            }
            else
            {
                document.body.innerHTML += panel_html;
            }

            document.getElementById(this.id).style.border = '1px solid #' + this.color_panelborder;
            document.getElementById(this.id).style.cursor = 'default';
            document.getElementById(this.id).style.background = '#' + this.color_panel;

            this.draw();
        }

        document.getElementById(this.id).style.left = x + 'px';
        document.getElementById(this.id).style.top = y + 'px';
        document.getElementById(this.id).style.visibility = 'visible';
    }


    this.set_checked = function set_checked(item_index, state)
    {
        this.items[item_index].checked = state;
        this.draw();
    }

    this.id = id;
    this.items = new Array();

    // menu panel configuration

    this.auto_hide_delay = 1000;
    this.color_panelborder = '888';
    this.color_panel = 'fff';
    this.color_iconsbackground = 'ddd';
    this.color_hover = 'fc6';
    this.font_color = '000';
    this.font = '12px Trebuchet MS';
    this.IE_filter_shadow = true;
//    this.hide_icons = true;
}


function Pdm_item(url, caption, icon, target_frame, submenu_id)
{
    this.url = url;
    this.caption = caption;
    this.icon = icon;
    this.target_frame = target_frame;
    this.submenu_id = submenu_id;
}

