﻿//---------------------------------------------------------------------------------
//  (c) 2007 Guardian Networks, Inc
//---------------------------------------------------------------------------------

GNgui.GNScreen = Class.create ( );

GNgui.GNScreen.prototype = 
{
	initialize : function ( ObjBaseName, shiftY, scrollable,  Footer)
	{
		this.ScreenName = '';
	    this.ControlDiv = $ce( "div" );
	    this.ContentDiv = $ce( "div" );
	    this.FooterExist = Footer;
        if (this.FooterExist == true)
            this.ContentDivFooter = $ce( "div"); 
	    
	    this.ControlDiv.id = ObjBaseName + "ScreenControlDiv";
	    this.ContentDiv.id = ObjBaseName + "ScreenContentDiv";
	    if (this.FooterExist == true)
	        this.ContentDivFooter.id = ObjBaseName + "ScreenContentFooterDiv";
        
	    this.Ypos = parseInt( shiftY );
	    
	    this.ControlDiv.className = "GNScreenControl";
	    this.ContentDiv.className = "GNScreenContent";
	    if (this.FooterExist == true)
	        this.ContentDivFooter.className = "GNScreenContentFooter";
	    
	    $ac( document.body , this.ControlDiv );
	    $ac( document.body , this.ContentDiv );
	    if (this.FooterExist == true)
	        $ac( document.body , this.ContentDivFooter );
	    
	    this.ContentDiv = $e(this.ContentDiv);
	    this.ControlDiv = $e(this.ControlDiv);
	    if (this.FooterExist == true)
	        this.ContentDivFooter = $e(this.ContentDivFooter);
	    
	    this.ContentDiv.GNScreen = this;
	    this.ControlDiv.GNScreen = this;
	    if (this.FooterExist == true)
	        this.ContentDivFooter.GNScreen = this;

		if (scrollable == true)
			this.ContentDiv.style.overflow = 'scroll';
	    
	    this.ContentWidth = parseInt(document.body.clientWidth) - $ew(this.ControlDiv);
	    this.ScreenHeight = parseInt(document.body.clientHeight) - this.Ypos  - 1;
	    if( this.ContentWidth <= 0)
	    {
	        this.ContentWidth = 0;
	    }
	    if ( this.ScreenHeight <= 0 )
	    {
	        this.ScreenHeight = 0;
	    }	    
	    if (this.FooterExist == true) 
	        $setStyle(this.ContentDiv, { width : this.ContentWidth, height : this.ScreenHeight - 20, left : $ew(this.ControlDiv), top : this.Ypos } );
	    else
	        $setStyle(this.ContentDiv, { width : this.ContentWidth, height : this.ScreenHeight, left : $ew(this.ControlDiv), top : this.Ypos } );
			$setStyle(this.ControlDiv, { height : this.ScreenHeight, top : this.Ypos  } );
	    if (this.FooterExist == true)
	        $setStyle(this.ContentDivFooter, { width : this.ContentWidth, height : 20, left : $ew(this.ControlDiv), top : this.ScreenHeight+20 } );
	    
	    Event.observe ( window , 'resize' , GNgui.GNScreen.prototype.ChangeSize.bindAsEventListener(this));
		  Gutenberg.Subscribe ( "ShowScreen" , this.ShowScreen.bindAsEventListener(this));
	},
	
	FooterID : function ()
    {
        return this.ContentDivFooter.id;
    },
	
	Hide : function ()
	{
	    $e(this.ControlDiv.id).style.visibility='hidden';
	    $e(this.ContentDiv.id).style.visibility='hidden';
	    if (this.FooterExist == true)
	    {
	        $e(this.ContentDivFooter.id).style.visibility='hidden';
	    }
	},
	
	Show : function ()
	{
      $e(this.ControlDiv.id).visibility='inherit';
	    $e(this.ContentDiv.id).visibility='inherit';
	    if (this.FooterExist == true)
	    {
	        $e(this.ContentDivFooter.id).visibility='inherit';
	    }  
	},
	
	ChangeSize : function ( event )
	{
        this.ContentWidth = parseInt(document.body.clientWidth) - $ew(this.ControlDiv);
        this.ScreenHeight = parseInt(document.body.clientHeight) - this.Ypos - 1;

        if( this.ContentWidth <= 0)
            this.ContentWidth = 0;
        if ( this.ScreenHeight <= 0 )
            this.ScreenHeight = 0;
        if (this.FooterExist == true)
        {
            $setStyle(this.ContentDiv, { width : this.ContentWidth, height : this.ScreenHeight - 20 } );
        }
        else
        {
            $setStyle(this.ContentDiv, { width : this.ContentWidth, height : this.ScreenHeight } );
        }
        $setStyle(this.ControlDiv, { height : this.ScreenHeight } );
        if (this.FooterExist == true) $setStyle(this.ContentDivFooter, { width : this.ContentWidth, top : this.ScreenHeight+20, height : 20 } );
        Gutenberg.PublishNow( "ContentWidth",this.ContentWidth);
        Gutenberg.PublishNow( "ContentHeight", this.ScreenHeight );
        Gutenberg.PublishNow( "GNScreenResize" , this.ScreenName );  
	},
	AssociateScreen : function ( ScreenName )
	{
        this.ScreenName = ScreenName;
	},
	
    ShowScreen : function( ScreenName )
    {
        if ( ScreenName == this.ScreenName )
        {
            if ( ScreenName == "LotImage")
            {
                LotImageProcess();
            }
            else
            {
                $show(this.ControlDiv);
                $show(this.ContentDiv);
                if (this.FooterExist == true)
                    $show(this.ContentDivFooter);
            }
        }
        else
        {
            if (ScreenName != "LotImage")
            {
                $hide(this.ControlDiv);
                $hide(this.ContentDiv);
                if (this.FooterExist == true)
                    $hide(this.ContentDivFooter);
            }
        }
    },
    
    AddFooterItem : function(Id, Left, Width, Text, Image, Expandable, ClickEventFunc)
    {
        var e = new GNgui.FooterItem(Id, Left, Width, Text, Image, Expandable, ClickEventFunc);
        $ac(this.FooterID(), e.newelement);
        return e;
    } 


   
}
LotImageProcess = function()
{
    // This subscription gets the installation name (which we could read off the menu, but I'd rather go
    // to data -- and more importantly, it returns a list of files in the directory of the installation
    // for subsequent display and choice.
    Gutenberg.Subscribe ( 'InstallationSingle' , InstallationResponse );
    var ParamString = "<ConsumerID>"+$e( 'ConsumerSelecter' ).options[$e( 'ConsumerSelecter' ).selectedIndex].value+"</ConsumerID>";
    XMLRequest.Send ( 'Asset' , 'GetConsumers' , ParamString , 'InstallationSingle' );
}

