/*
 * Copyright 1999-2007 by Vignette, Inc.,
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information
 * of Vignette, Inc. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Vignette.
 * 
 */
 
function cm_scrollup(y1,y2){
	for (var i=y1; i>y2;) {
		window.scroll(1,i)
		i-=SCROLL_SPD
	}
}
function cm_scrolldown(y1,y2){
	for (var i=y1; i<y2;) {
		window.scroll(1,i)
		i+=SCROLL_SPD
	}
}
function cm_scrollleft(x1,x2){
	for (var i=x1; i>x2;) {
		window.scroll(i,1)
		i-=SCROLL_SPD
	}
}
function cm_scrollright(x1,x2){
	for (var i=x1; i<x2;) {
		window.scroll(i,1)
		i+=SCROLL_SPD
	}
}
var EVENT_MOUSEOVER = 0;
var EVENT_MOUSEOUT = 1;
var EVENT_MOUSEUP = 2;
var EVENT_MOUSEDOWN = 3;

function DragCapability(obj,objStyle,dAnchorEleId,dTop,dLeft,dWidth,dHeight,dZIndex) {
	this.Obj = obj;
	this.ObjStyle = objStyle;
	this.Top = dTop;
	this.Left = dLeft;
	this.Width = dWidth;
	this.Height = dHeight;
	this.zIndex = dZIndex;
	this.Obj.anchorEleIndex = dAnchorEleId;
}
DragCapability.prototype.SetZIndex = function (dcZ) {
	this.ObjStyle.zIndex = dcZ;
	this.zIndex = dcZ;
}
DragCapability.prototype.SetTop = function (dcTop) {
	this.ObjStyle.top = dcTop;
	this.Top = dcTop;
}
DragCapability.prototype.SetLeft = function (dcLeft) {
	this.ObjStyle.left = dcLeft;
	this.Left = dcLeft;
}
DragCapability.prototype.Move = function (dcX,dcY) {
	this.SetTop(dcY);
	this.SetLeft(dcX);
}
DragCapability.prototype.BindEvent = function (eventType,aMethod) {
	var succeed = true;
	switch (eventType) {
		case 0:
			if (bw.ns4)
				this.Obj.captureEvents(Event.MOUSEOVER);
			this.Obj.onmouseover = aMethod;
			break;
		case 1:
			if (bw.ns4)
				this.Obj.captureEvents(Event.MOUSEOUT);
			this.Obj.onmouseout = aMethod;
			break;
		case 3:
			if (bw.ns4)
				this.Obj.captureEvents(Event.MOUSEDOWN);
			this.Obj.onmousedown = aMethod;
			break;
		case 2:
			if (bw.ns4)
				this.Obj.captureEvents(Event.MOUSEUP);
			this.Obj.onmouseup = aMethod;
			break;
		default:
			succeed = false;
			break;
	}
	return succeed;
}
var PANEL_SPACING=5, PANEL_MIN_H=50, PANEL_TOP=10, PANEL_BOT=10;
var MOD_FIX=0, MOD_DRG=1, MOD_NOT=2, MOD_TOP_Z=1000, MOD_BOT_Z=100;

var PageLayout;
var DraggMod;
var ModStartX = 0;
var ModStartY = 0;
var DropCol;
var DropIndex = 0;
var DropIndicator;
var IndicatorHeight = 0;
var Panels;
var MovEl;

function Row(divID,width,modType,anchorId,spcBtwRows,sameChildH,equalDist,endItem) {
	// the div DOM object
	this.view= new cm_makeObj(divID);
	this.id=divID;
	
	if(typeof width == "undefined" || width == null) this.width="100%";
	else this.width=width;
	
	this.height=-1;
	this.columns= new Array();
	this.parent=null;
	
	if(MOD_DRG == modType || MOD_NOT == modType) this.type=modType;
	else this.type=MOD_FIX;
	
	this.spc=0;
	if(spcBtwRows && typeof spcBtwRows == "number" && spcBtwRows != NaN) this.spc=spcBtwRows;

	if(sameChildH) this.sameHg=true;
	else this.sameHg=false;

	if(this.sameHg && equalDist) this.eqlDisb=1;
	else this.eqlDisb=0;
	
	// 1 if this row will not add any child
	if(endItem) this.ends=1;
	else this.ends=0;
	
	// the portlet position indication bar
	this.indicator=null;
	
	// the id for activating the drag action
	this.dragAnchorId=anchorId;
	return this;
}

