/* Copyright � 2000-2006 Dennis Bekkering 
 * Comeniusstraat 61 
 * 1065 BH Amsterdam 
 * The Netherlands. 
 * All rights reserved 
 */
  
function buildContextMenu(xevent){

	
	var baseContextMenuHolder = get('baseContextMenuHolder');
	baseContextMenuHolder.style.visibility='visible';
	var inf = get('infoBox');
	
	baseContextMenuHolder.style.display='block';
 	var back = baseContextMenuHolder.getElementsByTagName('div')[0];
 	back.style.width=baseContextMenuHolder.offsetWidth;
 	back.style.height=baseContextMenuHolder.offsetHeight;


	var len = window.propertyMouseMoveEditorArray.length
	for(i=0;i<len;i++){
		var button = searchComponent( window.propertyMouseMoveEditorArray[i] );
//		alert(window.propertyMouseMoveEditorArray[i]  );
		button.innerHTML ='';
	//	alert('building context menu , ' + button.innerHTML);
		button.innerHTML =   button.getAttribute('property') + '=' 
			+ window.currentDebug.style[button.getAttribute('property') ];
	}





//	inf.style.top='100px';
	//alert('hallo?');	
	window.contextMenuBuild=1;
}


function debug(str,element , xevent , patupDont , letBox)
{
	
	
	if(!element) {
		return;
	}

	
	if(window.debugStick == 1  || window.debugStart ==''){
		//alert('window.debugStick from debug='+window.debugStick);	
		
		return;
	}
	if(window.debugStick == 1 ){
//		return;
	}

//for (p in event.srcElement.style) alert(p);
	
	
	 
	var inf = get('infoBox');
	if (!inf) 	return;
	inf.style.display='block';

	
	                                     
	inf.style.background='white';
	inf.style.border ='1px solid black';
	inf.style.position='absolute';
	window.writer = '';
	if(	window.currentDebug ){
		window.currentDebug.style.borderLeft = 		window.currentDebug.xborderLeft;
		window.currentDebug.style.borderRight = 	window.currentDebug.xborderRight;
		window.currentDebug.style.borderTop = 		window.currentDebug.xborderTop;
		window.currentDebug.style.borderBottom = 	window.currentDebug.xborderBottom;
	}
	

	

	window.currentDebug = element;
	var baseContextMenu = get('baseContextMenu');
	if(baseContextMenu ){
		baseContextMenu
			.getElementsByTagName('input')[0]
			.value=
				window
				.currentDebug
				.getAttribute('componentId');
		
		//ie workaround, see MainForm$FormProperties 
		if(baseContextMenu
			.getElementsByTagName('input')[0]
			.value == '[object]')
		{
			baseContextMenu
			.getElementsByTagName('input')[0]
			.value = window
						.currentDebug
						.getAttribute('componentAttributeId');
		}
	}
	
//	render(element , 0);
	

	if(!patupDont){
	
		pathUp(element , 0);
		inf.innerHTML= window.writer;
		var items = inf.getElementsByTagName('TR');
		inf.activeChild = items[items.length-1];	
	}
	

	
	 
	buildContextMenu(xevent );
	
	
	
	var classContextMenu = get( "javaClass_" + element.getAttribute("javaClassName"))
	if(classContextMenu){
		classContextMenu.style.display='block';
	}
	if(window.currentClassContextMenu && window.currentClassContextMenu != classContextMenu){
		window.currentClassContextMenu.style.display='none';
	}
	window.currentClassContextMenu = classContextMenu ;


	if(window.currentMouseX > (screen.width / 2) && !letBox){
		inf.style.left = 0;
	}
	else if (!letBox){
		inf.style.left= document.body.clientWidth - inf.clientWidth;
	}
	inf.style.top = 0 ;

	var baseContextMenuHolder = get('baseContextMenuHolder');
	baseContextMenuHolder.style.top = inf.offsetTop + inf.offsetHeight;
	baseContextMenuHolder.style.left  = inf.offsetLeft;
	if(inf.offsetLeft==0){
	}
	else{
		baseContextMenuHolder.style.left  = inf.offsetLeft
		- ( baseContextMenuHolder.clientWidth - inf.clientWidth   );
	}
	

	
	 
	element.xborderLeft = element.style.borderLeft;
	element.xborderRight = element.style.borderRight;
	element.xborderTop = element.style.borderTop;
	element.xborderBottom = element.style.borderBottom;
	
	element.style.borderTop = '2px dotted #eeeeee';
	element.style.borderLeft = '2px dotted #eeeeee';
	element.style.borderRight = '2px dotted black';
	element.style.borderBottom = '2px dotted black';		

	document.oldclick = 	document.onclick ;
//	document.onclick = document_dragclick;
	

}

function pathUp(element , ind){
	var i=0;
	var elementArray = new Array();
	while(element){
		//if(!element){
		//	break;
		//}
		elementArray[i++] = element;
		element = element.parentNode;
	}	
	window.writer += '<table cellspacing=0 cellpadding=0>'; 
	window.writer += 
		'<tr><td align=center nowrap><b>TAG NAME</b></td><td align=center nowrap><b>JAVA CLASS</b></td><td align=center nowrap><b>CSS CLASS</b></td></tr>';
	
	for(i=elementArray.length-1;i>-1;i--){
		
		element = elementArray[i];
		var name = (element.getAttribute ? element.getAttribute('javaClassName') : '' );
		
		window.writer += 
		'<tr ispath="true" xcomponentId="'+ 
			( element.getAttribute ? element.getAttribute('componentId'): '' ) + 
			'"  style="'+ getPathClickFontWeight(element)+
			';cursor:pointer;font-size:12px"  onclick="pathClick(this,getEvent(event))" ' 
			+'onmouseover="this.style.color=\'#ffffff\';this.style.background=\'#000000\';" '+
			' onmouseout="this.style.color=\'#000000\';this.style.background=\'#ffffff\'"><td nowrap>- ' 
			+ (	element ? element.nodeName : element ) + '</td><td nowrap >'
			+ " : " + (name == null ? '[NULL]' : name.substring( name.lastIndexOf('.') + 1 ))  ;
		window.writer += '</td><td nowrap> : ';
		window.writer += element.className;
		window.writer += '</td></tr>'; 			
	}	
	window.writer += '</table>'; 			
	
}


