

/*
		Modified by Michiel
		
		- reacts only on tbody.dataList and tbody.propertyList
		- onMouseMove changed to onMouseOver
		- Added CSSProperty class:
				cg = new CollectionGrid("id");
				cg.addProperty(cssPropName, mouseOverValue, normalValue);
			If normalValue is not set, the last value of the property before the mouseover is used

*/

function CSSProperty(newName, on, off) {
	this.name = newName;
	this.onValue = (on == null ? "" : on);
	this.offValue = (off == null ? "" : off);
	this.offValueFound = (this.offValue != "");
}

CSSProperty.prototype.setOnValue = function(value) {
	this.onValue = value;
}

CSSProperty.prototype.setOffValue = function(value) {
	this.offValue = value;
}




function initCollectionGrids(){
	window.collectionGrids.init();
}
/* window CollectionGrid container object */
function WindowCollectionGrids(){
	this.collections=new Array();

	this.addNew							=mtd_wcg_addNew;
	this.getCollectionById	=mtd_wcg_getCollectionById;
	this.init								=mtd_wcg_init;

	setTimeout(function(){window.collectionGrids.init()},500) // er is al een onload gedefineerd.
	/*  window.onload=function(){window.collectionGrids.init()} initCollectionGrids; */

	return this;
}

function mtd_wcg_addNew(collectionGrid)	{this.collections[this.collections.length]=collectionGrid}
function mtd_wcg_getCollectionById(ID)	{	
	var i;

	for(i=0; i<this.collections.length; i++)
		if(this.collections[i].ID == ID) return this.collections[i];
	
	return null
}
function mtd_wcg_init(){
	var i;
	for(i=0; i<this.collections.length; i++)
		this.collections[i].init()
}

/* CollectionGrid container object */
function CollectionGrid(ID){
	// properties
	this.ID=ID;
	this.hndl=null;
	this.hndl_head=null;
	this.hndl_header=null;
	this.hndl_data=null;
	
	this.hashref=false;
	this.hashref2=false;
	this.sortable=false;
	this.rowselect=false;
	this.multiselect=false;
	
	this.hreftarget="_self";
	this.href2target="_self";

	this.dataBodies=new Array();
	this.datarows=new Array();
	
	this.cssProps = new Array();
	
	
	// constructor
	if(!window.collectionGrids) window.collectionGrids=new WindowCollectionGrids();
	window.collectionGrids.addNew(this);
	
	// methods
	this.init		=mtd_CollectionGrid_init;
	this.doHref =mtd_CollectionGrid_doHref;
	
	return this;
}

CollectionGrid.prototype.addProperty = function(pName, pOn, pOff) {
	this.cssProps[this.cssProps.length] = new CSSProperty(pName, pOn, pOff);
}




function mtd_CollectionGrid_init(){
		var i, ri, objectA;
	this.hndl=document.getElementById(this.ID);

	if(this.hndl){
		// detect the requested functionality
		this.hashref		= (getNodeItem(this.hndl,"href")!=null)
		this.hashref2		= (getNodeItem(this.hndl,"href2")!=null)
		this.rowselect	= (getNodeItem(this.hndl,"rowselect")!=null)
		this.multiselect= (getNodeItem(this.hndl,"multiselect")!=null)
		this.sortable		= (getNodeItem(this.hndl,"sortable")!=null)

		if(this.multiselect) this.rowselect=true;

		if(this.hashref){
			this.href		= getNodeItem(this.hndl,"href");
			this.hreftarget	= (getNodeItem(this.hndl,"target")==null ? '_self' : getNodeItem(this.hndl,"target"));
		}
		if(this.hashref2){
			this.href2				= getNodeItem(this.hndl,"href2");
			this.hreftarget2	= (getNodeItem(this.hndl,"target2")==null ? '_self' : getNodeItem(this.hndl,"target2"));
		}

		objectA=CollectionGrid_fetch_from_node(this.hndl);
		
		for(i=0; i<objectA.length; i++){
			// sign the datacontainer object with it's ID
			objectA[i].collectionGridID=this.ID;
			
			switch(objectA[i].className){
				case 'collection-head'		: this.hndl_head	=objectA[i]; break;
				case 'collection-header'	: this.hndl_header=objectA[i]; break;
				case 'colTable'		: this.hndl_data	=objectA[i]; break;
				case 'collection-data'		: this.hndl_data	=objectA[i]; break;
			}
		}
	}

	if(this.hndl_data){
		with(this.hndl_data)
			for(i=0; i<tBodies.length; i++)
				switch(tBodies[i].className){
					case 'dataList':
					case 'propertyList':
						this.dataBodies[this.dataBodies.length]=tBodies[i];
						for(ri=0; ri < tBodies[i].rows.length; ri++)
							this.datarows[this.datarows.length]=tBodies[i].rows[ri];
						break;
				}

		/* initialize the rows */
		if (this.hashref && !this.rowselect) 			CollectionGrid_init_rows_href(this)
		else if (this.hashref && this.rowselect)	CollectionGrid_init_rows_href_select(this)
		else if (!this.hashref && this.rowselect) CollectionGrid_init_rows_select(this);
		
		if(this.hashref2)
			CollectionGrid_init_rows_href2(this);
	}
}
function CollectionGrid_fetch_from_node(node){
	var i; 
	var objectArray=new Array();

	with(node)
			for(i=0; i<childNodes.length; i++)
				switch(childNodes[i].className){
					case 'collection-head'			: 
					case 'collection-header'		: 
					case 'colTable'			: objectArray[objectArray.length]=childNodes[i]; break;
					case 'collection-data'			: objectArray[objectArray.length]=childNodes[i]; break;
					case 'collection-container' : objectArray=objectArray.concat(CollectionGrid_fetch_from_node(childNodes[i]));	break;
				}
	return objectArray;
}