Row.prototype.getMinWidth = function() {
	if(!this.minw){
		if(!bw.ie && !bw.safari) this.view.css.position="relative";
		this.view.setWidth(1);
		this.minw=this.view.getWidth();
		if(!bw.ie && !bw.safari) this.view.css.position="absolute";
	}
	return this.minw;
}

Row.prototype.resizeWidth = function() {
	if(this.parent!=null&&this.width.toString().indexOf("%")!=-1){
		var p=Math.round(this.parent.view.getWidth()*parseInt(this.width.substring(0,this.width.length-1))/100);
		if(this.ends){
			this.minw=this.getMinWidth();
			if(this.minw>p){
				this.view.setWidth(this.minw);
			} else {
				this.view.setWidth(p);
			}
		} else {
			this.view.setWidth(p);
		}
	} else {
		this.view.setWidth(this.width);
	}
}

Row.prototype.setSameWidth = function(maxw) {
	if(!maxw) return;
	if(maxw>this.w){
		var dw=maxw-this.w;
		this.w=maxw;
		this.view.setWidth(this.w);
	} else {
		var dw=0;
	}
	if(!this.ends){
		var numOfCol=this.columns.length;
		if (numOfCol>0) {
			var x=this.x;
			var col
			for (var i=0;i<numOfCol-1;i++) {
				col = this.columns[i];
				col.moveTo(x,this.y);
				col.setSameWidth(col.w);
				x+=col.w+col.spc;
			}
			col=this.columns[numOfCol-1];
			col.moveTo(x,this.y);
			col.setSameWidth(dw+col.w);
		} else {
			this.h=this.view.getHeight();
		}
	} else {
		this.h=this.view.getHeight();
	}
	this.setIndicatorLoc(0,1,1);
}

Row.prototype.resizeHeight = function() {
	if (this.ends) return;
	var numOfCols = this.columns.length;
	var maxh = 0;
	var child=null;
	for(var i=0;i<numOfCols;i++){
		child=this.columns[i];
		if(maxh<child.h) maxh=child.h;
	}
	if(this.height==-1) this.height=this.view.getHeight();

	if(child!=null){
		this.view.setHeight(maxh);
	} else {
		this.view.setHeight(this.height);
	}
}

Row.prototype.setSameHeight = function(maxh) {
	if (!maxh || this.ends) return;
	if(maxh>this.h){
		this.h=maxh;
		this.view.setHeight(this.h);
	}
	var x=this.x;
	for(var i=0;i<this.columns.length;i++){
		var col=this.columns[i];
		col.moveTo(x,this.y);
		col.setSameHeight(maxh,this.eqlDisb);
		x+=col.w+col.spc;
	}
}

Row.prototype.moveFamilyTo = function(x,y) {
	var x1=x;
	for (var i=0;i<this.columns.length;i++) {
		var col=this.columns[i];
		col.moveFamilyTo(x1,y);
		x1+=col.w+col.spc;
	}
	this.moveTo(x,y);
}

Row.prototype.moveTo = function(x,y) {
	if(this.x!=x){
		this.x=x;
		this.view.setLeft(x);
		this.setIndicatorLoc(1);
	}
	if(this.y!=y){
		this.y=y;
		this.view.setTop(y);
		this.setIndicatorLoc(0,1);
	}
}

