/* Constants for node types, since IE doesn't support Node.TEXT_NODE */
var TEXT_NODE = 3;
var ELEMENT_NODE = 1;

function createElementandAppend (nodeName, strId, appendTo) 
// - Create an element of the given nodeName (e.g. div) and ID 
// - If "appendto" is provided, add the new element to it, 
// else append it to the document body
{
	var ele = document.createElement (nodeName);
  	ele.setAttribute ("id", strId);
  	if (appendTo && (appendTo != ""))
    	appendTo.appendChild (ele); 
  	else
    	document.body.appendChild (ele);
		
  	return ele; 
}

function createElementandInsertBefore (nodeName, strId, appendTo, sibling) 
// - Create an element of the given nodeName, give it the given strId and 
// insert it before the given sibling. Use the appendTo object if given.
{
  	var ele = document.createElement (nodeName);
  	ele.setAttribute ("id", strId);
  	if (appendTo)
    	appendTo.insertBefore (ele, sibling); 
  	else
    	document.body.insertBefore (ele, sibling); 

	return ele; 
}

function getIFrameDocument (iframe)
// - Return the document object of the given iframe
{
  	if (Detect.IE()) 
  		return iframe.document;
  	else
    	return iframe.contentDocument;
}

function getIFrame (strId)
// - Return the iframe identified by the given ID
{
  	if (Detect.IE()) 
  		return document.frames[strId];
  	else
    	return document.getElementById(strId);
}

function setIFrameBody(iframe, strStyle, innerHtml) 
// - Add a body with the given innerHtml and the given style to the given iframe
{
	//var iframeDoc = getIFrameDocument(iframe);
	//var iframeWin = iframe.contentWindow;
	//iframeWin.onfocus = setIframeFocus;
	//setIFrameEvent (iframe, "focus", setIframeFocus);
	
	if (!innerHtml) 
		innerHtml = '';

	if ((innerHtml == '') && Detect.IE())
    	innerHtml = '<div></div>';

  	var iframeDoc = getIFrameDocument (iframe);
  	iframeDoc.open ();
	iframeDoc.write ('<body style="' + strStyle + '">' + innerHtml + '</body>');
  	iframeDoc.close ();
	attachStylesheet (iframeDoc);
	//var iframeWin = iframe.contentWindow;
	//iframeDoc.onfocus = setIframeFocus;
	//setIFrameEvent (iframe, "focus", setIframeFocus);
}

function setIframeFocus ()
{
	var iframe = document.getElementById (VisEd.frameId);
  	if (Detect.IE()) 
  		iframe.contentWindow.focus();
  	else
    	iframe.contentDocument.body.focus();
}

function attachStylesheet (iframeDoc)
{
	var cssFile = "http://www" + getDomain ("") + "/css/SymtaxPages.css";
	var objStylesheet = iframeDoc.createElement ("LINK");
	objStylesheet.rel = "stylesheet";
	objStylesheet.type = "text/css";
	objStylesheet.href = cssFile;
	var objHead = iframeDoc.getElementsByTagName ('head' )[0];
	if (objHead)
		objHead.appendChild (objStylesheet);
}


function getElement (id) 
// - Returns an element identified by its ID
{
  	var ele = d(id);
  	if (!ele) 
    	alert ("Can't locate: " + id);

	return ele;
}

function d (id)
// - Return the element corresponding to the given id
{
	return document.getElementById (id);
}

function setIFrameEvent (iframe, eventName, func)
// - Add an event listener to the given iframe for events of the given name 
// and calling the given function when the event arises
{
  	if (document.all)
    	eval ('getIFrameDocument(iframe).on' + eventName + ' = func;');
  	else
    	iframe.contentWindow.addEventListener (eventName, func, true);
}

function hideElement (obj)
// - Stops the given element from being rendered
{
  	if (obj && obj.style)
  		obj.style.display = "none";
}

function showElement (obj)
// - Show a previously hidden element
{
	if (obj && obj.style)
	{
  		obj.style.display = getDisplayStyleByTagName (obj);
	}
}

function getDisplayStyleByTagName (obj)
// - Determine the display style for the given object.
// - For span, img and anchor return "inline" else "block"
{
  	nodeName = obj.nodeName.toLowerCase(); 
  	return ((nodeName == "span") || (nodeName == "img") || (nodeName == "a")) ? "inline" : "block";
}

function getStyle (el, style)
// - Returns the named style of the given element
// - NB Doesn't work too well in Safari
{
  	if (!document.getElementById || !el)
  		return;
  
  	if (document.defaultView && document.defaultView.getComputedStyle)
      return document.defaultView.getComputedStyle(el, "").getPropertyValue(style);

	else if (el.currentStyle) 
  	   return el.currentStyle[style];

	else 
       return el.style.display;
}

function getStyleAttribute(node) 
// - Get the style attribute of the given node
{
  	if (Detect.IE())
    	return node.getAttribute('style').value;
  	else
    	return node.getAttribute('style');
}

function showProps(obj)
// - Show all the properties for the given object
{
	var props = ""; 
	for (p in obj)
		props += (p + ": " + obj[p] + "\n<br />");

	document.write (props);
}

function getXY (ele)
// Return an array with the x and y co-ordinates of the given element e.g.
//  	var coord = getXY (obj);
// 	alert (coord.x + "" + coord.y);
{
  	var x = ele.offsetLeft;
  	var y = ele.offsetTop;
  	if (ele.offsetParent != null) 
  	{
    	var pos = getXY (ele.offsetParent);
    	x += pos.x;
    	y += pos.y;
  	}
  	return {x: x, y: y}
}

function dE (obj, tag)
// - Return a list of all elements in the given object of the given tag name e.g. "img"
{
	return obj.getElementsByTagName (tag);
}

function setInnerHTML (id, html) 
// - Set the inner html of the element identified bu=y its ID to the given html
{
  	try
	{
    	getElement(id).innerHTML = html;
	}
  	catch (ex)
	{
    	alert("Cannot set inner HTML: " + id);
	}
}

function removeClass (ele, clss) 
// - Remove the given class from given element
{
  	if (ele.className == null) 
  		return;
		
  	var classes = ele.className.split(" ");
  	var result = [];
  	var changed = false;
  	for (var i = 0; i < classes.length; i++) 
  	{
    	if (classes[i] != clss) 
		{
      	if (classes[i]) 
	  			result.push(classes[i]);
    	} 
		else 
      	changed = true;
  	} 

  	if (changed) 
  		ele.className = result.join(" ");
}

function hasClass (ele, clss)
// - Return true if the given element has the given class
{
  	if ((ele == null) || (ele.className == null)) 
    	return false;
	
  	var classes = ele.className.split(" ");
  	for (var i = 0; i < classes.length; i++) 
  	{
    	if (classes[i] == clss)
      	return true;
  	}
  	return false;
}

function addClass (ele, clss) 
// - Add the given class to the given element
{
  	if (hasClass(ele, clss)) 
  		return;
  	ele.className += " " + clss;
} 