function CollectionGrid_init_rows_href(cGrid){	// only href available
	var i;
	for(i=0;i<cGrid.datarows.length;i++) {
		cGrid.datarows[i].onmouseover=mtd_row_onmousemove; 
		cGrid.datarows[i].onmouseout=mtd_row_mouseout; 
		cGrid.datarows[i].onclick=mtd_row_href;
		
		cGrid.datarows[i].cgPointer=cGrid;
	}
}
function CollectionGrid_init_rows_href2(cGrid){	// only href available
	var i;
	for(i=0;i<cGrid.datarows.length;i++) {
		cGrid.datarows[i].oncontextmenu=mtd_row_href2;
	}
}

function CollectionGrid_init_rows_href_select(cGrid){ // href (dblclick), select (click)
	var i;
	for(i=0;i<cGrid.datarows.length;i++){
			cGrid.datarows[i].onmousemove=mtd_row_onmousemove; 
			cGrid.datarows[i].onmouseout=mtd_row_mouseout;
			cGrid.datarows[i].onclick=mtd_row_select; 
			cGrid.datarows[i].ondblclick=mtd_row_href;
			cGrid.datarows[i].oncontextmenu=function (){alert('menu')};

			cGrid.datarows[i].cgPointer=cGrid;
			
			setNodeItem(cGrid.datarows[i],"selected",false);
	}
}
function CollectionGrid_init_rows_select(cGrid){ // select (click)
	var i;
	for(i=0;i<cGrid.datarows.length;i++){
			cGrid.datarows[i].onmousemove=mtd_row_onmousemove; 
			cGrid.datarows[i].onmouseout=mtd_row_mouseout;
			cGrid.datarows[i].onclick=mtd_row_select;
			
			cGrid.datarows[i].cgPointer=cGrid;

			setNodeItem(cGrid.datarows[i],"selected",false)
	}
}
function mtd_CollectionGrid_doHref(Arguments,index){
	var href, target;

	if(index==2){
		href=this.href2;
		target=this.hreftarget2;
	}	else {
		href=this.href;
		target=this.hreftarget;
	}

	doHref(target, href, Arguments)
}

function mtd_row_onmousemove()	{ 
	var i, j;
	
	switch(this.parentNode.className) {
		case "dataList":
			for(j=0; j<this.cells.length; j++) {			
				for(i=0; i<this.cgPointer.cssProps.length; i++) {
					if(!this.cgPointer.cssProps[i].offValueFound) {
						this.cgPointer.cssProps[i].offValueFound = true;
						this.cgPointer.cssProps[i].offValue =
							this.cells[j].style[this.cgPointer.cssProps[i].name];
					}
					this.cells[j].style[this.cgPointer.cssProps[i].name] = this.cgPointer.cssProps[i].onValue;
				}
			}
			break;
		default:
			for(i=0; i<this.cgPointer.cssProps.length; i++) {
				if(!this.cgPointer.cssProps[i].offValueFound) {
					this.cgPointer.cssProps[i].offValueFound = true;
					this.cgPointer.cssProps[i].offValue =
						this.style[this.cgPointer.cssProps[i].name];
				}
				this.style[this.cgPointer.cssProps[i].name] = this.cgPointer.cssProps[i].onValue;
			}
	}

}
function mtd_row_mouseout()			{
	var i, j;
	
	switch(this.parentNode.className) {
		case "dataList":
			for(j=0; j<this.cells.length; j++) {			
				for(i=0; i<this.cgPointer.cssProps.length; i++) {
					this.cells[j].style[this.cgPointer.cssProps[i].name] = 
						(this.cgPointer.cssProps[i].offValue == undefined ? "" : this.cgPointer.cssProps[i].offValue);					
				}
			}
			break;
		default:
			for(i=0; i<this.cgPointer.cssProps.length; i++) {
				this.style[this.cgPointer.cssProps[i].name] = this.cgPointer.cssProps[i].offValue;
			}
	}
}