Row.prototype.endsWithRows = function() {
	if (this.columns.length==0) return 1;
	else {
		for(var i=0;i<this.columns.length;i++){
			var r=this.columns[i].endsWithRows();
			if(r) break;
		}
		return r;
	}
}
/*
Row.prototype.getLastColumn = function() {
	if (this.ends || this.columns.length==0) return this.parent;
	else return this.columns[this.columns.length-1].getLastColumn();
}
*/
Row.prototype.setIndicatorLoc = function(l,t,w) {
	if(this.indicator!=null) {
		var d=this.indicator;
		if (!IndicatorHeight) IndicatorHeight=this.spc/2;
		if (l&&d.x!=this.x) {
			d.x=this.x;
			d.setLeft(d.x);
		}
		if(t){
			var y=this.y-this.spc/2-IndicatorHeight;
			if(d.y!=y){
				d.y=y;
				d.setTop(y);
			}
		}
		if(w && d.w!=this.w){
			d.w=this.w;
			d.setWidth(d.w);
		}
		if (!d.h && d.h!=IndicatorHeight*2) d.setHeight(IndicatorHeight*2);
	}
}

Row.prototype.addColumn = function(column) {
	if(this.ends) return;
	if(this.columns.length>0) this.columns[this.columns.length-1].last=0;
	this.columns[this.columns.length]=column;
	column.parent=this;
	column.last=1;
}

Row.prototype.addIndicator = function(indicator) {
	this.indicator=indicator;
}

Row.prototype.switchClass = function(classNum) {
	if(this.classes&&classNum>=0&&this.currentClass!=classNum){
		if(classNum<this.classes.length){
			this.view.evnt.className=this.classes[classNum];
			this.currentClass=classNum;
		}
	}
}

Row.prototype.render = function(left,top,fixw) {
	if(typeof left!="undefined"){
		this.view.setLeft(left);
		this.x=left;
	} else this.x=this.view.getLeft();
	if(typeof top!="undefined") {
		this.view.setTop(top);
		this.y=top;
	} else this.y=this.view.getTop();

	if (fixw) this.view.setWidth(fixw);
	else this.resizeWidth();

	// renderring children
	var childLeft=this.x;
	var childTop=this.y;
	var fatChild=0;
	var numOfCols=this.columns.length;
	if(this.sameHg){
		this.maxCH=0;
	}
	var child=null;
	for(var i=0;i<numOfCols;i++){
		child=this.columns[i];
		child.render(childLeft,childTop);
		childLeft+=child.w;
		if(fatChild<child.w) fatChild=child.w;
		childLeft+=child.spc;
		if(this.sameHg){
			if(this.maxCH<child.h){
				this.maxCH=child.h;
			}
		}
	}
	// set children at same height
	if(this.sameHg){
		for(var i=0;i<numOfCols;i++){
			var child=this.columns[i];
			if(child.h<this.maxCH){
				child.setSameHeight(this.maxCH,this.eqlDisb);
			}
		}
	}

	// check if total width for children is larger than given width
	this.w=this.view.getWidth();
	var totalw=childLeft-this.x;
	if(child) totalw-=child.spc;
	if(totalw>this.w){
		this.w=totalw;
		this.view.setWidth(this.w);
	}

	this.resizeHeight();

	this.view.showIt();

	this.h=this.view.getHeight();

	if(this.type==MOD_DRG){
		this.dc= new DragCapability(this.view.evnt,this.view.css,this.dragAnchorId,this.y,this.x,this.w,this.h);
		this.dc.Obj.module=this;
		this.dc.BindEvent(EVENT_MOUSEDOWN,DownHandler);
		this.dc.BindEvent(EVENT_MOUSEOVER,OverHandler);
	}

	this.setIndicatorLoc(1,1,1);
}

