//try {
//	document.domain = "icatt.nl";
//} catch(exception) {}

function placeCovers(){
	for (var i=0;i<document.coverList.length;i++){
		document.coverList[i].placeCover();
	}
}

var COVERTYPE_CoverElement				= 'CoverElement'
var COVERTYPE_CoverBody					= 'CoverBody'			
var COVERTYPE_CoverNextElement			= 'CoverNextElement'	 
var COVERTYPE_CoverParent				= 'CoverParent'	 
var COVERTYPE_LabelAtElementPosition	= 'LabelAtElementPosition'
var COVERTYPE_LabelAtTopLefParent		= 'LabelAtTopLefParent'
var COVERTYPE_LabelAtTopLeftNextElement	= 'LabelAtTopLeftNextElement'

var LABLEWIDTH = 10;
var LABLEHEIGHT= 10;

/*********************************************************************************************************************/
//Start Cover Class

function Cover(elemId,cvrId,cvrType){
	
	this.coverId = cvrId;
	this.elementId = elemId;
	this.coverType = cvrType;
	this.cover = null
	this.element = null
	this.nextElement = null
	this.coverCoors = null
	this.coverDimens = null
	this.placeCover = Cover_placeCover
	
	if (!document.coverList) document.coverList = new Array();
	if (!document.coverIndex) document.coverIndex = new Array();
	
	document.coverList[document.coverList.length] = this;
	document.coverIndex[this.coverId] = this;
	
}

//  Get height BODY
function getSizeBody(){    
    var bodyheight=document.body.scrollHeight;
    var bodywidth=document.body.scrollWidth;
    return [bodyheight, bodywidth];
}

function Cover_placeCover(){

	//Get cover element
	if (!this.cover){		
		this.cover = document.getElementById(this.coverId);
	}
	if (!this.element && this.elementId) {
		this.element  = document.getElementById(this.elementId); 
	}
	if (!this.elementId)
		this.coverType = COVERTYPE_CoverBody;

	if (this.cover){
		if			(this.coverType == COVERTYPE_CoverElement				) {
			//--------------------------------------------
			// Places cover over entire page
			//--------------------------------------------
			if (this.element) {
				this.coverCoors = findPos(this.element);
				this.coverDimens = getSize(this.element);
				this.cover.style.top = this.coverCoors[1] + 'px';
				this.cover.style.left = this.coverCoors[0] + 'px';
				this.cover.style.width = this.coverDimens[0] + 'px';
				this.cover.style.height = this.coverDimens[1] + 'px';
			}
		} else if	(this.coverType == COVERTYPE_CoverBody					) {
			//--------------------------------------------
			// Places cover over entire page
			//--------------------------------------------

			this.coverSize = getSizeBody();			
			this.cover.style.top = '0px';
			this.cover.style.left = '0px';
			this.cover.style.width = this.coverSize[1] + 'px';
			this.cover.style.height = this.coverSize[0] + 'px';

		} else if	(this.coverType == COVERTYPE_CoverNextElement			) {
			//--------------------------------------------
			//Places cover over next sibling element
			//--------------------------------------------
			if (!this.nextElement && this.element){
				this.nextElement = findNextVisibleElement(this.element)
			}				
			if (this.nextElement){
				this.coverCoors = findPos(this.nextElement);
				this.coverDimens = getSize(this.nextElement);
				this.cover.style.top = this.coverCoors[1] + 'px';
				this.cover.style.left = this.coverCoors[0] + 'px';
				this.cover.style.width = this.coverDimens[0] + 'px';
				this.cover.style.height = this.coverDimens[1] + 'px';
			} 
		} else if	(this.coverType == COVERTYPE_CoverParent) {
			//--------------------------------------------
			// Places cover as label at topleft corner parent of element
			//--------------------------------------------
			if (this.element) {
				this.coverCoors = findPos(this.element.parentNode);
				this.coverDimens = getSize(this.element.parentNode);
				this.cover.style.top = this.coverCoors[1] + 'px';
				this.cover.style.left = this.coverCoors[0] + 'px';
				this.cover.style.width = this.coverDimens[0] + 'px';
				this.cover.style.height = this.coverDimens[1] + 'px';
			}
		} else if	(this.coverType == COVERTYPE_LabelAtElementPosition		) {
			//--------------------------------------------
			// Places cover as a label at topleft corner of element
			//--------------------------------------------
			if (this.element) {
				this.element.style.display='block'; //2007AR
				this.coverCoors = findPos(this.element);
				this.cover.style.top = this.coverCoors[1] + 'px';
				this.cover.style.left = this.coverCoors[0] + 'px';
				this.cover.style.width = LABLEWIDTH + 'px';
				this.cover.style.height = LABLEHEIGHT+ 'px';
			}
		} else if	(this.coverType == COVERTYPE_LabelAtTopLefParent		) {
			//--------------------------------------------
			// Places cover as label at topleft corner parent of element
			//--------------------------------------------
			if (this.element) {
				this.coverCoors = findPos(this.element.parentNode);
				this.cover.style.top = this.coverCoors[1] + 'px';
				this.cover.style.left = this.coverCoors[0] + 'px';
				this.cover.style.width = LABLEWIDTH + 'px';
				this.cover.style.height = LABLEHEIGHT+ 'px';
			}
		} else if	(this.coverType == COVERTYPE_LabelAtTopLeftNextElement) {
			//--------------------------------------------
			// Places cover as label at topleft corner of next element
			//--------------------------------------------
			if (!this.nextElement){
				this.nextElement = this.element.nextSibling
			}
			if (this.nextElement) {
				this.coverCoors = findPos(this.nextElement);
				this.cover.style.top = this.coverCoors[1] + 'px';
				this.cover.style.left = this.coverCoors[0] + 'px';
				this.cover.style.width = LABLEWIDTH + 'px';
				this.cover.style.height = LABLEHEIGHT+ 'px';
			} 
		} //End if..else if.. this.coverType
	}
	// End If Cover element founct
}