InstallationResponse = function ( XmlResponse )
{
    Gutenberg.Unsubscribe ( 'InstallationSingle' , InstallationResponse );
    var DataArray = XmlResponse.GetElements ( "Consumer" );
	
    if ( DataArray.length > 0 )
    {
	    ConsumerID = DataArray[ 0 ].getAttribute ( "ID" );
	    ConsumerName = DataArray[ 0 ].getAttribute ( "Name" );
	    ConsumerPMInstallationType = DataArray[ 0 ].getAttribute ( "PMInstallationType" );
		ConsumerNameForParams = ConsumerName.replace(" ", "%20");
        var FileArray = XmlResponse.GetElements ( "ImgName" );
        if (FileArray.length > 0)
        {
            var intext = "<table>";
            var i, onclicktext, FileName, FileWidth, FileHeight;
            var FileNameForParams;
            for ( i=0; i<FileArray.length; i++)
            {
                FileName = FileArray[ i ].getAttribute ("ImgFileName");
                FileNameForParams = FileName.replace(" ", "%20");
                FileWidth = parseInt(FileArray[ i ].getAttribute ("ImgFileWidth")) + 20;
                FileWidth = FileWidth.toString(10);
                FileHeight = parseInt(FileArray[ i ].getAttribute ("ImgFileHeight")) + 20;
                FileHeight = FileHeight.toString(10);
                onclicktext = "LotImageOnclick('"+ConsumerNameForParams+"','"+FileNameForParams+"','"+FileWidth+"','"+FileHeight+"');";
                intext = intext + '<TR><TD><button onclick='+onclicktext+'>'+FileName+'</Button></TD></TR>';
                
            }
            
            if (FileArray.length == 1)
            {
        
                $e( 'FilelistDiv' ).style.display = "none";
                if (ConsumerPMInstallationType == "SurfaceLot")
                {
                    window.open('./Parts/Monitoring/PictureViewer.asp?Installation='+ConsumerName+'&ImageName='+FileName,'Installation_Lot_Image', 'scrollbars= 0, alwaysRaised = true , directories = no , menubar = no , titlebar = no, location = no, width = '+FileWidth+', height='+FileHeight);
                }
                else
                {
                    alert("Only surface-lot consumers have lot images.");
                }
            }
            else
            {
                //intext = intext + "</select>";
                intext = intext + "</table>"
                $e( 'FileListTableCell' ).innerHTML = intext;
                $e( 'FilelistDiv' ).style.display = "";
            }
        }
    }
}     

LotImageOnclick = function( ConsumerNameForParams, FileNameForParams, FileWidth, FileHeight )
{
    
    var ConsumerName = ConsumerNameForParams.replace("%20", " ");
    var FileName = FileNameForParams.replace("%20", " ");
    window.open('./Parts/Monitoring/PictureViewer.asp?Installation='+ConsumerName+'&ImageName='+FileName,'Installation_Lot_Image', 'scrollbars= 0, alwaysRaised = true , directories = no , menubar = no , titlebar = no, location = no, width = '+FileWidth+', height='+FileHeight);
    $e( 'FilelistDiv' ).style.display = "none";}