function DownHandler (e) {
	// this is bind to DragCapability.Obj
	if (bw.ie) { e = window.event; }

	if (bw.ns6) {
		MovEl = e.target;
	} else {
		MovEl = e.srcElement;
	}
	if (this.anchorEleIndex) {
		var srcID = MovEl?MovEl.id:0;
		if (!srcID || srcID.indexOf(this.anchorEleIndex) == -1) {
			return;
		}
	}

	if (bw.ie || bw.ns6) {
		document.onmousemove = MoveHandler;
		document.onmouseup = UpHandler;
	} else {
		window.onMouseMove = MoveHandler;
		window.onMouseUp = UpHandler;
		window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
	}
	this.module.dc.SetZIndex(MOD_TOP_Z);
	ModStartX = (bw.ie ? e.clientX  + document.body.scrollLeft: e.pageX);
	ModStartY = (bw.ie ? e.clientY  + document.body.scrollTop: e.pageY);
	DraggMod = this.module;
	
	DraggMod.view.css.cursor="move";
	
	if (PageLayout.upModCl && PageLayout.dnModCl) {
		DraggMod.view.evnt.className=PageLayout.upModCl;
	}
	//DraggMod.view.css = dropShadow(offX=2, offY=2, color=#666666);

	// turn on module up location indicator if present
	if (PageLayout.upModFp) {
		PageLayout.upModFp.setLeft(DraggMod.x);
		PageLayout.upModFp.setTop(DraggMod.y);
		PageLayout.upModFp.setWidth(DraggMod.w);
		PageLayout.upModFp.setHeight(DraggMod.h);
		PageLayout.upModFp.showIt();
	}

	DropCol = null;
	return false;
}

function OverHandler (e) {
	return false;
}


function Column(divID,width,modType,spcBtwCols,btmSpace,absPos,miniHeight,topSpace,toChildWidth) {
	// the div DOM object
	this.view= new cm_makeObj(divID);
	this.id=divID;

	if(typeof width == "undefined" || width == null) this.width="100%";
	else this.width=width;

	this.height=-1;

	this.rows= new Array();

	this.parent=null;

	this.type=MOD_DRG;
	if (MOD_FIX == modType) this.type=MOD_FIX;

	this.spc=0;
	if(spcBtwCols && typeof spcBtwCols == "number" && spcBtwCols != NaN) this.spc=spcBtwCols;
	this.btmSpc=0;
	if(btmSpace && typeof btmSpace == "number" && btmSpace != NaN) this.btmSpc=btmSpace;
	this.topSpc=0;
	if(topSpace && typeof topSpace == "number" && topSpace != NaN) this.topSpc=topSpace;

	this.toChildWidth=0;
	if(toChildWidth) this.toChildWidth=toChildWidth;

	this.indicator=null;

	this.isPosAbsolute=true;
	if(false == absPos){
		this.isPosAbsolute=false;
		this.relativeX=0;
		this.relativeY=0;
	}

	this.minih=-1;
	if(miniHeight && typeof miniHeight == "number" && miniHeight != NaN) this.minih=miniHeight;
	
	this.last=0;
	
	this.toPageH=0;

	return this;
}

Column.prototype.setRelativePos = function(pX, pY) {
	this.relativeX = pX;
	this.relativeY = pY;
}

Column.prototype.resizeWidth = function() {

	if (this.parent!=null && this.width.indexOf("%")!=-1) {
		var p = Math.round(this.parent.view.getWidth()*parseInt(this.width.substring(0,this.width.length-1))/100);
		this.view.setWidth(p - this.spc);
	} else {
		this.view.setWidth(this.width);
	}
}

