// pixelpark (schweiz) ag
// name: divUtils.js
// version/date: v0.9 / 3.9.2000
// author: pascal betz (pascal.betz@pixelpark.ch)
// author2: pascal fuchser (pascal.fuchser@pixelpark.ch)
//
// description: handling of positional objects (<div></div>) move, get/set zIndex, drag'n'drop
// JAN-2001 / PF : update for NS6
//
// known bugs: 
//
//
// important: most of these functions rely on the dom-switch variables (doc, sty and htm) and on the
// browser detection using an Is() object named 'is' (both in Browser.js)

// ----------- Global Variables used by these Scripts ------------- //

// var dragZIndex: backup of the dragged Objects Z-Index
// var dragTopZIndex: Top-Most Z-Index (dragged object is set to this value to apear above all other (but NC Forms))
// var dragObj: The Object beeing dragged
// var dragXPos: x-Position of dragged Object
// var dragYPos: y-Position of dragged Object

// ----------- FUNCTION DESCRIPTIONS --------------- //

// ----------- Positioning / Position Information ----------- //

// moveTo(ob, x, y): moves an Object to given position
// ob: the object to move
// x: the x Position to move Object to
// y: the y Position to move Object to

// moveToX(ob, x): movesw an Object to given x position
// ob: Object to move
// x: the x Position to move Object to

// moveToY(ob, y): movesw an Object to given y position
// ob: Object to move
// y: the y Position to move Object to

// getPosX(ob): returns the x-Position of an Object
// ob: Object to get x-Position from

// getPosX(ob): returns the y-Position of an Object
// ob: Object to get y-Position from

// center(ob): centers an Object on Content-Space
// ob: Object to center

// ----------- Screen/Object-Size Information ----------- //

// getScreenWidth(): returns the Screen Width
// no arguments

// getScreenHeight(): returns the screen Height
// no arguments

// getContentWidth(): returns the Content Width (Innerwidth of Browser)
// no arguments

// getContentHeight(): returns the Content Height (Innerheight of Browser)
// no arguments

// getWidth(ob): returns the Size of an object (object size in IE, Clipping region in NC)
// ob: the object to return width from

// getHeight(ob): returns the Size of an object (object size in IE, Clipping region in NC)
// ob: the object to return height from

// ----------- Visibility / Color ----------- //

// show(ob): sets the objects visibility to visible
// ob: object to change visibility

// hide(ob): sets the objects visibility to hidden
// ob: object to change visibility

// showHide(ob): toggles the objects visibility (--> if hidden to visible / if visible to hidden)
// ob: the object to change visibility

// setColor(ob, col): changes the color of an object to given color
// ob: the Object to change color
// col: the color to set as string

// ----------- Z-Index ----------- //

// getZIndex(ob): returns the Z-Index of an Object
// ob: the Object to return the Z-Index from

// setZIndex(ob, z): sets the Z-Index of an Object to given value
// ob: the Object to set Z-Index
// z: the Z value to set


// ----------- Object/Source Info ----------- //
// getObject(ob): returns an Object reference
// ob a String/Object representation of an Object

// getSource(evt): returns the source of an Event 
// evt: the event (obly needed for Netscape, MSIE uses window.event)


// ----------- Dragging ----------- //
// allows dragging of div's. either place an <img> with same name as <div> name or <a> tag in the <div> you want to drag

// dragHandlerDown(evt): eventhandler for drag'n'drop
// evt: the event
// modify behavior (drag object allowed ?, drop range, return values) here and not in clickObject(evt) !!

// dragHandlerMove(evt): eventhandler for drag'n'drop
// evt: the event
// modify behavior (drag object allowed ?, drop range, return values) here and not in dragObject(evt) !!

// dragHandlerUp(evt): eventhandler for drag'n'drop
// evt: the event
// modify behavior (drag object allowed ?, drop range, return values) here and not in dropObject(evt) !!

// clickObject(evt): helper method for drag'n'drop
// evt: the event

// dragObject(evt): helper method for drag'n'drop
// evt: the event

// dropObject(evt): helper method for drag'n'drop
// evt: the event





