// registered Object Array contains objects with following elements.
// id, type, anchorRight (true, false), anchorBottom( true, false),xoffset, yoffset, minWidth, minHeight.
//type = { element, flash}

//Instructions:  Flash objects have to have their width and height values set to 100% if they're to be resized.

var registeredObjects = new Array();
var IE = (navigator.appName.indexOf("Microsoft") != -1);
var debug = false;
function registerDynamicSizeObject(  element, type, anchorRight, anchorBottom, xOffset, yOffset, minWidth, minHeight, initialization)
{
	var registeredObject = { 
							type: "element",
							element: null,
							anchorRight: true,
							anchorBottom: true,
							xOffset: 0,
							yOffset: 0,
							minHeight: 300,
							minWidth: 700,
							timer : null,
							initializationVariables : {},
							getId: function()
								{
									if( !IE && type == "flash")
									{
										return registeredObject.element.name;
									}
									else 
									{
										return registeredObject.element.id;
									}
								},
							checkReady: function()
								{
									var id = registeredObject.getId();
									var flashready = registeredObject.flashReady();
									//debug && alert( "flash ready? " + id + " " + flashready);
									if( flashready > 0)
									{
										//debug && alert( "flash is ready, calling draw()");
										clearInterval( registeredObject.timer);
										registeredObject._draw();
									}
								},
							initialize: function()
								{
									var debug = false;
									debug && alert( "initialize() called");
									for( var item in registeredObject.initializationVariables)
									{
										registeredObject.element.setValues( item, registeredObject.initializationVariables[item]);
										debug && alert( "Set values called, item: " + item + " value: " + registeredObject.initializationVariables[item]);
									}
								},
							setValue: function(item, value)
								{
									var debug = false;
									debug && alert( "setValue() called \n" + item + "\n" + value)
									registeredObject.element.setValues(item, value);
								},
							_draw: function()
								{
									var debug = false;
									var flashready = registeredObject.flashReady();
									debug && alert(" _draw() called, flashready: " + flashready);
									if( flashready < 3)
									{
										registeredObject.initialize();
										registeredObject.element.load();
									}
									else
									{
										registeredObject.element.draw();
									}
								},
							startDraw: function()
								{
									var debug = false;
									debug && alert( "startDraw() called");
									var id = registeredObject.getId();
									var flashready = registeredObject.flashReady();
									debug && alert( id + " flash ready? " + flashready);
									if( (flashready < 1) || !IE)
									{
										debug && alert( "startDraw called");
										registeredObject.timer = setInterval( registeredObject.checkReady, 25);
									}
									else
									{
										debug && alert( "calling flash draw()");
										registeredObject._draw();
									}
								},
							reLoad: function()
								{
									var debug = false;
									var flashready = registeredObject.flashReady();
									if( flashready > 0)
									{
										registeredObject.initialize();
										registeredObject.element.load();
									}
								},
							flashReady: function()
								{
									var flashready;
									try
									{
										flashready = registeredObject.element.status();
									}
									catch( ex)
									{
										//alert( "flashready error: message: \n" +  ex.message);
										flashready = 0;
									}
									return flashready;									
								}
							
							};
	if( element)
	{
		registeredObject.element = element;
		if( type) { registeredObject.type = type;}
		if( anchorRight != null) { registeredObject.anchorRight = anchorRight; }
		if( anchorBottom != null) { registeredObject.anchorBottom = anchorBottom;}
		if(xOffset) { registeredObject.xOffset = xOffset;}
		if(yOffset) { registeredObject.yOffset = yOffset;}
		if(minWidth) { registeredObject.minWidth = minWidth;}
		if(minHeight) { registeredObject.minHeight = minHeight;}
		if(initialization) 
		{
			var args = getArgs();
			initialization.xVal = (args.xVal) ? args.xVal : initialization.xVal;
			initialization.yVal = (args.yVal) ? args.yVal : initialization.yVal;
			registeredObject.initializationVariables = initialization;
		}
	}
	registeredObjects.push( registeredObject);
}
function plotIt( chartId, yVal, xVal, scale)
{
	if( yVal) { changeChartValues( chartId, "yVal",yVal);}
	if( xVal) { changeChartValues( chartId, "xVal",xVal);}
	if( scale) { changeChartValues( chartId, "scale",scale);}
	loadChart( chartId);
	return false;
}
function changeChartValues( chartId, key,value)
{
	var robj;
	for( var i = 0; i <registeredObjects.length; i++)
	{
		robj = registeredObjects[i];
		if( robj.getId() == chartId && robj.type == "flash")
		{
			robj.setValue( key, value)
			break;
		}
	}
}
function changeChartSecurityIds( chartId, securityids )
{
	var robj;
	for( var i = 0; i < registeredObjects.length; i++)
	{
		robj = registeredObjects[i];
		if( robj.getId() == chartId && robj.type == "flash")
		{
			robj.initializationVariables["securityIds"] = securityids;
			robj.reLoad();
		}
	}
}
function loadChart( chartId)
{
	var robj;
	for( var i = 0; i <registeredObjects.length; i++)
	{
		robj = registeredObjects[i];
		if( robj.getId() == chartId && robj.type == "flash")
		{
			robj.element.load();
		}
	}
}
function resize(id)
{
	//alert( "here");
	for( var i = 0; i < registeredObjects.length; i++)
	{
		var robj = registeredObjects[i];
		if( id != null)
		{
			if( robj.getId() == id) 
			{
				SetSize( robj); 
				break;
			}				
		}
		else
		{
			
			SetSize( robj);
		}
	}
}
function SetSize( regObj)
{
	debug = false;
	var size = get_windowSize();
	var x = 0;//getX(regObj.element);
	var y = 0;//getY(regObj.element);
	var height = size.height - ( y + regObj.yOffset);
	var width = size.width - ( x + regObj.xOffset);
	if( regObj.type == "element") 
	{
		if( regObj.anchorBottom)
		{
			height = regObj.minHeight > height ? regObj.minHeight : height;
			debug && alert( "setting height to : " + height + " for " + regObj.element.id);
			regObj.element.style.height = height + "px";
		}
		if( regObj.anchorRight)
		{
			width = regObj.minWidth > width ? regObj.minWidth : width;
			debug && alert( "setting width to : " + width + " for " + regObj.element.id);
			regObj.element.style.width = width + "px";
		}
	}
	if( regObj.type == "flash")
	{
		debug && alert(
			"id:           " + regObj.element.id + "\n" +
			"type:         " + regObj.type + "\n" +
			"anchorRight:  " + regObj.anchorRight + "\n" +
			"anchorBottom: " + regObj.anchorBottom + "\n" +
			"xOffset:      " + regObj.xOffset + "\n" +
			"yOffset:      " + regObj.yOffset + "\n" +
			"minHeight:    " + regObj.minHeight + "\n" +
			"minWidth:     " + regObj.minWidth + "\n" +
			"---------------------------------------\n" + 
			"calculated Left offset: " + x + "\n" +
			"calcualted Top offset:  " + y + "\n" +
			"window size, width:     " + size.width + " height: " + size.height + "\n" +
			"calculated width:       " + width + "\n" +
			"calculated height:      " + height + "\n"
		); 
		regObj.startDraw();
	}
	if( regObj.type == "table")
	{
		var rootdiv = document.getElementById("RootDiv");
		var contentwell = document.getElementById("ContentWell");
		var newWidth = 320 + regObj.element.rows[0].cells.length  * 80 ;
		newWidth = (newWidth < 790 ? 790 : newWidth);
		rootdiv.style.width = (parseInt( rootdiv.style.width) > newWidth + 207 ? parseInt( rootdiv.style.width) : newWidth + 207) + "px";
		contentwell.style.width = (parseInt( contentwell.style.width) > newWidth ? parseInt( contentwell.style.width) : newWidth) + "px";
	}
	debug = false;
}
function thisMovie(movieName) 
{
	if( IE && document.forms.length > 0)
	{
		return document.forms[0][movieName];
	}
	if (IE) 
	{
		return window[movieName];
	} 
	else 
	{
		return document[movieName];
	}
}

function get_windowSize()
{
	var size = { width: 0, height: 0};
	if( !IE )
	{ //Non-IE
		size.height = window.innerHeight - 25;
		size.width = window.innerWidth - 20;
	}
	else
	{
		if ( document.documentElement && ( document.documentElement.clientWidth ||    document.documentElement.clientHeight ) )
		{ //IE 6+ in 'standards compliant mode'
			size.height = document.documentElement.clientHeight - 2;
			size.width = document.documentElement.clientWidth;
		}
		else
		{
			if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
			{ //IE 4 compatible
				size.height = document.body.clientHeight - 2;
				size.width = document.body.clientWidth;
			}
		}
	}
	return size;
}

function getX(e) 
{
	var x = 0;
	while( e) 
	{
		x += e.offsetLeft;
		e = e.offsetParent;
	}
	return x;
}
function getY(e) 
{
	var y = 0;
	while( e) 
	{
		y += e.offsetTop;
		e = e.offsetParent;
	}
	return y;
}
