/**
 * @author HP
 */
jQuery( function($) { // In here $ is jQuery

bbSortPages = {
	handleText: 'drag',
	handle: '',
	sortCfg: {
		accept: 'page-item',
		handle: 'strong.sort-handle',
		opacity: .3,
		helperclass: 'helper',
		onStop: function() {
			bbSortPages.place = null;
			bbSortPages.recolor();
		}
	},
	editText: '',
	saveText: 'Save Page Order &#187;',
	place: null,  // The id of the list item it's currently hovering before
	placed: null, // The id of the list item it's been made a child of
	rtl: 'rtl' == $('html').attr( 'dir' ),

	recolor: function() {
		$('#page-list-root li').css( 'background-color', '' ).filter(':even').removeClass('alt').end().filter(':odd').addClass('alt');
	},

	checkHover: function(el, doit) {
		if ( this.place == el.id && doit )
			return;

		if ( !doit ) {
			this.place = null;
			return;
		}

		this.place = el.id;
		if ( $('#' + this.place).children('ul[li:visible]').size() ) // Don't shift over if there's already a UL with stuff in it
			return;

		var id = this.place.split('-')[1];
		$('#' + this.place).not('[ul]').append("<ul id='forum-root-" + id + "' class='list-block holder'></ul>").end().children('ul').append(jQuery.iSort.helper.get(0)); // Place in shifted box
		this.placed = 'forum-' + id;
	},

	serialize: function () {
		h = '';
		$('#page-list-root, #page-list-root ul').each( function() {
			var i = this.id;
			$('#' + i + '> .page-item').each( function () {
				if (h.length > 0)
					h += '&';
				var root = 'page-list-root' == i ? 0 : i.split('-')[2];
				h += 'root[' + root + '][]=' + this.id.split('-')[1];
			} );
		} );
		return h;
	},

	init: function() {
		this.handle = "<strong class='sort-handle'>[" + this.handleText + "]&nbsp;</strong>";
		$('#page-list-root').after("<p class='submit'><input type='button' id='page-order-edit' value='Edit Page Order &#187;' /></p>");
		this.editText = $('#page-order-edit').val();
		var div = document.createElement('div'); div.innerHTML = this.saveText; // Save the raquo!
		this.saveText = div.childNodes[0].nodeValue;
		div = null;

		$('#page-order-edit').toggle( function() {
			$(this).val(bbSortPages.saveText);
			$('#page-list-root li div.page-list-first-col span').prepend(bbSortPages.handle);
			$('#page-list-root').Sortable( bbSortPages.sortCfg );
		}, function() {
			$(this).val(bbSortPages.editText);
			$('.sort-handle').remove();

			var hash = bbSortPages.serialize();
			hash += '&' + $.SortSerialize('page-list-root').hash.replace(/page-list-root/g, 'order').replace(/forum-/g, '')
			$('#page-list-root').SortableDestroy();

			$.post(
				'admin-ajax.php',
				'action=order-forums&cookie=' + encodeURIComponent(document.cookie) + '&' + hash
			);
		} );
	}
}

// overwrite with more advanced function
jQuery.iSort.checkhover = function(e,o) {
	if (!jQuery.iDrag.dragged)
		return;

	if ( e.dropCfg.el.size() > 0 ) {
		var bottom = jQuery.grep(e.dropCfg.el, function(i) { // All the list items whose bottom edges are inside the draggable
			var x = bbSortPages.rtl ? i.pos.x + i.pos.wb > jQuery.iDrag.dragged.dragCfg.nx + jQuery.iDrag.dragged.dragCfg.oC.wb : i.pos.x < jQuery.iDrag.dragged.dragCfg.nx;
			return i.pos.y + i.pos.hb > jQuery.iDrag.dragged.dragCfg.ny && i.pos.y + i.pos.hb < jQuery.iDrag.dragged.dragCfg.ny + 30 && x;
		} );

		if ( bottom.length > 0 ) { // Use the lowest one one the totem pole
			var x = bbSortPages.rtl ? bottom[bottom.length-1].pos.x + bottom[bottom.length-1].pos.wb - 30 > jQuery.iDrag.dragged.dragCfg.nx + jQuery.iDrag.dragged.dragCfg.oC.wb : bottom[bottom.length-1].pos.x + 30 < jQuery.iDrag.dragged.dragCfg.nx;
			if ( bbSortPages.placed != bottom[bottom.length-1].id || !x ) { // Testing to see if still placed in shifted box
				bbSortPages.placed = null;
				jQuery(bottom[bottom.length-1]).after(jQuery.iSort.helper.get(0));
			}
			bbSortPages.checkHover(bottom[bottom.length-1], x); // If far enough right, shift it over
			return;
		}

		// Didn't find anything by checking bottems.  Look at tops
		var top = jQuery.grep(e.dropCfg.el, function(i) { // All the list items whose top edges are inside the draggable
			var x = bbSortPages.rtl ? i.pos.x + i.pos.wb > jQuery.iDrag.dragged.dragCfg.nx : i.pos.x < jQuery.iDrag.dragged.dragCfg.nx;
			return i.pos.y > jQuery.iDrag.dragged.dragCfg.ny && i.pos.y < jQuery.iDrag.dragged.dragCfg.ny + 30 && x;
		} );

		if ( top.length ) { // Use the highest one (should be only one)
			jQuery(top[0]).before(jQuery.iSort.helper.get(0));
			bbSortPages.checkHover(top[0], false);
			return;
		}
	}
	jQuery.iSort.helper.get(0).style.display = 'block';
}

if ( 'undefined' != typeof bbSortPagesL10n )
	$.extend( bbSortPages, bbSortPagesL10n );

bbSortPages.init();

} );
