/**
* 	This file contains functions for creating and manipulating objects.
*
*	@author Herman Bredewoud
*	@version 1.00
*	@name Object Script
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var m_bScrollPause			= false;
var m_iScrollPosition		= 0;
var m_oTimer;
var m_oTimer2;

var m_iLoop					= 5;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
*	This function will return the target parent where the className is equal to the given one.
*
*	@param object a_oObj
*	@param mixed a_aClass
*	@param int a_iLoop
*	@return object
*/
function OBJ_getObjectParentByClass(a_oObj, a_aClass, a_iLoop) {
	a_iLoop = a_iLoop != null ? m_iLoop : 5;
	
	for (var i=0; i<a_iLoop; i++) {
		if (OBJ_hasParentNode(a_oObj)) {
			// If a_aClass is not an array.
			if (!OBJ_isArray(a_aClass)) {
				if (a_oObj.className == a_sClass) {
					return a_oObj.parentNode;
				}
			} else {
				for (var j=0; j<a_aClass.length; j++) {
					if (a_oObj.className == a_aClass[j]) {
						return a_oObj;
					}
				}
			}
			
			// Set a_oObj as parent.
			a_oObj = a_oObj.parentNode;
		}
	}
}

/**
*	This function will return the target parent if exists.
*
*	@param object a_oObj
*	@return object
*/
function OBJ_getParent(a_oObj) {
	if (OBJ_hasParentNode(a_oObj)) {
		return a_oObj.parentNode;
	}
	return false;
}


/**
*	This function will return the target object.
*
*	@param object a_oEvent
*	@return object
*/
function OBJ_getTarget(a_oEvent) {
	return a_oEvent.target != null ? a_oEvent.target : a_oEvent.srcElement;
}

/**
*	This function will return the value of the requested attribute of the given object.
*
*	@param object a_oObj
*	@param string a_sAttr
*	@return string
*/
function OBJ_getAttribute(a_oObj, a_sAttr) {
	if (OBJ_hasAttributes(a_oObj)) {
		// If the getAttribute failes use a_sAttr as array index of the object.
		if (a_oObj.getAttribute(a_sAttr)) {
			return a_oObj.getAttribute(a_sAttr);
		} else if (a_oObj[a_sAttr]) {
			return a_oObj[a_sAttr];
		} else {
			for (var i=0; i<a_oObj.attributes.length; i++) {
				if (a_oObj.attributes[i].nodeName.toLowerCase() == a_sAttr) {
					return a_oObj.attributes[i].nodeValue;
				}
			}
		}
	}
	
	return false;
}

/**
*	This function will set the value of the requested attribute of the given object.
*
*	@param object a_oObj
*	@param string a_sAttr
*	@param string a_sValue
*/
function OBJ_setAttribute(a_oObj, a_sAttr, a_sValue) {
	if (OBJ_isObject(a_oObj)) {
		a_oObj.setAttribute(a_sAttr, a_sValue);
	}
}

/**
*	This function will return true if the given object has attributes and returns false if not.
*
*	@param object a_oObj
*	@return boolean
*/
function OBJ_hasAttributes(a_oObj) {
	if (OBJ_isObject(a_oObj)) {
		if (a_oObj.attributes != null) {
			return true;
		}
	}
	
	return false;
}

/**
*	This function will return true if the given object has childnodes and returns false if not.
*
*	@param object a_oObj
*	@return boolean
*/
function OBJ_hasChildNodes(a_oObj) {
	if (OBJ_isObject(a_oObj)) {
		return a_oObj.hasChildNodes();
	}
	
	return false;
}

/**
*	This function will return true if the given object has parentNodes and returns false if not.
*
*	@param object a_oObj
*	@return boolean
*/
function OBJ_hasParentNode(a_oObj) {
	if (OBJ_isObject(a_oObj)) {
		if (a_oObj.parentNode) {
			return true;
		}
	}
	
	return false;
}

