$(document).ready(function() {
	//if we're on a tab the items are sortable otherwise there draggable
	var is_tab = false;
	var pieces = window.location.href.split('?');
	var url = pieces[0];
	var parts = url.split('/');
	if(parts[parts.length - 1] == 'public_tab.php') is_tab = true;
	
	if(is_tab) {
		$('#sortables_container').sortable({
			scroll : true,
			opacity: 0.35,
			handle: '.handle'
		});
	}
	else {
		$('div.container').draggable({
			scroll : true,
			revert : true,
			opacity: 0.35,
			handle: $('.handle')
		});

	}
	
	$('#tab_menu li:not(#mp,#add_tab,#inbox)').droppable({
		accept : 'div',
		hoverClass : 'hover',
		tolerance : 'pointer',
		drop : function(e, ui) {
			if($(this).hasClass('selected')) return;
			
			var item = ui.draggable;
			var tab_id = this.id.split('_')[1];
			var item_id = $(item).attr('id').split('_')[1];
			var tab = this;
			
			var action = 'move_from_public';
			var current_tab_id = '';
			if(!is_tab) 
				action = 'add_from_public';
			else {
				current_tab_id = $('#tab_menu .selected').attr('id').split('_')[1];
			}
	
			$.ajax({
				type : 'POST',
				url : 'ajax_request/public_c.php',
				data : 'tab_id=' + tab_id + '&id=' + item_id + '&current_tab_id=' + current_tab_id + '&action=' + action,
				dataType : 'json',
				success : function(data) {
					if(data == false) { 
						return alert('No. of allowed images reached');
					}
					
					if(!is_tab) {
						$(tab).effect("highlight", {}, 2000);
					}
					else {
						item.draggable('destroy');
						item.remove();
					}
				} 
			});
		}
	});
});