function findDynObj(obj){
	while(typeof(obj.className) == 'undefined' || obj.className != "dynamicContent"){
		obj = obj.parentNode ? obj.parentNode : obj.parentElement;
		if(obj == null){
			return null;
		}
	}
	//alert(obj);
	return obj;
}

//Window reorder functions
var isreorder=false;
var issize=false;
var isdrag=false;
var x,y;
var movingobj,movingobj;
function bringFront(movingobj){
	//alert("front: "+movingobj.id);
	movingobj.style.zIndex = 89;
	if(KPWindowFindParent(movingobj.parentNode) != null && KPWindowFindParent(movingobj.parentNode).id != "kpwindowscrollpane"){
		KPWindowFindParent(movingobj.parentNode).style.zIndex = 89;
	}
    for(x=0; x<KPWindow.length; x++){
		if(KPWindow[x] != null && KPWindow[x] != movingobj){
			/*alert("back: "+KPWindow[x].id);
			alert(KPWindow[x].style.zIndex);
			alert(window.getComputedStyle(KPWindow[x],'').zIndex);*/
			//alert(typeof(window.getComputedStyle));
			if(typeof(window.getComputedStyle) == 'undefined'){
				//alert(KPWindow[x].currentStyle.zIndex);
				if(KPWindow[x].currentStyle.zIndex >= 80)
					KPWindow[x].style.zIndex=KPWindow[x].currentStyle.zIndex-1;
			}else{
				//alert(window.getComputedStyle(KPWindow[x],'').zIndex);
				if(window.getComputedStyle(KPWindow[x],'').zIndex >= 80)
					KPWindow[x].style.zIndex=window.getComputedStyle(KPWindow[x],'').zIndex-1;
			}
		}
	}
}
function mouseDown(e1){
	movingobj = e1 ? e1.target : event.srcElement;
	//find parent window and bring in front on click
	bringFront(findDynObj(movingobj));
	//find if size or drag elements are hit
	if(movingobj.className == 'KPWindowSizeBut' || movingobj.getAttribute("name") == 'KPWindowSizeBut'){
		issize = true;
	}
	if(movingobj.className == 'KPWindowMoveBut' || movingobj.getAttribute("name") == 'KPWindowMoveBut'){
		isdrag = true;
	}	
	if(isreorder){
		movingobj = findDynObj(movingobj);
	    //addDropLayer();
	    document.getElementsByTagName("body")[0].style.cursor = 'move';
	    return false;
	}
	if(issize){
	    movingobj = findDynObj(movingobj);
	    //tx = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj.getElementsByTagName("table")[0],'').width+0) : parseInt(movingobj.currentStyle.width+0); //element start pos.
	    //ty = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj.getElementsByTagName("table")[0],'').height+0) : parseInt(movingobj.currentStyle.height+0);
    	tx = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').width+0) : parseInt(movingobj.currentStyle.width+0); //element start pos.
	    ty = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').height+0) : parseInt(movingobj.currentStyle.height+0);
    	
    	//movingobj2 is the content row of the dialogborder wich needs static size for proper scrollbars withing a table... pruts.
    	movingobj2 = DyCoGetContentRow(movingobj);
    	if(movingobj2 != null){
	    	//tx2 = parseInt(movingobj2.style.width);
	    	//ty2 = parseInt(movingobj2.style.height);
		    tx2 = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj2,'').width+0) : parseInt(movingobj2.currentStyle.width+0); //element start pos.
		    ty2 = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj2,'').height+0) : parseInt(movingobj2.currentStyle.height+0);
	    }
	    if(isNaN(tx)){
	    	tx = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').width+0) : parseInt(movingobj.currentStyle.width+0); //element start pos.
	    }
	    if(isNaN(ty)){
	    	ty = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').height+0) : parseInt(movingobj.currentStyle.height+0);
	    }
	    if(movingobj2 != null && isNaN(tx2)){
	    	tx2 = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').width+0) : parseInt(movingobj.currentStyle.width+0); //element start pos.
	    }
	    if(movingobj2 != null && isNaN(ty2)){
	    	ty2 = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').height+0) : parseInt(movingobj.currentStyle.height+0);
	    }
	    movingobj.style.width=tx;
	    movingobj.style.height=ty;
	    x = e1 ? e1.clientX : event.clientX; //event start pos.
	    y = e1 ? e1.clientY : event.clientY;
	    document.onmousemove=sizeWin;
	    //addDropLayer();
	    movingobj.style.cursor = 'se-resize';
	    return false;
	}
	if(isdrag){
	    movingobj = findDynObj(movingobj);
	    //bringFront(movingobj);
	    tx = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').left+0) : parseInt(movingobj.currentStyle.left+0); //element start pos.
	    ty = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').top+0) : parseInt(movingobj.currentStyle.top+0);
	    x = e1 ? e1.clientX : event.clientX; //event start pos.
	    y = e1 ? e1.clientY : event.clientY;
	    if(isNaN(tx) || isNaN(ty)){
	    	alert("nan");
	    	movingobj.style.position = 'fixed';
    		tx = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').left+0) : parseInt(movingobj.currentStyle.left+0);
    		ty = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').top+0) : parseInt(movingobj.currentStyle.top+0);
    		width = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').width+0) : parseInt(movingobj.currentStyle.width+0);
    		height = window.getComputedStyle ? parseInt(window.getComputedStyle(movingobj,'').height+0) : parseInt(movingobj.currentStyle.height+0);
    		if(isNaN(tx)){
    			tx = x-(width/2);
    		}
    		if(isNaN(ty)){
    			ty = y-10;
    		}
			movingobj.style.width = width+'px';
			movingobj.style.height = height+'px';
	    }
	    //alert(x+" "+y+" "+" "+tx+" "+ty);
	    document.onmousemove=dragWin;
	    movingobj.style.cursor = 'move';
	    return false;
	}
	movingobj.focus();
}
function dragWin(e4)
{
  if (isdrag)
  {
    ileft = tx + (e4 ? e4.clientX : event.clientX) - x;
    itop =  ty + (e4 ? e4.clientY : event.clientY) - y;
    //alert(ileft+" - "+itop);
    movingobj.style.left = ileft+'px';
    movingobj.style.top = itop+'px';
    return false;
  } 
}
//make fixed obj relative.
function snapBack(movingobj){
	//TODO make kpwindows reset function for each windows sepperate, including safe/restore user position and use this,
	//now it only works properly for divframes, not kpwindow frames.
	movingobj = findDynObj(movingobj);
	movingobj.style.width = '';
    	movingobj.style.height = '';
    	movingobj.style.position = '';
    	movingobj.style.left = '';
    	movingobj.style.position = '';
    	movingobj.style.top = '';
}
function sizeWin(e2){
  if (issize)
  {
    ileft = tx + (e2 ? e2.clientX : event.clientX) - x;
    itop =  ty + (e2 ? e2.clientY : event.clientY) - y;
    //movingobj2 is the content row of the dialogborder wich needs static size for proper scrollbars withing a table... pruts.
    
    if(movingobj2 != null){
    	movingobj2.style.width =  tx2 + (e2 ? e2.clientX : event.clientX) - x + 'px';
    	movingobj2.style.height =  ty2 + (e2 ? e2.clientY : event.clientY) - y + 'px';
    }
    movingobj.style.width = ileft+'px';
    movingobj.style.height = itop+'px';
    return false;
  }	
}
function mouseUp(e3){
	if(isreorder){
		releaseobj = e3 ? e3.target : event.srcElement;
		releaseobj = findDynObj(releaseobj);
		//removeDropLayer();
		try {
			parentPane = releaseobj.parentNode ? releaseobj.parentNode : releaseobj.parentElement;
		} catch(er) { return false; }
		
		parentPane.replaceChild(movingobj,releaseobj);
		parentPane.insertBefore(releaseobj,movingobj);
	}
	if(issize||isreorder||isdrag){
		//removeDropLayer();
		movingobj.style.cursor = 'default';
		//if movingobj changed save settings
		KPWindowSaveUserSettingsWindow(movingobj.id);
	}
	isreorder=false;
	issize=false;
	isdrag=false;
	document.mousemove=null;
}

