/**
 * Imtech JavaScript library | Visual functions
 * Composed 2006 by Waldek Mastykarz | Imtech ICT BS
 * All rights reserved  
 * @created		2006-11-06 09:15:00
 * @modified	2006-11-06 10:30:00
 * @version		1.1
 * 
 * CHANGES LOG:
 * 1.1		+ own namespace   
 */
 
Imtech.Visual = Imtech.Visual || {}; // set own namespace

/**
 * Returns all elements with the given attribute="value" within the given parent
 * @version	1.2 
 * @param		object|string	parent object
 * @param		string			tag name
 * @param		string			attribute
 * @param		string			attribute's value
 * @return		array			found objects
 * @dependency	Imtech.isFunction
 * @dependency	Imtech.$
 * @dependency	Imtech.isUndefined
 * @dependency	Imtech.isObject    
 * 
 * Copyright Robert Nyman, http://www.robertnyman.com
 * Free to use if this text is included
 * Updated by Waldek Mastykarz | Imtech ICT BS
 * 
 * CHANGES lOG:
 * 1.2		+ parameters recognition 
 * 1.1		+ $()    		 		 
 */		 		 		 		 		 		 		
Imtech.Visual.getElementsByAttribute = function(oElm, strTagName, strAttributeName, strAttributeValue) {
	if (!Imtech.isFunction ||
		!Imtech.isFunction(Imtech.$) ||
		!Imtech.isFunction(Imtech.isUndefined) ||
		Imtech.isUndefined(oElm) ||
		Imtech.isUndefined(strTagName) ||
		Imtech.isUndefined(strAttributeName) ||
		Imtech.isUndefined(strAttributeValue))
		return false;
		
	oElm = Imtech.$(oElm);
	if (!Imtech.isObject(oElm))
		return false;
		
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++){
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == "string" && oAttribute.length > 0){
            if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
                arrReturnElements.push(oCurrent);
            }
        }
    }
    return arrReturnElements;
}

/**
 * Returns elements of the given class
 * @version 1.2
 * @param		string			class name		 
 * @param		object|string	parent object; default document
 * @param		string			tag name; * for any tag (default)
 * @return		array			found objects
 * @dependency	Imtech.isFunction
 * @dependency	Imtech.$
 * @dependency	Imtech.isString
 * @dependency	Imtech.isObject    
 * 
 * Written by Jonathan Snook, http://www.snook.ca/jonathan
 * Add-ons by Robert Nyman, http://www.robertnyman.com
 * Updated by Waldek Mastykarz | Imtech ICT BS
 * 
 * CHANGES LOG:
 * 1.2		+ parameters recognition 
 * 1.1		+ $()	 		 		 
 */		 			 		 		 		
Imtech.Visual.getElementsByClassName = function(strClassName, oElm, strTagName) {
	if (!Imtech.isFunction ||
		!Imtech.isFunction(Imtech.$) ||
		!Imtech.isFunction(Imtech.isString) ||
		!Imtech.isString(strClassName))
		return false;
		
	// Imtech: 2006-11-03 | Set defaults
	oElm = oElm || document;
	strTagName = strTagName || "*";
	
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++) {
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)) {
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements);
}

/**
 * Sets equal height|width to given elements
 * Takes as parameters both objects and objects' ID (string)		 
 * @author	Waldek Mastykarz | Imtech ICT BS
 * @version	1.2 
 * @param		string			height|width
 * @dependency	Imtech.isFunction
 * @dependency	Imtech.$
 * @dependency	Imtech.isUndefined     
 * @dependency	this.getStyle
 * 
 * CHANGES LOG:
 * 1.2		+ parameters recognition		 
 * 1.1		+ $()
 */		 		 		
Imtech.Visual.equalSize = function(sDim) {
	if (!Imtech.isFunction ||
		!Imtech.isFunction(Imtech.isUndefined) ||
		Imtech.isUndefined(sDim))
		return false;
		
	// control whether the required functions are available and the dimension
	// is set correctly
	if (this.equalSize.arguments.length < 3 ||
		(sDim != 'height' && sDim != 'width') ||
		!Imtech.isFunction ||
		!Imtech.isFunction(Imtech.$) ||
		!Imtech.isFunction(this.getStyle))
		return false;
	
	// set variables
	var iMax = 0; // max height
	var iCurrent = 0; // current height
	var obj; // object
	var args = this.equalSize.arguments; // function arguments
	
	// process the parameters to obtain the height of the highest element	
	for (var i = 1; i < args.length; i++) {
		obj = Imtech.$(args[i]);
		if (obj) {
			iCurrent = this.getStyle(obj, sDim);
			iCurrent = parseInt(iCurrent);
			iMax = (iCurrent > iMax) ? iCurrent : iMax;
		}
	}
	
	// set the height to all elements
	for (var i = 1; i < args.length; i++) {
		obj = Imtech.$(args[i]);
		if (obj)
			obj.style[sDim] = iMax + "px";
	}
}	

/**
 * Returns rendered style of the given object
 * @version	1.1 
 * @param		object|string	object|object's id
 * @param		string			CSS rule
 * @return		string			rendered CSS value
 * @dependency	Imtech.isObject 
 * @dependency	Imtech.userAgent
 * @dependency	Imtech.isFunction
 * @dependency	Imtech.$   
 * 
 * Written by Robert Nyman, http://www.robertnyman.com
 * Updated by Waldek Mastykarz | Imtech ICT BS
 * 
 * CHANGES LOG:
 * 1.1		+ $()   	 		 		  		 
 */		 		 		 		 		
Imtech.Visual.getStyle = function(oElm, strCssRule) {
	if (!Imtech.isFunction ||
		!Imtech.isFunction(Imtech.$) ||
		!Imtech.isFunction(Imtech.isObject) ||
		!Imtech.isObject(Imtech.$(oElm)))
		return false;
		
	oElm = Imtech.$(oElm); // be sure it's an object
		
	var strValue = "";
	
	// Imtech: 2006-11-03 | IE fix
	if (Imtech.isObject &&
		Imtech.isObject(Imtech.userAgent) &&
		Imtech.UserAgent.name == "Explorer" &&
		(strCssRule == "height" || strCssRule == "width")) {
		var firstLetter = strCssRule.substring(0,1);
		firstLetter = firstLetter.toUpperCase();
		strValue = oElm["offset" + firstLetter + strCssRule.substring(1,strCssRule.length)];
	}
	else if(document.defaultView && document.defaultView.getComputedStyle) {
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

/**
 * Toggles object
 * @author	Waldek Mastykarz | Imtech ICT BS
 * @version	1.2 		 
 * @param		string|object		Object to toggle
 * @dependency	Imtech.isFunction
 * @dependency	Imtech.$ 
 * 
 * CHANGES LOG:
 * 1.2		+ parameters recognition 
 * 1.1		+ $()     
 */		 		 		
Imtech.Visual.toggle = function(obj) {
	if (!Imtech.isFunction ||
		!Imtech.isFunction(Imtech.$) ||
		!Imtech.isObject(Imtech.$(obj)))
		return;
	
	obj = Imtech.$(obj); // be sure it's an object
		
	obj.style.display = (obj.style.display == "none") ? "" : "none";					
}