Column.prototype.setSameWidth = function(maxw,begin) {
	if (!maxw) return;
	if (begin) {
		if (this.toChildWidth) {
			var x=this.x+this.leftSpc+dw/2;
			dw=0;
		} else {
			var x=this.x;
		}
		var y=this.y+this.topSpc;
		for (var i=0;i<this.rows.length;i++) {
			var row = this.rows[i];
			row.moveTo(x,y);
			row.setSameWidth(maxw);
			y+=row.h+row.spc;
		}
	} else {
		if (maxw>this.w){
			var dw=maxw-this.w;
			this.w=maxw;
			this.view.setWidth(this.w);
		} else {
			var dw=0;
		}
		if (this.toChildWidth) {
			var x=this.x+this.leftSpc+dw/2;
			dw=0;
		} else {
			var x=this.x;
		}
		var y=this.y+this.topSpc;
		var row=null;
		for (var i=0;i<this.rows.length;i++) {
			row = this.rows[i];
			row.moveTo(x,y);
			row.setSameWidth(row.w+dw);
			y+=row.h+row.spc;
		}
		this.setIndicatorLoc(0,1,1);
	}
}

Column.prototype.resizeHeight = function() {

	var numOfRows = this.rows.length;
	var h = 0;
	var child=null;
	for (var i=0;i<numOfRows; i++) {
		child = this.rows[i];
		h += child.h + child.spc;
	}
	if (child != null) {
		h -= child.spc;
	}
	h += this.btmSpc+this.topSpc;

	if (this.height==-1) {
		if (this.toPageH) this.view.setHeight("100%");
		this.height=this.view.getHeight();
	}
	if (child!=null) {
		this.view.setHeight(h);
	} else if (this.minih > -1) {
		this.view.setHeight(this.minih);
	} else {
		this.view.setHeight(this.height);
	}
}

Column.prototype.setSameHeight = function(maxh,eqlDisb) {
	if (!maxh) return;
	if (maxh>this.h) {
		var dh=maxh-this.h;
		this.h=maxh;
		this.view.setHeight(this.h);
	} else {
		var dh=0;
	}
	var numOfRows = this.rows.length;
	if (numOfRows>0) {
		if (eqlDisb) {
			var dh1 = dh/numOfRows;
			if (this.toChildWidth) var x=this.x + this.leftSpc;
			else var x=this.x;
			var y=this.y + this.topSpc;
			for (var i=0;i<numOfRows;i++) {
				var row = this.rows[i];
				row.moveTo(x,y);
				row.setSameHeight(dh1+row.h);
				y+=row.h+row.spc;
			}
			if (this.toChildWidth && row) {
				this.setIndicatorLoc(1,0,1);
			}
		} else {
			var isEmpty = new Array(numOfRows);
			var ctr=0;
			for (var i=0;i<numOfRows;i++) {
				if (!this.rows[i].endsWithRows()){
					isEmpty[i]=1;
					ctr++;
				} else isEmpty[i]=0;
			}
			// give all extra space to the last non-empty row
			var luckyChild=0;
			for (var i=numOfRows-1;!luckyChild&&i>=0;i--){
				if (!isEmpty[i]) luckyChild=i;
			}
			if (this.toChildWidth) var x=this.x + this.leftSpc;
			else var x=this.x;
			var y=this.y + this.topSpc;
			for (var i=0;i<numOfRows;i++) {
				var row = this.rows[i];
				row.moveTo(x,y);
				if (luckyChild==i) row.setSameHeight(dh+row.h);
				else row.setSameHeight(row.h);
				y+=row.h+row.spc;
			}
			/*
			if (ctr==numOfRows) {
				for (var i=0;i<numOfRows;i++) {
					var row = this.rows[i];
					row.moveTo(x,y);
					row.setSameHeight(dh+row.h);
					y+=row.h+row.spc;
					dh=0;
				}
			} else {
				var dh1 = dh/(numOfRows-ctr);
				for (var i=0;i<numOfRows;i++) {
					var row = this.rows[i];
					row.moveTo(x,y);
					if(isEmpty[i]) {
						var newh=row.h;
					}
					else var newh=dh1+row.h;
					row.setSameHeight(newh);
					y+=row.h+row.spc;
				}
			}
			*/
			if (this.toChildWidth && row) {
				this.setIndicatorLoc(1,0,1);
			}
		}
	}
	this.setIndicatorLoc(0,1);
}