/*var dummyCount = 1;
function getDummy(){
	dropLayer = createEl('http://www.w3.org/1999/xhtml','div');
	dropLayer.className = 'dropLayer';
	dropLayer.id = 'dropLayer'+dummyCount++;
	return dropLayer;
}

var dropLayers = new Array();
function addDropLayer(){
    list = document.getElementsByTagName("iframe");
    while(list.length > 0){
    	dummy = getDummy();
    	dropLayers[dummy.id] = list[0];
		list[0].parentNode.replaceChild(dummy, list[0]);
    }
}
function removeDropLayer(){
	for(k in dropLayers){
		if(dropLayers[k]!=null){
			dl = document.getElementById(k);
			dl.parentNode.replaceChild(dropLayers[k],dl);
			dropLayers[k] = null;
		}
	}
	dummyCount = 1;
}*/
//registerMouseUp(mouseUp);
//registerMouseDown(mouseDown);

//document.onmousedown=mouseDown;
//document.onmouseup=mouseUp;
function DyCoInit(){
	aelements = document.getElementsByName("KPWindowSizeBut");
	for(q = 0; q<aelements.length; q++){
		KPWindowFindParent(aelements[q]).onmousedown=mouseDown;
		KPWindowFindParent(aelements[q]).onmouseup=mouseUp;
	}
}

function DyCoGetContentRow(targetel){
	sizeContentRows = document.getElementsByName('content_row');
	returnvalue = sizeContentRows[0];
	for(q = 1; q < sizeContentRows.length && KPWindowFindParent(returnvalue) != KPWindowFindParent(targetel); q++){
		returnvalue = sizeContentRows[q];
	}	
	if(KPWindowFindParent(returnvalue) != KPWindowFindParent(targetel)){
		return null;
	} else {
		return returnvalue;
	}
}
