/*
 * interface drag - http://www.eyecon.ro/interface/
 *
 * Copyright (c) 2006 Stefan Petre
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

$.drug = null;
$.dragHelper = null;

$.dragstart = function(e)
{
	if (window.event) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
		this.dE.d.sx = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		this.dE.d.sy = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	} else {
		e.preventDefault();
		e.stopPropagation();
		this.dE.d.sx = e.clientX;
		this.dE.d.sy = e.clientY;
	}
	if ($.hasWord(this.dE.parentNode, 'fxWrapper')) {
		p = $(this.dE.parentNode);
		p.before(this.dE);
		$(this.dE).css(
			{
				top : p.css('top'),
				left : p.css('left'),
				position : p.css('position'),
				marginTop : this.dE.om.t,
				marginRight : this.dE.om.r,
				marginBottom : this.dE.om.b,
				marginLeft : this.dE.om.l
			}
		);
		p.remove();
	}
	this.dE.d.oC = $.getPos(this.dE);
    this.dE.d.dX = this.dE.d.sx - this.dE.d.oC.x;
    this.dE.d.dY = this.dE.d.sy - this.dE.d.oC.y;
    this.dE.d.oD = $.css(this.dE, 'display');
    this.dE.d.oP = $.css(this.dE, 'position');
	if (this.dE.d.oP != 'relative' && this.dE.d.oP != 'absolute') {
		$(this.dE).css('position', 'relative');
	}
    this.dE.d.oF = $.css(this.dE, 'float');
	//remember the old relative position
	this.dE.d.oR = {
		x : $.intval($.css(this.dE, 'left')),
		y : $.intval($.css(this.dE, 'top'))
	};
	$.dragHelper.html('');
	
	c = this.dE.cloneNode(true);
	if (this.dE.d.opacity) {
		$(c).css('opacity', this.dE.d.opacity);
		if (window.ActiveXObject) {
			$(c).css('filter', 'alpha(opacity=' + this.dE.d.opacity * 100 + ')');
		}
	}
	//$(c)..css('width', this.dE.d.oC.wb + 'px').css('height',this.dE.d.oC.hb + 'px');
	$(c).css(
		{
			display:	'block',
			left:		'0px',
			top: 		'0px'
		}
	);
	$.dragHelper.append(c)
				.css('left', this.dE.d.oC.x + 'px')
				.css('top', this.dE.d.oC.y + 'px' )
				.css('width', this.dE.d.oC.wb + 'px')
				.css('height',this.dE.d.oC.hb + 'px' )
				.css(
					{
						display:	'block',
						overflow:	'hidden'
					}
				);
	if (this.dE.d.ghosting == false) {
		$(this.dE).css('display', 'none');
	}
    $.drug = this.dE;
	$.drug.d.prot = false;
	if (this.dE.d.contaiment) {
		if (this.dE.d.contaiment.constructor == String) {
			if (this.dE.d.contaiment == 'parent') {
				this.dE.d.cont = $.getPos(this.dE.parentNode);
			} else if (this.dE.d.contaiment == 'document') {
				clnt = $.getClient();
				this.dE.d.cont = {
					x : 0,
					y : 0,
					w : clnt.w,
					h : clnt.h
				};
			};
		} else if (this.dE.d.contaiment.constructor == Array && this.dE.d.contaiment.length == 4) {
			this.dE.d.cont = {
				x : $.intval(this.dE.d.contaiment[0]),
				y : $.intval(this.dE.d.contaiment[1]),
				w : $.intval(this.dE.d.contaiment[2]),
				h : $.intval(this.dE.d.contaiment[3])
			};
		}
	}
	if ($.droppables && $.droppables.length > 0 ){
		$.drophighlight();
	}
	if (this.dE.d.zIndex != false) {
		$.dragHelper.css('zIndex', this.dE.d.zIndex);
	}
	$.drag(e);
};

$.dragstop = function(e)
{
    if ($.drug == null || $.drug.d.prot == true) {
		return;
    }
	if ($.drug.d.so == true) {
		$($.drug).css('position', $.drug.d.oP);
	}
	hp = $.getPos($.dragHelper.cur[0]);
	if ($.drug.d.revert == false) {
		nx = $.drug.d.oR.x + ($.drug.d.nx - $.drug.d.oC.x);
		ny = ($.drug.d.oR.y + ($.drug.d.ny - $.drug.d.oC.y));
		if ($.drug.d.fx > 0 && nx != $.drug.d.oC.x && ny != $.drug.d.oC.y) {
			x = new $.fx.Left($.drug,$.drug.d.fx);
			y = new $.fx.Top($.drug,$.drug.d.fx);
			x.custom($.drug.d.oC.x,nx);
			y.custom($.drug.d.oC.y,ny);
		} else {
			$($.drug).css(
				{
					left: nx + 'px',
					top: ny + 'px'
				}
			);
		}
		$.dragHelper.css('display','none');
	} else {
		if ($.drug.d.fx > 0 && hp.x != $.drug.d.oC.x && hp.y != $.drug.d.oC.y) {
			$.drug.d.prot = true;
			y = new $.fx.Top($.dragHelper.cur[0],{duration:$.drug.d.fx});
			x = new $.fx.Left(
				$.dragHelper.cur[0],
				{
					duration : $.drug.d.fx,
					onComplete : function()
					{
						$(this).css('display','none');
						if ($.drug.d.ghosting == false) {
							$($.drug).css('display', $.drug.d.oD);
						}
						$.drug = null;
					}
				}
			);
			if($.overzone && $.sortables) {
				dh = $.getPos($.sortHelper.cur[0]);
			} else {
				dh = false;
			}
			x.custom(hp.x,dh ? dh.x : $.drug.d.oC.x);
			y.custom(hp.y,dh ? dh.y : $.drug.d.oC.y);
		} else {
			$.dragHelper.css('display','none');
		}
	}
	
	if ($.droppables && $.droppables.length > 0 ){
		$.dropcheck($.drug);
	}
	if ($.sortables && $.drug.d.so == true) {
		$.sortcheck($.drug);
	}
	if ($.drug.d.prot == false) {
		if ($.drug.d.ghosting == false) {
			$($.drug).css('display', $.drug.d.oD);
		}
	    $.drug = null;
	}
};

$.drag = function(e)
{
    if ($.drug == null || $.drug.d.prot == true) {
		return;
    }
	if (window.event) {
		sx = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		sy = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	} else {
		sx = e.clientX;
		sy = e.clientY;
	}
    $.drug.d.nx = (sx - $.drug.d.dX);
    $.drug.d.ny = (sy - $.drug.d.dY);

	if ($.drug.d.gx) {
		$.drug.d.nx = parseInt($.drug.d.nx/$.drug.d.gx) * $.drug.d.gx;
		$.drug.d.ny = parseInt($.drug.d.ny/$.drug.d.gy) * $.drug.d.gy;
	}

	if ($.drug.d.cont) {
		$.drug.d.nx = $.drug.d.nx < $.drug.d.cont.x ? $.drug.d.cont.x : (($.drug.d.nx + $.drug.d.oC.w) > ($.drug.d.cont.x + $.drug.d.cont.w) ? ($.drug.d.cont.x + (($.drug.d.cont.w - $.drug.d.oC.w) >= 0 ? ($.drug.d.cont.w - $.drug.d.oC.w) : $.drug.d.cont.w)) : $.drug.d.nx);
		$.drug.d.ny = $.drug.d.ny < $.drug.d.cont.y ? $.drug.d.cont.y : (($.drug.d.ny + $.drug.d.oC.h) > ($.drug.d.cont.y + $.drug.d.cont.h) ? ($.drug.d.cont.y + (($.drug.d.cont.h - $.drug.d.oC.h) >= 0 ? ($.drug.d.cont.h - $.drug.d.oC.h) : $.drug.d.cont.h)) : $.drug.d.ny);
	}

	$.dragHelper.css('left', $.drug.d.nx + 'px').css('top', $.drug.d.ny + 'px');
	
	if ($.droppables && $.droppables.length > 0 ){
		$.dropcheckhover(sx, sy, $.drug.d.nx, $.drug.d.ny);
	}
};
$.draginit = false;

$.fn.Draggable = function(o)
{
    if (!$.draginit) {
		$('body').bind('mousemove', $.drag).bind('mouseup', $.dragstop);
		$.draginit = true;
    }
    if (!$.dragHelper) {
		$('body',document).append('<div id="dragHelper"></div>');
		$.dragHelper = $('#dragHelper');
		$.dragHelper.css(
			{
				position:	'absolute',
				display:	'none',
				cursor:		'move'
			}
		);
		if (window.ActiveXObject) {
			$.dragHelper.cur[0].onselectstart = function(){return false;};
			$.dragHelper.cur[0].ondragstart = function(){return false;};
		}
    }
    if (!o) {
		o = {};
	}
    return this.each(
		function()
		{
			if (this.isDraggable)
				return;
			var dhe = o.handle ? $(this).find(o.handle) : $(this);
			this.d = {
				revert : o.revert ? true : false,
				ghosting : o.ghosting ? true : false,
				so : o.so ? o.so : false,
				zIndex : o.zIndex ? $.intval(o.zIndex) : false,
				opacity : o.opacity ? parseFloat(o.opacity) : false,
				fx : $.intval(o.fx),
				hpc : o.hpc ? o.hpc : false
				
			};
			if (o.contaiment && ((o.contaiment.constructor == String && (o.contaiment == 'parent' || o.contaiment == 'document')) || (o.contaiment.constructor == Array && o.contaiment.length == 4) )) {
				this.d.contaiment = o.contaiment;
			}
			if(o.grid){
				if(typeof o.grid == 'number'){
					this.d.gx = this.d.gy = $.intval(o.grid);
				} else if (o.grid.length == 2) {
					this.d.gx = $.intval(o.grid[0]);
					this.d.gy = $.intval(o.grid[1]);
				}
			}
			dhe.get(0).dE = this;
			dhe.bind('mousedown', $.dragstart);
		}
	);
};