Column.prototype.moveFamilyTo = function(x,y) {
	var y1=y+this.topSpc;
	for (var i=0;i<this.rows.length;i++) {
		var row = this.rows[i];
		row.moveFamilyTo(x,y1);
		y1+=row.h+row.spc;
	}
	this.moveTo(x,y);
}

Column.prototype.moveTo = function(x,y) {

	if (this.x != x) {
		this.x=x;
		this.view.setLeft(x);
		this.setIndicatorLoc(1);
	}
	if (this.y != y) {
		this.y=y;
		this.view.setTop(y);
		this.setIndicatorLoc(0,1);
	}
}

Column.prototype.endsWithRows = function() {
	if (this.rows.length==0) return 0;
	else {
		for (var i=0;i<this.rows.length;i++) {
			var r=this.rows[i].endsWithRows();
			if(r) break;
		}
		return r;
	}
}
/*
Column.prototype.getLastColumn = function() {
	if (this.rows.length==0) return this;
	else return this.rows[this.rows.length-1].getLastColumn();
}
*/
Column.prototype.setIndicatorLoc = function(l,t,w) {
	if(this.indicator!=null){
		var d=this.indicator;
		var lastRow=0,spcRow=0;
		var i=this.rows.length-1;
		var j=i;
		for(;i>=0;i--){
			// find the last row which is not MOD_NOT type
			if(this.rows[i].type!=MOD_NOT){
				lastRow=this.rows[i];
				break;
			}
		}
		// spcRow is a MOD_NOT type just after lastRow
		if(i<j)spcRow=this.rows[i+1];
		if(!IndicatorHeight) IndicatorHeight=1;
		if(l){
			var x=lastRow?lastRow.x:(spcRow?spcRow.x:this.x);
			if(d.x!=x){
				d.x=x;
				d.setLeft(x);
			}
		}
		if(t){
			if(lastRow) var y=lastRow.y+lastRow.h+lastRow.spc/2-IndicatorHeight;
			else if(spcRow) var y=spcRow.y-(2*IndicatorHeight);
			else var y=this.y+this.h/2-IndicatorHeight;
			if(d.y!=y){
				d.y=y;
				d.setTop(y);
			}
		}
		if(w){
			var w=lastRow?lastRow.w:(spcRow?spcRow.w:this.w);
			if(d.w!=w){
				d.w=w;
				d.setWidth(w);
			}
		}
		if(!d.h) d.setHeight(IndicatorHeight*2);
	}
}

Column.prototype.addRow = function(row) {
	this.rows[this.rows.length]=row;
	row.parent=this;
	if(this.type == MOD_FIX) row.type=MOD_FIX;
}

Column.prototype.addIndicator = function(indicator) {
	this.indicator=indicator;
}

Column.prototype.removeRow = function(row) {
	var numOfRows=this.rows.length
	for(var i=0;i<numOfRows;i++){
		if(row == this.rows[i]){
			row.parent=null;
			row.view.hideIt();
			for(var j=i+1;j<numOfRows;j++){
				this.rows[j-1]=this.rows[j];
			}
			this.rows=this.rows.slice(0,numOfRows-1);
			break;
		}
	}
}

Column.prototype.insertRow = function(row, index) {
	var numOfRows=this.rows.length;
	if(index >= numOfRows){
		this.addRow(row);
	}else{
		var rowsPart1=this.rows.slice(0,index);
		var rowsPart2=this.rows.slice(index,numOfRows);
		rowsPart1[rowsPart1.length]=row;
		row.parent=this;
		this.rows=rowsPart1.concat(rowsPart2);
	}
}
Column.prototype.switchClass = function(classNum) {
	if (this.classes && classNum >= 0 && this.currentClass != classNum) {
		if (classNum < this.classes.length) {
			this.view.evnt.className = this.classes[classNum];
			this.currentClass = classNum;
		}
	}
}