// End Cover Class
/*********************************************************************************************************************/

/*finds next sibling element that has a visible display*/
function findNextVisibleElement(oElement){
	
	var oNext = oElement.nextSibling
	while (getVisibility(oNext) == false) {
		oNext = oNext.nextSibling
		if (!oNext) break;
	}
	return oNext
	
}
function getVisibility(oElement){
	var invisibleTagList = new Array('SCRIPT','BR')

	var tagName = oElement.tagName
	var isInvisibleTag = false
	var isVisible = false
	for (i=0;i<invisibleTagList.length;i++){
		if (tagName == invisibleTagList[i] ) {
			isInvisibleTag = true
			break ;
		}
	}
	if (!isInvisibleTag) {
		//Check visibility

		if (oElement.currentStyle) {
			if (oElement.currentStyle.display != 'none') {
				isVisible = true
			}
		}
	}
	
	return isVisible
}

/* sets the transparent layer over the element */
function coverElement(oCoveredElement,oCover)
{

	var coors = findPos(oCoveredElement);
	var dimens = getSize(oCoveredElement);
	oCover.style.top = coors[1] + 'px';
	oCover.style.left = coors[0] + 'px';
	oCover.style.width = dimens[0] + 'px';
	oCover.style.height = dimens[1] + 'px';
	
	return coors
}

function coverElementById(elementId,coverId)
{
	var oElement,oCover
	
	if (!elementId) 
		oElement = document.body;
	else	
		oElement = document.getElementById(elementId); 

	oCover = document.getElementById(coverId);
	
	if (oElement && oCover) {
		if (oElement.coverParent) {
			//cover parent of cover element
			oElement = oElement.parentNode
		}
		var coors = coverElement(oElement,oCover);
		if (oCover.labelId) {
			placeCoverLabel(coors,oCover.labelId)
			oCover.onmouseover = new Function(  "showLabel('"+oCover.labelId+"')"  )
			oCover.onmouseout = new Function(  "hideLabel('"+oCover.labelId+"')"   )
		}
	}
}

function findPos(oElement)
{/*finds the elements position*/
	if (!oElement) return;
 
	var curleft = curtop = 0;
	if (oElement.offsetParent) {
		curleft = oElement.offsetLeft
		curtop = oElement.offsetTop
		while (oElement = oElement.offsetParent) {
			curleft += oElement.offsetLeft
			curtop += oElement.offsetTop
		}
	}
	return [curleft,curtop];
}
function getSize(oElement) 
{/*finds the elements size*/
	if (!oElement) return;
	
	var cw,ch
	if (oElement.clientWidth && oElement.clientHeight){
		cw = oElement.clientWidth;
		ch = oElement.clientHeight;
	} else {
		cw = oElement.offsetWidth;
		ch = oElement.offsetHeight;
	}
	return [cw,ch];
}

