/*
nDeviX Drag & Drop.
Written by admin (aka: DoT_TxT)

USAGE:

--defines variables. Containing element is a wrapper. See useage for it below. If you choose not to use it leave it blank
<script type="text/javascript" language="javascript">
    var containingelement = 'inner_wrap';
	var containerlist = new Array('ndxddheader','ndxdd2');	
</script>

--tmpdiv is the object container wile dragging
<div id="tmpdiv" style="position:absolute; top:0; left:0; height:0; width:0;"></div>

--inner wrap must surround the page you are working on, or at least the working area. Reason for its existance is a bug that happens if the document scrolls while dragging. If the div scrolls positioning is not thrown off. If you prefer not to do this keep inner_wrap
<div id="inner_wrap" style="width: 100%;height: 100%;margin: 0;padding: 0; overflow:auto; ">

--this div is defined in the script as one of the containers.
<div id="ndxddheader" style="width:200px; min-height:100px; background:#efefef; border:1px solid #ddd;">
--this is the object being dragged. 
<div id="obj_to_drag" onmousedown="move(event,'obj_to_drag');" style="width:100px; height:20px; position:relative; border:1px solid #000; text-align:center;">test</div>
</div>
</div>


*/


var dragobj = new Object();
var containers = new Object();
var div = new Object();
//this doesnt work yet

var containervars = new Object();
//set container vars on html page
var parentminheight;

containervars.background = '#efefef';
containervars.dragin = '#bebebe';



var containingelement = '';

var containerlist = new Array();	


function containingEleST() {
	if(containingelement=='') {
		return 0;	
	} else {
		return document.getElementById(containingelement).scrollTop;	
	}
}

var modid=0;
containers.coordinates = new Array();
containers.lastcontainer = 0;

function containborders() {
//gets the edges of the borders ahead of time.
for(x in containerlist) {
	var element = document.getElementById(containerlist[x]);
	var X = findPosX(element);
	var Y = findPosY(element);

	var RightBorder = parseInt(X, 10)+parseInt(element.offsetWidth, 10);
	var BottomBorder = parseInt(Y, 10)+parseInt(element.offsetHeight, 10);

	containers.coordinates[x] = Array(X,RightBorder,Y,BottomBorder,containerlist[x]);
}
}

function startcontainer(x,y) {
// x and y are the mouse cords.
var i = 0;
while(1) {
	if(x>=containers.coordinates[i][0] && x<=containers.coordinates[i][1] && y>=containers.coordinates[i][2] && y<=containers.coordinates[i][3]) {
			document.getElementById(containers.coordinates[i][4]).style.background=containervars.dragin;
			
			return containers.coordinates[i][4];
	}
	i++;
	if(i==containers.coordinates.length) { 
	containers.startcontainer='';
	break;
	}
}
}

