var WM = new function()
{

    this.A_windows;
    this.activeWindow;	 
    this.leftList;

	// WM
    this.dump = function()
    {
		var str = '';
		for(var i = 0;i<this.A_windows.length;i++)
		{
		    var obj = this.A_windows[i];
		    if(obj)
		    {
				str += '[' + obj.title + '] ' + 
					obj.domNode.style.left + ' ' + obj.domNode.style.top + ' ' + 
					obj.domNode.style.width + ' ' + obj.domNode.style.height + '\n';
				if(obj.windowType == 'user')
				{
				    for(var j=0;j<obj.listPtr.contents.length;j++)
				    {
						str += obj.listPtr.contents[j] + '\n';
					}
				}
		    }
		}
		return str;
    }

	// WM
    this.findWindow = function(name)
    {
		for(var i = 0;i<this.A_windows.length;i++)
		{
			if(this.A_windows[i] && this.A_windows[i].title == name) return i;
		}
		return false;
    }


	// WM
	//--------------------------------------------------------------------------------//
	//-------------------------------- Load Workspace --------------------------------//
	//----- This function is called when a user attempts to load a workspace.    -----//
	//--------------------------------------------------------------------------------//
    this.loadDump = function(dump)
    {
		var arr = dump.split("\n");
		for(var i =0;i<arr.length;i++)
		{
		    var line = arr[i];
		    if(line.substr(0,1) == '[')
		    {
				var attrs = line.split(" ");
				var window_title = attrs[0].substr(1,attrs[0].length-2);
				var x = attrs[1].substr(0,attrs[1].length-2);
				var y = attrs[2].substr(0,attrs[2].length-2);
				var w = attrs[3].substr(0,attrs[3].length-2);
				var h = attrs[4].substr(0,attrs[4].length-2);
				var flag = true;
				var num = new RegExp('[^0-9]');
				// ORIGINAL: var str = new RegExp('[^a-z|A-Z|0-9|=|+|_|.|-]');
				var str = new RegExp('[^a-z|A-Z|0-9|=|+|_|.|,|-]'); // Allow the comma (,) character to fix species_filter comma delimiter problem. -- Ben Figueroa, 2008.07.24

				// DEBUG.
				//alert(
				//	"window_title: " + window_title +
				//	"\nx: " + x +
				//	"\ny: " + y +
				//	"\nw: " + w +
				//	"\nh: " + h
				//);

				if(x.match(num)) flag = false;
				if(y.match(num)) flag = false;
				if(w.match(num)) flag = false;
				if(h.match(num)) flag = false;
				if(window_title.match(str)) flag = false;

				if(flag == false)
				{ alert('Load Error on line ' + (i+1)); }
				else
				{
				    MCP.chooseWindow(window_title);
				    this.placeWindow(this.activeWindow,x,y);
				    this.resizeWindow(this.activeWindow,w,h);
				}
		    }
		    else
		    {
				var act = this.lookupActive();
				if(this.lookupActive().type == 'user')
				{
				    MCP.listAdd(act.listPtr,line);
				}
				else if(line == '')
				{ }
				else
				{ alert('Load Error on line ' + (i+1)); }
			}
		}
    }
	    
	// WM
    this.byIndex = function(ind)
    { return this.A_windows[ind]; }

	// WM
    this.registerWindow = function(ptr)
    {
		var index = this.A_windows.push(ptr) -1;
		return index;
    }

	// WM
    this.bringToTop = function(ptr)
    {
		if(this.byIndex(this.activeWindow) != ptr)
		{
			var temp_windows = [];
			for(var x=0;x<this.A_windows.length;x++)
			{
				if(this.A_windows[x] && ptr != this.A_windows[x]) 
				{
					temp_windows.push(this.A_windows[x]); 
				}
			}

			temp_windows.sort(function(a,b) { return a.domNode.style.zIndex - b.domNode.style.zIndex; });
			temp_windows.push(ptr);
			var startingZ = 100;
			for(x=0;x<temp_windows.length;x++)
			{
				if(temp_windows[x])
				{
					temp_windows[x].domNode.style.zIndex = startingZ + x; 
				}
			}
		}
	}

	// WM
	this.activateWindow = function(index)
	{
		if(this.activeWindow != index)
		{
		    WM.bringToTop(WM.byIndex(index));
		    Animations.fadeOut(this.activeWindow);
		    Animations.fadeIn(index);
		    this.activeWindow = index;
		}
	}
    
	// WM
	this.resetActive = function()
	{
		WM.bringToTop(WM.byIndex(0));
		Animations.fadeIn(0);
		this.activeWindow = 0;
	}
	    
	  
	// WM
	this.lookupActive = function()
	{
		return this.A_windows[this.activeWindow];
	}
    
	// WM
	this.closeWindow = function(index)
	{ 
		this.A_windows[index].closeWindow(); 
		//this.removeIndex(this.A_windows,index); 
		this.resetActive();
	}

	// WM
	this.cleanupWindow = function(index,type)
	{
		if(this.activeWindow == index)
		{
			this.resetActive();
		}
		this.removeIndex(this.A_windows,index);
	}

	// WM    
	this.removeIndex = function(arr,ind)
	{
		delete(arr[ind]);
		var ptr = arr.length;
		while(arr[ptr] == undefined && ptr > -1) {ptr --;}
		ptr ++;

		if(ptr < arr.length) 
		{
			var start = ptr;
			var len = arr.length - ptr; 
			arr.splice(start,len);
		}
		//var str = arr.length + " ";
		//for(var i =0;i<arr.length;i++) str += arr[i] + " ";
		//alert(str);
	}

	// WM
	this.findInitialPlacement = function(x,y)
	{
		// searches to find an open spot on the screen.  If a spot isn't open, shuffle down and right
		// by 15 px and try again.  If y coord > 400, restart search @ 100,20 then 200,20 etc.
		while(!this.checkPlacement(x,y))	
		{
			if(y>400)
			{
				y=20;
				x-=300;
			}
			x+=15;
			y+=15;
		}
		var returned = [x,y];
		return(returned);
	}

	// WM
	this.checkPlacement = function(x,y)
	{
		// Searches through all windows on the screen to find if they're within 10 px of the
		// proposed window's top left corner.
		var mx = x-10;
		var Mx = x+10;
		var my = y-10;
		var My = y+10;
		for(var i=0;i<this.A_windows.length;i++)
		{
			if(this.A_windows[i])
			{
				var wx = this.A_windows[i].domNode.style.left;
				var wy = this.A_windows[i].domNode.style.top;
				wx = wx.substring(0,wx.length-2);
				wy = wy.substring(0,wy.length-2);
				if(wx<Mx && wx>mx && wy<My && wy>my) 
				{	
					return false; 
				}
			}
		}
		return true;
	}

	// WM
	this.resizeWindow = function(index,x,y)
	{
		this.A_windows[index].resizeTo(x,y);
	}

	// WM
    this.placeWindow = function(index,x,y)
    {
		var wind = this.A_windows[index];
		wind.domNode.style.top = y + 'px';
		wind.domNode.style.left = x + 'px';
    }
};

