var mousedown = 0;
var firstContainer = null;
var lastContainer = null;
var tableWidth = 2;
var portletBottemHeight = 10;

var Position = {
	column : null,
	row : null,
	beforePortlet : null,
	currentPortlet:null,
	currentContainer:null,
	endContainer:null
}


function initDragDrop(columnId){
	
	var column = document.getElementById("layout-column_" + columnId);
	if(column){
		column.columnId = columnId;
		var num = parseInt(columnId.substring(columnId.indexOf("_") + 1));
		if(num && num-1>0){
			var previousColumn = document.getElementById("layout-column_column_"+ new String(num-1));
			if(previousColumn){
				column.previousContainer = previousColumn;
				previousColumn.nextContainer = column;
			}
		}
		if(num){
			var nextColumn = document.getElementById("layout-column_column_"+ new String(num+1));
			if(nextColumn){
				column.nextContainer = nextColumn;
				nextColumn.previousContainer = column;
			}
		}
		
		var items = column.childNodes;
		
		for (var i = 0; i < items.length; i++) {
			var item = items[i];
			if(item.className == "portlet-boundary"){
				//debugger;
				item.style.zIndex = 0;
				if(!(item.value=="static")){
					var titleItems = item.getElementsByTagName("div");
					for(var j = 0;j < titleItems.length;j++){
						if(titleItems[j].className && titleItems[j].className.match("base")){
							alert("11111");
						}
						if(titleItems[j].className && titleItems[j].className.match("portlet-title")){
							titleItems[j].onmousedown = mouseDown;
							titleItems[j].portletBody = item;
							item.container = column;
						}
					}
				}
			}
		}
	}
}

var initX = 0;
var initY = 0;

function mouseDown(){
	//debugger;
	mousedown = 1;
	 
	Position.currentPortlet=this.portletBody;
	Position.currentContainer=Position.currentPortlet.container;
	//初始化映像
	var shadow = document.createElement("shadow");
	
	shadow.style.position = "absolute";
	shadow.style.width = Position.currentPortlet.offsetWidth;
	shadow.style.height = Position.currentPortlet.offsetHeight;
	shadow.style.filter = "alpha(opacity=60)";
	shadow.style.cursor = "move";
	
	//shadow.style.borderStyle = "solid";
	//shadow.style.borderWidth = "2px";
	//shadow.style.borderColor = "blue";

	Position.currentContainer.insertBefore(shadow,Position.currentPortlet);

	//设置映像的属性
	shadow.id = "shadow";
	shadow.portletId = Position.currentPortlet.id;
	shadow.onmousemove = mouseMove;
	shadow.onmouseup = mouseUp;

	initX = event.offsetX;
	initY = event.offsetY;

	shadow.style.left = event.clientX - initX + document.body.scrollLeft;
	shadow.style.top = event.clientY  - initY + document.body.scrollTop;

	//debugger;
	shadow.originStyleLeft = shadow.style.left;
	shadow.originStyleTop = shadow.style.top;
	shadow.originOffsetLeft = shadow.offsetLeft;
	shadow.originOffsetTop = shadow.offsetTop;
	shadow.setCapture();
	shadow.style.zIndex = 100;

}