function incontainer(x,y) {
// x and y are the mouse cords.
var i = 0;

while(1) {
	
	if(x>=containers.coordinates[i][0] && x<=containers.coordinates[i][1] && y>=containers.coordinates[i][2] && y<=containers.coordinates[i][3]) {
		if(containers.coordinates[i][4]!==containers.lastcontainer) {
				if(div.parentNode) {
					div.parentNode.removeChild( div );
				}
				
			div = document.createElement("div");
			
			div.setAttribute('style', 'width:'+document.getElementById(containers.coordinates[i][4]).style.width+'; height: 40px; position: relative; border: 1px dashed #e8e8e8; textAlign: center; margin-bottom: 5px;');
			div.setAttribute('id','ndxdragdropplacholder');
			div.id='ndxdragdropplacholder';
			div.style.width=''+document.getElementById(containers.coordinates[i][4]).style.width+'';
			div.style.height='40px';
			div.style.position='static';
			div.style.border='1px dashed #e8e8e8';
			div.style.textAlign='center';
			div.style.marginBottom='5px';
if(document.getElementById(containers.coordinates[i][4]).childNodes.length==0){
	document.getElementById(containers.coordinates[i][4]).appendChild( div );
}
			for(z=0; z<document.getElementById(containers.coordinates[i][4]).childNodes.length; z++) {

				if((y<findPosY(document.getElementById(containers.coordinates[i][4]).childNodes[z])+(parseInt((document.getElementById(containers.coordinates[i][4]).childNodes[z].offsetHeight-2), 10)/2)) && (y>findPosY(document.getElementById(containers.coordinates[i][4]).childNodes[z])-parseInt((document.getElementById(containers.coordinates[i][4]).childNodes[z].offsetHeight-2), 10))) {
				document.getElementById(containers.coordinates[i][4]).insertBefore(div,document.getElementById(containers.coordinates[i][4]).childNodes[z]);
				break;
				}
				if(z==(document.getElementById(containers.coordinates[i][4]).childNodes.length-1)) {
					document.getElementById(containers.coordinates[i][4]).appendChild( div );
				}
				
			}
			
			containers.lastcontainer=containers.coordinates[i][4];
			document.getElementById(containers.lastcontainer).style.background=containervars.dragin;
		} else {

			if(div) {
				if(findPosY(dragobj)!==findPosY(div)) {
		if(document.getElementById(containers.coordinates[i][4]).childNodes.length==0) {
document.getElementById(containers.coordinates[i][4]).appendChild( div );		
				} else {
					
					for(z=0; z<document.getElementById(containers.coordinates[i][4]).childNodes.length; z++) {
			
						if((y<findPosY(document.getElementById(containers.coordinates[i][4]).childNodes[z])+(parseInt((document.getElementById(containers.coordinates[i][4]).childNodes[z].offsetHeight-2), 10)/2)) && (y>findPosY(document.getElementById(containers.coordinates[i][4]).childNodes[z])-parseInt((document.getElementById(containers.coordinates[i][4]).childNodes[z].offsetHeight-2), 10))) {
							
							document.getElementById(containers.coordinates[i][4]).insertBefore(div,document.getElementById(containers.coordinates[i][4]).childNodes[z]);

						break;
						}
					if(z==(document.getElementById(containers.coordinates[i][4]).childNodes.length-1)) {
						document.getElementById(containers.coordinates[i][4]).appendChild( div );
					}
				}
			}
			}
			}
		}
		return containers.coordinates[i][4];
	}
	i++;
	if(i==containers.coordinates.length) { 
	if(div.parentNode) {
		div.parentNode.removeChild( div );
	}
	
	for(x in containerlist) {
		var element = document.getElementById(containerlist[x]);
		element.style.background=containervars.background;
	}
	
	containers.lastcontainer=0;
	break;
	}
}
}

function move(event, id, containerids) {
	
if (typeof containerids !== 'undefined' ) {
	containerlist = containerids.split(',');
}



containborders();
	dragobj = document.getElementById(id);
	//dragobj.childNodes[2].style.display='none';
	
	dragobj.parentminheight = dragobj.parentNode.style.minHeight;
	dragobj.firstparentid = dragobj.parentNode.id;
	dragobj.parentNode.style.minHeight = dragobj.parentNode.offsetHeight+'px';

	
	var st = containingEleST();
	
	  if (browser.isIE) {
		dragobj.tmpy=findPosY(dragobj) - dragobj.parentNode.scrollTop  - st;
		dragobj.tmpx=findPosX(dragobj);
		dragobj.startX = findPosX(dragobj);
		dragobj.startY = findPosY(dragobj) - dragobj.parentNode.scrollTop  - st;
	  }
	  if (browser.isNS) {
		dragobj.tmpy=findPosY(dragobj)-dragobj.parentNode.scrollTop - st;
		dragobj.tmpx=findPosX(dragobj);
		dragobj.startX = findPosX(dragobj);
		dragobj.startY = findPosY(dragobj) - dragobj.parentNode.scrollTop - st;
	  }

			for(z=0; z<document.getElementById(id).parentNode.childNodes.length; z++) {
				if(document.getElementById(id).parentNode.childNodes[z].id==id) {
					if(div.parentNode) {
						div.parentNode.removeChild( div );
					}
					//alert(document.getElementById(id).parentNode.childNodes.id);

					div = document.createElement("div");

					div.setAttribute('style', 'width:'+document.getElementById(id).parentNode.style.width+'; height: 40px; position: relative; border: 1px dashed #e8e8e8; textAlign: center; margin-bottom: 5px;');
					div.setAttribute('id','ndxdragdropplacholder');
					div.id='ndxdragdropplacholder';
					div.style.width=''+document.getElementById(id).parentNode.style.width+'';
					div.style.height='40px';
					div.style.position='static';
					div.style.border='1px dashed #e8e8e8';
					div.style.textAlign='center';
					div.style.marginBottom='5px';
					if(browser.isIE) {
						if(document.getElementById(id).parentNode.childNodes.length==3){
							document.getElementById(id).parentNode.appendChild(div);
						} else {
							document.getElementById(id).parentNode.insertBefore(div,document.getElementById(id).parentNode.childNodes[z]);
						}
					}
					if (browser.isNS) {
						document.getElementById(id).parentNode.insertBefore(div,document.getElementById(id).parentNode.childNodes[z]);
					}
					containers.lastcontainer=document.getElementById(id).parentNode.id;
				break;
				}
			}
		
	dragobj.style.width='200px';
	//dragobj.style.background='#023864';
	dragobj.style.filter='alpha(opacity=85)';
	dragobj.style.opacity='0.85';
	dragobj.style.zIndex='1';
	dragobj.style.height='auto';
	dragobj.style.position='relative';
	dragobj.style.top=dragobj.startY+'px';
	dragobj.style.left=dragobj.startX+'px';
	

		document.getElementById('tmpdiv').appendChild( dragobj )
		dragobj.style.top=dragobj.tmpy;
		dragobj.style.left=dragobj.tmpx;
	//offset
	  if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop + containingEleST();
	  }
	  if (browser.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY + containingEleST();
	  }

	containers.startcontainer=startcontainer(x,y);
	dragobj.startcontainer=startcontainer(x,y);
	dragobj.offsetX = dragobj.startX - x;
	dragobj.offsetY = dragobj.startY - y;
	dragobj.sctop = containingEleST();
	if (browser.isIE) {
		document.attachEvent("onmousemove", dragGo);
		document.attachEvent("onmouseup",   dragStop);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
 	}
 	if (browser.isNS) {
		document.addEventListener("mousemove", dragGo,   true);
		document.addEventListener("mouseup",   dragStop, true);
		event.preventDefault();
 	}
}

