var Dialog =
{
    current_dialog_box : null,
    dialog_boxes : null,
    center_dialog : function( dialog_box )
    {
        var page_size = PositionUtils.getPageSize();

        var overlay_div = $( "overlayAdmin" );
        overlay_div.style.height = page_size[ 1 ] + 'px';
        overlay_div.style.display = 'block';
        if( $( dialog_box ) ) dialog_box = $( dialog_box );

        //if the page size is greater than 1024 x 768, we will move the height of the dialog box up.
        if( page_size[ 3 ] < 768 ){ adjustTop = 2; }
        else                      { adjustTop = 1; }

        dialog_box.style.display = 'block';
        var dialog_width = dialog_box.offsetWidth;
        var dialog_height = dialog_box.offsetHeight;
        dialog_box.style.left = ( page_size[ 2 ] / 2 ) - ( dialog_width / 2 ) + 'px';
        var top = ( ( page_size[ 3 ] / 2 ) - dialog_height / adjustTop ) + PositionUtils.getScrollY();
        if( top <= 50 ){ top = 50; }
        dialog_box.style.top = top + "px";
        dialog_box.style.visibility = 'visible';
        Dialog.current_dialog_box = dialog_box;
    },
    center_complex_dialog : function( dialog_box )
    {
        var page_size = PositionUtils.getPageSize();

        var overlay_div = $( "overlayDiv" ) ? $( "overlayDiv" ) : $( "overlayAdmin" );
        if( overlay_div )
        {
            overlay_div.style.height = page_size[ 1 ] + PositionUtils.getScrollY() + 'px';
            overlay_div.style.display = 'block';
            if( $( dialog_box ) ) dialog_box = $( dialog_box );
            if( dialog_box )
            {
                dialog_box.style.display = 'block';
                var dialog_width = dialog_box.offsetWidth;
                var dialog_height = dialog_box.offsetHeight;
                dialog_box.style.left = ( page_size[ 2 ] / 2 ) - ( dialog_width / 2 ) + 'px';
                dialog_box.style.top =  ( ( page_size[ 3 ] / 2 ) - dialog_height / 2 ) + PositionUtils.getScrollY() + 'px';
                dialog_box.style.visibility = 'visible';
                Dialog.current_dialog_box = dialog_box;
            }
        }
    },
    create_div_overlay : function()
    {
        var objBody = document.getElementsByTagName("body")[0];
        var overlayDiv = document.createElement("div");
        overlayDiv.setAttribute('id','overlayDiv');
        overlayDiv.style.display = 'none';
        overlayDiv.style.position = 'absolute';
        overlayDiv.style.top = '0';
        overlayDiv.style.left = '0';
        overlayDiv.style.zIndex = '90';
        overlayDiv.style.width = '100%';
        objBody.appendChild( overlayDiv );
    },
    position_dialog_over_element : function( dialog_box, element )
    {
        var page_size = PositionUtils.getPageSize();
        var newX = PositionUtils.findPosX( element );
        var newY = PositionUtils.findPosY( element );

        var overlay_div = $( "overlayAdmin" );
        overlay_div.style.height = page_size[ 1 ] + 'px';
        overlay_div.style.display = 'block';

        // Position dialog box over the html element (left)
        if( $( dialog_box ) ) dialog_box = $( dialog_box );
        dialog_box.style.display = 'block';
        dialog_position = newX - ( dialog_box.offsetWidth / 2 );
        if( !Dialog.has_scroll() ) dialog_position += element.offsetWidth / 4;
        else                       dialog_position += element.offsetWidth / 2;
        dialog_position += "px";
        dialog_box.style.left = dialog_position;

        var offHeight = dialog_box.offsetHeight;
        var offWidth = dialog_box.offsetWidth;

        // Position dialog box over the html element (top)
        if( ( newY + offHeight ) >= ( page_size[ 3 ] + PositionUtils.getScrollY() ) )
        {
            dialog_box.className = "top";
            dialog_box.style.top = newY - offHeight + "px"; //show the dialog on the top
        }
        else
        {
            dialog_box.className = "bottom";
            dialog_box.style.top = newY + element.offsetHeight + "px"; //show the dialog on the bottom
	    }

        // Move the pointer relative to the position of the html element
        var position = "";
        if( (newX + ( offWidth / 2 ) ) >= page_size[ 2 ] )
        {   // If the html element is against the right side of the page, reposition the box and pointer
            dialog_position = newX - dialog_box.offsetWidth + element.offsetWidth;
            if( !Dialog.has_scroll() ) dialog_position -= element.offsetWidth / 2;
            dialog_position += "px";
            dialog_box.style.left = dialog_position;
            position = dialog_box.offsetWidth - ( ( element.offsetWidth + ( element.offsetWidth / 4 ) ) / 2 ) + "px";
        }
        else if( newX - ( offWidth / 2 ) < 0 )
        {   // If the html element is against the left side of the page, reposition box and pointer
            dialog_box.style.left = newX + "px";
            position = ( element.offsetWidth - ( element.offsetWidth / 4 ) ) / 2 + "px";
         }
        else
        {   // The box and pointer need to be centered on the html element
            position += "50%";
        }

        if( dialog_box.className == "top" ) position += " 100%";
        else                                position += " 0px";

        dialog_box.getElementsByTagName( 'div' )[ 0 ].style.backgroundPosition = position;
        dialog_box.className += " dialog";
        dialog_box.style.visibility = 'visible';
    },
    hide_overlays : function() //Think of this as a private function
    {
        $("overlayAdmin").style.display = 'none';
        if( $("overlayDiv") ) $("overlayDiv").style.display = 'none';
    },    
    hide_current_form : function()
    {
        Dialog.hide_overlays();

        $( Dialog.current_dialog_box ).style.visibility = 'hidden';
    },
    
    hide_form : function(dialog_name) //optional parameter; otherwise, all dialogs traversed
    {
    
        Dialog.hide_overlays();
        
        if(!dialog_name) //do it the old way of hiding everything
        {
        	for( var i = 0; i < Dialog.dialog_boxes.length; i++ )
        	{
        	    $( Dialog.dialog_boxes[ i ].id ).style.visibility = 'hidden';
        	}
        }else
        {
        	$( dialog_name ).style.visibility = 'hidden';
        }
    },
    create_overlay : function()
    {
        if( !$('overlayAdmin') )
        {
            var objBody = document.getElementsByTagName("body")[0];
            // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
            var overlay = document.createElement("iframe");
            overlay.src = "/public/imageserver/webservices/opensource.gov/";
            overlay.src += "static_content/iframeBlank.htm";
            overlay.setAttribute('id','overlayAdmin');
            overlay.style.display = 'none';
            overlay.style.position = 'absolute';
            overlay.style.top = '0';
            overlay.style.left = '0';
            overlay.style.zIndex = '190';
            overlay.style.width = '100%';
            objBody.appendChild(overlay);
        }
        if( !Dialog.dialog_boxes )
        {
            Dialog.dialog_boxes = $$('div.boso-dialog');
        }
    },
    has_scroll : function()
    {
        var fudgeFactor = document.all ? 4 : 24; // Moz only
        var docHeight, totalHeight;

        if( document.all && document.documentElement && document.documentElement.clientHeight )
            docHeight = document.documentElement.scrollHeight;
        else if( document.all ) docHeight = document.body.scrollHeight;
        else                    docHeight = document.height;
        docHeight = parseInt(docHeight) + fudgeFactor;

        if( self.innerHeight ) totalHeight = parseInt(self.innerHeight);
        else                   totalHeight = parseInt(document.body.offsetHeight);

        return docHeight > totalHeight;
    },
    addEvent : function( target, type, listener, useCapture )
    {
        if( document.getElementById( target ) ) target = document.getElementById( target );
        // If firefox
        if( target.addEventListener ) target.addEventListener( type, listener, useCapture ? useCapture : false );
        else                          target.attachEvent( 'on' + type, listener );
    }
};

Dialog.addEvent( window, 'load', Dialog.create_overlay );