function getPathClickFontWeight(element){
	
	if(!element.getAttribute){
		return '';
	}
 	if(window.currentDebug.getAttribute('componentId') == 
 			element.getAttribute('componentId')){
 		return "font-weight:900" ;
 	}
 	return '';
}

function pathClick(element,xevent){

	var inf = get('infoBox');
	if (!element.getAttribute('xcomponentId') ){ 
		alert('no component id') 
	}
	inf.activeChild.style.fontWeight='';
	
	inf.activeChild = element;
	element.style.fontWeight='900';
	window.debugStick =''  ;

	debug('', searchComponent(  element.getAttribute('xcomponentId')  ) ,xevent,true,true);

}

function document_dragclick(){
	//alert( 'drag' );
}
 

window.contextMenuBuild;
window.capturePropertyEditorArray = new Array();
function preCapture(element,xevent){
	if(window.mode=='captureProperty'){
		return alert( 'not while capturing'); 
	}
	window.mode = 'waitCapture';
	var axis = element.getAttribute('axis');
	if (axis=='x'){
		if(window.capturePropertyEditorX ){
			deCapture(window.capturePropertyEditorX , xevent);
		}
		window.capturePropertyEditorX = element;	
	}
	else if (axis=='y'){
		if(window.capturePropertyEditorY ){
			deCapture(window.capturePropertyEditorY , xevent);
		}	
		window.capturePropertyEditorY = element;	
	}
	element.style.fontWeight='900';
	element.style.color='#000033';
	element.style.background=element.getAttribute('overBackgroundColor');
	element.state='active';
	//info(xevent);	
	
} 
function deCapture(element , xevent){
	if(window.mode=='captureProperty'){
		return alert( 'not while capturing'); 
	}
	element.style.fontWeight='';
	
	element.style.background=element.getAttribute("originalBackgroundColor");	
	element.style.color=element.getAttribute("originalColor");	
	
//	window.mode = 'waitCapture';
	var axis = element.getAttribute('axis');
	if (axis=='x'){
		window.capturePropertyEditorX = '';	
	}
	else if (axis=='y'){
		window.capturePropertyEditorY = '';	
	}	
	if(!window.capturePropertyEditorY && !window.capturePropertyEditorX){
//		alert('window.mode=x');
		var mouseFocus = get('MouseFocus');
		mouseFocus.style.visibility='hidden';
		window.mode='';
		//5555
	}

	element.state='';
	
}
function preDragClick (event){
		var xevent = getEvent(event);
		if (window.mode == 'preDrag' && xevent.srcElement.id!='button_drag'){
			drag(window.currentDebug,xevent);
		}
		return false;
}
function preDimClick (event){
		var xevent = getEvent(event);
		
		if (window.mode == 'preDim' && xevent.srcElement.id!='button_dimension'){
		
			dim(window.currentDebug,xevent);
		}
		return false;
}




function drag(element,xevent){
	window.mode='drag';
	window.originalEventX  = xevent.clientX;
	window.originalEventY  = xevent.clientY;
	window.originalElementX  = extractNumber(window.currentDebug.style.left);
	window.originalElementY  = extractNumber(window.currentDebug.style.top);
}
function dim(element,xevent){
	//info(window.currentDebug);
	//return;
	window.mode='dim';
	window.originalEventX  = xevent.clientX;
	window.originalEventY  = xevent.clientY;

	window.originalElementWidth  = extractNumber(window.currentDebug.style.width);
	window.originalElementHeight = extractNumber(window.currentDebug.style.height);
	//alert('dim');
}

function sendComponentValues(  )
{
	var comp = arguments[0];
	if(comp.tagName!='INPUT' && comp.type != 'HIDDEN'){
		alert( 'value sender must be a component of type hidden' );  
		return;
	}
	document.forms[0]['componentId'].value= comp.getAttribute('componentId');
	comp.value='';
	for(i=1;i<arguments.length;i++){
		var name = "p"+arguments[i];
		var value = comp.getAttribute( name  );
		comp.value += name + '=' + value + ';';
//		alert(name+'='+value);
	}

	document.forms[0].target='main';
	document.forms[0].submit();


}
function sendComponentValuesDynamic(  )
{
	var comp = arguments[0];

	if(comp.tagName!='INPUT' && comp.type != 'HIDDEN'){
		alert( 'value sender must be a component of type hidden' );  
		return;
	}
	document.forms[0]['componentId'].value= comp.getAttribute('componentId');
	comp.value='';
	for(i=1;i<arguments.length;i++){
		var name = "p"+arguments[i];
		var value = comp.getAttribute( name  );
		comp.value += name + '=' + value + ';';
		//alert(name+'='+value);
	}

	document.forms[0].target='hiddenSubmitter';
	document.forms[0].submit();
	document.forms[0].target='main';
}


function getUltimateOffSetLeft(element){
	var ret =0;
	while (element.parentNode){
		if(element.offsetLeft){
			ret += element.offsetLeft;
		}
		element = element.parentNode;
	}
	return ret;
}
function getUltimateOffSetTop(element){
	var ret =0;
	while (element.parentNode){
		if(element.offsetTop){
			ret += element.offsetTop;
		}
		element = element.parentNode;
	}
	return ret;
}


