// JavaScript Document
    // Copyright 2007 www.flexcapacitor.com, www.drumbeatinsight.com 
    // Version 1.0.0
    
    // global array of html elements
    htmlControls = new Array();

	// add HTML element to the page
    function addChild(o) {
    	if (o.type=="division") {
    		addChildDivision(o);
    	}
    	else if (o.type=="iframe") {
    		addChildIFrame(o);
    	}
    	else if (o.type=="editor") {
    		addChildEditor(o);
    	}
    }
    
    // add iframe to the page
    function addChildIFrame(o) {
    
    	if (getElement(o.id)) {  
			// do nothing for now
    	}
    	
		var newElement = document.createElement("iframe");
		newElement.id = o.id;
		newElement.name = o.name;
		newElement.movieId = o.movieId;
		newElement.width = o.width;
		newElement.height = o.height;		
		newElement.frameBorder = o.frameborder;
		newElement.style.position = o.position;
		setSize(newElement,o.width,o.height);
		moveElementTo(newElement,o.x,o.y);
		//newElement.style.backgroundColor = "transparent";
		// always 0px - do not add a border to the iframe itself
		// add a child div and add a border to that or add border in mxml
		newElement.style.border = o.border;
		newElement.src = o.source;
		
		//  use innerHTML or DOM element creation methods to put content into body
		document.body.appendChild(newElement);
		
		newElement.onload = new function() {
			// set a flag so the application knows the page is loaded
			// looking for a reliable method that works cross browser
		}
    }
    
    // add division to the page
    function addChildDivision(o) {
		var newElement = document.createElement("div");
		newElement.id = o.id;
		newElement.name = o.name;
		newElement.movieId = o.movieId;
		
		newElement.style.position = o.position;
		setSize(newElement,o.width,o.height);
		moveElementTo(newElement,o.x,o.y);
		newElement.style.backgroundColor = "#" + o.backgroundColor;
		newElement.style.padding = "0px";
		newElement.style.margin = "0px";
		// always 0px - do not add a border to the container div tag
		// add a border in mxml or add a child div tag in the htmlText property and add a border to that
		newElement.style.border = o.border;
		newElement.innerHTML = o.htmlText;
		
		document.body.appendChild(newElement);
		setScrollPolicyById(o.id, o.htmlScrollPolicy);
		
		addToGlobalArray(newElement, o.type);
    }
    
    // add an editor to the page
    function addChildEditor(o) {
		var editorName = o.name + "Editor";
		var newElement = document.createElement("div");
		newElement.id = o.id;
		newElement.name = o.name;
		newElement.movieId = o.movieId;
		
		newElement.style.position = o.position;
		setSize(newElement,o.width,o.height);
		moveElementTo(newElement,o.x,o.y);
		newElement.style.backgroundColor = "#" + o.backgroundColor;
		newElement.style.padding = "0px";
		newElement.style.margin = "0px";
		newElement.style.border = o.border;
		
		var textareaElement = document.createElement("textarea");
		textareaElement.id = editorName;
		textareaElement.name = editorName;
		textareaElement.value = o.htmlText;
		setSize(textareaElement,"100%","100%");
		newElement.appendChild(textareaElement);
		
		document.body.appendChild(newElement);
		setScrollPolicyById(o.id, o.htmlScrollPolicy);
		
		// Create an instance of FCKeditor (using the target textarea as the name).
		var oFCKeditor = new FCKeditor( editorName ) ;
		oFCKeditor.BasePath = o.editorPath;
		if (o.configPath) {
			oFCKeditor.Config["CustomConfigurationsPath"] = o.configPath +"?" + ( new Date() * 1 ) ;
		}
		oFCKeditor.Width = '100%' ;
		oFCKeditor.Height = '100%' ;
		oFCKeditor.ReplaceTextarea() ;
		
		addToGlobalArray(newElement, o.type);
		
    }
	
	// add to associative array
	function addToGlobalArray(el, elementType) {
		var newElement = new Object();
		newElement.element = el;
		newElement.id = el.id;
		newElement.loaded = false;
		newElement.type = elementType;
		newElement.editor = "";
		if (elementType == "editor") {
			newElement.editor = document.getElementById(el.id + "Editor");
		}
		htmlControls[el.id] = newElement;
	}
    
	// The FCKeditor_OnComplete function is a special function called everytime an
	// editor instance is completely loaded and available for API interactions.
	function FCKeditor_OnComplete( editorInstance ) {
		var editor = document.getElementById( editorInstance.Name );
		htmlControls[editor.parentNode.id].loaded = true;
	}
	
	// gets the element by name
	function getElement(id) {
		if (htmlControls[id]) {
			return htmlControls[id].element;
		}
		return document.getElementById(id);
	}
	
	// cannot get height of pages loaded from different domains
	// ie, page is hosted at www.yoursite.com and you load www.google.com will fail with return value of -1
	// works in ff and ie. not tested in mac browsers - 
	function getElementHeight(id){
		var el = getElement(id);
		moz = (document.getElementById && !document.all);
		
		if (el){
		
			// check the height value
			try {
			
				/*** return div height ***/
				if (el.nodeName.toLowerCase()=="div") {
					var scrollHeight = el.scrollHeight;
					var divHeight = el.style.height;
					divHeight = (scrollHeight > parseInt(divHeight)) ? scrollHeight : divHeight;
					return divHeight;
				}
				
				/*** return iframe height ***/
				//moz
				if (moz) {
					return el.contentDocument.body.scrollHeight;
				}
				else if (el.Document) {
					return el.Document.body.scrollHeight;
				}
			}
			catch(e)
			{
				//An error is raised if the IFrame domain != its container's domain
				//alert('Error: ' + e.number + '; ' + e.description+'\nPossibly - Cannot access because page domain does not equal container domain');
				return -1;
			}
		}
	}

	// get property value
	function getElementValue(id, elProperty){
		
		// if periods are in the name assume absolute path 
		// otherwise assume element id
		if (id.indexOf('.')!=-1) {
			var newArr = id.split('.');
			var elValue = "";
			
			try {
				el = window;
				for (var i=0;i < newArr.length;i++) {
					el = el[newArr[i]];
				}
				return el;
			}
			catch (e) {
				//alert("Whoooops!! Cant find " + elId);
				// should return null or undefined here
				return -1;
			}
		}
		else {
			// try and get property value
			try {
				var el = getElement(id);
				var elValue = el[elProperty];
				return elValue;
			}
			catch(e) {
				//alert("Error: Can't find " + elId + "." + elProperty);
				// should return null or undefined here
				return -1;
			}
		}
	}
	
	// get HTML content
	function getHTML(id, elementType) {
		var el = getElement(id);
		if (el!=null) {
			
			if (elementType =="division") {
				return el.innerHTML;
			}
			else if (elementType == "editor") {
				var oEditor;
			
				if ( typeof( FCKeditorAPI ) != 'undefined' ) {
					oEditor = FCKeditorAPI.GetInstance( id + "Editor" );
					if ( oEditor )	{
						// Get the current text in the editor.
						return oEditor.GetXHTML();
					}
				}
			}
		}
		return "";
	}

	// get reference to the flash movie	
	function getMovieById(id) {
		if (navigator.appName.indexOf ("Microsoft") !=-1) {
			return window[id];
		} else {
			return window.document[id];
		}
	}
	
	// hide element by name
	// set height of content to 0px so the 
	// flash context menu appears in the right location
	function hide(id, hideOffscreen, offscreenOffset) {
		var el = getElement(id);
		if (hideOffscreen) {
			el.style.width = "0px";
			el.style.height = "0px";
		}
		el.style.visibility = "hidden";
	}
	
	// move element to new location
	function moveElementTo(el,x,y) {
		el.style.left = parseInt(x) + "px";
		el.style.top = parseInt(y) + "px";
	}
	
	// forces redraw
	function refresh(id) {
		var el = getElement(id);
		el.style.cssText = el.style.cssText;
	}
	
	// remove
	function remove(id) {
		hide(id, true);
		var el = getElement(id);
		//el = undefined; // not sure how to delete
	}
    
	// set document title
    function setDocumentTitle(title) {
        window.document.title = title;
        return 1;
    }
	
	// set HTML content
	function setHTML(id, htmlText, elementType) {
		var el = getElement(id);
		if (el!=null) {
		
			if (elementType =="division") {
				el.innerHTML = htmlText;
			}
			else if (elementType == "editor") {
				var oEditor;
			
				if ( typeof( FCKeditorAPI ) != 'undefined' ) {
					oEditor = FCKeditorAPI.GetInstance( id + "Editor" );
					if ( oEditor )	{
						// Set the text in the editor.
						oEditor.SetHTML( htmlText ) ;
					}
				}
			}
		}
	}
	
	// set position
	function setPosition(id,x,y) {
		var el = getElement(id);

		// LEFT
		if (x != undefined) {
			el.style.left = x + "px";
		}
		// TOP
		if (y != undefined) {
			//alert(y)
			el.style.top = y + "px";
		}
	}
	
	// set scroll policy of element
	function setScrollPolicy(el, overflow) {
		if (overflow != "resize") {
			el.style.overflow = overflow;
		}
	}
	
	// set scroll policy of element by id
	function setScrollPolicyById(id, overflow) {
		var el = getElement(id);
		
		// setting this to anything other than auto in ff fails it
		if (overflow != "resize") {
			el.style.overflow = overflow;
		}
	}
	
	// set size
	function setSize(el,w,h) {
		// WIDTH
		if (w != undefined) {
			// if width is a percentage pass in the string as is
			if (String(w).indexOf("%")!=-1) {
				el.style.width = w;
			}
			else {
				el.style.width = parseInt(w) + "px";
			}
		}
		
		// HEIGHT
		if (h!=undefined) {
			if (String(h).indexOf("%")!=-1) {
				el.style.height = h;
			}
			else {
				el.style.height = parseInt(h) + "px"; 
			}
		}
	}
	
	// set size called by id
	function setSizeByValue(id,w,h) {
		var el = getElement(id);
		if (el.style.visibility=="hidden") { return; }
		
		// WIDTH
		if (w != undefined) {
			// if width is a percentage pass in the string as is
			if (String(w).indexOf("%")!=-1) {
				el.style.width = w;
			}
			else {
				el.style.width = parseInt(w) + "px";
			}
		}
		
		// HEIGHT
		if (h!=undefined) {
			if (String(h).indexOf("%")!=-1) {
				el.style.height = h;
			}
			else {
				el.style.height = parseInt(h) + "px"; 
			}
		}
	}
	
	// set iframe location
	function setSource(id,source){
		var el = getElement(id);
		el.src = source;
	}
	
	// show element by name
	function show(id, hideOffscreen, left, width, height) {
		var el = getElement(id);
		el.style.visibility = "visible";
		if (hideOffscreen) {
			el.style.width = parseInt(width) + "px";
			el.style.height = parseInt(height) + "px";
		}
	}