// Chris Pyper 2006

// This file contain functions that wrap yahoo user interface library function. This allows us to 
// swap out any functionality with our own if need be, or modify any of yahoos without interfering with
// thier code.

// This function will return styles even if they have not been iniated via inline styles or style sheets
// Built around the yahoo developer library
// ***WARNING*** - Javascript has issues in IE dynamically fetching 'position' attribute, even YAHOO libary can't
// pull if off so you have to manually use DOM to fetch.
function getStyle(doc, styleName, removePX)
{
	var style = YAHOO.util.Dom.getStyle(doc, styleName);
	
	if((styleName == 'zIndex' || styleName == 'top' || styleName == 'left') && style == null)
		style = '0';

	if(styleName == 'height' && style == null)
		style = doc.offsetHeight;

	if(styleName == 'width' && style == null)
        style = doc.offsetWidth;
	
	if(removePX)
	{
		style = style.toString();
		style = parseInt(style.replace('px', ''));
	}
		
	debug("getStyle(): " + styleName + " = " + style, 'info');
	return style;
}

// This function will add a inline CSS style to a DOM node
// Built around the yahoo developer library
function setStyle(doc, styleName, value, addPX)
{
	// Commented out except for when needed, this function is called a lot so if clobber's the debugger
	//debug("setStyle(): " + styleName + " = " + value, 'info');
	
	if(addPX)
		value = value + "px";
		
	return YAHOO.util.Dom.setStyle(doc, styleName, value);
}

// This function adds style classes to a DOM node.
// Built around the yahoo developer library
function addClass(docRef, className)
{
	debug("addClass(): Class = " + className, 'info');
	return YAHOO.util.Dom.addClass(docRef, className);
}

// This function removes style classes from a DOM node.
// Built around the yahoo developer library
function removeClass(docRef, className)
{
	debug("removeClass(): Class = " + className, 'info');
	return YAHOO.util.Dom.removeClass(docRef, className);
}

// Assign an unique id to a element, necessary for undo/redo tracking
function generateId(docRef, force)
{
	debug("generateId()", 'info');

	var duplicate = true;

	// Clean up ID's if necessary, some old sites might have old id styles
	if(!docRef.getAttribute('ESWID', 0) && docRef.id && docRef.id.search(IDPREFIX) >= 0 && !force)
	{
		docRef.setAttribute('ESWID', docRef.id);
		return true;
	}

	// If unique id exists
    if(docRef.getAttribute('ESWID', 0) && !force)
        return true;

	// Hack time, it would be better to make this change in the YUI library but that is against
	// the Pyper commandments of programming. Basically we got some issues, it just keeps a counter but
	// does not check if it already exists.  This sucks, got to hack to fix
	
	// Cached current id
	var currentId = docRef.id;

	while(duplicate)
	{
		// Get generated id	
		var id = YAHOO.util.Dom.generateId(docRef, IDPREFIX);

		// Set a temporary id, in case these is a duplicate this will keep from seeing the current one when you search
		docRef.id = '';
		docRef.setAttribute('ESWID', '');	

		if(!getElementByESWId(id))
		{
			duplicate = false;
			// Overwrite current ID only if it is a generated ID
			if(currentId && currentId.search(IDPREFIX) < 0)
				docRef.id = currentId;
			else
				docRef.id = id;
			docRef.setAttribute('ESWID', id);
		}
	}	
	return id;
}

// Fetch element by our own unique id, we build our own id system to allow templates to define id based css classes
function getElementByESWId(id)
{
	debug("getElementByESWId()", 'info');

	// Use built in fast search for id, then revert to home mode id
	if(document.getElementById(id))
		return document.getElementById(id);

	// Very cool recursive function to takes a function reference as arguament, use a function to match our id's
    var docArray = YAHOO.util.Dom.getElementsBy(testESWId);

	// Take returned array and return first matching element
    if(docArray && docArray.length > 0)
    {
       	for(var i = 0; i < docArray.length; ++i)
       	{
           	if(docArray[i].getAttribute('ESWID', 0) == id)
               	return docArray[i];
       	}
    }

	return false;
}

// Function to test if selected node if what we are looking for
function testESWId(docObj)
{
	if(docObj && docObj.getAttribute('ESWID', 0))
		return true;

	return false;
}

// Fetch id of passed object, give preference to our own id system
function getId(docObj)
{
	debug("getId()", 'info');

	if ( typeof(docObj) == "string" )
	{
//	  if ( docObj.indexOf(IDPREFIX) > -1 )
		return docObj;
	}

	if(docObj && docObj.getAttribute('ESWID', 0))
		return docObj.getAttribute('ESWID', 0);

	if(docObj && docObj.id)
		return docObj.id;

	return false;
}

// Fetch an attribute base on name attribute
function getElementByNameAttribute(name)
{
	debug("getElementByNameAttribute()", 'info');

	var docArray = YAHOO.util.Dom.getElementsBy(testNameAttribute);

	if(docArray && docArray.length > 0)
	{
		for(var i = 0; i < docArray.length; ++i)
		{
			if(docArray[i].getAttribute('name', 0) == name)
				return docArray[i];
		}
	}
	return false;
}

// Function to test if selected node is is what we are looking for
function testNameAttribute(docObj)
{
	if(docObj && docObj.getAttribute('name', 0))
		return true;

	return false;
}

// Function to fetch , fuck it, who is gonna read this anyway...
function removeSpellCheckHighlighting(docRef)
{
	debug('removeSpellCheckHighlighting()', 'info');

	if(!docRef && !docSourceClone)
		return false;

	// Use currently edited content
	if(!docRef && docSourceClone)
		docRef = docSourceClone;	

	// Check only span tags, use the edited element as root
	var docArray = YAHOO.util.Dom.getElementsBy(testSpellCheck, 'span', docRef);

    // Take returned array and return first matching element
    if(docArray && docArray.length > 0)
    {
        for(var i = 0; i < docArray.length; ++i)
        {
			var replacement = document.createTextNode(docArray[i].innerHTML);
    		docArray[i].parentNode.replaceChild(replacement, docArray[i]);
        }
    }

    return false;
}

function testSpellCheck(docObj)
{
	if(docObj && docObj.className && docObj.className.search(/spellCheckWord/gi) >= 0)
		return true;

	return false;
}