function dragGo(event) {
containborders();
if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop + containingEleST();
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY + containingEleST();
  }
	incontainer(x,y);
	
	//check for offset changes if the page gets bigger or smaller... teh interweb makes me not so smart
	if(dragobj.sctop!==containingEleST()) {
		var diff = dragobj.sctop-containingEleST();
		dragobj.offsetY = dragobj.offsetY+diff;
		dragobj.sctop=containingEleST();
	}
	
	dragobj.style.top=y+dragobj.offsetY+'px';
	dragobj.style.left=x+dragobj.offsetX+'px';
}

function dragStop(event) {
if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop + containingEleST();
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY + containingEleST();
  }

	var inc = incontainer(x,y);

	if(incontainer(x,y)) {

		dragobj.style.left='0px';
		dragobj.style.top='0px';
		
		if(div.parentNode) {
			for(z=0; z<document.getElementById(inc).childNodes.length; z++) {
				if(document.getElementById(inc).childNodes[z].id=='ndxdragdropplacholder') {
				break;
				}
			}
			div.parentNode.removeChild( div );
		}
		if(z==document.getElementById(inc).childNodes.length) {
			document.getElementById(inc).appendChild( dragobj );
			//dragobj.style.width=dragobj.parentNode.style.width;
			dragobj.style.width='auto';
		} else {
			if(document.getElementById(inc).childNodes.length==0) {
				document.getElementById(inc).appendChild( dragobj );
			//dragobj.style.width=dragobj.parentNode.style.width;
			dragobj.style.width='auto';
			} else {
				document.getElementById(inc).insertBefore(dragobj,document.getElementById(inc).childNodes[z]);
			//dragobj.style.width=dragobj.parentNode.style.width;
			dragobj.style.width='auto';
			}
		}
	} else  {
		document.getElementById(dragobj.startcontainer).appendChild(dragobj);
		dragobj.style.top='0px';
		dragobj.style.left='0px';
		dragobj.style.width='auto';
	}
	for(x in containerlist) {
	var element = document.getElementById(containerlist[x]);
		element.style.background=containervars.background;
	}
	
	dragobj.style.filter='alpha(opacity=100)';
	dragobj.style.opacity='1';
	dragobj.style.zIndex='0';
	dragobj.style.height='auto';
	dragobj.style.position='static';

	containers.lastcontainer=0;

	if (browser.isIE) {
		document.detachEvent("onmousemove", dragGo);
		document.detachEvent("onmouseup",   dragStop);
	}
	if (browser.isNS) {
		document.removeEventListener("mousemove", dragGo,   true);
		document.removeEventListener("mouseup",   dragStop, true);
	}
	
	document.getElementById(dragobj.firstparentid).style.minHeight = parentminheight;
}

