﻿///<reference path="../Asserter.js"/>

var njs;
if (!njs) njs = {};
if (!njs.ui) njs.ui = {};

//Creates new Element associated with the container specified in the constructor
njs.ui.Element = function (container) 
{
    njs.ui.Element.Instance = this;
    this.Type = 'njs.ui.Element';
    this.ClassAttributeName = window.ActiveXObject? 'className':'class';    
    this.Container = (typeof container == 'string')? $get(container):container;
}

///Instance Methods

///Adds another Element to this Element
njs.ui.Element.prototype.Add = function(element)
{
    this.AddElement(element.Container);
}

 ///Adds DOM Element to this Element
njs.ui.Element.prototype.AddElement = function(element)
{
    this.Container.appendChild(element);
}

///Adds an Event to this Element

njs.ui.Element.prototype.AddEvent = function(eventType, eventFunction, capture, callingElement)
{
    if (njs.Asserter.IsNullOrEmpty(eventFunction)) return;
    
    if (capture == "undefined") 
	    capture = false;
	
	var funcCheck = /"function()"/;
	var passedFunction =    (typeof eventFunction == "string" && funcCheck.test(eventFunction)) 
	                            ? eval(eventFunction) 
	                            : (typeof eventFunction == "string" && !funcCheck.test(eventFunction)) 
	                            ? Function(eventFunction) 
	                            : eventFunction;
	try 
	{
	    if(typeof this.Container.addEventListener != "undefined")
	    { 
		    this.Container.addEventListener(eventType, passedFunction, capture);
		}
		else if(typeof this.Container.attachEvent != "undefined")
		{
		    if(typeof callingElement != "undefined")
		    {
		        var thisListener = function(e){eventFunction.call(callingElement, e);};
			    this.Container.attachEvent('on' + eventType, thisListener);
			}
			else
			{
			    this.Container.attachEvent('on' + eventType, eventFunction);
			}
	    }
	    else
	    {
	        this.Container['on' + eventType] = eventFunction;
	    }
	}
	catch (e) 
	{
	} 
}

njs.ui.Element.prototype.RemoveEvent = function(eventType, eventFunction, capture)
{
    if (!capture) 
	    capture = false;
	
	var funcCheck = /"function()"/;
	var passedFunction = (typeof eventFunction == "string" && funcCheck.test(eventFunction)) ?
			                eval(eventFunction) : 
			             (typeof eventFunction == "string" && !funcCheck.test(eventFunction)) ?
			                Function(eventFunction) : eventFunction;

    try 
    {
	    if (this.Container.addEventListener) 
		    this.Container.removeEventListener(eventType, passedFunction, capture);
		else 
			this.Container.detachEvent('on' + eventType,passedFunction)
	}
	catch (e) {} 
}
///Adds a text node to this Element
njs.ui.Element.prototype.AddText = function(text)
{
    this.AddElement(document.createTextNode(text));
}

njs.ui.Element.prototype.AddHtml = function(htmlText)
{
    if(typeof htmlText != 'string')
        return;
        
    this.Container.innerHTML = htmlText;
}

njs.ui.Element.prototype.GetHtml = function()
{
    return this.Container.innerHTML;
}

njs.ui.Element.prototype.GetText = function()
{
    return this.Container.textContent || this.Container.innerText;
}

///Removes all children nodes
njs.ui.Element.prototype.Clear = function()
{
    while (this.Container.hasChildNodes()) {
        this.Container.removeChild(this.Container.childNodes[0]);
    }
}

///Gets Attribute
njs.ui.Element.prototype.GetAttribute = function (key)
{
    return this.Container.getAttribute(key);
}  

///Gets CSS class of the internal element. 
///Note that by convention, this will be equal to 'ElementClass-ChildClass'
njs.ui.Element.prototype.GetChildClass = function (childClassName)
{
    return this.GetClass() + '-' + childClassName ;
} 

///Gets CSS class
njs.ui.Element.prototype.GetClass = function ()
{
    return this.Container.className;
} 

///Gets CSS class
njs.ui.Element.prototype.GetTag = function ()
{
    return this.Container.tagName;
} 

///Gets CSS class
njs.ui.Element.prototype.GetType = function ()
{
    return this.Type;
} 

njs.ui.Element.prototype.IsEmpty = function()
{
    return !(this.HasChild() && (this.Container.innerHTML.trim().length > 0)) ;
}
 
njs.ui.Element.prototype.HasChild = function() {
    return this.Container.hasChildNodes();
}

///Sets Attribute
njs.ui.Element.prototype.SetAttribute = function (key, value)
{
    this.Container.setAttribute(key, value);
}

///Gets Attribute
njs.ui.Element.prototype.GetAttribute = function(key) {
    return this.Container.getAttribute(key);
}

///Sets CSS class
njs.ui.Element.prototype.SetClass = function(className) 
{
    this.Container.className = className;
    //this.SetAttribute(this.ClassAttributeName, className);
}   

///Adds class to current CSS class

njs.ui.Element.prototype.AddClass = function(className) 
{
    this.Container.className += className;
    //var _currentClass = this.GetAttribute("class") || "";
    //this.SetAttribute(this.ClassAttributeName, className + " " + _currentClass);
}

///Sets ID
njs.ui.Element.prototype.SetID = function (id)
{
    this.SetAttribute('id', id);
}  

    
///Sets the Type, which is useful for reflection
njs.ui.Element.prototype.SetType = function (typeName)
{
    this.Type = typeName;
}  
    
///Sets visibility
njs.ui.Element.prototype.SetVisibility = function(isVisible, visibleClassNameOverride) {
    if (isVisible == false) {
        this.VisibleClassName = visibleClassNameOverride || this.GetClass();
        this.SetClass("elementHidden");
    }
    else if ((isVisible == true) && (this.GetClass() == "elementHidden")) {
        this.SetClass(this.VisibleClassName);
    }
}