/*
 * interface sortables - http://www.eyecon.ro/interface/
 *
 * Copyright (c) 2006 Stefan Petre
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

$.sortchanged = [];
$.sortHelper = false;
$.overzone = false;
$.sortstart = function ()
{
    if ($.drug == null) {
		return;
    }
	var i;
	$($.drug).css('display', 'none');
	$.sortHelper.addClass($.drug.d.hpc).css('display', $.drug.d.oD);
	$.sortHelper.oC = $.getPos($.sortHelper.cur[0]);
	$.sortHelper.css('float',$.drug.d.oF)
				.css('width', $.drug.d.oC.wb + $.sortHelper.oC.w - $.sortHelper.oC.wb + 'px')
				.css('height', $.drug.d.oC.hb + $.sortHelper.oC.h - $.sortHelper.oC.hb + 'px');
	$($.drug).after($.sortHelper.cur[0]);
};
$.sortcheck = function (e)
{
	$.sortHelper.removeClass(e.d.hpc)
				.css('display','none')
				.css('float','none')
				.html('');
	ts = [];
	fnc = false;
	for(i in $.sortchanged){
		id = $.attr($.droppables[$.sortchanged[i]], 'id');
		ser = $.Sortserialize(id);
		if ($.droppables[$.sortchanged[i]].f.os != ser.hash) {
			$.droppables[$.sortchanged[i]].f.os = ser.hash;
			if (fnc == false && $.droppables[$.sortchanged[i]].f.onchange) {
				fnc = $.droppables[$.sortchanged[i]].f.onchange;
			}
			ser.id = id;
			ts[ts.length] = ser;
		}
	}
	if (fnc != false && ts.length > 0) {
		fnc(ts);
	}
	$.sortchanged = [];
	$.overzone = false;

};
$.sortremeasure = function (e)
{
    if ($.drug == null) {
		return;
    }
	var i;
	e.f.el.each (
		function ()
		{
			this.pos = $.getPos(this);
		}
	);
};
$.sortcheckhover = function(e,o)
{
	if (!$.drug)
		return;
	$.sortHelper.css('display',$.drug.d.oD);
	var cur = false;
	var i = 0;
	if ( e.f.el.cur.length > 0) {
		for (i = e.f.el.cur.length; i >0; i--) {
			if (e.f.el.cur[i-1] != $.drug) {
				if (
				(e.f.el.cur[i-1].pos.x + e.f.el.cur[i-1].pos.w) > o.x && 
				(e.f.el.cur[i-1].pos.y + e.f.el.cur[i-1].pos.h) > o.y 
				) {
					cur = e.f.el.cur[i-1];
				}
			}
		}
	}
	helpos = $.getPos($.sortHelper.cur[0]);
	if (cur) {
		$(cur).before($.sortHelper.cur[0]);
	} else{
		$(e).append($.sortHelper.cur[0]);
	}
};
$.Sortserialize = function(s)
{
	var i;
	var h = '';
	var o = {};
	if (s) {
		if ($.sortables[s] ) {
			o[s] = [];
			$('#' + s + ' .' + $.sortables[s]).each(
				function ()
				{
					if (h.length > 0) {
						h += '&';
					}
					h += s + '[]=' + $.attr(this,'id');
					o[s][o[s].length] = $.attr(this,'id');
				}
			);
		} else {
			for ( a in s) {
				if ($.sortables[s[a]] ) {
					o[s[a]] = [];			
					$('#' + s[a] + ' .' + $.sortables[s[a]]).each(
						function ()
						{
							if (h.length > 0) {
								h += '&';
							}
							h += s[a] + '[]=' + $.attr(this,'id');
							o[s[a]][o[s[a]].length] = $.attr(this,'id');
						}
					);
				}
			}
		}
	} else {
		for ( i in $.sortables){
			o[i] = [];
			$('#' + i + ' .' + $.sortables[i]).each(
				function ()
				{
					if (h.length > 0) {
						h += '&';
					}
					h += i + '[]=' + $.attr(this,'id');
					o[i][o[i].length] = $.attr(this,'id');
				}
			);
		}
	}
	return {hash:h, o:o};
};
$.fn.Sortable = function (o)
{
	if (o.accept) {
		if (!$.sortHelper) {
			$('body',document).append('<div id="sortHelper"></div>');
			$.sortHelper = $('#sortHelper');
			$.sortHelper.css(
				{
					display:	'none'
				}
			);
		}
		$('.' + o.accept).Draggable(
			{
				revert : o.revert? true : false,
				zindex : 3000,
				opacity : o.opacity ? parseFloat(o.opacity) : false,
				hpc : o.helperclass ? o.helperclass : false,
				fx : o.fx ? o.fx : false,
				so : true,
				handle: o.handle ? o.handle : null,
				contaiment: o.contaiment ? o.contaiment : null
			}
		);
		this.Droppable(
			{
				accept :  o.accept,
				activeclass : o.activeclass ? o.activeclass : false,
				hoverclass : o.hoverclass ? o.hoverclass : false,
				helperclass : o.helperclass ? o.helperclass : false,
				ondrop: function (zone, drag, fx) 
						{
							$.sortHelper.after(drag);
							if (fx > 0) {
								$(drag).fadeIn(fx);
							}
						},
				sortable : true,
				onchange : 	o.onchange ? o.onchange : false,
				fx : o.fx ? o.fx : false,
				tolerance: o.tolerance ? o.tolerance : 'pointer'
			}
		);
	}
};