Column.prototype.render = function(left,top) {

	if (typeof left != "undefined") {
		this.view.setLeft(left);
		this.x = left;
	} else this.x = this.view.getLeft();
	if (typeof top != "undefined") {
		this.view.setTop(top);
		this.y = top;
	} else this.y = this.view.getTop();

	this.resizeWidth();

	if (this.isPosAbsolute) {
		var childLeft = this.x;
		var childTop = this.y;
	} else {
		var childLeft = this.relativeX;
		var childTop = this.relativeY;
		this.x = this.relativeX;
		this.y = this.relativeY;
	}

	if (this.topSpc) childTop += this.topSpc;
	var numOfRows = this.rows.length;
	if (this.toChildWidth) {
		var fatChild=0;
		for (var i=0;i<numOfRows;i++) {
			var child = this.rows[i];
			var cw=child.getMinWidth();
			if (fatChild < cw) fatChild = cw;
		}
		this.w = this.view.getWidth();
		if (this.w > fatChild) {
			this.leftSpc = (this.w-fatChild)/2;
		} else {
			this.leftSpc = 0;
			this.w = fatChild;
			this.view.setWidth(fatChild);
		}
		childLeft += this.leftSpc;
		// renderring children
		for (var i=0;i<numOfRows;i++) {
			var child = this.rows[i];
			child.render(childLeft,childTop,fatChild);
			childTop += child.h + child.spc;
		}
	} else {
		// renderring children
		var fatChild = 0;
		var wdiff = -1;
		for (var i=0;i<numOfRows; i++) {
			var child = this.rows[i];
			child.render(childLeft,childTop);
			childTop += child.h + child.spc;
			if (fatChild < child.w) {
				wdiff++;
				fatChild = child.w;
			} else if (fatChild > child.w) {
				wdiff++;
			}
		}
		this.w = this.view.getWidth();
		if (wdiff>0) this.setSameWidth(fatChild,1);
		if (fatChild > this.w) {
			this.view.setWidth(fatChild);
			this.w = fatChild;
		}
	}

	this.resizeHeight();
	
	this.view.showIt();

	this.h = this.view.getHeight();

	if (this.toPageH) {
		if (this.height > this.h) {
			this.setSameHeight(this.height);
			this.h=this.height;
		}
	}

	this.setIndicatorLoc(1,1,1);
	
	if (!this.isPosAbsolute) {
		if (this.posAnchor) {
			var newLeft=_getPageOffsetLeft(this.posAnchor.evnt);
			var newTop=_getPageOffsetTop(this.posAnchor.evnt);
			if (newLeft != this.relativeX || newTop != this.relativeY) {
				this.relativeX=newLeft;
				this.relativeY=newTop;
				this.moveFamilyTo(newLeft,newTop);
			}
		}
		if (cm_posCheckE) eval(cm_posCheckE);
	}
}

var SCROLL_OFFSET=20,SCROLL_SPD=3,SCROLL_DST=15;