// Move Object to x/y position
function moveTo(ob, xPos, yPos){
	if (is.ie4){
		//dragObj = eval("window.document.all.wordDiv.style");
		ob.pixelLeft = xPos;
		ob.pixelTop = yPos;
	} else if (is.nc4){
		ob.moveTo(xPos, yPos);	
	} else if (is.nc6){
		ob = ob.style;
		ob.left = xPos;
		ob.top = yPos;
	}
}
// Moves the Object to x Position without changing the y Position
function moveToX(ob, xPos){
	//var ob = getObject(ob);
	if (is.ie4){
		ob.pixelLeft = xPos;
	} else if (is.nc4){
		var yPos = ob.top;
		ob.moveTo(xPos, yPos);	
	} else if (is.nc6){
		ob = ob.style;
		var yPos = ob.top;
		ob.left = xPos;
		ob.top = yPos;
	}	
}
// Moves the Object to y Position without changing the x Position
function moveToY(ob, yPos){
	if (is.ie4){
		ob.pixelTop = yPos;
	} else if (is.nc4){
		var xPos = ob.left;
		ob.moveTo(xPos, yPos);
	} else if (is.nc6){
		ob = ob.style;
		var xPos = ob.left;
		ob.left = xPos;
		ob.top = yPos;	
	}	
}
// returns the xPosition of object
function getPosX(ob){
	if (is.nc6){ob = ob.style;}
	return parseInt(ob.left);
}
// returns the yPosition of object
function getPosY(ob){
	if (is.nc6){ob = ob.style;}
	return parseInt(ob.top);
}
// Centers a Object on Content-Space
function center(ob){
	var w = getContentWidth();
	var obW = getWidth(ob);
	var h = getContentHeight();
	var obH = getHeight(ob);
	if (is.ie4 || is.nc4) {
		moveTo(ob, (w-obW)/2, (h-obH)/2);
	} else if (is.nc6){
		ob = ob.style;
		ob.left = (w-obW)/2;
		ob.top = (h-obH)/2;
 	}
	

}

// Returns the screen width
function getScreenWidth(){
	return screen.width;	
}

// returns the screen height
function getScreenHeight(){
	return screen.height
}


// returns browser content width
function getContentWidth(){
	var retVal;
	if (is.ie){
		retVal = document.body.clientWidth;		
	} else {
		retVal = window.innerWidth;	
	}
	return retVal;	
}
// returns browser content height
function getContentHeight(){
	var retVal;
	if (is.ie){
		retVal = document.body.clientHeight;		
	} else {
		retVal = window.innerHeight;	
	}
	return retVal;	
}



// Set an Objects Visibility to visible
function show(ob){
	if (is.nc6){ob = ob.style;}
	ob.visibility = "visible";	
}


// Set an Objects Visibility to hidden
function hide(ob){
	if (is.nc6){ob = ob.style;}
	ob.visibility = "hidden";	
}

// Toggles the layers visibility
function showHide(ob){
	// Check if hidden (IE) or hide (NC)
	if (is.nc6){
		if (ob.style.visibility == "hidden" || ob.style.visibility == "hide"){
			show(ob);	
		} else {
			hide(ob);
		}
	} else {
		if (ob.visibility == "hidden" || ob.visibility == "hide"){
			show(ob);	
		} else {
			hide(ob);
		}
	}	
}

// Sets the Z-Index of the Object
function setZIndex(ob, z){
	if (is.nc6){ob = ob.style;}
	ob.zIndex = z;
}

// gets the Z-Index of the Object
function getZIndex(ob){
	if (is.nc6){ob = ob.style;}
	return ob.zIndex;
}


// returns the width of object (IE as defined, NC Clipping width)
function getWidth(ob){
	if(is.ie4){
		return ob.posWidth;
	} else if(is.nc4){
		return ob.clip.width;	
	} else if(is.nc6){
   		return parseInt(document.defaultView.getComputedStyle(ob, "").getPropertyValue("width"));
	}
}


// returns the height of object (IE: height as defined, NC Clipping height)
function getHeight(ob){
	var retVal;
	if(is.ie4){
		return ob.posHeight;
	} else if(is.nc4){
		return ob.clip.height;
	} else if(is.nc6){
   		return parseInt(document.defaultView.getComputedStyle(ob, "").getPropertyValue("height"));
	}
}

function setColor(ob, col){
	//var ob = getObject(ob);
	if (is.ie4){
		ob.backgroundColor = col;
	} else if(is.nc6){
		ob.style.backgroundColor = col;
	} else {
		ob.bgColor = col;
	}	
}


// Dragging functions and Vars


var dragObj;
var dragOffX = 0;
var dragOffY = 0;
// at begin holding Z-Index of topmost layer, during execution stores the Z-Value of layer dragged at this moment
var dragObjZ = 9999;

var tem = 0;