function mtd_row_select() { this.className= setNodeItem(this,"selected",!rowIsSelected(this)) ? "selected" : "hover" }

function rowIsSelected(row){
	var value=getNodeItem(row,"selected");
	if(value==null|| !eval(value)) return false
	else return true;
}

function mtd_row_href(){
	window.collectionGrids.getCollectionById(this.parentNode.parentNode.collectionGridID).doHref(getNodeItem(this,"params"),1);
}
function mtd_row_href2(){
	window.collectionGrids.getCollectionById(this.parentNode.parentNode.collectionGridID).doHref(getNodeItem(this,"params"),2);
	return false;
}

function getNodeItem(node,item){
	var attrib;

	if(node.attributes != null){
		if ( node.attributes.getNamedItem ) { // DOM 1 compliant
		attrib=node.attributes.getNamedItem(item);
		if(attrib != null) return attrib.nodeValue;
		} else { // ie5 workaround for missing getNamedItem, solve by using IE5 specific method
			attrib = node.getAttribute( item );
			if(attrib != null) return attrib;
		}
	}
	
	if(node[attrib])
		return node[attrib];
	
	return null;
}
function setNodeItem(node,attribName,attribValue){
	var attrib;

	if(node.attributes != null){
		attrib = document.createAttribute(attribName);
		attrib.value = attribValue;

		node.attributes.setNamedItem(attrib);
	} else {
		node[attrib]=value;
	}

	return attribValue;
}


/* HREF & TARGET code */
function doHref(target,href,Arguments) {
	findTarget(target).location.href=hrefEval(href,Arguments); return false;
}

function getBaseUrl(){
	var url=window.location.href;
	var i=url.indexOf("rsAction");	//assume no other params at the end

	if(i==-1)	
		return url
	else
		return	url.substring(0,i-1);
};

function hrefEval(href,Arguments){
	var i; var hrefA=href.split("%"); 
	var params;

	if(/%params%|%param\[[0-9]+\]%/.test(href)){
		if(Arguments==null){
			alert('error: dynamic href but row exposed no Arguments');
			return false;
		}else{
			// resolve dynamic Arguments
			Arguments="optionbase-1,"+Arguments;
			params=Arguments;	param=params.split(",");

			for(var i=0;i<hrefA.length;i++){
				if(/^param\[[0-9]+\]$/.test(hrefA[i])) hrefA[i]=eval(hrefA[i]);
				if(/^param$/.test(hrefA[i])) hrefA[i]=Arguments.join(',');
			}
			return hrefA.join('');
		}
	}
	return href;
	
};

/* target finder */
function findTarget(target){
	var oTarget;
	
	switch(target.toLowerCase()){
		case '_self' 	: return window; break;
		case '_blank' : return window.open("about:blank","_blank") ; break;
		case '_parent': return window.parent; break;
		case '_top' 	: return window.top; break;
	};

	// assume frame name
	if(oTarget=findFrame(target,window.top))
		return oTarget;
	
	// else open new named Win 
	return window.open("about:blank",target);
};

function findFrame(name,oHndl){
	var i;
	var dHndl;

	if(oHndl.frames && oHndl.frames.length){
		for(i=0;i<oHndl.frames.length;i++)
			if(oHndl.frames[i].name == name)
				return oHndl.frames[i];

		// not yet found, return deep search
		for(i=0;i<oHndl.frames.length;i++)
			if(oHndl.frames[i].frames.length)
				if(dHndl=findFrame(name,oHndl.frames[i]))
					return dHndl;

		return null;
	} else {
		return null
	};
}
function CheckAll(checked,field) {
	var len = document.f1.elements.length;
	var i=0;
	for( i=0; i<len; i++)
		if (document.f1.elements[i].name==field)
			document.f1.elements[i].checked=checked;
}

function collapseTeaser(id){
	var stlHndl =document.getElementById("teaserTable"+id).style;
	var hrefHndl=document.getElementById("teaserLink"+id);
	if(stlHndl.display != "none"){
		stlHndl.display="none";
		hrefHndl.innerHTML="<img src=\"/lib/i/interface/miniplus.gif\" border=0 vspace=2 hspace=2>";
	}else{
		stlHndl.display="block";
		hrefHndl.innerHTML="<img src=\"/lib/i/interface/minimin.gif\" border=0 vspace=2 hspace=2>";
	};
};