$(document).ready(function(){
		$('ul.static_block_right li').each(function(index) {
		$( ".static_block_left").find('[data-id='+$(this).attr("data-id")+']').hide();
	});
	//to save blocks in menu items
		$( "ul.static_block_right" ).sortable({
		connectWith: ".connectedSortable",
		start: function(event, ui) {
			$( ".static_block_delete" ).hide();
		},			
		update: function(event, ui) {
			$( ".static_block_delete" ).hide();
			if(typeof ui.item.attr('itemblockid')=='undefined')
			 ui.item.attr('itemblockid','new');
			setStaticBlocks();	
			$( ".static_block_left").find('[data-id='+ui.item.attr('data-id')+']').hide();			
		}
		});	
		$(".static_block_left li").draggable({
			connectToSortable: ".connectedSortable",
			helper: function(){
				return $(this).clone();
			}
		})
		
		$(document).on('click','ul.static_block_right li .static_block_delete',function() {
			var elmt = $(this).closest("li");
			elmt.fadeOut('slow', function() {
				var deleteBlock = elmt.attr("itemblockid");
				setStaticBlocks(deleteBlock,elmt);
				$( ".static_block_left").find('[data-id='+elmt.attr('data-id')+']').show();
		});
		});
		
		$(document).on('mouseenter','ul.static_block_right li',function(){
		   $( "ul.static_block_right .static_block_delete" ).hide();
		   $(this).find(".static_block_delete").show();
		});	
		
		$(document).on('mouseleave','ul.static_block_right li',function(){
			$( "ul.static_block_right .static_block_delete" ).hide();
		});
		$( ".static_block_left, .static_block_right" ).disableSelection();	
		
});	
function setStaticBlocks(deleteBlock,removeObj)  {
	var tags = [];
	 $('ul.static_block_right li').each(function(index) {
		  if($(this).attr("itemblockid") != deleteBlock) {
			var obj			=	{};
			obj.blockID	=	$(this).attr("data-id");
			if(typeof $(this).attr("itemBLockID")!='undefined')
				obj.itemBLockID	=	$(this).attr("itemblockid");
			else
				obj.itemBLockID	=	'';
			tags.push(obj);
		  }
	 });
	if($('#menuItemID').val() > 0) {
		var menuItemID = $('#menuItemID').val();
	}
	$.ajax({		   
	  url: GLOBAL_BASE_PATH+"/custommenu/ajaxSetStaticBlocks/",
	  type: "POST",
	  cache: false,
	  async: false,
	  global: false,
	 data: 'items=' + $.stringify(tags,0,0) + "&menuItemID=" + menuItemID + "&deleteBlock=" + deleteBlock,
	  success: function(data) {		
		  if(typeof deleteBlock=='undefined') {
			  if(data)
				$('[itemblockid=new]').attr('itemblockid',$.trim(data));
		  } else
			removeObj.remove();
	  }
	});
}	