/**
*	This function will check if the given object is an array and will return false if not an array
*
*	@param mixed a_oObj
*	@return boolean
*/
function OBJ_isArray(a_oObj) {
	return (a_oObj instanceof Array);
}

/**
*	This function will check if the given object is an object and will return false if not an object
*
*	@param mixed a_oObj
*	@return boolean
*/
function OBJ_isObject (a_oObj) {
	return typeof a_oObj == 'object' ? true : false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*	This function will pause the scrolling by setting the pause variable.
*
*	@param boolean a_bEnabled
*/
function OBJ_pauseScrolling(a_bEnabled) {
	m_bScrollPause = a_bEnabled;
}

/**
*	This function will scroll automaticaly within the object.
*
*	@param object a_oObj
*/
function OBJ_setScrolling(a_oObj) {
	a_iScrollHeight	= a_oObj.scrollHeight;
	a_iObjectHeight	= parseInt(a_oObj.style.height);
	
	a_iScrollLength = (a_iScrollHeight-a_iObjectHeight);
	
	if (m_iScrollPosition < a_iScrollLength && a_iScrollLength > 0) {
		if (!m_bScrollPause) {
			a_oObj.scrollTop=m_iScrollPosition;
			m_iScrollPosition+=1;
		}
		
		m_oTimer = setTimeout(function() {OBJ_setScrolling(a_oObj);},40);
	} else {
		m_iScrollPosition=0;
		clearTimeout(m_oTimer);
		clearTimeout(m_oTimer2);
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*	This function will create a div element and will set the default style.
*	
*	@param string a_sObject_id
*	@param string a_sClass
*	@param int a_iWidth
*	@param int a_iHeight
*	@return object
*/
function OBJ_create(a_sObject_id, a_sClass, a_iWidth, a_iHeight) {
	if (!(oObj = document.getElementById(a_sObject_id))) {
		var oElement = document.createElement('div');
		oElement.setAttribute('id', a_sObject_id);
		oElement.setAttribute('class', a_sClass);
		document.body.appendChild(oElement);

		oObj = document.getElementById(a_sObject_id);

		oObj.style.display = 'none';
		oObj.style.zIndex = '900';
		oObj.style.position = 'absolute';
		oObj.style.left = '0px';
		oObj.style.top = '0px';
		oObj.style.width = a_iWidth+'px';
		oObj.style.height = a_iHeight+'px';
		oObj.style.overflow = 'auto';
		oObj.style.overflowX = 'hidden';
		
		oObj.style.backgroundColor = '#FFFFFF';
		oObj.style.border = '1px solid rgb(0,0,0)';
		oObj.style.padding = '2px';
	}
	
	return oObj;
}

/**
*	This function will show any given object at the mouse cursor location.
*
*	@param object a_oEvent
*	@param string a_sObject_id
*/
function OBJ_show(a_oEvent,a_sObject_id) {
	if (!a_oEvent) {
		a_oEvent=window.event;
	}
	if (oObj = document.getElementById(a_sObject_id)) {
		if (oObj.innerHTML != "" || oObj.src != "") {
			if (oObj.style.display == 'block' && a_oEvent.type=="mouseout") {
				oObj.style.display = 'none';
			}else if (a_oEvent.type=="mouseover") {
				oObj.style.display = 'block';

				if (m_oBrowserType.isIE) {
					if ((parseInt(a_oEvent.clientX)+parseInt(oObj.style.width)) < document.body.clientWidth) {
						oObj.style.left = (a_oEvent.clientX + 20) + document.body.scrollLeft + 'px';
					} else {
						oObj.style.left = (a_oEvent.clientX - 20 - parseInt(oObj.style.width)) + document.body.scrollLeft + 'px';
					}
					oObj.style.top = a_oEvent.clientY + document.body.scrollTop + 'px';
				} else {
					if ((parseInt(a_oEvent.pageX)+parseInt(oObj.style.width)) < window.innerWidth) {
						oObj.style.left = (parseInt(a_oEvent.pageX) + 20) + 'px';
					} else {
						oObj.style.left = (parseInt(a_oEvent.pageX) - 20 - parseInt(oObj.style.width)) + 'px';
					}
					oObj.style.top = a_oEvent.pageY + 'px';
				}
			}
		}
	}
}

/**
*	This function will set the position of the object to the center of the screen.
*
*	@param object a_oObj
**/
function OBJ_center(a_oObj){
	if (m_oBrowserType.isIE){
		a_oObj.style.left = ((parseInt(document.body.clientWidth)/2)-(parseInt(a_oObj.offsetWidth)/2))+"px";
		a_oObj.style.top = ((parseInt(document.body.clientHeight)/2)-(parseInt(a_oObj.offsetHeight)/2))+"px";
	} else {
		a_oObj.style.left = ((parseInt(window.innerWidth)/2)-(parseInt(a_oObj.offsetWidth)/2))+"px";
		a_oObj.style.top = ((parseInt(window.innerHeight)/2)-(parseInt(a_oObj.offsetHeight)/2))+"px";
	}
}

/**
*	This function will center the object horizontal.
*
*	@param object a_oObj
**/
function OBJ_center_horizontal(a_oObj) {
	if (m_oBrowserType.isIE) {
		a_oObj.style.left = ((parseInt(document.body.clientWidth)/2)-(parseInt(a_oObj.offsetWidth)/2))+"px";
	} else {
		a_oObj.style.left = ((parseInt(window.innerWidth)/2)-(parseInt(a_oObj.offsetWidth)/2))+"px";
	}
}

/**
*	This function will set the position of the object to the center of the screen height.
*
*	@param object a_oObj
**/
function OBJ_center_vertical(a_oObj) {
	var iMarginTop = 0;	
	
	if (m_oBrowserType.isIE) {
		iMarginTop = ((parseInt(document.body.clientHeight))-(parseInt(a_oObj.offsetHeight)))/2;
	} else {
		iMarginTop = (parseInt(window.innerHeight)/2)-(parseInt(a_oObj.offsetHeight)/2);
	}
	
	if (iMarginTop > 0) {
		a_oObj.style.marginTop = iMarginTop+"px";
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*	This function will include a new (given) css file.
*	Files are only available with a next page reload.
*
*	@param string a_sFile
**/
function OBJ_include_css(a_sFile) {
    var html_doc = document.getElementsByTagName('head')[0];
    var css = document.createElement('link');
    css.setAttribute('rel', 'stylesheet');
    css.setAttribute('type', 'text/css');
    css.setAttribute('href', a_sFile);
    html_doc.appendChild(css);
    
    return false;
}

/**
*	This function will include a new (given) js file.
*	Files are only available with a next page reload.
*
*	@param string a_sFile
**/
function OBJ_include_js(a_sFile,a_bDefer) {
    var html_doc = document.getElementsByTagName('head')[0];
    var js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', a_sFile);
    if (a_bDefer) {
    	js.setAttribute('defer', 'defer');
    }
    html_doc.appendChild(js);
	
    return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*	This function will loop through the children of the given object to search for flash objects.
*	
*	@param object a_oObj
*	@example <div id="swf2" f_swf="/flvplayer.swf" f_swfid="swf2" f_file="/wmo_almere.flv" f_allowfullscreen="true" f_img="/preview_med_wmo.jpg" f_width="400" f_height="320" f_version="7"><a href="http://www.macromedia.com/go/getflashplayer">Download de Flash Player om deze video te bekijken.</a></div>
*	SWFObject(f_sSWF, f_sSWFID, iWidth, iHeigth, iVersion, sBGcolor [, quality, xiRedirectUrl, redirectUrl, detectKey]);
*	alert('FLASH:'+bFLASHObjectFound+i+'\nSWF: '+f_sSWF+"\nSWFID: "+f_sSWFID+"\nFILE: "+f_sFile+"\nIMG: "+f_sImg+"\nFULLSCREEN: "+f_sFullscreen+"\nWIDTH: "+f_iWidth+"\nHEIGHT: "+f_iHeight+"\nVERSION: "+f_iVersion+"\n");
*/
function OBJ_flash(a_oObj) {

	var f_sSWF = CONF_sSWFplayer;
	var f_sSWFID = null;
	var f_sFile = null;
	var f_sFullscreen = 'true';
	var f_f_sImg;
	var f_iWidth = 400;
	var f_iHeight = 320;
	var f_iVersion = 7;
	var f_sBGcolor = '#FFFFFF';
	var bFLASHObjectFound = false;
	
	// Check to see if the object has childnodes
	if (OBJ_hasChildNodes(a_oObj)) {
		// Loop the childnodes of the object.
		for (var i=0; i<a_oObj.childNodes.length; i++) {
			
			oChildnode = a_oObj.childNodes[i];
			
			// Check if the node has attributes.
			if (OBJ_hasAttributes(oChildnode)) {
				
				// Loop all the attributes.
				for (var j=0; j<oChildnode.attributes.length; j++) {
//					if (oChildnode.attributes[j].nodeName.toLowerCase() == 'f_swf') {
//						f_sSWF = oChildnode.attributes[j].nodeValue;
//					}
					if (oChildnode.attributes[j].nodeName.toLowerCase() == 'f_swfid') {
						f_sSWFID = oChildnode.attributes[j].nodeValue;
					}
					if (oChildnode.attributes[j].nodeName.toLowerCase() == 'f_file') {
						f_sFile = oChildnode.attributes[j].nodeValue;
					}
					if (oChildnode.attributes[j].nodeName.toLowerCase() == 'f_allowfullscreen') {
						f_sFullscreen = oChildnode.attributes[j].nodeValue;
					}
					if (oChildnode.attributes[j].nodeName.toLowerCase() == 'f_img') {
						f_sImg = oChildnode.attributes[j].nodeValue;
					}
					if (oChildnode.attributes[j].nodeName.toLowerCase() == 'f_width') {
						f_iWidth = oChildnode.attributes[j].nodeValue;
					}
					if (oChildnode.attributes[j].nodeName.toLowerCase() == 'f_height') {
						f_iHeight = oChildnode.attributes[j].nodeValue;
					}
					if (oChildnode.attributes[j].nodeName.toLowerCase() == 'f_version') {
						f_iVersion = oChildnode.attributes[j].nodeValue;
					}
					if (oChildnode.attributes[j].nodeName.toLowerCase() == 'f_bgcolor') {
						f_sBGcolor = oChildnode.attributes[j].nodeValue;
					}
				}
				
				// If the f_sSWF an f_sSWFID are filled than the object is a flash object.
				if (f_sSWF != null && f_sSWFID != null) {
					bFLASHObjectFound = true;
				}
				
				// Create the SWF object
				if (bFLASHObjectFound) {
					var flash = new SWFObject(f_sSWF, "single", f_iWidth, f_iHeight, f_iVersion, f_sBGcolor);
					flash.addParam("allowfullscreen",f_sFullscreen);
					flash.addVariable("file",f_sFile);
					flash.addVariable("image",f_sImg);
					flash.write(f_sSWFID);
					
					// Reset boolean.
					bFLASHObjectFound = false;
				}
			}
			
			// Loop recursive through the childs of the childs.
			OBJ_flash(a_oObj.childNodes[i]);
		}
	}
}

function OBJ_multiArray(a_iRows, a_iCols) {
	
	var arr = new Array(a_iRows); 
	for (var i=0; i < a_iRows; i++) {
		arr[i] = new Array(a_iCols); 
		
		for (var j=0; j < a_iCols; j++) { 
		   arr[i][j] = ""; 
		}
	} 
	
	return(arr);
}