function MoveHandler(e) {

	if(!DraggMod) return true;

	absX = bw.ie?window.event.clientX + document.body.scrollLeft:e.pageX;
	absY = bw.ie?window.event.clientY + document.body.scrollTop:e.pageY;
	if (typeof cmpage != "undefined" && cmpage) {
		var scrollleft = cmpage.scrollX();
		var scrolltop = cmpage.scrollY();
		if (absY+SCROLL_OFFSET>cmpage.y2+scrolltop) {
			cm_scrolldown(scrolltop,scrolltop+SCROLL_DST);
		}
		else if (absY-SCROLL_OFFSET<scrolltop) {
			cm_scrollup(scrolltop,scrolltop-SCROLL_DST);
		}
		if (absX+SCROLL_OFFSET>cmpage.x2+scrollleft) {
			cm_scrollright(scrollleft,scrollleft+SCROLL_DST);
		}
		else if (absX-SCROLL_OFFSET<scrollleft) {
			cm_scrollleft(scrollleft,scrollleft-SCROLL_DST);
		}
	}
	deltaX = absX - ModStartX;
	deltaY = absY - ModStartY;
	//window.status = "mouse:"+absX+","+absY+",module: "+deltaX+","+deltaY+",win: "+cmpage.x2+","+cmpage.y2;
	//window.status = "mouse:"+absX+","+absY+",win:"+document.body.offsetWidth+","+document.body.offsetHeight+",scrly="+scrlY;

	DraggMod.dc.Move(DraggMod.x+deltaX, DraggMod.y+deltaY);

	// find out if the mouse is still in the same panel
	if (DropCol != null && (absX > DropCol.x) && (absX < DropCol.x + DropCol.w) && (absY > DropCol.y) && (absY < DropCol.y + DropCol.h)) {
		// still inside the same panel.
	} else {
		// different panel
		var lastDropCol = DropCol;
		DropCol = null;
		for (var i = 0; i < Panels.length; i++) {
			if (lastDropCol != Panels[i]) {
				if ((absX > Panels[i].x) && (absX < Panels[i].x + Panels[i].w) && (absY > Panels[i].y) &&
						(absY < Panels[i].y + Panels[i].h)) {
					DropCol = Panels[i];
					break;
				}
			}
		}
		if (DropCol == null) {
			//window.status = "you are not inside any column";
			if (DropIndicator != DraggMod.indicator) {
				if (DropIndicator != null) {
					DropIndicator.hideIt();
				}
				DropIndicator = DraggMod.indicator;
				DropIndicator.showIt();
			}
			return false;
		}
	}
	if(!lp) var lp=PageLayout.lockedPanel;
	if(!msg) var msg=PageLayout.msg;
	if(DropCol.type == MOD_FIX) {
		// set locked panel and msg positions
		if(lp) {
			if (lp.x != DropCol.x || lp.y != DropCol.y || lp.w != DropCol.w || lp.h!= DropCol.h){
				lp.setLeft(DropCol.x);
				lp.setTop(DropCol.y);
				lp.setWidth(DropCol.w);
				lp.setHeight(DropCol.h);
				if(msg) {
					msg.setLeft(DropCol.x);
					msg.setTop(DropCol.y);
					msg.setWidth(DropCol.w);
				}
				lp.x=DropCol.x
				lp.y=DropCol.y
				lp.w=DropCol.w
				lp.h=DropCol.h
			}
			if (!lp.isShown) {
				lp.showIt();
				if(msg) msg.showIt();
				lp.isShown=1;
			}
		}
		var drop = null;
	}else{
		if(lp){
			if(lp.isShown){
				lp.hideIt();
				if(msg) msg.hideIt();
				lp.isShown=0;
			}
		}
		DropIndex=0;
		var lastIdx=DropCol.rows.length-1;
		for(;lastIdx>=0;lastIdx--){
			// find the last row which is not MOD_NOT type
			if(DropCol.rows[lastIdx].type!=MOD_NOT) break;
		}
		if(lastIdx >= 0){
			for(var i=0;i<=lastIdx;i++){
				var r=DropCol.rows[i];
				if(r.type!=MOD_NOT){
					if(absY>r.y) DropIndex=i;
					else break;
				}
			}
			var rowMidPt=DropCol.rows[DropIndex].y+Math.round(DropCol.rows[DropIndex].h/2);
			if(absY > rowMidPt) DropIndex++;
		}
		//window.status="you are inside column "+DropCol.id+" index="+DropIndex+" lastIdx="+lastIdx;

		if(DropIndex <= lastIdx) var drop=DropCol.rows[DropIndex].indicator;
		else var drop=DropCol.indicator;
	}

	if (drop != DropIndicator) {
		if(DropIndicator != null){
			DropIndicator.hideIt();
		}
		DropIndicator=drop;
		if(DropIndicator != null){
			DropIndicator.showIt()
		}
	}
	return false;
}

