﻿//---------------------------------------------------------------------------------
//  (c) 2008 Guardian Networks, Inc.
//---------------------------------------------------------------------------------

GNgui.GNguiAsset = Class.create();
GNgui.GNguiAsset.prototype = 
{
	initialize : function ( )
	{
		this.sPrefix			 = '';
		this.sPersistent		= 'true';
		this.sViewPort		   = '';
		this.Name				= '';
		this.sSelected		  = 'false';
		this.System			  = '';
		this.sClassName		  = '';
		this.sDeviceType		 = '';
		this.iImageHeight		= 0;
		this.iImageWidth		 = 0;
		this.iTop				= 0;
		this.iLeft			   = 0;
		this.Devices			 = new Array();
		this.iZIndex			 = 0;
		this.sImageSrc		   = '';
		this.sMapIconPath		= '';
		
		this.GetDevices();
	   
	},
	
	SetUpDevice : function()
	{
		//Overload this method
	},
	
	GetDevices : function ()
	{
		var i = 0;
		
		if ($e( 'MapContainer' ))
		{
			elems = $e( 'MapContainer' ).getElementsByTagName("IMG");
			l=$l( elems );
			for (var i= 0 ; i< l ; i++)
			{
				if ($readAttribute(elems[i], this.sDeviceType)!=null)
				{
					this.Devices[elems[i].DeviceID] = elems[i];
				}
			}
		}
	},
	
	GenerateIcon : function ()
	{
		//Over load this method
	},
	
	AddDevice:function(DeviceID,MacID,ParentTile,Top,Left)
	{
	
		this.iTop=Top;
		this.iLeft=Left;
	
		//Create Device div and set attributes
		this.Devices[DeviceID]=$ce('img');
		this.Devices[DeviceID].setAttribute( 'DeviceID', DeviceID );
		this.Devices[DeviceID].setAttribute( 'id', this.sPrefix + DeviceID );
		this.Devices[DeviceID].setAttribute( 'className', this.sClassName );
		this.Devices[DeviceID].setAttribute( 'ParentTile', ParentTile );
		this.Devices[DeviceID].setAttribute( 'Persistent', this.sPersistent );
		this.Devices[DeviceID].setAttribute( 'Selected', this.sSelected );
		this.Devices[DeviceID].setAttribute( this.sDeviceType, 'true' );
		this.Devices[DeviceID].setAttribute( 'MacID', MacID );
		this.Devices[DeviceID].setAttribute( 'src', this.sImageSrc);

		this.Devices[DeviceID].style.zIndex = this.iZIndex;
		$setStyle(this.Devices[DeviceID], { width: this.iImageWidth + 'px', height: this.iImageHeight + 'px', left: this.iLeft + 'px', top: this.iTop + 'px' });
		this.Devices[DeviceID].Delete = function()
		{
			$remove(this);
		}
		
		this._SetAttributes( DeviceID );
		this.Draw( DeviceID );
		
		this._eventAddDevice( DeviceID );
	},
	
	_eventAddDevice : function ( DeviceID )
	{
		//Overload this method
	},
	
	_eventRemoveDevice:function(DeviceID)
	{
	},
	
	_SetAttributes : function ( DeviceID )
	{
		//Overload this method
	},
	
	Draw : function ( DeviceID )
	{
		if (this.Devices.length == 0)
		{
			this.GetDevices();
		}

		$ac(MapDisplayObject.mapArrayDiv, this.Devices[DeviceID] );
	},
	
	ReDraw : function ( DeviceID )
	{
		if (this.Devices.length == 0)
		{
			this.GetDevices();
		}
		
		this.Remove( DeviceID );
		this.Draw( DeviceID );
	},
	
	ReDrawAll : function ()
	{
		var item;
		var items = this.Devices;
		
		if (this.Devices.length == 0)
		{
			this.GetDevices();
		}

		for (item in items)
		{
			if (item != 'undefined')
			{
				if (typeof(items[item]) == 'object')
				{
					if ($readAttribute(items[item], this.sDeviceType)!= null)
					{
						$remove(items[item]);
						$ac( MapDisplayObject.mapArrayDiv, $e( items[item] ) );
					}
				}
			}
		} 
	},
	
	UpdateIcon : function ( DeviceID )
	{
		//OverLoad This method   
	},

	Remove : function ( DeviceID )
	{
		if (this.Devices.length == 0)
		{
			this.GetDevices();
		}
		if ( this.Devices[DeviceID] != 'undefined' && typeof(this.Devices[DeviceID])=='object')
		{
			try
			{
				$remove(this.Devices[DeviceID]);
				this._eventRemoveDevice(DeviceID);
			}
			catch(e)
			{
			;
			}
			
		}
	},

	RemoveAll:function()
	{
		var item;
		var items = this.Devices;
		
		if (this.Devices.length == 0)
		{
			this.GetDevices();
		}

		for (item in items)
		{
			if (item != 'undefined')
			{
				if (typeof(items[item]) == 'object')
				{
					if ($readAttribute(items[item], this.sDeviceType)!=null)
					{
						try
						{
							$remove(items[item]);
						}
						catch(er)
						{
						}
					}
				}
			}
		}
	},

	RemoveFromTile:function(TileID)
	{
		elems=$e('MapContainer').getElementsByTagName('IMG');
		l = elems.length;
		for(i=0;i<l;i++)
		{
			if($readAttribute(elems[i],'ParentTile')!=null)
			{
				if(elems[i].ParentTile.id==TileID.id)
				{
					try
					{
						$remove(elems[i]);
					}
					catch(er)
					{
					}
				}
			}
		}
	},

	Show:function(DeviceID)
	{
		if(this.Devices.length==0)
		{
			this.GetDevices();
		}
		
		$show(this.Devices[DeviceID]);
	},
	
	ShowAll:function()
	{
		var item;
		var items=this.Devices;
		
		if(this.Devices.length==0)
		{
			this.GetDevices();
		}

		for (item in items)
		{
			if (item != 'undefined')
			{
				if (typeof(items[item]) == 'object')
				{
					if ($readAttribute(items[item], this.sDeviceType)!=null)
					{
						$show(items[item]);
					}
				}
			}
		}
	},
	
	Hide:function ( DeviceID )
	{
		if(this.Devices.length==0)
		{
			this.GetDevices();
		}
		
		$hide(this.Devices[DeviceID]);
	},
	
	HideAll:function ()
	{
		var item;
		var items = this.Devices;
		
		if (this.Devices.length == 0)
		{
			this.GetDevices();
		}
		for(item in items)
		{
			if(item!='undefined')
			{
				if(typeof(items[item])=='object')
				{
					if ($readAttribute(items[item], this.sDeviceType)!=null)
					{
						$hide(items[item]);
					}
				}
			}
		}
	},
	
	Move : function ( Top, Left, DeviceID )
	{
		if (this.Devices.length == 0)
		{
			this.GetDevices();
		}
		
		$setStyle(this.Devices[DeviceID], { top: parseFloat(Top), left: parseFloat(Left) });

	   // this.ReDraw( DeviceID );
	},

	SetSelected : function ( Selected ) 
	{
		this.sSelected = Selected;
		this.SetImage ();				
	},
	
	ClearSelected : function ()
	{
		var item;
		var items = this.Devices;
		
		if (this.Devices.length == 0)
		{
			this.GetDevices();
		}

		for (item in items)
		{
			if (item != 'undefined')
			{
				if (typeof(items[item]) == 'object')
				{
					if ($readAttribute(items[item], this.sDeviceType)!=null)
					{
						$e(items[item]).setAttribute('Selected', 'false');
					}
				}
			}
		}
	},
	
	ApplyMapFilter : function()
   {
		//overload this method
   },
   
   Command : function( command )
   {
		//overload this method
   },
   
   SetStatus : function( DeviceID, StatusID )
   {	
		if (this.Devices.length == 0)
		{
			this.GetDevices();
		}
		
		this.sStatusID = StatusID;
		this.Devices[DeviceID].statusid = StatusID;
   }
	
	
};


