/*
 * $Id: CssHelper.js,v 1.1 2010/07/20 17:26:24 ggortsema Exp $
 * Copyright (C) 2007 IP Labs GmbH <http://www.iplabs.de/>
 * All rights reserved.
 */

jade.require("aspadmin");
jade.require("aspadmin.AspHelp");
 
/**
 * helper css methods
 * 
 * @author Vladimir Sakhovski
 * @version $Revision: 1.1 $
 */

aspadmin.CssHelper = function(){}

/** returns whether an element has a class */
aspadmin.CssHelper.hasClass = function(elementOrId, className) {
	var element = elementOrId;
	if (typeof elementOrId == 'string') { element = document.getElementById(elementOrId); }
	if (!element.className) return false;
   	return (" " + element.className + " ").indexOf(" " + className + " ") != -1;
};

/** adds a class to an element, maybe a second time */
aspadmin.CssHelper.addClass = function(elementOrId, className) {
	var element = elementOrId;
	if (typeof elementOrId == 'string') { element = document.getElementById(elementOrId); }
	if (aspadmin.CssHelper.hasClass(element, className))  return;
	var old = element.className ? element.className : "";
	element.className = old + " " + className;
};

/** removes a class from an element */
aspadmin.CssHelper.removeClass = function(elementOrId, className) {
	var element = elementOrId;
	if (typeof elementOrId == 'string') { element = document.getElementById(elementOrId); }
	var old = element.className ? element.className : "";
	element.className = old.replace(className, "");
};

/** 
 * sets style.display for specified element, 
 * if element does not exists, silently ignore. 
 */
aspadmin.CssHelper.setDisplay = function(elementOrId, display)
{
	var el = elementOrId;
	if (typeof elementOrId == 'string') { el = document.getElementById(elementOrId); }
	if (!el) { return; }
	el.style.display = display;
};


/** 
 * Removes all children from specified element. 
 */
aspadmin.CssHelper.removeAllChildren = function(elementOrId)
{
	var el = elementOrId;
	if (typeof elementOrId == 'string') { el = document.getElementById(elementOrId); }
	while (el.hasChildNodes()) {
		el.removeChild(el.firstChild);
	}
};

/** 
 * Append an element to the parent,
 * fill it with the optional text.
 *
 * @parameter parentEl parent element to which newly created element should belong
 * @parameter newEl an Object with the settings for the element to be created
 * 					the following attributes can be set:
 *		elType - required, type of the new element like TR, INPUT, etc.
 *		common elements attrs : className, id, onclick, onmouseover, onmouseout
 *		INPUT elements attrs : type, value, checked, disabled
 *		IMG elements attrs : src
 *					
 * 		text : if present this will be the text content of the newly created element
 *		helpText : optional help text, which should be shown on mouseover event 
 * 
 */
aspadmin.CssHelper.appendElement = function(parentEl, newEl)
{
	var elType = newEl.elType.toUpperCase();
	
	var el;
	if ('TR' == elType) {
		// parentEl can only be a table
		if (parentEl.tagName != 'TABLE')
		{
			alert('parent element for the TR should be TABLE, but '+parentEl.tagName+' is found');
			return null;
		}
		el = parentEl.insertRow(-1);
	} 
	else if ('INPUT' == elType) {

		if (typeof newEl.type == 'undefined') {
			alert('type attribute is required for the input elements');
			return null;  
		}
		
		try {
   			el = parentEl.appendChild(document.createElement('<input type="'+newEl.type+'">'));
	    }
	    catch(err) {
	    	el = parentEl.appendChild(document.createElement('input'));
	     	el.type = newEl.type;
	    }
		
		if (typeof newEl.checked != 'undefined') { el.checked = newEl.checked; }
		if (typeof newEl.disabled != 'undefined') { el.disabled = newEl.disabled; }
		if (typeof newEl.value != 'undefined') { el.value = newEl.value; }
	}
	else if ('OPTION' == elType) {
		el = parentEl.appendChild(document.createElement(elType));
		if (typeof newEl.value != 'undefined') { el.value = newEl.value; }
	}
	else 
	{
		el = parentEl.appendChild(document.createElement(elType));
	}

	// element-specific attributes
	if ('IMG' == elType) {
		if (typeof newEl.src != 'undefined') { el.src = newEl.src; }
	}

	if (typeof newEl.className != 'undefined') { el.className = newEl.className; }
	if (typeof newEl.id != 'undefined') { el.id = newEl.id; }
	
	if (typeof newEl.onclick != 'undefined') { el.onclick = newEl.onclick; }
	if (typeof newEl.onmouseover != 'undefined') { el.onmouseover = newEl.onmouseover; }
	if (typeof newEl.onmouseout != 'undefined') { el.onmouseout = newEl.onmouseout; }

	if (typeof newEl.helpText != 'undefined' && newEl.helpText != '') { 
		aspadmin.AspHelp.bindHelpText(el,newEl.helpText);
	}
	
	if (typeof newEl.text != 'undefined') { el.appendChild(document.createTextNode(newEl.text)); }
	
	return el;
};


/** 
 * De-activate specified control element. 
 */
aspadmin.CssHelper.deactivateControlEl = function(elementOrId)
{
	jade.Log.debug('aspadmin.CssHelper.deactivateControlEl('+elementOrId+')');
	var controlEl = elementOrId;
	if (typeof elementOrId == 'string') { controlEl = document.getElementById(elementOrId); }
	if (!controlEl) return;
	
	// make sure control element is not deactivated already
	var deactElId = controlEl.id + '_deactivated';
	if (document.getElementById(deactElId)) return;
	
	var isTxtControlBtn = aspadmin.CssHelper.hasClass(controlEl, 'txtControlBtn');
	
	var deactEl = controlEl.cloneNode(true);
	deactEl.id = deactElId;
	aspadmin.CssHelper.addClass(deactEl, 'deactivated');
	deactEl.onclick = null;
	deactEl.onmouseover = null;
	deactEl.onmouseout = null;
	
	if (!isTxtControlBtn)
	{
		var innerDiv = deactEl.appendChild(document.createElement("DIV"));
		innerDiv.style.position = 'absolute';
		innerDiv.style.width = '100%';
		innerDiv.style.height = '100%';
		innerDiv.style.top = '0px';
		innerDiv.style.left = '0px';
		innerDiv.style.background = '#FFF';
		innerDiv.style.color = '#FFF';
		innerDiv.style.fontSize = 'xx-small';
		innerDiv.appendChild(document.createTextNode('x'));
		$(innerDiv).css('opacity','0.5');
	}
	controlEl.style.display = 'none';
	controlEl.parentNode.insertBefore(deactEl, controlEl.nextSibling);
};

/** 
 * Reactivate specified control element. 
 */
aspadmin.CssHelper.reactivateControlEl = function(elementOrId)
{
	jade.Log.debug('aspadmin.CssHelper.reactivateControlEl('+elementOrId+')');
	var controlEl = elementOrId;
	if (typeof elementOrId == 'string') { controlEl = document.getElementById(elementOrId); }
	if (!controlEl) return;
	
	var deactElId = controlEl.id + '_deactivated';
	var deactEl = document.getElementById(deactElId);
    if (deactEl)
    {
    	$(deactEl).remove();
    }

    $('#'+controlEl.id).show();
};

