
//widget object
function Widget(_title, _url, _intro, initialX, initialY, targetX, targetY, _height, _width)
{
	this.Index = Wmgr.CurrentWidgetIndex+=1;
	
	//events
	this.MouseOver = false;
	this.Docked = false;
	this.onDock = function() 		{	this.Docked = true;	};
	this.onUnDock = function()		{	this.Docked = false; this.Paused = false; 
	
	
							
							this.onMouseOffWidget();	
							this.DIV.firstChild.onmouseout = new Function("Wmgr.ExecCallback('"+this.Name+"','onMouseOffWidgetBuffer')");
							this.DIV.firstChild.onmouseover = new Function("Wmgr.ExecCallback('"+this.Name+"','onMouseOnWidget')");
							this.DIV.firstChild.onmousemove = new Function("Wmgr.ExecCallback('"+this.Name+"','onMouseOnWidget')");
							this.DIV.lastChild.onmouseout = new Function("Wmgr.ExecCallback('"+this.Name+"','onMouseOffWidget')");
							this.DIV.lastChild.onclick = new Function("Wmgr.ExecCallback('"+this.Name+"','onWidgetClick')");
							
							Tween.AnimateSize(this, null);
						};
	this.onMouseOnWidget = function()	{	//show description & pause float animation
							if(!this.Docked) 
							{
								if(Wmgr.SelectedWidget!=this&&Wmgr.SelectedWidget!=null)
								{
									Wmgr.SelectedWidget.onMouseOffWidget();
									Wmgr.SelectedWidget = this;
								}
								else if(Wmgr.SelectedWidget==null)
									Wmgr.SelectedWidget = this;
								this.MouseOver = true;
								this.DIV.style.zIndex = GLOBAL_MAX_Z_INDEX; 
								this.Paused = true;

								this.DIV.childNodes[1].style.top = 0+"px";
							}
							else
							{
								this.DIV.style.zIndex = GLOBAL_MAX_Z_INDEX; 
								this.DIV.childNodes[1].style.top = 0+"px";
							}
							
						};
	this.onMouseOffWidget = function()	{	//hide description & resume float animation
							this.MouseOver = false;
							this.DIV.style.zIndex = this.Zindex; 
							this.Paused = false; 
							/*if(!this.Docked) this.onWidgetFloat();*/

							this.DIV.childNodes[1].style.top = 100+"%";
						};
	this.onMouseOffWidgetBuffer = function() {this.MouseOver = false; };
	this.onWidgetBuffer = function(){};
	this.onWidgetClick = function()		{
							if(this.ReverseLinking==null)
							{
								//init white box animation
								var whitebox = document.createElement("DIV");
								    whitebox.appendChild(document.createTextNode(" "));
								    whitebox.className = "whitebox";
								    whitebox.style.height = this.DefaultHeight+"px";
								    whitebox.style.width = this.DefaultWidth+"px";
								    whitebox.style.top = this.YPos+"px";
								    whitebox.style.left = this.XPos+"px";
								    whitebox.style.zIndex = GLOBAL_Z_INDEX+=1;
								    whitebox.style.display = "none";
								//add to window
								document.body.appendChild(whitebox);
								Transparency.SetOpacity(whitebox, 0);
								whitebox.style.display = "";

								//set target dimensions for animation parameters
								whitebox.Height = this.DefaultHeight;
								whitebox.Width = this.DefaultWidth;
								whitebox.TargetHeight = GLOBAL_WHITEBOX_HEIGHT;
								whitebox.TargetWidth = GLOBAL_WHITEBOX_WIDTH;
								whitebox.XPos = this.XPos;
								whitebox.YPos = this.YPos;
								whitebox.TargetXPos = GLOBAL_WHITEBOX_APPEAR_AT_X;
								whitebox.TargetYPos = GLOBAL_WHITEBOX_APPEAR_AT_Y;
								whitebox.Name = "";
								whitebox.Linking = this;
								this.ReverseLinking = whitebox;
								//invoke animations
								Tween.AnimateDOM("whitebox_"+this.Name, whitebox, null);
								Tween.AnimateDOMResize("whitebox_"+this.Name, whitebox, this.onShowPreloaderLogo);
								Transparency.FadeIn("whitebox_"+this.Name, whitebox, null);
								Wmgr.List.foreach(function(arg){arg.Paused = true;}); //pause floats to conserve resources
								
								var _callback="Tween.DOM_Sprites['whitebox_"+this.Name+"'].Linking.onContentLoadStart()";
								Wmgr.List[this.Index] = this;
								Wmgr.List[this.Index].ReverseLinking = whitebox;
							}
							else
								this.ReverseLinking.style.zIndex = GLOBAL_Z_INDEX+=1;
						};
	this.onShowPreloaderLogo = function() 	{ 
							var _callback="Tween.DOM_Sprites['whitebox_"+this.Linking.Name+"'].Linking.onContentLoadStart()";
							//make preloader the flash movie
							this.appendChild(Flash.GenerateMovieObj("flash/logo.swf?callback="+_callback, "100%", 70, "#000000", true)); 
							
						};
	this.onContentLoadStart = function() 	{
							AJAX_SetTarget(this, this.onContentLoaded, this.AjaxURL, "GET", null);
						};
	this.onContentLoaded = function(wgz, xmlHttp){
							//render content into panel
							var content = DOM.Create("DIV");
							    content.className="content";
							    content.innerHTML=xmlHttp.responseText;
							    
							DOM.Clear(wgz.ReverseLinking);    
							
							
							
							
							//create container for panel controls
							var cpanel = DOM.Create("DIV");
							    cpanel.className = "controlpanel";
							    cpanel.style.top - "-300px";
							    cpanel.XPos = 0;
							    cpanel.YPos = -300;
							    cpanel.TargetXPos = 0;
							    cpanel.TargetYPos = 0;
							    
							var ctclose = DOM.Create("IMG");
							    ctclose.src = "images/panel_close.gif";
							    ctclose.onclick = wgz.onContentClose;
							    ctclose.style.cursor = "pointer";
							    ctclose.setAttribute("title","Close Panel");
							
							cpanel.appendChild(ctclose);
							wgz.ReverseLinking.appendChild(cpanel);
							wgz.ReverseLinking.appendChild(content);
							cpanel.style.top = "-300px";
							Tween.AnimateDOM("cpanel_"+wgz.Name, cpanel, null);
							
							Wmgr.List.foreach(function(arg)
							{
								Panel.Dock(arg);
							}); //dock widgets
						};
	this.onContentClose = function()	{
							try{
								this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
								this.parentNode.parentNode.Linking.ReverseLinking = null;							    					
							} 
							catch(eee) 
							{
								this.ReverseLinking.parentNode.removeChild(this.ReverseLinking);
								this.ReverseLinking = null;							    					
							}
							Wmgr.List.foreach(function(arg)
							{
								Panel.UnDock(arg);
							}); //dock widgets
						};
	this.onWidgetFloat = function()		{
							if(!this.Paused)
							{
								/*
								//store default dimensional state if not cached
								if(this.VectorPath.length<1)
								{
									this.DefaultHeight = this.Height;
									this.DefaultWidth = this.Width;
									this.DefaultYPos = this.YPos;
									this.DefaultXPos = this.XPos;

									//store list of cached vector path points
									var iNumOfVectorPathPoints = 100;
									for(var i=0;i<iNumOfVectorPathPoints;i++)
										this.VectorPath[i] = {"x":Random.Generate(this.XPos-20,this.XPos+20), "y":Random.Generate(this.YPos-10,this.YPos+10)};
								}
								var vectorPoint = this.VectorPath[Random.Generate(0,(this.VectorPath.length-1))];
								this.TargetXPos = vectorPoint.x;
								this.TargetYPos = vectorPoint.y;
								Tween.AnimatePos(this,this.onWidgetFloat, true, 100);
								*/
							}
							
						};
	this.onWidgetFinishSize = function()	{
							//set to bigger boundary
							this.TargetHeight = this.Height-10;
							this.TargetWidth = this.Width-10;
							
							Tween.AnimateSize(this, null);
							
							//add click events
							this.DIV.firstChild.onmouseout = new Function("Wmgr.ExecCallback('"+this.Name+"','onMouseOffWidgetBuffer')");
							this.DIV.firstChild.onmouseover = new Function("Wmgr.ExecCallback('"+this.Name+"','onMouseOnWidget')");
							this.DIV.firstChild.onmousemove = new Function("Wmgr.ExecCallback('"+this.Name+"','onMouseOnWidget')");
							this.DIV.lastChild.onmouseout = new Function("Wmgr.ExecCallback('"+this.Name+"','onMouseOffWidget')");
							this.DIV.lastChild.onclick = new Function("Wmgr.ExecCallback('"+this.Name+"','onWidgetClick')");
						};
	this.onWidgetBounceExpand = function()	{
							//set to bigger boundary
							this.TargetHeight = this.DefaultHeight+10;
							this.TargetWidth = this.DefaultWidth+10;
							Tween.AnimateSize(this, this.onWidgetFinishSize);
						};
	this.onWidgetBounceCompress = function(){	//set to smaller size
							this.TargetHeight = GLOBAL_INTRO_HEIGHT-50;
							this.TargetWidth = GLOBAL_INTRO_WIDTH-50;
							Tween.AnimateSize(this, this.onWidgetBounceExpand);
						};

	var blnFoundCSS = false;

	do{
		this.CSS = Wmgr.CSSRange[Random.Generate(0,Wmgr.CSSRange.length-1)];
		if(Wmgr.CSSSelectedRange[this.CSS]==null)
		{
			blnFoundCSS = true;
			Wmgr.CSSSelectedRange[this.CSS] = 1;
		}
	}
	while(!blnFoundCSS)

	//dimensions(pos)
	this.VectorPath = [];
	this.XPos = initialX;
	this.YPos = initialY;
	this.DefaultXPos = targetX;
	this.DefaultYPos = targetY;
	this.TargetXPos = targetX;
	this.TargetYPos = targetY;
	
	//dimensions(depth)
	this.Height = GLOBAL_INTRO_HEIGHT;
	this.Width = GLOBAL_INTRO_WIDTH;
	this.DefaultHeight = _height;//runtime generated value
	this.DefaultWidth = _width;//runtime generated value
	this.TargetHeight = GLOBAL_INTRO_HEIGHT;//runtime generated value
	this.TargetWidth = GLOBAL_INTRO_WIDTH;//runtime generated value
	
	this.Paused = false;
	this.ThreadAnimPos = null;
	this.ThreadAnimDimension = null;
	this.AjaxURL = _url;
	this.Intro = _intro;
	this.Name = _title;
	this.Zindex = GLOBAL_Z_INDEX+=1;
	this.AnimateIntro = function()
	{
		//create dom object
		this.DIV = Wmgr.CreateWidgetHTML(this.Name, this.AjaxURL, this.Intro, this.Zindex);
		this.DIV.className = this.CSS; //set the widgets class in css

		//set size
		this.DIV.style.height = this.Height+"px";
		this.DIV.style.width = this.Width+"px";

		//set position
		this.DIV.style.position = "absolute";

		this.DIV.style.left = this.XPos+"px";
		this.DIV.style.top = this.YPos+"px";

		Tween.AnimatePos(this, this.onWidgetBounceCompress, false, null);
	};
	this.DIV = null;
}


