// DCO v1.2.0 - Drag coordination object

Drag = {
	Init : new function(){
		document.attachEvent("onmousemove", function(){
			Drag.Update();
			for(var i = 0; i < Drag.Collection.length; i++){
				if(Drag.Collection[i].dragging && Drag.Collection[i].mode == "DmU"){
					return false;
				}
			}
		});
	},
	Collection : [],
	Register : function(options){
		Drag.Collection[Drag.Collection.length] = options;
		options.object.style.left = options.object.style.left != "" ? options.object.style.left : "0px";
		options.object.style.top = options.object.style.top != "" ? options.object.style.top : "0px";
		var RegID = Drag.Collection.length - 1;
		if(options.mode == "FMp"){
			Drag.Collection[RegID].dragging = true;
			Drag.Collection[RegID].objectX = 0;
			Drag.Collection[RegID].objectY = 0;
		}if(options.mode == "DmU"){
			options.object.attachEvent("onmousedown", function(){
				for(var i = 0; i < 2; i++){
					Drag.Collection[RegID]["object" + (i == 0 ? "X" : "Y")] = event["client" + (i == 0 ? "X" : "Y")] + document.documentElement["scroll" + (i == 0 ? "Left" : "Top")] - options.object["client" + (i == 0 ? "Left" : "Top")] - parseInt(options.object.style[i == 0 ? "left" : "top"]);
				}
				Drag.Collection[RegID].dragging = true;
			});
			options.object.attachEvent("onmouseup", function(){
				Drag.Collection[RegID].dragging = false;
			});
		}else{
			options.object.attachEvent("onclick", function(){
				for(var i = 0; i < 2; i++){
					Drag.Collection[RegID]["object" + (i == 0 ? "X" : "Y")] = event["client" + (i == 0 ? "X" : "Y")] + document.documentElement["scroll" + (i == 0 ? "Left" : "Top")] - options.object["client" + (i == 0 ? "Left" : "Top")] - parseInt(options.object.style[i == 0 ? "left" : "top"]);
				}
				Drag.Collection[RegID].dragging = Drag.Collection[RegID].dragging && Drag.Collection[RegID].mode != "FMp" ? false : true;
			});
		}
		return RegID;
	},
	Update : function(){
		for(var i = 0; i < Drag.Collection.length; i++){
			if(Drag.Collection[i] != null){
				if(Drag.Collection[i].dragging){	
					Drag.Collection[i].object.style.position = (Drag.Collection[i].object.style.position == "") ? "relative" : Drag.Collection[i].object.style.position;
					if(Drag.Collection[i].dir != "h") Drag.Collection[i].object.style.top = event.clientY + document.documentElement.scrollTop - Drag.Collection[i].objectY;
					if(Drag.Collection[i].dir != "v") Drag.Collection[i].object.style.left = event.clientX + document.documentElement.scrollLeft - Drag.Collection[i].objectX;
					if(Drag.Collection[i].limits){
						for(var j = 0; j < 4; j++){
							if((parseInt(Drag.Collection[i].object.style[j < 2 ? "top" : "left"]) > Drag.Collection[i].limits[(j < 2 ? "y" : "x") + (j % 2 == 0 ? "High" : "Low")]) ^ (j % 2) && typeof Drag.Collection[i].limits[(j < 2 ? "y" : "x") + (j % 2 == 0 ? "High" : "Low")] == "number"){
								Drag.Collection[i].object.style[j < 2 ? "top" : "left"] = Drag.Collection[i].limits[(j < 2 ? "y" : "x") + (j % 2 == 0 ? "High" : "Low")];
							}
						}
					}
				}
			}
			if(Drag.Collection[i].onDrag && Drag.Collection[i].dragging){
				Drag.Collection[i].onDrag();
			}
		}
	}
};
