	$(document).ready(
		function () {
			$("body:not(.stumbleButton *)").mouseup(function(){
				$(".stumbleButton").children("img").show();
				$(".stumbleButton").children(".btnDown").hide();
				return true;
				}
			);
			$(".stumbleButton").mouseup(function(){return addthis_sendto('stumbleupon');});
			$(".stumbleButton").mousedown(function(){
				$(this).children("img").hide();
				$(this).children(".btnDown").show();
			});
			updateItemNumbers();
			addSelectedSortable();
		});
		
	
 	
 	function addSelectedSortable(){
		if($('#sortSelDtr').isSortable) $('#sortSelDtr').SortableDestroy();
		$("#sortSel .itemBlock .draggable").unbind('hover');
		$('#sortSelDtr').Sortable(
			{
				accept: 'itemBlock',				
				helperclass: 'sortHelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				handle: '.draghandle',
				tolerance: 'pointer',
				//containment: 'parent', //this doesn't seem to work
				axis: 'both',
				fx: 0,
				fit: true,
				opacity: 0.7,
				onChange : function(ser)
				{
				},
				onStart : function()
				{
				},
				onStop : function()
				{
					$(this).css('background-color', 'transparent');
					$(this).css('left', 0);
					$(this).css('top', 'auto');
					saveSelections();
				}
			}
		);
		
		$("#sortSel .itemBlock .draggable").hover(function(){ $(this).children('.toolsSelected').fadeIn('fast');},function(){$(this).children('.toolsSelected').fadeOut('fast');});
		
		//checking the itemcount and decide whether or not to display the "see the rest"
		if( parseInt($('#itemcount').val()) > 5 ){
			$('#seethrest').show();
		}else{
			$('#seethrest').hide();
		}
	}
	
	function addSelection(itemID){
		blockUI();
		//$('#e' + itemID + " .toolsNominee").hide();
		//$('#e' + itemID + " .toolsExisting").show();

		if (($("#sortSel").length>0)){
			if ($("#sortSelDtr").length==0){
				$("#sortSelDtr_ctl").append("<div id='sortSelDtr'></div>");
				addSelectedSortable();
			}
			
			$('#e' + itemID).clone().hide().prependTo("#sortSelDtr").attr('id','i'+itemID).slideDown('slow', function(){
			
				saveSelections();
			
			});
		
			if ($("#sortSelDtr .itemBlock").length>5) $("#sortSelDtr .itemBlock:last").slideUp('slow');
			
		}else{
			$("#txtSortOrder").attr("value",$("#txtSortOrder").attr("value") + ',' + 'i' + itemID.toString());
			saveSelections();	
		}

		$('#e' + itemID).css({backgroundColor:"#ddd"});

		//$('#e' + itemID).animate({backgroundColor:"#B8B8B8"}, 1000);	
		$('#e' + itemID).animate({backgroundColor:"#ffffff"}, 1000);		
		
		 	
	}
	
	function removeItem(itemID){
		blockUI();
		$('#i' + itemID).slideUp('slow',function(){
			$('#i' + itemID).remove();
			saveSelections();
		});
	}
	
	
	
	function saveSelections(){
		/*// ask if user wants to watch topic, but only the first time they make selections
		if ($('#watchStatus').attr("value")=='' || $('#watchStatus').attr("value")==undefined){
			qc.pA('TopicView', 'pxyShowWatchConfirm', 'QClickEvent', '', '');
		}else{
			saveSelectionsContinue();
		}*/
		saveSelectionsContinue();
	}
	
	function saveSelectionsContinue(watch){
		$('#messageBoxModalConfirmWatch').jqm().jqmHide();
		if (watch) $('#watchStatus').attr("value", watch);
		if ($("#sortSelDtr").length>0){
			$("#txtSortOrder").attr("value", $.SortSerialize('sortSelDtr').o.sortSelDtr);
		}

		qc.pA('TopicView', 'pxyAddSelection', 'QClickEvent', document.getElementById('txtSortOrder').value, '');

		updateItemNumbers();

		addSelectedSortable();

		
	}
	
	function resetNewItem(){
		
		$("#newItem .description").hide();
		$("#btnShowOptions").show();
	}
	
	
	function updateItemNumbers(){
		var x=1;
		$("#sortSelDtr .itemBlock").each(function(){
				$(this).find(".positionID").html(x + '.');
				x++;
			}
		);
		// need to disable "move up" and "move down" for first and last items
		//$("#sortSelDtr .itemBlock:first)
	}
	
	function moveItem(itemID, movement){
		blockUI();
		if (movement=="up"){
			$("#" + itemID).slideUp('slow', function(){
					//$(this).prev('.itemBlock').before($(this));
					var curindex = $(".itemBlock").index(this);
					
					curindex=curindex-1;
					$(".itemBlock:eq("+(curindex)+")").before($(this));
					updateItemNumbers();
					$(this).slideDown('normal', function(){
						saveSelections();
					});
				}
			);
		}
		if (movement=="down"){
					
			$("#" + itemID).slideUp('slow', function(){
					var curindex = $(".itemBlock").index(this);
					
					curindex=curindex+1;
					//alert( curindex );
					$(".itemBlock:eq("+(curindex)+")").after($(this));				
					//$(this).next('.itemBlock').after($(this));
					//alert($(".itemBlock:eq("+curindex+")").next().attr('id'));
					updateItemNumbers();
										
					$(this).slideDown( 'normal', function(){
						//alert("after slide down");	
						saveSelections();
					});					
				});
		}
	}
	
	function concatObject(obj) {
	  str='';
	  for(prop in obj)
	  {
	    str+=prop + " value :"+ obj[prop]+"\n";
	  }
	  return(str);
	}
	
	function toggleOptions(){
		$("#newItem .description").toggle("slow");
		
		if( $("#btnShowOptions").text() == "Add image, video, description and/or link" ){
			$("#btnShowOptions").addClass('arrowDown');					
			$("#btnShowOptions").text("Hide options");
		}else{
			$("#btnShowOptions").removeClass('arrowDown');
			$("#btnShowOptions").text("Add image, video, description and/or link");
		}
	}	

	