function selectObject(evt){
	if (is.ie){
		var temp = event.srcElement;
		// Search the first DIV Tag in Object Tree
		while(temp.tagName != 'DIV' && temp.tagName !='BODY'){
			temp = temp.parentElement;	
		}
		if (temp.tagName == 'DIV'){
			dragObj = temp.style
		} else {
			dragObj = null; 	
		}
	} else if (is.nc4){
		var xPos = evt.pageX;
		var yPos = evt.pageY;
		var tempObj;
		// check every Object in reverse order! (cascaded divs)
		for(var i = document.layers.length-1; i > -1; i--){
			tempObj = document.layers[i];
			// check click range of elements
			if ((xPos > tempObj.left) && (xPos < tempObj.left + tempObj.clip.width) &&
			    (yPos > tempObj.top) && (yPos < tempObj.top + tempObj.clip.height)){
			  	if (dragObj && dragObj.zIndex > tempObj.zIndex){
						// keep this Object selected
			  	} else {
			  		// new element found or element is on zIndex above the old
			  		dragObj = tempObj;	
			  	}
			}
		}
	} else if (is.nc6){
		// for NS6 by pascal fuchser
		var xPos = evt.pageX;
		var yPos = evt.pageY;
		var tempObj;
		var tempObjtop;
		var tempObjleft;
		var tempObjwidth;
		var tempObjheight;

		//alert(evt.pageX +" / "+ evt.pageY);
		//alert(document.getElementsByTagName('div').length);
		// check every Object in reverse order! (cascaded divs)
		for(var i = document.getElementsByTagName('div').length-1; i > -1; i--){
			tempObj = document.getElementsByTagName('div')[i];
			//alert(parseInt(document.defaultView.getComputedStyle(tempObj,null).getPropertyValue("height")));
			// check click range of elements
			var tempObjtop = parseInt(tempObj.style.top);
			var tempObjleft = parseInt(tempObj.style.left);
			var tempObjwidth = parseInt(document.defaultView.getComputedStyle(tempObj,null).getPropertyValue("width"));
			var tempObjheight = parseInt(document.defaultView.getComputedStyle(tempObj,null).getPropertyValue("height"))
			if ((xPos > tempObjleft) && (xPos < tempObjleft + tempObjwidth) &&
			    (yPos > tempObjtop) && (yPos < tempObjtop + tempObjheight)){				
			  	if (dragObj && dragObj.style.zIndex > tempObj.style.zIndex){
						// keep this Object selected
			  	} else {
			  		// new element found or element is on zIndex above the old
			  		dragObj = tempObj;
					//alert ("dragObj = " +tempObj);	
			  	}
			}
		}
	} else {
		dragObj = null;		
	}

	if(dragObj){
		var temp = getZIndex(dragObj);
		setZIndex(dragObj, dragObjZ);
		dragObjZ = temp;
	}
}

function dragObject(evt){
	if (dragObj){
		var x;
		var y;
		if (is.ie){
			x = window.event.clientX-dragOffX;
			y = window.event.clientY-dragOffY;
			moveTo(dragObj, x, y);
			return false;	
		}else if(is.nc4){
			x = evt.pageX - dragOffX;
			y = evt.pageY - dragOffY;
			moveTo(dragObj, x, y);	
			return false;
		}else if(is.nc6){
			x = evt.pageX - dragOffX;
			y = evt.pageY - dragOffY;
			moveTo(dragObj, x, y);	
			return false;
		}
	}
}

function dropObject(evt){
	if (dragObj){
		var temp = getZIndex(dragObj);
		setZIndex(dragObj, dragObjZ);
		dragObjZ = temp;
		dragObj = null;
	}
	// passing event
	return true;
}

function clickObject(evt){
	selectObject(evt);
	if (dragObj){
		if (is.ie){
			dragOffX = window.event.offsetX;
			dragOffY = window.event.offsetY;
		}else if(is.nc4){
			dragOffX = evt.pageX - dragObj.left;
			dragOffY = evt.pageY - dragObj.top;
		}else if(is.nc6){
			dragOffX = evt.pageX - parseInt(dragObj.style.left);
			dragOffY = evt.pageY - parseInt(dragObj.style.top);
		}
	}
}


function dragHandlerDown(evt){		
  if (is.nc4){
    if (evt.which == 3 || evt.which == 2){
      return false;
    } else {
      return clickObject(evt);
    }
  } else if (is.ie){ 
    if(event.button == 2 || event.button == 3){
      alert("interdit!");
      return false;
    } else {
	    return clickObject(evt);	
    }
  } else if (is.nc6){
    if (evt.which == 3 || evt.which == 2){
      alert("interdit!");
      return false;
    } else {
      return clickObject(evt);
    }
  }

}
		
function dragHandlerMove(evt){
	// other mouse move hanndling here
	return dragObject(evt);
}
		
function dragHandlerUp(evt){
	// other mouse up handling here
	return dropObject(evt);
}


// Returns a Browser Specific Reference to an Object
function getObject(obName){
	if (is.ie4 || is.nc4){
	return eval(doc+"."+obName+sty);
 	} else if (is.nc6){
	// div möglichkeiten.
	//alert(document.getElementById("object1").style);
	//alert(document.getElementById("object1"));
	//alert(document.getElementsByTagName("object1"));
	return eval(doc+obName+sty);
	}
}
