1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
Script: Sortables.js
    Contains <Sortables> Class.

License:
    MIT-style license.
*/

/*
Class: Sortables
    Creates an interface for drag and drop sorting of a list or lists.

Arguments:
    list - required, the list or lists that will become sortable.
        This argument can be a string id, an element, or an object or array of either strings or elements. When a single
        list (or id) is passed, that list will be sortable only with itself. To enable sorting between lists, one or more
        lists or id's must be passed using an array or an object. See Examples below.
    options - an Object, see options and events below.

Options:
    clone - whether or not to display a copy of the actual element while dragging. defaults to true.
    cloneOpacity - opacity of the place holding element
    elementOpacity - opacity of the element being dragged for sorting
    revert - whether or not to use an effect to slide the element into its final location after sorting. If you pass an object it will be treated as true and used as options for the revert effect. defaults to false.
    handle - a selector which be used to select the element inside each item to be used as a handle for sorting that item.  if no match is found, the element is used as its own handle.
    constrain - whether or not to constrain the element being dragged to its parent element. defaults to false.

Events:
    onStart - function executed when the item starts dragging
    onComplete - function executed when the item ends dragging

Example:
    (start code)
    var mySortables = new Sortables('list-1', {
        revert: {duration: 500, transition: Fx.Transitions.Elastic.easeOut}
    });
    //creates a new Sortable instance over the list with id 'list-1' with some extra options for the revert effect
    
    var mySortables = new Sortables(['list-1', 'list-2', 'list-3']);
    //creates a new Sortable instance allowing sorting between the lists with id's 'list-1', 'list-2, and 'list-3'
    (end)

*/

var Sortables = new Class({
    
    options: {
        constrain : false,
        clone: true,
        cloneOpacity: 0.7,
        elementOpacity: 0.7,
        handle: false,
        revert: false,
        onStart: $empty,
        onComplete: $empty
    },

    initialize: function(lists, options){
        this.setOptions(options);
        if(this.options.revert) this.options.revert = $merge(this.options.revert, {duration: 250, wait: false, onComplete: this.reset.bind(this)})
        
        this.idle = true;
        this.lists = [];
        this.hovering = false;
        this.newInsert = false;
        this.bound = {
            'start': [],
            'end': this.end.bind(this),
            'move': this.move.bind(this)
        };
        
        switch($type(lists)){
            case 'array': for (var i = 0, l = lists.length; i < l; i++) this.lists[i] = $(lists[i]); break;
            case 'object': for (var e in lists) this.lists.push($(lists[e])); break;
            case 'string': case 'element': this.lists.push($(lists)); break;
            default: return;
        }
        
        this.reinitialize();
        if (this.options.initialize) this.options.initialize.call(this);
    },
    
    /*
    Property: reinitialize
        Allows the sortables instance to be reinitialized after making modifications to the DOM such as adding or removing elements from any of the lists.
    */
    
    reinitialize: function(){
        if (this.handles) this.detach();
        
        this.handles = [];
        var elements = [];
        
        this.lists.each(function(list){
            elements.extend(list.getChildren());
        });
        
        this.handles = !this.options.handle ? elements : elements.map(function(element){
            return element.getElement(this.options.handle) || element;
        }.bind(this));
        
        this.handles.each(function(handle, i) {
            this.bound.start[i] = this.start.bind(this, elements[i], true);
        }, this);

        this.attach();
    },
    
    /*
    Property: attach
        Attaches the mousedown event to all the handles, enabling sorting.
    */
    
    attach: function(){
        this.handles.each(function(handle, i){
            handle.addEvent('mousedown', this.bound.start[i]);
        }, this);
    },

    /*
    Property: detach
        Detaches the mousedown event from the handles, disabling sorting.
    */
    
    detach: function(){
        this.handles.each(function(handle, i){
            handle.removeEvent('mousedown', this.bound.start[i]);
        }, this);
    },

    check: function(element, list){
        element = element.getCoordinates();
        var coords = list ? element : {
            left: element.left - this.list.scrollLeft,
            right: element.right - this.list.scrollLeft,
            top: element.top - this.list.scrollTop,
            bottom: element.bottom - this.list.scrollTop
        };
        return (this.curr.x > coords.left && this.curr.x < coords.right && this.curr.y > coords.top && this.curr.y < coords.bottom);
    },
    
    where: function(element){
        if (this.newInsert) { this.newInsert = false; return 'before'; }
        var dif = { x : this.curr.x - this.prev.x, y : this.curr.y - this.prev.y };
        return dif[['y', 'x'][(Math.abs(dif.x) >= Math.abs(dif.y)) + 0]] <= 0 ? 'before' : 'after';
    },
    
    reposition: function(){
        if (this.list.positioned) {
            this.position.y -= this.offset.list.y - this.list.scrollTop;
            this.position.x -= this.offset.list.x - this.list.scrollLeft;
        }
        else if (Client.engine.opera) {
            this.position.y += this.list.scrollTop;
            this.position.x += this.list.scrollLeft;
        }
    },
    
    start: function(event, element){
        if (!this.idle) return;

        this.idle = false;
        this.prev = {x : event.page.x, y : event.page.y};
        
        this.styles = element.getStyles('margin-top', 'margin-left', 'padding-top', 'padding-left', 'border-top-width', 'border-left-width', 'opacity');
        this.margin = {
            'top': this.styles['margin-top'].toInt() + this.styles['border-top-width'].toInt(),
            'left': this.styles['margin-left'].toInt() + this.styles['border-left-width'].toInt()
        };

        this.element = element;
        this.list = this.element.getParent();
        this.list.hovering = this.hovering = true;
        this.list.positioned = this.list.getStyle('position').test('relative|absolute|fixed');
        
        var coords,
            children = this.list.getChildren(),
            bounds = children.shift().getCoordinates();
        children.each(function(element){
            coords = element.getCoordinates();
            bounds.left = Math.min(coords.left, bounds.left);
            bounds.right = Math.max(coords.right, bounds.right);
            bounds.top = Math.min(coords.top, bounds.top);
            bounds.bottom = Math.max(coords.bottom, bounds.bottom);
        });
        this.bounds = bounds;
        
        this.position = this.element.getPosition([this.list]);
        this.offset = {
            'list': this.list.getPosition(),
            'element': {'x': event.page.x - this.position.x, 'y': event.page.y - this.position.y}
        };
        this.reposition();
        
        if (this.options.clone) this.clone = this.element.clone().setOpacity(this.options.cloneOpacity);
        else this.clone = new Element(this.element.getTag()).setStyles({'visibility' : 'hidden'}).setHTML('&nbsp;');
        this.clone.injectBefore(this.element.setStyles({
            'position' : 'absolute',
            'top' : this.position.y - this.margin.top,
            'left' : this.position.x - this.margin.left,
            'opacity': this.options.elementOpacity
        }));

        document.addEvent('mousemove', this.bound.move);
        document.addEvent('mouseup', this.bound.end);
        this.fireEvent('onStart', this.element);
        event.stop();
    },
    
    move: function(event){
        this.curr = {'x': event.page.x, 'y': event.page.y};
        this.position = {'x': this.curr.x - this.offset.element.x, 'y': this.curr.y - this