//widget manager
var Wmgr =
{
	SelectedWidget : null,
	CSSSelectedRange : [],
	CSSRange : ["green","purple","red","blue","blueshade","default","black"],
	List : [],
	CurrentWidgetIndex : -1,
	Init : function()
	{
		this.List.foreach(function(wgz){wgz.AnimateIntro();}); //loop through all items and show individual screen movements
		//set the last index
		GLOBAL_MAX_Z_INDEX = GLOBAL_Z_INDEX+=1;
	},
	CreateWidgetHTML : function(_name, _url, _intro, Zindex)
	{
		try{
			var div = document.createElement("DIV");
				div.style.zIndex = Zindex;
				
				
				//create title
				var ttldiv = document.createElement("DIV");
					ttldiv.className = "titlebox";
					ttldiv.appendChild(document.createElement("H2"));
					ttldiv.firstChild.onclick = function() { return false; };
					ttldiv.setAttribute("align","center");
					ttldiv.style.height="100%";
					ttldiv.style.width="100%";
					ttldiv.firstChild.appendChild(document.createTextNode(_name));
					//ttldiv.onclick = function(){Wmgr.ExecCallback(_name,"onWidgetClick");};
					//ttldiv.onmouseout = function(){Wmgr.ExecCallback(_name,"onMouseOffWidgetBuffer");};
					//ttldiv.onmouseover = function(){Wmgr.ExecCallback(_name,"onMouseOnWidget");};
				div.appendChild(ttldiv);
				
				var introdiv = document.createElement("DIV");
					introdiv.className = "introbox";
					introdiv.style.zIndex = GLOBAL_Z_INDEX+=1;
					//introdiv.onmouseout = function(){Wmgr.ExecCallback(_name,"onMouseOffWidget");};
					//introdiv.onclick = function(){Wmgr.ExecCallback(_name,"onWidgetClick");};
					introdiv.innerHTML = _intro;
				div.appendChild(introdiv);
				
				
			document.body.appendChild(div);
			Transparency.SetOpacity(introdiv, 85);
			return div;
		} catch(ee) { alert(ee.message); }
	},
	ExecCallback : function(_name,_evthandler)
	{
		this.List.foreach(function(wgz){
			if(wgz.Name==_name)
			{
				switch(_evthandler)
				{
					case "onWidgetClick": wgz.onWidgetClick(); break;
					case "onMouseOnWidget": wgz.onMouseOnWidget(); break;
					case "onMouseOffWidget": wgz.onMouseOffWidget(); break;
					case "onMouseOffWidgetBuffer" : wgz.onMouseOffWidgetBuffer(); break;
				}

			}
		});
	}
};