/* 
	Used to position cover 'coverId' and place element '' in the center of the cover 
	
	The coverId must be registered in the document.coverIndex
*/
function placePopup(inputElementId,coverId){
//alert("placePopup(inputElementId,coverId): " + inputElementId+","+coverId)
	var oCover = document.coverIndex[coverId]
	if (oCover) {
		var oElement = document.getElementById(inputElementId)
		if (oElement) {
			oCover.placeCover();
			centerElementOnCover(oElement,oCover.cover)
		}
	}
}

/* 
	Places oElement in the center of element oCover 
	
	Used by placePopup.

*/
function centerElementOnCover(oElement,oCover){
	if (oCover && oElement){
		var ew = oElement.offsetWidth;
		var eh = oElement.offsetHeight;

		var oCoverFrame = document.getElementById('coverLayerFrame');
		
		oCoverFrame.style.top = '0px';
	    oCoverFrame.style.left = '0px';
	    oCoverFrame.style.height = document.body.scrollHeight;
	    oCoverFrame.style.width = document.body.scrollWidth-2;
		oCoverFrame.style.display='block';
		
		oCover.style.top = '0px';
	    oCover.style.left = '0px';
	    oCover.style.height = document.body.scrollHeight;
	    oCover.style.width = document.body.scrollWidth-2;
		
		var coors = findPos(oCover);
		var dimens = getSize(oCover);
		var marginWidth = Math.round((dimens[0] - ew) / 2);
		var marginHeight = Math.round((dimens[1] - eh) / 2);
		
		oElement.style.left= (coors[0] + marginWidth) + 'px';
		oElement.style.top = (coors[1] + marginHeight) + 'px';
	}
}

function centerElementOnCoverById(elementId,coverId){
	var oElement	= document.getElementById(elementId);
	var oCover		= document.getElementById(coverId);
	centerElementOnCover(oElement,oCover)
}

/*=========================================================================================
	Functions that set the ControlEditorArguments in a hidden field of the parent document
	and call the doEditorPostback function of the parent document
	
	The resulting postback is handled by the WebAdmin webapplication.
==========================================================================================*/
function editControl(containerId,ordinal,objectId){
	//alert("editControl")
	var doc = window.parent.document
	if (doc != document){
		var x = new String();
		
		var args = 'edit;'+containerId+';'+ordinal+';'+objectId
		window.parent.document.forms[0].elements['__CONTROLEDITORARGS__'].value = args
		window.parent.doEditorPostback(args)
	}
}

function deleteControl(containerId,ordinal,objectId){
	//alert("deleteControl")
	if (confirm("Do you want to permenantly delete this control?")){
		var doc = window.parent.document
		if (doc != document){
			
			var args = 'delete;'+containerId+';'+ordinal+';'+objectId
			window.parent.document.forms[0].elements['__CONTROLEDITORARGS__'].value = args
			window.parent.doEditorPostback(args)
		}
	}
}

function addControl(containerId,ordinal,typeId){
	//alert("addControl")
	var doc = window.parent.document
	if (doc != document){
		
		var args = 'add;'+containerId+';'+ordinal+';'+typeId
		window.parent.document.forms[0].elements['__CONTROLEDITORARGS__'].value = args
		window.parent.doEditorPostback(args)
	}
}
/*=========================================================================================
	End ControlEditor postback functions
=========================================================================================*/
function SetcoverTransparent () {
	var fr = document.getElementById('coverTransparentFrame');
	if (fr) {
		fr.style.top = '0px';
		fr.style.left = '0px';
		fr.style.height = document.body.scrollHeight;
		var newWidth = document.body.scrollWidth - 2;
		if (newWidth > 0 ){
			fr.style.width = newWidth + 'px'
		}
		fr.style.display='block';
		fr.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
	}

}

appendOnLoad(SetcoverTransparent);
appendOnResize(SetcoverTransparent);