/**
 * Zarządza dragowanymi obiektami
 */
function DraggablessMonitor(){
	this.ie6 = document.all;
	this.nn6 = document.getElementById&&!document.all;
	this.isdrag=false;
	this.x = 0;
	this.y = 0;
	this.dobj = null;
	
	this.mousemove = function(e){
		thiz = window.draggables_monitor;
		if(!thiz.nn6&&!window.opera){
			if(event.button%2!=1){
				thiz.mouseup(null);
				return false;
			}
		}   
		
		if(thiz.isdrag){
			poz_x = (thiz.nn6 ? thiz.tx + e.clientX: thiz.tx + event.clientX);
			poz_y = (thiz.nn6 ? thiz.ty + e.clientY: thiz.ty + event.clientY);
			thiz.dragobj.move(poz_x, poz_y);
		} else if(thiz.dragobj.mouseover){
			poz_x = (thiz.nn6 ? thiz.tx + e.clientX: thiz.tx + event.clientX);
			poz_y = (thiz.nn6 ? thiz.ty + e.clientY: thiz.ty + event.clientY);
			thiz.dragobj.mouseover(poz_x, poz_y);
		}
	}
	
	this.paused = false;
	this.pause =function(){
		this.paused = true;
	}
	
	this.resume = function(){
		this.paused = false;
	}
	
	this.mousedown = function(e){
		thiz = window.draggables_monitor;
		if(thiz.paused)
			return true;
		var fobj       = thiz.nn6 ? e.target : event.srcElement;
  		var topelement = thiz.nn6 ? "HTML" : "BODY";
		while (fobj && fobj.tagName != topelement && (!fobj.dragobj) ){
			fobj = thiz.nn6 ? fobj.parentNode : fobj.parentElement;
		}
		if(fobj && fobj.dragobj){
			fobj.dragobj.startdrag(thiz.nn6 ? e.clientX : event.clientX, thiz.nn6 ? e.clientY : event.clientY);
			document.onselectstart = new Function('return false;');
			thiz.tx = parseInt(fobj.style.marginLeft);
			if(isNaN(thiz.tx))
				thiz.tx = 0;
			thiz.ty = parseInt(fobj.style.marginTop);
			if(isNaN(NaN))
				thiz.ty = 0;
			thiz.isdrag = true;
			thiz.dragobj = fobj.dragobj;
			thiz.dragobj.isdrag = true;
			document.onmousemove=thiz.mousemove;
			return false;	
		}	
		return true;
	}
	
	this.mouseup = function(e){
		thiz = window.draggables_monitor;
		if(thiz.isdrag){
			document.onselectstart = null;
			thiz.dragobj.stopdrag(thiz.nn6 ? e.clientX : event.clientX, thiz.nn6 ? e.clientY : event.clientY);
			thiz.isdrag = false;
			thiz.dragobj.isdrag = false;
			thiz.dragobj = null;
			document.onmousemove=null;
		}
	}
	
		
	this.init = function(){
		document.onmousedown=this.mousedown;
		document.onmouseup= this.mouseup;
		this.init = function(){}
	}
}

window.draggables_monitor = new DraggablessMonitor(); 

/**
 * Jeżeli wewnątrz przeciągalnego ma być klikalny element to powinien wyłączać przeciaganie jak mysza jezd nad nim
 * @param {Object} elem
 */
function notdraggable(elem){
	this.onmouseover = function(e){
		window.draggables_monitor.pause();
		return false;
	}
	
	this.onmouseout = function(e){
		window.draggables_monitor.resume();
		return false;	
	}	
	
	this.init = function(elem){
		this.elem = elem;
		addListener(elem, 'mouseover', this.onmouseover)
		addListener(elem, 'mouseout', this.onmouseout)
	}	
	
	
	this.init(elem);
}

function draggable(){
	
	this.init = function(elem, options){
		this.elem = elem
		this.options = options
		elem.dragobj = this;
		window.draggables_monitor.init();
	}
	
	this.startdrag = function(x,y){
	}
	
	this.stopdrag = function(x, y){
	}

	this.move = function(x, y){
	}
}


/**
 * Przestawia obiekty w tryb przeciagania
 * @param {string} classname nazwa klasy przeciąganych
 * @param {function} drag_class kostruktor js do obsługi dragdropa
 * @param {Object} options dodatkowe opcje
 */
function setDraggableByClass(classname, drag_class, options){
	elements = document.getElementsByTagName('div');
	for (var i = 0; i < elements.length; i++) 
		if (obtainClass(elements[i], classname)) 
			new drag_class(elements[i], options);
		
}

/**
 * Zamienia jeden z nodów na przeciągalny i wysyła do niego odpowiednie opcje
 * @param {Object} objid id wybranego obiektu
 * @param {Object} drag_class konstruktor przeciągalnego
 * @param {Object} options opcje dla konstruktora przeciągalnego.
 */
function setDraggableById(objid, drag_class, options){
	new drag_class($(objid), options);
}
