function qs_move_listItem(item) {
	
	pos = jQuery(item).parent().children().index(item);
	id = jQuery(item).attr('id').split("_")[1];
	type = jQuery(item).attr('id').split("_")[0];
	if(type=="wl" || type == "sp") {
		pos = jQuery(item).parent().children(".listElem").index(item);
	}
	mode = (type == "wl" || type == "wlitem") ? "1" : "2";
	
	jQuery.ajax({
		type: "POST",
		dataType: "jsonp",
		data: ({				   	
			id: id,
			pos: pos,
			mode: mode}),
		url: globalPortalURL + "/ajax-move-item",
		success: function(data){
			
			jQuery(item).removeClass('NoClick');
			switch(type) {
				case "wl":
					refList = "#watchlists_list";
					refListItems = "li";
					refItem = '#wlitem_' + id;
					break;
				case "wlitem":
					refList = "#watch_lists_dropdown";
					refListItems = "li.listElem";
					refItem = '#wl_' + id;
					break;
				case "sp":
					refList = "#searchprofiles_list";
					refListItems = "li";
					refItem = '#spitem_' + id;
					break;
				case "spitem":
					refList = "#searchprofile_dropdown";
					refListItems = "li.listElem";
					refItem = '#sp_' + id;
					break;
			}
			
			refPos = jQuery(refList + ' ' + refListItems).index(jQuery(refItem));
			
			if(pos == 0) {
				jQuery(refItem).insertBefore(refList + " " + refListItems + ":eq(0)");
			} else if(refPos > pos) {
				jQuery(refItem).insertBefore(refList + " " + refListItems + ":eq("+(pos)+")");
			} else {
				jQuery(refItem).insertAfter(refList + " " + refListItems + ":eq("+(pos)+")");
			}
			
			
			
		},
		error: function(){
			
		}
	});	

}


jQuery(function() {
	
		dragDropOpacity = 0.8;
		placeHolderSizeOffset = 0;
		notIE6 = true;
		
		if(jQuery.browser.msie && jQuery.browser.version < 8) {	
		
			placeHolderSizeOffset = 2;		
			jQuery('div#page_head #watch_lists_dropdown').css({
				width: 200,
				left: 2
			});			
			jQuery('div#page_head #searchprofile_dropdown').css({
				width: 200,
				left: 2
			});			
			if(jQuery.browser.version < 7) {
				notIE6 = false;
				jQuery('.dragIndicator span').html('');
				dragDropOpacity = 1;
			}
		}
		if(notIE6) {
			jQuery("ul#watch_lists_dropdown, ul#searchprofile_dropdown").sortable({
				items: "li.listElem",
				placeholder: 'dragdrop_placeholder',
				axis: 'y',
				distance: 10,
				forcePlaceholderSize: true,
				opacity: dragDropOpacity,
				start: function(event,ui) {
					jQuery(ui.item).addClass('NoClick');
				},
				stop: function(event,ui) {
					window.setTimeout(function() {
						jQuery(ui.item).removeClass('NoClick');
					}, 500);
				},
				update: function(event, ui) {				
					qs_move_listItem(ui.item);
				}
			});
		}
		
		
		
		jQuery("ul#watchlists_list, ul#searchprofiles_list").sortable({
			items: "li",
			placeholder: 'dragdrop_placeholder',
			axis: 'y',
			forcePlaceholderSize: true,
			distance: 10,
			opacity: dragDropOpacity,
			start: function(event, ui) {
				
				jQuery(ui.placeholder).height(jQuery(ui.helper).height()+placeHolderSizeOffset);
			},
			update: function(event, ui) {
				qs_move_listItem(ui.item);
			}
		});
		
		jQuery('.aspan').click(function() {
			if(!jQuery(this).parent().hasClass('NoClick')) {
				location.href=jQuery(this).attr('rel');
			}			
		});
		
		
		
		// Insert Placeholders if a least 1 Element in DropDown
		if(jQuery('ul#watch_lists_dropdown li.listElem').length > 0) {
			jQuery('<li />').addClass('firstListElem placeholder').insertAfter('ul#watch_lists_dropdown li.dd_header');	
			jQuery('<li />').addClass('placeholder lastListElem').insertBefore('ul#watch_lists_dropdown li.dragIndicator');										
		} else {
			jQuery('ul#watch_lists_dropdown li.dd_header').addClass('lastListElem');	
		}
		if(jQuery('ul#watch_lists_dropdown li.listElem').length > 1) {
			jQuery('ul#watch_lists_dropdown li.lastListElem').removeClass('lastListElem');
			jQuery('ul#watch_lists_dropdown li.dragIndicator').addClass('lastListElem').show();				
		}
		
		if(jQuery('ul#searchprofile_dropdown li.listElem').length > 0) {
			jQuery('<li />').addClass('firstListElem placeholder').insertAfter('ul#searchprofile_dropdown li.dd_header');	
			jQuery('<li />').addClass('placeholder lastListElem').insertBefore('ul#searchprofile_dropdown li.dragIndicator');										
		} else {
			jQuery('ul#searchprofile_dropdown li.dd_header').addClass('lastListElem');	
		}
		if(jQuery('ul#searchprofile_dropdown li.listElem').length > 1) {
			jQuery('ul#searchprofile_dropdown li.lastListElem').removeClass('lastListElem');	
			jQuery('ul#searchprofile_dropdown li.dragIndicator').addClass('lastListElem').show();	
		}
				
				
				
				
		jQuery('.watch_lists_trigger').hover(
		function() {
			jQuery('#watch_lists_dropdown').show();
            jQuery('#searchprofile_dropdown').fadeOut(150);
		},
		function() {
			jQuery('#watch_lists_dropdown').fadeOut(150);
		});		
        
        jQuery('.searchprofile_trigger').hover(
        function() {
            jQuery('#searchprofile_dropdown').show();
			jQuery('#watch_lists_dropdown').fadeOut(150);
        },
        function() {
            jQuery('#searchprofile_dropdown').fadeOut(150);
        });
		
	});