function findPosY(obj)
{
var curtop = 0;
 if(obj.offsetParent)
     while(1)
       {
       curtop += obj.offsetTop;
       if(!obj.offsetParent)
         break;
       obj = obj.offsetParent;
     }
 else if(obj.y)
    curtop += obj.y;
return curtop;
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function saveorder() {
	var savedata='';
	for(x in containerlist) {
	var element = document.getElementById(containerlist[x]);
		for(var n = 0; n<element.childNodes.length; n++) {
			if(element.childNodes[n].id) {
			savedata = savedata+element.childNodes[n].id+':';
			}
		}
		document.getElementById('orderlist').value=savedata;
		document.orderform.submit();
	}
}

function fillmodReq(action,element) {
if (http == undefined)
{
var http = createRequestObject();
}

try {
	document.getElementById(element).childNodes[2].innerHTML = '<img src="http://dev.ndevix.com/eleanor/design/images/ajaxload.gif">';
    http.open('get', action);
    http.onreadystatechange = function() {
    
	if(http.readyState == 4){
    var response = http.responseText;
 	document.getElementById(element).childNodes[2].innerHTML = response;
    }
	}
	http.send(null);
} catch(err) {
	var errstr = 'document.getElementById';
	if(errstr.indexOf(err.message)!==-1) {
		setTimeout('sndReq(\''+action+'\',\''+element+'\')',800);
	}
}
}

function autosave() {
	var savedata='';
	for(x in containerlist) {
	var element = document.getElementById(containerlist[x]);
	savedata = savedata+''+containerlist[x]+'('
		for(var n = 0; n<element.childNodes.length; n++) {
			savedata = savedata+element.childNodes[n].id+':';	
		}
	savedata = savedata+')';
	}
	//sndPst
	document.getElementById('autosave').innerHTML='<img src="http://dev.ndevix.com/eleanor/design/images/ajaxload.gif">';
	autosavePst('data='+savedata);
}

function autosavePst(action) {
if (http == undefined)
{
var http = createRequestObject();
}

try {
    http.open('post', 'exec.php?act=savelayout');
	http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    http.onreadystatechange = function() {
    
	if(http.readyState == 4){
    	var response = http.responseText;
 			document.getElementById('autosave').innerHTML = response;
    	}
	}
	http.send(action);
	} catch(err) {
	var errstr = 'document.getElementById';
	if(errstr.indexOf(err.message)!==-1) {
		setTimeout('autosavePst(\''+action+'\',\''+url+'\',\''+element+'\')',800);
	}
	}
}

function nalert(html) {
div = document.createElement('div');
	div.id = 'alertbox';
	div.setAttribute('style','display:block; position:absolute; left:400px; top:200px; width:200px; height:200px; border:1px solid #000;');
	div.innerHTML='[<a href="javascript:n();" onclick="document.getElementById(\'alertbox\').style.display=\'none\';">close</a>]<br />'+html;
	document.body.appendChild(div);
}

function n() {
	//this function Rocks.. 
}


function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

function makewys(obj) {
obj.onmouseover='';
if(browser.isNS) {
obj.contentDocument.designMode="on";
} else {
obj.document.designMode = 'on'; 
}
}

function sh(id,type) {
if(type=='display') {
var hid='none';	
var show='block';
	if(document.getElementById(id).style.display==hid) {
		document.getElementById(id).style.display=show;
		return 'show';
	} else {
		document.getElementById(id).style.display=hid;
		return 'hid';
	}
}
if(type=='visibility') {
var hid='hidden';
var show='visible';
	if(document.getElementById(id).style.visibility==hid) {
		document.getElementById(id).style.visibility=show;
		return 'show';
	} else {
		document.getElementById(id).style.visibility=hid;
		return 'hid';
	}
}	
}

function lightbox(div) {
var obj = document.getElementById(div);
obj.style.position='absolute';
obj.style.display='block';
obj.style.top='0px';
obj.style.left='0px';
obj.style.width=document.body.clientWidth+'px';
obj.style.height=document.body.clientHeight+'px';
obj.style.background='#000';
obj.style.zIndex='1000';
}