function mouseMove(){
	
	if(mousedown){
		
		
		var shadow = document.getElementById("shadow");
		shadow.style.cursor = "move";
		shadow.style.left = event.clientX  - initX + document.body.scrollLeft;
		shadow.style.top = event.clientY  - initY + document.body.scrollTop;

		if((parseInt(shadow.offsetLeft)+parseInt(shadow.offsetWidth)*0.2) < parseInt(shadow.originOffsetLeft)){
			var container =Position.currentContainer.previousContainer;
			var distance = 0;
			while(container != null){
				distance += container.offsetWidth;
				if(Math.abs(shadow.offsetLeft - shadow.originOffsetLeft) > distance + tableWidth){
					container = container.previousContainer;
				}else{
					Position.column = container;
					//shadow.style.width = Position.column.offsetWidth;
					break;
				}
			}
			if(container == null){
				Position.column = null;
			}
		}else if(parseInt(shadow.offsetLeft) > (parseInt(shadow.originOffsetLeft)+parseInt(shadow.offsetWidth)*0.2)){
			var container = Position.currentContainer.nextContainer;
			var distance = Position.currentContainer.offsetWidth;
			while(container != null){
				distance += container.offsetWidth;
				if(Math.abs(shadow.offsetLeft - shadow.originOffsetLeft) > distance + tableWidth){
					container = container.nextContainer;
				}else{
					Position.column = container;
					//shadow.style.width = Position.column.offsetWidth;
					break;
				}
			}
			if(container == null){
				Position.column = null;
			}
		}else if((parseInt(shadow.offsetLeft) < (parseInt(shadow.originOffsetLeft) + shadow.offsetWidth * 0.2)) && (parseInt(shadow.offsetLeft) > (parseInt(shadow.originOffsetLeft) - shadow.offsetWidth * 0.2))){
			//debugger;
			Position.column = Position.currentContainer;
			//shadow.style.width = Position.column.offsetWidth;
		}
		
		if(Position.column){
				//debugger;
				//judge row position
				var portlets = Position.column.childNodes;
				var rowPos = 0;
				Position.column.style.position = "absolute";
				var topDistance = Position.column.offsetTop;
				Position.column.style.position = "";
				for(var i = 0;i<portlets.length;i++){
					if(portlets[i].className.match("portlet-boundary")){
						topDistance += portlets[i].offsetHeight;
						if(shadow.offsetTop < (topDistance - portletBottemHeight - portlets[i].offsetHeight * 0.5)){
							var portlet = Position.currentPortlet;
							if(portlets[i].id != portlet.id){
								Position.beforePortlet = portlets[i];
								//显示portlet插入位置
								showPortletPlace("place_" + portlets[i].id);
								Position.row = rowPos;
							}else{
								Position.beforePortlet = null;
								//显示portlet插入位置
								showPortletPlace("place_" + portlet.id);
							}
							break;
							
						}else{
							
							if(shadow.portletId != portlets[i].id){
								rowPos++;
							}else{
								//显示portlet插入位置
								showPortletPlace("place_" + portlets[i].id);
							}
						}
					}
					if(i==portlets.length-1){
							//debugger;
						
							for(var u=0;u<portlets.length;u++){
								if(portlets[u].className.match("layout-blank-portlet")){
									Position.beforePortlet = portlets[u];
									//显示portlet插入位置
									showPortletPlace("place_" + Position.column.id);
									Position.row = rowPos;
								}
							}
					}
				}
			}else{
				showPortletPlace("");
			}
		if (event.clientY > window.document.body.clientHeight - 10 ){
			window.scrollBy(0, 10);
		}
		if (event.clientX > window.document.body.clientWidth - 10 ){
			window.scrollBy(10, 0);
		}
    
	}
	//window.status ="eventX: " + window.event.x + "   eventY: " + window.event.y + "   clientX:" + event.clientX + "   clientY:" + event.clientY ;//+ "  offsetLeft:" + this.offsetLeft + "   offsetTop:" + this.offsetTop + " container:" + this.container.offsetLeft;
}

function mouseUp(){
	if(mousedown){
		//debugger;
	
		var shadow = document.getElementById("shadow");
		var portlet = Position.currentPortlet;

		mousedown = 0;
		if(Position.column && (Position.row||Position.row==0) && Position.beforePortlet){
			//debugger;
			
			Position.currentContainer.removeChild(portlet);
		
			
			portlet.style.left = Position.beforePortlet.style.left;
			portlet.style.top = Position.beforePortlet.style.top;
			
			//debugger;
			portlet.style.width = Position.beforePortlet.offsetWidth;
			
			Position.column.insertBefore(portlet,Position.beforePortlet);
			portlet.style.width = Position.beforePortlet.offsetWidth;
			resizeColumnPortlet(Position.column);
			
			//debugger;
			portlet.container = Position.column;
			
			initMenuPortletsAreaBg();
			//this.container = Position.column;
			//持久化移动结果
			movePortlet(portlet.id, Position.column.columnId, Position.row);
			
			
		}else{
			//portlet.style.left = portlet.originStyleLeft;
			//portlet.style.top = portlet.originStyleTop;
		}
		
		Position.currentContainer.removeChild(shadow);
	
		
		hideAllPortletPlace();
	}
	
}


function getColumnFirstPortlet(column){
	var children = column.childNodes;
	var portlet = null;
	for(var i=0;i<children.length;i++){
		if(children[i].className.match("portlet-boundary")){
			portlet = children[i];
			break;
		}
	}
	return portlet;
}

function resizeColumnPortlet(column){
	var divs = column.getElementsByTagName("div");
	for(var i=0;i<divs.length;i++){
		if(divs[i].className == "portlet-boundary"){
			divs[i].style.width = column.offsetWidth;
		}
	}
}

function showPortletPlace(placeId){
	//debugger;
	var divs = document.getElementsByTagName("div");
	for(var i=0;i<divs.length;i++){
		if(divs[i].className=="portlet-place"){
			divs[i].style.display = "none";
		}
	}
	var show = document.getElementById(placeId);
	if(show){
		show.style.display = "block";
	}
}

function hideAllPortletPlace(){
	var divs = document.getElementsByTagName("div");
	for(var i=0;i<divs.length;i++){
		if(divs[i].className=="portlet-place"){
			divs[i].style.display = "none";
		}
	}
}

function refreshIFrame(portlet){
	var iframe = portlet.getElementsByTagName("IFRAME");
	if(iframe && iframe.length){
		//debugger;
		iframe[0].document.location.reload();
	}
}
