var circle = circle || {};

circle.tabClick = true;
circle.currentLimit = 5;
circle.currentOffset = 0;
circle.totalSize = 0;
circle.currentCategory = -1;
circle.sortOptions = undefined;
circle.storyShow = undefined;
circle.firstLoad = true;



circle.postItem = function(clicker){


	var title= "";
	var content = "";
	var type="";
	var listId = $('#postToList').attr('value');
	switch(clicker.id){
	
	
	case 'postText':
	
	title = $('#postTitle').attr('value');
	content = $('#postText').attr('value');
	type='text';
	break;
	default: 'bar';	
	}

	$.getJSON("/api/postItem.php?list="+listId+"&title="+title+"&description="+content+"&type="+type, function(data){	
		circle.showCircle(listId,'circleStreamShow');		
	});

};


circle.getImageIcon = function(type){
	 var retval = "";
	switch(type){
	
		case "text":
		 retval = "/images/famfam/storyposted.png";
		break;
		case "image":
		 retval = "/images/famfam/photoposted.png";
		break;
		case "video":
		 retval = "/images/famfam/videoposted.png";
		break;
		case "audio":
		 retval = "/images/famfam/soundposted.png";	
		break;
		case "quote":
		 retval = "/images/famfam/quoteposted.png";		
		break;
		case "link":
		 retval = "/images/famfam/linkposted.png";		
		break;
		
		default:
		 retval = "/images/famfam/storyposted.png";
		break;
	
	
	}
		return retval;

}


//If the "circleCategory" drop down changes we need to do stuff to filter the category
circle.publicListCategoryChange = function(){
	
	var circleCategoryValue = $('#circleCategory').attr('value');
	
	circle.currentLimit = 5;
	circle.currentOffset = 0;
	circle.totalSize = 0;
	circle.currentCategory = circleCategoryValue;
	
	circle.getPublicLists(circle.currentLimit,circle.currentOffset);

	

	
};



circle.pageListNext = function(){
	circle.currentOffset = circle.currentOffset + circle.currentLimit;

	if(circle.currentOffset > (circle.totalSize - circle.currentLimit)){
		circle.currentOffset = circle.totalSize - circle.currentLimit;
	}
	circle.getPublicLists(circle.currentLimit,circle.currentOffset);
};

circle.pageListPrevious = function(){
	circle.currentOffset = circle.currentOffset - circle.currentLimit;
	
	if(circle.currentOffset < 0){
		circle.currentOffset = 0;
	}
	
	circle.getPublicLists(circle.currentLimit,circle.currentOffset);

};

circle.getPublicLists = function(limit, offset){
	
	circle.currentOffset = offset;
	circle.currentLimit = limit;
	
	var sortOption = "";
	if(circle.sortOptions == undefined){
	
	}else{
	 
	 sortOption = "&sortOption="+circle.sortOptions;
	
	}
	
	var tags = "public_circle";
	var value = "true";
	
	
	if(circle.currentCategory != -1){
	
		tags +="|category";
		value +="|"+circle.currentCategory;
	}
		
	var totalCountUrl = "/api/circle.php?c=getListCountByTag&tags="+tags+"&values="+value;
	$.getJSON(totalCountUrl,function(data){
		
		
		
		var startVal = offset+1;
		var endVal = offset + limit;
		var totalCount = parseInt(data.result);
		circle.totalSize = totalCount;
		if(endVal > totalCount) { endVal = totalCount; }
		if(startVal == 1) { 
			$('.pageLeft').unbind('click').addClass('inactiveLink');
		} else {
			$('.pageLeft').click(function(){
				circle.pageListPrevious();
			}).removeClass('inactiveLink');
		}
		if(endVal == totalCount) {
			$('.pageRight').unbind('click').addClass('inactiveLink');
		} else {
			$('.pageRight').click(function(){
				circle.pageListNext();
			}).removeClass('inactiveLink');
		}

		$('.pageCenter').get(0).innerHTML = startVal+" - " + endVal + " of " + totalCount + " circles found";
	
	
		
	});

	
	var url = "/api/circle.php?c=getListsByTag&limit="+limit+"&offset="+offset+"&tags="+tags+"&values="+value+sortOption;
	
	$.getJSON(url,function(data){
		
		$("#circleList").get(0).innerHTML = "";
		
		var lists = data.result;
		if(data.response == 1){
		$(lists).each(function(i,list){					
			var circleListItem = document.createElement("div");
				circleListItem.className = "circleListItem";
				
			var circleListName = document.createElement("span");
				circleListName.className = "circleListName";
				
			var circleListNameLink = document.createElement("a");
				circleListNameLink.href = "/circle.php?l="+list.id;
		    var circleListNameLinkTxt = document.createTextNode(list.title+" ");
		    	circleListNameLink.appendChild(circleListNameLinkTxt);
		    	circleListName.appendChild(circleListNameLink);
		    			
			var circleListDescription = document.createElement("span");
				circleListDescription = document.createElement("span");
			var circleListDescriptionTxt = document.createTextNode(list.description);
				circleListDescription.appendChild(circleListDescriptionTxt);
			
			var circleListNeighborCount = document.createElement("div");
				circleListNeighborCount.className = "circleListNeighborCount";
				
				var word = "Neighbor";
				
				if(data.cnt == "1"){
					
				}else{
					word = "Neighbors";
				
				}
			var  clnc = document.createTextNode(list.cnt + " "+ word);
			
			circleListNeighborCount.appendChild(clnc);
			
			circleListItem.appendChild(circleListName);
			circleListItem.appendChild(circleListDescription);
			circleListItem.appendChild(circleListNeighborCount);
			var circleList = document.getElementById("circleList");
			circleList.appendChild(circleListItem);	
					
		});
	
		}
	});


};

circle.addRSS = function(listId){
	var pageUrl = $("#addRssInput").val();
	var pageName = $("#addRssName").val();
	var pageDescription = $("#addRssDescription").val();
	var pageCategory = $("#addRssCategory").val();
	
	var url = "/api/modifyRSS.php?c=add&listId="+listId+"&feedUrl="+pageUrl+"&feedName="+pageName+"&feedCategory="+pageCategory+"&feedDescription="+pageDescription;
	
		var warningDialog = document.createElement("div");
		warningDialog.id = "warningDialog";
		warningDialog.className="flora";
		
		$(warningDialog).html("Please wait while that feed is processed.");
		var bc = document.getElementById('bodycontainer');
		bc.appendChild(warningDialog);
	
		$("#warningDialog").dialog({
			buttons:{
				"Ok" : function(){}
			}
		});
		
			$(".ui-dialog-buttonpane button").hide();
		
		
		
	$.getJSON(url + "&jsoncallback=?",function(data){
		$("#warningDialog").dialog("close");
		$("#warningDialog").remove();			

		if(data.result != "false"){
			
				var successDialog = document.createElement("div");
					successDialog.id = "successDialog";
					successDialog.className="flora";
					$(successDialog).html("That feed has been added! Thanks! We're now taking you back to the Blog Ticker.");
					var bc = document.getElementById('bodycontainer');
					bc.appendChild(successDialog);
					$("#successDialog").dialog({
					buttons:{
						"Ok" : function(){}
						}
					});
					
					$(".ui-dialog-buttonpane button").hide();

				  setInterval(function(){
						window.location="/blogTicker.php";
					  }, 3000);

		} else {
			
			
				var failureDialog = document.createElement("div");
					failureDialog.id = "failureDialog";
					failureDialog.className="flora";
					$(failureDialog).html("Sorry, there was an error! Please check your RSS info, or try again later.");
					var bc = document.getElementById('bodycontainer');
					bc.appendChild(failureDialog);
					$("#failureDialog").dialog({
					buttons: { 
					  "Ok": function() { 
							var dialogFoo = $(this);
							$(dialogFoo).dialog("close");
							$(dialogFoo).remove();					  
						   }
					}

					
					});
			
		/*
	
			$("#failure").dialog({				
				buttons: { 
					  "Ok": function() { 
							var dialogFoo = $(this);
							$(dialogFoo).dialog("close");
							$(dialogFoo).remove();					  
						   }
					}
				});		
*/
		}
		
	});
}

circle.removeRSS = function(pageUrl, listId){
	
	var url = "/api/modifyRSS.php?c=remove&listId="+listId+"&feedUrl="+pageUrl;
	
	$.getJSON(url,function(data){
		alert(data);	
	});

}



/*
	TODO NEEDED IN REDESIGN  (X)
*/
circle.leaveCircle = function(listId,title){
	
	
	var url = "/api/circle.php?c=leave&listId="+listId;
	
	
	var confirmDialog = document.createElement("div");
		confirmDialog.id = 'confirmDialog';
		confirmDialog.className="flora";
		$(confirmDialog).html("<div style='text-align: center; margin-bottom: 10px;'>Are you sure that you want to leave "+unescape(title)+" </div>");
		
	var successDialog = document.createElement("div");
		successDialog.id = 'successDialog';
		successDialog.className="flora";
		$(successDialog).html("<div style='text-align: center; margin-bottom: 10px;'>You have left "+unescape(title)+" </div>");
	
		$("#bodyRight").append(confirmDialog);
		
		
		$(confirmDialog).dialog({
			buttons: {
				"Ok" : function(){
					$.getJSON(url,function(data){	
						$(confirmDialog).dialog("close");
						$(confirmDialog).remove();
						$("#bodyRight").append(successDialog);
						$(successDialog).dialog({
							buttons: {
								"Ok": function(){
								window.location = window.location.href;									
								}						
							}
						});
					});
				},
				"Cancel" : function(){
					$(confirmDialog).dialog("close");
					$(confirmDialog).remove();					
				}
			}
		});

}


/*
	TODO NEEDED IN REDESIGN (X)
*/
circle.joinCircle = function(listId){

	var url = "/api/circle.php?c=join&listId="+listId;
	
	var submitDialog = document.createElement("div");
		submitDialog.id = "submitDialog";
		submitDialog.className="flora";
		$(submitDialog).html("<div style='text-align: center; margin-bottom: 10px;'>You are being added to this circle.</div><div style='text-align: center;'><img src='/images/v3/loading.gif' style='width:32px; height: 32px;'></div>");
		
		var bodyRight = document.getElementById("bodyRight");
		
		bodyRight.appendChild(submitDialog);
		
	 $(submitDialog).dialog({"buttons":{"Ok": function(){} }});
		$(".ui-dialog-buttonpane button").hide();

	
	
	$.getJSON(url,function(data){
		$(submitDialog).dialog("close");
		$(submitDialog).remove();
		window.location = "/circle.php?l="+listId+"&joined=yes";
	});
}


circle.itemEdit = function (id){

	var myLocation = window.location.pathname;


	window.location="/editPost.php?itemId="+id+"&ref="+myLocation;
	

}


circle.follow = function(listId, followname){

var url = "/api/circle.php?c=follow&listId="+listId;

	var followDialog = document.createElement("div");
		followDialog.id = "followDialog";
		followingDialog.className="flora";
		$(followDialog).html("Are you sure you want to start watching <b>" + followname + "</b>?");
		var bc = document.getElementById('bodycontainer');
		bc.appendChild(followDialog);
	
		$("#followDialog").dialog({
			buttons:{
				"Ok" : function(){				
					$.getJSON(url,function(data){
						var sURL = unescape(window.location.href);
						window.location.href=sURL;
					});
				}
			}
		});
		
		
		

	
	

	
	
}


circle.unfollow = function(listId,followname){

var url = "/api/circle.php?c=unfollow&listId="+listId;

	var followDialog = document.createElement("div");
		followDialog.id = "followDialog";
		followDialog.className="flora";
		$(followDialog).html("Are you sure you want to stop watching <b>" + unescape(followname) + "</b>?");
		var bc = document.getElementById('bodycontainer');
		bc.appendChild(followDialog);
	
		$("#followDialog").dialog({
			buttons:{
				"Ok" : function(){				
					$.getJSON(url,function(data){
						var sURL = unescape(window.location.href);
						window.location.href=sURL;
					});
				}
			}
		});
	
}


/*
	TODO NEEDED IN REDESIGN (X)
*/
circle.frontPageRemove = function(id,title){
	$.getJSON(url,function(data){	
		//window.location = "/circle.php?l="+listId;
	});

	if(typeof title === "undefined"){
		title = "this item";
	}
//	var listId = $("#postToList").attr("value");
	var url = "/api/itemAction.php?c=frontCarouselRemove&itemId="+id;
	var itemId = id;
	var warningDiv = document.createElement("div");
		warningDiv.id = "promote_"+itemId;
		warningDiv.className="flora";
		$(warningDiv).html("Click OK to remove "+title+"  from the front page carousel!.");
		var cs = document.getElementById("bodyRight");
		cs.appendChild(warningDiv);
		
		$("#promote_"+itemId).dialog(
			{
			
			buttons: { 
      		  "Ok": function() { 

      	      	var dialogFoo = $(this);
            	
				$.getJSON(url,function(data){	
			
				if(data.response == 1){
					  $(dialogFoo).dialog("close");
		      	      $(dialogFoo).remove();	
		      	      window.location="/";					
				}else{

					  $(dialogFoo).html(data.errorMessage);
				}
			});
       		  
       		   }, 
       		 "Cancel": function() { 
          	  $(this).dialog("close"); 
          	  $(this).remove();
        	} 
  	  		}});



/* 
$.getJSON(url,function(data){
			if(data.response == 1){
				//Get the card for his item...
			
				

				var cardUrl = "/api/itemAction.php?c=getCard&itemId="+itemId;
				$.getJSON(cardUrl, function(data){
				
				if(data.result == 1){
					var card =  $(".card.default_temp").get(0);
					
					if(card != undefined){
						$(card).html("");
						$(card).append(data.cardHtml);
						card.className = "card";
					}		
				}	
				});
			}
	});

	*/





}


/*
	TODO NEEDED IN REDESIGN (X)
*/
circle.frontPage = function(id){


//	var listId = $("#postToList").attr("value");
	var url = "/api/itemAction.php?c=frontCarousel&itemId="+id;
	var itemId = id;
	var warningDiv = document.createElement("div");
		warningDiv.id = "promote_"+itemId;
		warningDiv.className='flora';
		var str = "<div id='dialogContentBlock'>";
		    str +="<div id='dialogFormContent'>Promoting this item will place it on the front page carousel!. Click ok to continue!";
			str +="<div class='addImage' style='margin: 5px 0px; float: left;'><a id='attachImage'>Attach a custom image</a></div>";
			str +="<div id='carouselImageForm' style='display: none; clear: both;'>";
			str +="<form method='post' name='uploadImageForm' id='uploadImageForm' action='/api/carouselImage.php' enctype='multipart/form-data'>";
			str +="<input type='file' id='carouselImageUpload' name='carouselImageUpload'>";
			str +="</form></div></div></div>";
			str +="<script type='text/javascript'>"
		
		$(warningDiv).html(str);
		var cs = document.getElementById("bodyRight");
		cs.appendChild(warningDiv);
		try{
		
		$(warningDiv).dialog(
			{
			
buttons: { 
	  "Ok": function() { 
		  	   	$(".addImage").unbind("click");


								
      		  	var uploadImage = $("#carouselImageUpload").val();
      		  if(uploadImage != "" && typeof uploadImage != "undefined"){
   
   				
   					$("<div/>").attr("style","text-align: center").html("Sending to the carousel...<br/><img src='/images/v3/loading.gif' />").prependTo("#dialogContentBlock");
   					$("#dialogFormContent").css("display","none");
  	  		  		$("#uploadImageForm").ajaxForm(function(){
						  						
  							    		  
      		  		});
      		  		//Call what's above it! sweet eh?
      		 		var options = {
      		 			success: function(data){
      		 				url = url+"&img="+data;
							$.getJSON(url,function(data){	
								if(data.response == 1){
									  $("#promote_"+itemId).dialog("close");
									  $("#promote_"+itemId).remove();	
									  window.location="/";					
								}else{	
									  $(dialogFoo).html(data.errorMessage);
								}
							});
      		 			}
      		 		
      		 		}
      		 		$("#uploadImageForm").ajaxSubmit(options);      		 		
      		 		
      		  	}else{      		  	
	      	      	var dialogFoo = $(this);
					
					$.getJSON(url,function(data){	
						if(data.response == 1){
							  $(dialogFoo).dialog("close");
							  $(dialogFoo).remove();	
							  window.location="/";					
						}else{	
							  $(dialogFoo).html(data.errorMessage);
						}
					});
					
				}
       		  
       		  }, 
       		 "Cancel": function() { 
      		  	$(".addImage").unbind("click");
          	  $(this).dialog("close"); 
          	  $(this).remove();
        	} 
  	  		}
  	  		
  	  		});
	
		$(".addImage").click(function(){
			$("#carouselImageForm").slideToggle();
		});

		}catch(e){
			alert(e.message);
		}

}


circle.removeNeighbor = function(duid,name,circleName){

	var dialogDiv = document.createElement("div");
		dialogDiv.id = "removeUserDialog";
		dialogDiv.className="flora";
		$(dialogDiv).html("Are you sure you want to remove "+name+" from "+circleName+"?");
		$("#bodyRight").append(dialogDiv);
		
		
	var waitDiv = document.createElement("div");
		waitDiv.id="removeWait";
		waitDiv.className = "flora";
		$(waitDiv).html("<div style='text-align: center'><img src='/images/v3/loading.gif'></div>");	
		
		
		var successDialog = document.createElement("div");
		successDialog.id = "successDialog";
		successDialog.className="flora";
		$(successDialog).html("User "+name+" has been removed from circle "+circleName+"?");

		
		$(dialogDiv).dialog({
			buttons: {
			"Ok":function(){
			$(dialogDiv).dialog("close");
			$(dialogDiv).remove();
			
			$("#bodyRight").append(waitDiv);
			
			$(waitDiv).dialog();	
			
			var listId = $("#postToList").attr("value");
			var url = "/api/circle.php?c=removeNeighbor&duid="+duid+"&listId="+listId;
			
			$.getJSON(url,function(data){	
				$(waitDiv).dialog("close");
				$(waitDiv).remove();		
				$("#bodyRight").append(successDialog);
				$(successDialog).dialog({
				buttons: {
				"Ok": function(){
					$(successDialog).dialog("close");
					$(successDialog).remove();
					var sURL = unescape(window.location.href);
								window.location.href=sURL;	
				}}});
				
			});
		},
		"Cancel": function(){
			$(dialogDiv).dialog("close");
			$(dialogDiv).remove();
			
		}
		}});
	
}


/*
	TODO NEEDED IN REDESIGN (X)
*/
circle.removeEditor = function(duid){

	var dialogDiv = document.createElement("div");
		dialogDiv.id = "dialog_"+duid;
		dialogDiv.className="flora";
		
		$(dialogDiv).html("Would you like to remove this user as a editor?");
		
		
		var cs = document.getElementById("circleStreamShow");

		cs.appendChild(dialogDiv);
		
		$("#dialog_"+duid).dialog({
				buttons: { 
					  "Ok": function() { 
						var dialogFoo = $(this);
							$(".ui-dialog-buttonpane button").hide();
							var listId = $("#postToList").attr("value");
	
							var url = "/api/circle.php?c=removeEditor&duid="+duid+"&listId="+listId;
	
							$.getJSON(url,function(data){
								
								$("#dialog_"+duid).text("This user has been removed from the editor list");
								setTimeout(function(){
								$(dialogFoo).dialog("close");
								$(dialogFoo).remove();
								}				
								,2000);
	
							});
			  
					   }, 
					 "Cancel": function() { 
						$(this).dialog("close"); 
						$(this).remove();
						} 
				}
  	  		});

		
		
/*
	var listId = $("#postToList").attr("value");
	
	var url = "/api/circle.php?c=makeEditor&duid="+duid+"&listId="+listId;
	
	$.getJSON(url,function(data){
	
		//alert("user has been removed");
	
	});

*/


}




/*
	TODO NEEDED IN REDESIGN (X)
*/
circle.makeEditor = function(duid){

	var dialogDiv = document.createElement("div");
		dialogDiv.id = "dialog_"+duid;
		dialogDiv.className="flora";
		
		$(dialogDiv).html("Would you like to make this user a circle editor?");
		
		
		var cs = document.getElementById("circleStreamShow");

		cs.appendChild(dialogDiv);
		
		$("#dialog_"+duid).dialog({
				buttons: { 
					  "Ok": function() { 
						var dialogFoo = $(this);
							$(".ui-dialog-buttonpane button").hide();
							var listId = $("#postToList").attr("value");
	
							var url = "/api/circle.php?c=makeEditor&duid="+duid+"&listId="+listId;
	
							$.getJSON(url,function(data){
								
								$("#dialog_"+duid).text("This user has been promoted");
								setTimeout(function(){
								$(dialogFoo).dialog("close");
								$(dialogFoo).remove();
								}				
								,2000);
	
							});
			  
					   }, 
					 "Cancel": function() { 
						$(this).dialog("close"); 
						$(this).remove();
						} 
				}
  	  		});

		
		
/*
	var listId = $("#postToList").attr("value");
	
	var url = "/api/circle.php?c=makeEditor&duid="+duid+"&listId="+listId;
	
	$.getJSON(url,function(data){
	
		//alert("user has been removed");
	
	});

*/


}

/*
	TODO NEEDED IN REDESIGN (X)
*/
circle.reblogStory = function(itemId,title){
	if (!circle.loggedin) {
		this.showLoginBox();
		return;
	}

	var dialogDiv = document.createElement("div");
		dialogDiv.id = "dialog_"+itemId;
		dialogDiv.className='flora';

		htmlStr = "<div id='dialogMessage' style='margin-bottom: 20px;'>Cool! You'd like to re-blog "+unescape(title)+" to another circle!</div>";

		htmlStr += "<div id='dialogWait' style='text-align: center;width: 300px; margin-bottom: 20px;'><img src='/images/v3/loading.gif' style='width:32px; height: 32px;'></div>";
		
		htmlStr += "<div id='dialogContent' style='display: none;width: 300px; margin-bottom: 20px;'>";

		htmlStr += "<div style='clear: both; float: left; width: 350px; margin-bottom: 10px;'>Pick which of your circles that this will be posted to. "+unescape(title)+"</div></div>";
		
		htmlStr += "<div style='clear: both; float: left; width: 450px; margin-bottom: 10px;'>Enter your comment<textarea id='reblogContent' style='width: 450px;'></textarea></div>";
		
		var dialogDivOk = document.createElement("div");
		dialogDivOk.id = "dialogOk_"+itemId;
		dialogDivOk.className='flora';
		
		
		htmlStr2 = "<div id='dialogMessage2' style='margin-bottom: 10px;'>"+unescape(title)+" has been reposted!</div>";
		//htmlStr += "<div id='dialogContent' style='width: 200px; margin-bottom: 10px;'></div>";
		$(dialogDivOk).html(htmlStr2);
		$(dialogDivOk).css("display","none");
		$(dialogDiv).html(htmlStr);
		url = "/api/circle.php?c=getListMember";
		
		var cs = document.getElementById("bodycontainer");
		cs.appendChild(dialogDiv);
		cs.appendChild(dialogDivOk);

		var selectObject = document.createElement("select");
			selectObject.id="pickCircle";
			$(selectObject).attr("name","pickCircle");						
		var dialogContent = document.getElementById("dialogContent");
			dialogContent.appendChild(selectObject);
			
		$("#dialog_"+itemId).dialog(
			{
			width: 480,
			height: 400,
			buttons: { 
      		  "Ok": function() { 
					var destinationId = $("#pickCircle").val();
					var reblogComment = $("#reblogContent").val();
  				var url = "/api/repostItem.php?sourceStory="+itemId+"&destinationList="+destinationId+"&comment="+escape(reblogComment);
  				
  				
    				$.getJSON(url,function(data){
						if(data.postId > 1){
							$("#dialog_"+itemId).dialog("close");
							$("#dialog_"+itemId).remove();
							$("#dialogOk_"+itemId).css("display","block");
							$("#dialogOk_"+itemId).dialog({width: 480,
										height : 500,
										buttons : { "Ok": function(){
											$("#dialogOk_"+itemId).dialog("close"); 
											$("#dialogOk_"+itemId).remove();
											$(".ui-dialog").remove();
											}
										}
									});
						}else{
							$("#dialogMessage").text("We're sorry, but we couldn't reblog this at this time.");
						}
						
					}); 


					},
				 "Cancel": function() { 
		          	  $(this).dialog("close"); 
        		  	  $(this).remove();
				}					
					
					}	      	

				});
	 $(".ui-dialog-buttonpane button").hide();
		$.getJSON(url,function(jsondata){
				$(jsondata.response).each(function(i,itemdata){
				
				var optionObj = document.createElement("option");
					$(optionObj).attr("value",itemdata.id);
					$(optionObj).text(itemdata.name);					
					selectObject.appendChild(optionObj);
				});
				
				$(".ui-dialog-buttonpane button").hide();
				$("#dialogWait").hide();
				$("#dialogContent").show();
				$(".ui-dialog-buttonpane button").show();
		});
};





circle.repostStory = function(itemId,title){
	var dialogDiv = document.createElement("div");
		dialogDiv.id = "dialog_"+itemId;
		dialogDiv.className="flora";
		htmlStr = "<div id='dialogMessage' style='margin-bottom: 10px;'>Cool! You'd like to re-post "+unescape(title)+" to another circle!</div>";

		htmlStr += "<div id='dialogWait' style='text-align: center;width: 300px; margin-bottom: 10px;'><img src='/images/v3/loading.gif' style='width:32px; height: 32px;'></div>";
		
		htmlStr += "<div id='dialogContent' style='display: none;width: 300px; margin-bottom: 10px;'>";

		htmlStr += "<div style='clear: both; float: left; width: 350px; margin-bottom: 10px;'>Pick which of your circles that this will be posted to. "+unescape(title)+"</div></div>";
		
		
		var dialogDivOk = document.createElement("div");
		dialogDivOk.id = "dialogOk_"+itemId;
		dialogDivOk.className="flora";
		
		htmlStr2 = "<div id='dialogMessage2' style='margin-bottom: 10px;'>"+unescape(title)+" has been reposted!</div>";
		//htmlStr += "<div id='dialogContent' style='width: 200px; margin-bottom: 10px;'></div>";
		$(dialogDivOk).html(htmlStr2);
		$(dialogDivOk).css("display","none");
		$(dialogDiv).html(htmlStr);
		url = "/api/circle.php?c=getListMember";
		
		var cs = document.getElementById("bodycontainer");
		cs.appendChild(dialogDiv);
		cs.appendChild(dialogDivOk);

		var selectObject = document.createElement("select");
			selectObject.id="pickCircle";
			$(selectObject).attr("name","pickCircle");						
		var dialogContent = document.getElementById("dialogContent");
			dialogContent.appendChild(selectObject);
			
		$("#dialog_"+itemId).dialog(
			{
			width: 480,
			height: 300,
			buttons: { 
      		  "Ok": function() { 
					var destinationId = $("#pickCircle").val();
  				var url = "/api/repostItem.php?sourceStory="+itemId+"&destinationList="+destinationId;
  				
  				
    				$.getJSON(url,function(data){
						if(data.postId > 1){
							$("#dialog_"+itemId).dialog("close");
							$("#dialog_"+itemId).remove();
							$("#dialogOk_"+itemId).css("display","block");
							$("#dialogOk_"+itemId).dialog({width: 480,
										height : 300,
										buttons : { "Ok": function(){
											$("#dialogOk_"+itemId).dialog("close"); 
											$("#dialogOk_"+itemId).remove();
											$(".ui-dialog").remove();
											}
										}
									});
						}else{
							$("#dialogMessage").text("We're sorry, but we couldn't repost this at this time.");
						}
						
					}); 


					},
				 "Cancel": function() { 
		          	  $(this).dialog("close"); 
        		  	  $(this).remove();
				}					
					
					}	      	

				});
	 $(".ui-dialog-buttonpane button").hide();
		$.getJSON(url,function(jsondata){
				$(jsondata.response).each(function(i,itemdata){
				
				var optionObj = document.createElement("option");
					$(optionObj).attr("value",itemdata.id);
					$(optionObj).text(itemdata.name);					
					selectObject.appendChild(optionObj);
				});
				
				$(".ui-dialog-buttonpane button").hide();
				$("#dialogWait").hide();
				$("#dialogContent").show();
				$(".ui-dialog-buttonpane button").show();
		});
};




/*
	TODO NEEDED IN REDESIGN (X)
*/
circle.deletePost = function(itemId){

	var url = "/api/itemAction.php?c=removeFromList&itemId="+itemId;
	
	var warningDiv = document.createElement("div");
		warningDiv.id = "delete_"+itemId;
		warningDiv.className="flora";		
		$(warningDiv).html("Are you sure you want to delete this?");

		var cs = document.getElementById("bodyRight");
		
		cs.appendChild(warningDiv);
		
		$("#delete_"+itemId).dialog(
			{
			
			buttons: { 
      		  "Ok": function() { 
      	      	
      	      	var dialogFoo = $(this);
            	$.getJSON(url,function(data){	
				var result = data.result;	
				if(result.response == 1){
					  $(dialogFoo).dialog("close");
		      	      $(dialogFoo).remove();
					$('#item_'+itemId).remove();
					
				}
			});
       		  
       		   }, 
       		 "Cancel": function() { 
          	  $(this).dialog("close"); 
          	  $(this).remove();
        	} 
  	  		}});	
}




circle.circleCarousel = function(id){

	var listId = $("#postToList").attr("value");
	var url = "/api/itemAction.php?c=circleCarousel&itemId="+id+"&listId="+listId;
	var itemId = id;
	
	var warningDiv = document.createElement("div");
		warningDiv.id = "promote_"+itemId;
		warningDiv.className="flora";
		$(warningDiv).html("Promoting this item will place it in your circle's carousel. Click ok to continue!");

		var cs = document.getElementById("circleStreamShow");
		
		cs.appendChild(warningDiv);
		
		$("#promote_"+itemId).dialog(
			{
			
			buttons: { 
      		  "Ok": function() { 

      	      	var dialogFoo = $(this);
            	
				$.getJSON(url,function(data){	
			
				if(data.response == 1){
					  $(dialogFoo).dialog("close");
		      	      $(dialogFoo).remove();
		      	      window.location = window.location;
		      	      						
				}else{

					  $(dialogFoo).html(data.errorMessage);
				}
			});
       		  
       		   }, 
       		 "Cancel": function() { 
          	  $(this).dialog("close"); 
          	  $(this).remove();
        	} 
  	  		}});



/* 
$.getJSON(url,function(data){
			if(data.response == 1){
				//Get the card for his item...
			
				

				var cardUrl = "/api/itemAction.php?c=getCard&itemId="+itemId;
				$.getJSON(cardUrl, function(data){
				
				if(data.result == 1){
					var card =  $(".card.default_temp").get(0);
					
					if(card != undefined){
						$(card).html("");
						$(card).append(data.cardHtml);
						card.className = "card";
					}		
				}	
				});
			}
	});

	*/
}

circle.removeCircleCarousel = function(id,title){

	var submitDialog = document.createElement("div");
		submitDialog.id = "dialog_"+id;
		
		submitDialog.className = "flora";
		
		$(submitDialog).html("<div style='text-align: center; margin-bottom: 10px;'>Are you sure that you want to remove "+ title+" from this circle's carousel?</div>");

		$("#bodyRight").append(submitDialog);

	var successDialog = document.createElement("div");
	$(successDialog).html("<div style='text-align: center; margin-bottom: 10px;'>"+title+" has been removed from this circles carousel.</div>");
		successDialog.className="flora";
	var errorDialog = document.createElement("div");
	$(errorDialog).html("<div style='text-align: center; margin-bottom: 10px;'>"+title+" could not be removed.</div>");
		errorDialog.className="flora";
	 $(submitDialog).dialog({"buttons":
	 	{"Ok": function(){
			var listId = $("#postToList").attr("value");
			var url = "/api/itemAction.php?c=removeCircleCarousel&itemId="+id+"&listId="+listId;
			var itemId = id;
			$.getJSON(url,function(data){
					if(data.response == 1){
						$(submitDialog).dialog("close");
						$(submitDialog).remove();
						$("#bodyRight").append(successDialog);
						$(successDialog).dialog({buttons : { "Ok":function(){
							$(successDialog).dialog("close");
							$(successDialog).remove();
							
							var sURL = unescape(window.location.href);
								window.location.href=sURL;							
						}}});
					}else{
					
						$(submitDialog).dialog("close");
						$(submitDialog).remove();
						$("#bodyRight").append(errorDialog);
						$(errorDialog).dialog({buttons : { "Ok":function(){
							$(errorDialog).dialog("close");
							$(errorDialog).remove();
						
						}}});
					}
			});	 		
	 	},
	 	"Cancel": function(){
	 		$(submitDialog).dialog("close");
	 		$(submitDialog).remove();
	 	} 
	 	}
	 	});
		

	



}

circle.showWordTag = function(itemId){
	if (circle.loggedin) {
		$("#addWordTag_"+itemId).slideToggle();
	} else {
		circle.showLoginBox();
	}
}

circle.addTag = function(itemId,edit){
	var wordTagValue = $("#addWordTag_tag_"+itemId).val();
	var inputItemId = itemId;
	var params = { cmd : "addWordTag", value : wordTagValue, sid : itemId }

	$("<div/>").addClass('flora').attr("id","waitingDialog").html("<div style='text-align: center'>Adding tags. <br/> <img src='/images/v3/loading.gif' /></div>").dialog();
	
	$.getJSON("/api/tagging.php",params, function(json){
		$("#waitingDialog").dialog("close");
		$("#waitingDialog").remove();
		var res = json.result;
		if(res.response == 1){
			
			
			var tagContainer = $("#tags_"+itemId);
			

			
			if(tagContainer.length == 0){
				$("<div/>").addClass("postedTagContainer").attr("id","tags_"+itemId).addClass("clearfix").html("Tags: ").insertAfter("#item_"+itemId+" .postedContentBodyWrap .postedContentText");	
			}
			
			var tagBits =  wordTagValue.split(",");
			
			$(tagBits).each(function(i,item){ 
   			   
   			   item = "<a href='/search.php?p=0&navFind_input="+item+"&type=tag'>"+item+"</a>";
				var totalPosts = $("#tags_"+itemId+" .posttag").length;				
				var comma = "";
				if(totalPosts > 0){
					comma = ", ";
				}
				
				if(edit == 1){
					$("<span/>").addClass("posttag").html(comma + item +" (<span class='rmtag'>x</span>)").appendTo("#tags_"+itemId);
				}else{
					$("<span/>").addClass("posttag").html(comma + item).appendTo("#tags_"+itemId);
				}
			});
			
		}	
	
		$(".rmtag").unbind("click");
		$(".rmtag").click(circle.removeTag);
		
		circle.showWordTag(itemId);		
		
	});
						
						
	
	
	
	

}

circle.filterCircle = function(){

		var circleId = $('#circleFilter').val();
		
		circle.showCircle(circleId,"circleStreamShow");

}


circle.getCircleItem = function(listId, circleStream,sid){		
		circle.storyShow = sid;
		circle.showCircle(listId,circleStream);
		circle.storyShow = undefined;
}



circle.circleCarouselAction = function(id){

}


circle.deleteSingleComment = function(commentId,comment){
	
	var dialogDiv = document.createElement("div");
		dialogDiv.id = "dialogDiv";
		dialogDiv.className="flora";
		$(dialogDiv).html("Are you sure that you want to delete comment:<p>"+unescape(comment)+"</p>");
		$("#bodyRight").append(dialogDiv);
			
		$(dialogDiv).dialog({
			buttons: {
				"Ok":function(){
					var url = "/api/itemAction.php?c=removeFromList&itemId="+commentId;
					
					$(dialogDiv).dialog("close");
					$(dialogDiv).remove();
					
					$.getJSON(url,function(data){
						var surl = unescape(window.location.href);
						window.location=surl;
					});
					

				},
				"Cancel":function(){
					$(dialogDiv).dialog("close");
					$(dialogDiv).remove();
				
				}
			
			}
		});
	


}



/*
	TODO NEEDED BY REDESIGN (X)
*/
circle.deleteComment = function(commentId,comment,listId,itemId){

	var dialogDiv = document.createElement("div");
		dialogDiv.id = "dialogDiv";
		dialogDiv.className="flora";
		$(dialogDiv).html("Are you sure that you want to delete comment:<p>"+unescape(comment)+"</p>");
		$("#bodyRight").append(dialogDiv);
			
		$(dialogDiv).dialog({
			buttons: {
				"Ok":function(){
					
					var url = "/api/itemAction.php?c=removeFromList&itemId="+commentId;
					
					$(dialogDiv).dialog("close");
					$(dialogDiv).remove();
					
					
					$.getJSON(url,function(data){
						getComments(listId, itemId);					
					});
					

				},
				"Cancel":function(){
					$(dialogDiv).dialog("close");
					$(dialogDiv).remove();
				
				}
			
			}
		});
	


}


/*
	TODO NEEDED MY REDESIGN (X)
*/
circle.showMosaic = function(listId, circleStream, userId){
	$('.actionInputBlock').hide();
	
	var url = "/api/getList.php";
	
	if(typeof listId != "undefined" && listId != ""){
		url += "?l="+listId;
	}
	url += "&filter=image&resizeImage=160x160";

	if(userId){
		url += "&userID="+userId;
	}
	
	$("#"+circleStream).html("<div style='text-align: center';margin-top: 30px;'><img src='/images/v3/loading.gif'></div>");

	$.getJSON(url, function(data) {
		$("#bottomPageBlock").hide();
		$("#pageBlock").hide();
	
		if (data.items.length === 0) {
			$('#circleStreamShow').text('Sorry, no images found!');
			return;
		}
		
		$('#circleStreamShow').text('YAY! ' + data.items.length + " photos found!");
		var imgWrap = $('<div/>').addClass('mosaic_wrap');
		
		var images = [], match, desc, content;
		
		for (var i=0; i<data.items.length; i++) {
			match = data.items[i].description.match(/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i);
			
			
			if(match != null && typeof match !="undefined"){
			if (match.length > 1) {
				imgWrap.append($('<a/>').addClass('mosaic_link').attr('href', '#mosaic_zoom_' + i).append($('<img/>').addClass('mosaic_img').attr('width', '160').attr('height', '160').attr('src', match[1])));
				// Get the original version of this image for proper zoom
				if (match[1].indexOf('_160x160.') != -1) {
					match[1] = match[1].replace('_160x160.', '.');
				}
				content = $('<div/>').attr('id','mosaic_zoom_' + i).addClass('mosaic_hide').append($('<img/>').attr('src', match[1]));

				desc = data.items[i].description.indexOf('<br/>');
				if (desc != -1) {
					desc = data.items[i].description.substr((desc + 5), (data.items[i].description.length - desc - 5));
					content.append($('<p/>').addClass('mosaic_p').attr('id', 'mosaic_zoom_' + i + '_caption').html(desc));
				}
				
				content.append($('<p/>').addClass('mosaic_pLast').html('<a href="/post.php?sid=' + data.items[i].id + '">Go to the post &raquo;</a>'));
				
				imgWrap.append(content);
			}
			}
		}
		
		$('#circleStreamShow').html('').append(imgWrap);
		
		if (jQuery.browser.msie && jQuery.browser.version == "6.0") {
			$('#circleStreamShow a.mosaic_link').click(function(event) {
				event.preventDefault();
				var el = event.target.parentNode.href;
				if ($(el).css('display') == 'none') {
					$('.mosaic_hide').css('display', 'none');
					$(el).css('display', 'block');
				} else {
					$('.mosaic_hide').css('display', 'none');
				}
			});
		} else {
			$('#circleStreamShow a.mosaic_link').fancyZoom({scaleImg: true, directory: '/images/v3/fancyzoom'});
		}

		circle.tabClick = true;		

	});
};



circle.generatePagination = function(listId,totalCount,currentPage,userOnlyStr){
var pageBlock = $('#pageBlock').get(0); 
var totalPages = Math.ceil(totalCount/25);
			if(pageBlock){
				$(pageBlock).show();
				pageBlock.innerHTML = "";
			}
if(pageBlock && totalPages > 1){
				
			pageBlock.innerHTML = "";
			
		var prevSpan = document.createElement("span");
		var firstSpan = document.createElement("span");				
			if(currentPage ==0){
			 	$(prevSpan).css('color','black');
				$(firstSpan).css("color","black");
				firstSpan.innerHTML="1";
			 	prevSpan.innerHTML="Prev";			 	
			}else{
				var prevLink = parseInt(currentPage) - 1;				
				var pageLink = document.createElement('a');
					pageLink.innerHTML = "Prev";
					pageLink.href = "javascript:circle.showCircle("+listId+", 'circleStreamShow',"+ prevLink+", undefined, undefined,"+userOnlyStr+");";
				//	pageLink.href = "javascript:circle.showCircle("+listId+", 'circleStreamShow',"+ prevLink+", undefined, undefined,"+userOnlyStr+");"

					prevSpan.appendChild(pageLink);	
				var firstLink = document.createElement('a');
					firstLink.innerHTML = '1';
					firstLink.href = "javascript:circle.showCircle("+listId+", 'circleStreamShow',"+ 0 +", undefined, undefined,"+userOnlyStr+");";
					//firstLink.href ="javascript:circle.showBlogTickerContents("+listId+", 'circleStreamShow',"+ 0 +", undefined, '"+category+"');"; 			
					firstSpan.appendChild(firstLink);				
			}

		firstSpan.style.marginLeft='4px';
		prevSpan.style.marginLeft='4px';
		pageBlock.appendChild(prevSpan);
		pageBlock.appendChild(firstSpan);
		var start = 1;
		var end = 6;				
			
		if(currentPage < 4 || totalPages <= 8){
			start =1;
			end = 6;
		}else{
			if(parseInt(currentPage) == (totalPages -1)){
				start = parseInt(currentPage) -3;
			}else{
				start = parseInt(currentPage) -2;
			}
			end = parseInt(currentPage) + 3;
		}
		
		if(currentPage >= 4 && totalPages > 8){		
			var ellipsesSpan = document.createElement("span");
			ellipsesSpan.style.marginLeft='4px';	
			if(start==2){
				var ellipsesSpanLink = document.createElement("a");
				ellipsesSpanLink.innerHTML='2';
				ellipsesSpanLink.href ="javascript:circle.showCircle("+listId+", 'circleStreamShow',"+ 1 +", undefined, undefined,"+userOnlyStr+");";			
				ellipsesSpan.appendChild(ellipsesSpanLink);
			}else{
				ellipsesSpan.innerHTML ='...';
			}
			
			pageBlock.appendChild(ellipsesSpan);
		}
	
		for(i = start; i < end && i <(totalPages-1); i++){
			var show = i+1;		
			var pageToShow = document.createElement("span");
				pageToShow.style.marginLeft='4px';
			if(i == currentPage){
				pageToShow.innerHTML=show;
			}else{
				pageToShowLink = document.createElement("a");
				pageToShowLink.innerHTML = show;
				pageToShowLink.href = "javascript:circle.showCircle("+listId+", 'circleStreamShow',"+ i +", undefined, undefined,"+userOnlyStr+");";			
				pageToShow.appendChild(pageToShowLink);		
			}		
			pageBlock.appendChild(pageToShow);
		}
			
		if(end < (totalPages - 1) && ( (totalPages - 1) > end) ){
				last = totalPages - 1;
				nextToLast = totalPages -2;
				var ellipsesSpan = document.createElement("span");
				ellipsesSpan.style.marginLeft='4px';	
				if(totalPages - 2 == end){
					var ellipsesSpanLink = document.createElement("a");
					ellipsesSpanLink.innerHTML=last;
					ellipsesSpanLink.href = "javascript:circle.showCircle("+listId+", 'circleStreamShow',"+ nextToLast +", undefined, undefined,"+userOnlyStr+");";			
					ellipsesSpan.appendChild(ellipsesSpanLink);			
				}else{
					ellipsesSpan.innerHTML ='...';
				}
				pageBlock.appendChild(ellipsesSpan);
		}
		
		var lastPage = document.createElement("span");
			lastPage.style.marginLeft='4px';
		if(currentPage == (totalPages - 1)){
			lastPage.innerHTML = totalPages;
		}else{
			last = totalPages - 1;
			var lastPageLink = document.createElement("a");
				lastPageLink.innerHTML = totalPages;
				lastPageLink.href="javascript:circle.showCircle("+listId+", 'circleStreamShow',"+ last +", undefined, undefined,"+userOnlyStr+");";			
			lastPage.appendChild(lastPageLink);		
		}
			pageBlock.appendChild(lastPage);	
		
		var nextPage = document.createElement('span');
			nextPage.style.marginLeft ='4px';	
			
		if(currentPage == (totalPages - 1) ){
			nextPage.innerHTML='Next';
		}else{
			var nextPageID = parseInt(currentPage)+1;
			nextPageLink = document.createElement("a");
			nextPageLink.innerHTML='Next';
			nextPageLink.href="javascript:circle.showCircle("+listId+", 'circleStreamShow',"+ nextPageID +", undefined, undefined,"+userOnlyStr+");";			
			nextPage.appendChild(nextPageLink);
		}
			
			pageBlock.appendChild(nextPage);
						
			}else{
			
				$("#pageBlock").css("display","none");
				$("#topPageBlock").hide();
			}



}



circle.showCircle = function(listId, circleStream, currentPage, filterType, tagValue, userOnly){
		$('.actionInputBlock').hide();		
		var filterListId = $("#filterList").val();
		//Get's Default feed if no parameters were specified.
		var url = "/api/getPosts.php";
		
		if(typeof currentPage == "undefined"){
			currentPage = 0;
		}
	
		
		
		var refVal = $("#refVal").val();
		
		if(typeof userOnly == "undefined"){
			//currentPage = 0;
			userOnlyStr ="undefined";
		}else{
			userOnlyStr  = userOnly;
		}
		
		if(listId != "" && listId != undefined ){
			url = url+"?l="+listId;
		}
		
		url = url+"&r="+refVal;
		
		var circleOnly = $("#circleOnly").val();
		
		if(typeof circleOnly !== "undefined" && circleOnly !=null && circleOnly != ""){
			url = url+"&circle=true";
		}
		
				
		if(filterListId == "All" || filterListId == "" || typeof filterListId ==='undefined'){
					//We want to show everything. do nothing
		}else{
				url = url+"&filterList="+filterListId;				
		}
		

		if(userOnly){
			url = url+"&userID="+userOnly;
		}
		
		if(filterType !="" && filterType != undefined){
			url = url+"&filter="+filterType;
		}
		
	/*
	if(listRestrict != "" && listRestrict != undefined){
			url = url+"&listRestrict="+listRestrict;
		}
*/
		
		if(circle.storyShow != undefined){
		
			url = url+"&sid="+circle.storyShow;
		}
		
		if(currentPage !="" && currentPage != undefined){
			url = url+"&page="+currentPage;
		}
		
		$("#"+circleStream).html("<div style='text-align: center';margin-top: 30px;'><img src='/images/v3/loading.gif'></div>");
	
		$("#pageBlock").html("");
		$("#bottomPageBlock").html("");
	

		circle.tabClick = false;	
		circle.scrollTo();
	
		$("#"+circleStream).load(url,function(){
			circle.tabClick = true;		
	
			circle.initControls();
		});
		
		
		
}

circle.scrollTo = function(){
	
	
	if(circle.firstLoad){
		circle.firstLoad = false;
	}else{

		var offset = $("#circleStreamShow").offset();
		$('html,body').animate({scrollTop: 0},1000);
		return false;
	}
};


/*
	TODO NEEDED BY REDESIGN (X)  --  Needs Redone...
*/
circle.initControls = function(){
	$(".rmtag").click(circle.removeTag);
	
	$(".postAddComment").unbind("click.circle");
	$(".postAddComment").bind("click.circle", function(){
		if (circle.loggedin) {
			var itemId = $(this).parent(".postedContentActionBlock").attr("id").split("_")[1];
			$("#addComment_input_"+itemId).slideToggle(1000);
		} else {
			circle.showLoginBox();
		}
		
		return true;
	});
	
	if (document.location.href.indexOf('post.php') != -1 && circle.loggedin === true) {
		$(".postAddComment").click();
	}
	
	$(".videoIcon").unbind();
	$(".videoIcon").click(function(){
		var animateParams = { width: "370px", height: "350px"};
		var theParent = $(this).parent(".postedContentVideoContainer");
		var theVideo = $(theParent).children(".videoShow");
		var theIcon = $(this);
		$(theParent).animate(animateParams,333,function(){
			$(theIcon).fadeOut(333,function(){
				$(theVideo).fadeIn(333);
			});	
		});
	});
	
	$(".imageIcon").unbind();
	$(".imageIcon").click(function(){
		var theParent = $(this).parent(".postedContentImageContainer");
		var theImageContainer = $(theParent).children(".imageShow");
		var theImage = $(theImageContainer).children("img");
		var theIcon = $(this);

    	var imageClone = $(theImageContainer).clone().get(0);
    	$(imageClone).css({"visibility":"hidden","position":"absolute","top":"-300"}).css("display","block").appendTo("body");
    
		var imgHeight = $(imageClone).children("img").height();

		imgHeight = imgHeight+"px";
		$(imageClone).remove();		
	
		var animateParams = {height: imgHeight};
	
		$(theParent).animate(animateParams,333,function(){
			$(theIcon).fadeOut(333,function(){
				$(theImageContainer).fadeIn(333);
			});
		});
	});
	
	$(".dropDownImg").unbind();
	$(".dropDownImg").click(function(){
		try{
			var itemId = $(this).attr("id").split("_")[1];
			var itemOffset = $(this).offset();
			var dropDownItemMenu = $("#dropDownItemMenu_"+itemId);
			var topPos = itemOffset.top + 17;
			var leftPos = itemOffset.left -79;
			$(dropDownItemMenu).css({"top":topPos+"px", "left": leftPos+"px"});
			if($(dropDownItemMenu).css("display") == "block"){
				$(dropDownItemMenu).fadeOut("fast");
			}else{
				$(".dropDownEditMenu").fadeOut("fast");
				$(dropDownItemMenu).fadeIn("fast");
				$(dropDownItemMenu).click(function(){ $(this).fadeOut();});
				$(dropDownItemMenu).bind("mouseleave",function(){
					$(this).fadeOut();
				});
			}
		}catch(e){}
	});
}


/*
	TODO NEEDED BY REDESIGN (X)
*/
circle.showLoginBox = function() {
	var dialogHeight = 222;
	if($.browser.msie){
		dialogHeight = 240;
	}
		$('#loginDialog').css('display', 'block').dialog({
			"modal" : true,
			"width" : 490,
			"height" : dialogHeight,
			"dialogClass" : "loginBox"
		});
	
}

fixImageWidths = function(){
	var max_width = 410;
	var selector = '.circleStreamBody img';	
	$('.circleStreamBody img').each(function(i, item){
		var width = $(item).width();
		var height = $(item).height();
	
		if (width > max_width) {
			//Set variables	for manipulation
			var ratio = (height / width );
			var new_width = max_width;
			var new_height = (new_width * ratio);
			
			//Shrink the image and add link to full-sized image
			$(item).height(new_height).width(new_width);
//			$(item).css("cursor","pointer");
	
/*
			$(item).click(function(){
				if($(item).hasClass("autoSizeImage")){
					$(item).height(new_height).width(new_width);
				
				} else {
					$(item).height("auto").width("auto");
				
				}
				$(item).toggleClass("autoSizeImage");
			});
*/
		} //ends if statement
	} //ends each function
	);
		
};



circle.submitCommentSingle = function(itemId){
	
	var commentTxt = $("#comment_text_"+itemId).val();
	var listId = $('#postToList').attr('value');	
	var ref = itemId;
	var title = "";
	
	var description = commentTxt;
	commentTxt = escape(commentTxt);
    var type = "text";
	
	var submitDialog = document.createElement("div");
		submitDialog.id = "submitDialog";
		submitDialog.className="flora";
		
		
		$(submitDialog).html("<div style='text-align: center; margin-bottom: 10px;'>Your comment is being posted</div><div style='text-align: center;'><img src='/images/v3/loading.gif' style='width:32px; height: 32px;'></div>");
	var bodyRight = document.getElementById("bodyRight");

	bodyRight.appendChild(submitDialog);
	 $(submitDialog).dialog({"buttons":{"Ok": function(){} }});
		$(".ui-dialog-buttonpane button").hide();

	
	
	$.getJSON("/api/postItem.php?list="+listId+"&title="+title+"&description="+commentTxt+"&type="+type+"&ref="+ref, function(data){	
		//circle.showCircle(listId,'circleStreamShow');		
		var sURL = unescape(window.location.href.replace("#comments",""));
			window.location.href=sURL;
			
			
		
	});	
			
	

}


/*
	TODO NEEDED BY REDESIGN (X)
*/
circle.submitComment = function(itemId,itemPheed,userDuid,userName,userImg){

	var commentTxt = $("#comment_text_"+itemId).val();
	 $("#comment_text_"+itemId).val("");
//	var listId = $('#postToList').attr('value');	
	var listId = itemPheed;
	var ref = itemId;
	var title = "";
	var description = commentTxt;
	commentTxt = commentTxt.replace("+", "%2B");
	commentTxt = escape(commentTxt);

    var type = "text";
	
	var submitDialog = document.createElement("div");
		submitDialog.id = "submitDialog";
		submitDialog.className="flora";
		$(submitDialog).html("<div style='text-align: center; margin-bottom: 10px;'>Your comment is being posted</div><div style='text-align: center;'><img src='/images/v3/loading.gif' style='width:32px; height: 32px;'></div>");

	var bodyRight = document.getElementById("bodyRight");
	bodyRight.appendChild(submitDialog);

	 $(submitDialog).dialog({"buttons":{"Ok": function(){} }});
		$(".ui-dialog-buttonpane button").hide();

	$.getJSON("/api/postItem.php?list="+listId+"&title="+title+"&description="+commentTxt+"&type="+type+"&ref="+ref, function(data){	
		
		if (typeof(chat) === "undefined") {
			//<div class="postedContentComment">" This is a test comment. " by <a href="/profile.php?u=296">sgtjody</a></div>
			var headShotString = "<div class='commentHeadIcon'><a href='/profile.php?u="+userDuid+"'><img width='16' class='postThumb' src='"+userImg+"'/></a></div>";
	
	
			var newComment = $("<div/>").addClass("postedContentComment").addClass('clearfix').html(headShotString+'<div class="commentContentBody"> " '+description+' "'+" by <a href='/profile.php?u="+userDuid+"'>"+userName+"</a></div>");
	
	
	
			var commentBlock = $("#item_"+itemId+" .postedContentBodyWrap .postedContentCommentBlock");
	
			if(commentBlock.length > 0){
			}else{
	
				commentBlock = 	$("<div/>").addClass("postedContentCommentBlock");
	
				$(commentBlock).insertBefore("#actions_"+itemId);
			}

			$(commentBlock).prepend(newComment);
	
			if ($("#item_"+itemId+" .postedContentBodyWrap .postedContentComments").length == 0) {
				$('<div/>').addClass('postedContentComments').html('<strong>Comments:</strong>').insertBefore(commentBlock);
			}
		}
		

		$(submitDialog).dialog("close");
		$(submitDialog).remove();
		if (document.location.href.indexOf('post.php') == -1) {
			$("#actions_"+itemId+" .postAddComment").click();
		}
	});	
			

	

}

circle.showComments = function(item,circleStreamContent){
			
			var listId = $('#postToList').attr('value');	

			//var circleStreamContent = document.getElementById("circleStreamContent_"+item.id);
				
			if(item.childrenCount  <=0 ){
			
			
				
			var commentBlock = document.createElement("div");
				commentBlock.className="commentWrap";
				commentBlock.id = "commentWrap_"+item.id;
	
				$(commentBlock).text("No Comments");
				
				circleStreamContent.appendChild(commentBlock);	
					
			
			}else{
			
				var params = { l: listId, sid : item.id , children: true};
				$.getJSON("/api/getList.php",params, function(json){
					var items = json.items;
					var count =0;
					var commentWrap = document.createElement("div");
					commentWrap.className = "commentWrap";
					commentWrap.id = "commentWrap_"+item.id;

					
					$(items).each(function(i,data){
						if(data.id !=item.id){
							var commentContent = document.createElement("div");
								commentContent.className = "commentContent";
								commentContent.id = "comment_"+data.id;
									
							if(count==0){
								var commentVerb = document.createElement("div");
									commentVerb.className = "commentHeading";
									$(commentVerb).html("<span style='font-weight: bold;'>COMMENTS</span> ( <a href='post.php?sid="+ item.id+"'>Read all "+item.childrenCount+" Comments</a> )");
								commentContent.appendChild(commentVerb);
							}
							
							count++;
	
							
							var headshot = document.createElement("div");
								headshot.className = "circleStreamHeadShot";
								headshot.id = "circleStreamHeadShot_"+data.duid+"_"+data.id;
							var headshotImg = document.createElement("img");
						
							if(data.headshotThumb == "" || data.headshotThumb == null){
								data.headshotThumb = "/images/MotherhoodAvatar.png";
							}
								headshotImg.src = data.headshotThumb;
								headshot.appendChild(headshotImg);
								commentContent.appendChild(headshot);
							
							var commentAuthor = document.createElement("div");
								$(commentAuthor).css({"margin-bottom": "5px"});
								$(commentAuthor).html("<a href='/profile.php?u="+data.duid+"'>"+data.authorDisplayName+"</a> says:");
								
								commentContent.appendChild(commentAuthor);
							
							var commentDate = document.createElement("div");
								var datestr =data.created.split(".")[0];
								var dateObj = new Date(getDateFromFormat(datestr,"yyyy-MM-dd HH:mm:ss"));
								var createdDate =  formatDate(dateObj,"MMM dd, yyyy");
								commentDate.className="commentDate";
								$(commentDate).text(createdDate);
								commentContent.appendChild(commentDate);
								
							var commentBody = document.createElement("div");
								commentBody.className = "commentBody";
								commentBody.id = "commentBody_"+data.id;
								$(commentBody).text(data.description);								
								commentContent.appendChild(commentBody);
								commentWrap.appendChild(commentContent);
						}
				});
				
						var commentVerb = document.createElement("div");
							commentVerb.className = "commentHeading";
							$(commentVerb).html("<a href='/post.php?sid="+item.id+"'>Read all "+item.childrenCount+" Comments</a>");
							commentWrap.appendChild(commentVerb);
						circleStreamContent.appendChild(commentWrap);

			});
	}
}





//*FOllow Functions */


follow = {};


follow.blockFollowing = function(follower,followed,followname,watchedname){

var url = "/api/circle.php?c=listUnfollow&follower="+follower+"&followed="+followed;
		

	var followDialog = document.createElement("div");
		followDialog.id = "followDialog";
		followDialog.className="flora";
		$(followDialog).html("Are you sure that you want to stop " + unescape(followname) + " from watching "+unescape(watchedname) + "?");
		
	var doneDialog = document.createElement("div");
		doneDialog.id="doneDialog";
		doneDialog.className="flora";
		$(doneDialog).html(unescape(followname) + " is no longer watching "+ unescape(watchedname) );
		$(doneDialog).css("display","none");
		
		var bc = document.getElementById('bodycontainer');
		bc.appendChild(followDialog);
		bc.appendChild(doneDialog);
		
		$("#followDialog").dialog({
			buttons:{
				"Ok" : function(){				
					$.getJSON(url,function(data){
						$("#followDialog").dialog("close");
						$("#followDialog").remove();
						
						$("#doneDialog").css("display","block");
						$("#doneDialog").dialog({ buttons: 
						{ "Ok": function(){ 
							$(this).dialog("close"); 
							$(this).remove(); 
							
					var sURL = unescape(window.location.href);
						window.location.href=sURL;
				
							
						 }}});
						
						
					});
				}
			}
		});
	
}


follow.unfollow = function(follower,followed,followname){

var url = "/api/circle.php?c=listUnfollow&follower="+follower+"&followed="+followed;
		

	var followDialog = document.createElement("div");
		followDialog.id = "followDialog";
		followDialog.className="flora";
		$(followDialog).html("Are you sure that you want to stop watching <b>" + unescape(followname) + "</b>?");
		
	var doneDialog = document.createElement("div");
		doneDialog.id="doneDialog";
		doneDialog.className="flora";
		$(doneDialog).html(unescape(followname) + " is no longer being watched");
		$(doneDialog).css("display","none");
		
		var bc = document.getElementById('bodycontainer');
		bc.appendChild(followDialog);
		bc.appendChild(doneDialog);
		
		$("#followDialog").dialog({
			buttons:{
				"Ok" : function(){				
					$.getJSON(url,function(data){
						$("#followDialog").dialog("close");
						$("#followDialog").remove();
						
						$("#doneDialog").css("display","block");
						$("#doneDialog").dialog({ buttons: 
						{ "Ok": function(){ 
							$(this).dialog("close"); 
							$(this).remove(); 
							
					var sURL = unescape(window.location.href);
						window.location.href=sURL;
				
							
						 }}});
						
						
					});
				}
			}
		});
	
}





follow.followList= function(followed,duid, title){

		//Follower Will be that users's default Pheed when it comes in
		//Followed will be the list id of the circle / blog to follow
		//DUID is the user's DUID
		//Title is the text of the thing to show.

		var dialogDiv = document.createElement("div");
		dialogDiv.id = "dialog_"+followed;
		dialogDiv.className="flora";
		htmlStr = "<div id='dialogMessage' style='margin-bottom: 10px;'>Cool. You want to watch "+unescape(title)+"!</div>";
		htmlStr += "<div id='dialogWait' style='text-align: center;width: 300px; margin-bottom: 10px;'><img src='/images/v3/loading.gif' style='width:32px; height: 32px;'></div>";
		
		htmlStr += "<div id='dialogContent' style='display: none;width: 300px; margin-bottom: 10px;'>";
		htmlStr += "<div style='clear: both; float: left; width: 350px; margin-bottom: 10px;'>Pick which of your circles will watch "+unescape(title)+"</div></div>";
		
		
		var dialogDivOk = document.createElement("div");
		dialogDivOk.id = "dialogOk_"+followed;
		dialogDivOk.className="flora";
		htmlStr2 = "<div id='dialogMessage2' style='margin-bottom: 10px;'>You are now watching "+unescape(title)+"</div>";
		//htmlStr += "<div id='dialogContent' style='width: 200px; margin-bottom: 10px;'></div>";
		$(dialogDivOk).html(htmlStr2);
		$(dialogDivOk).css("display","none");
		



		$(dialogDiv).html(htmlStr);
		url = "/api/circle.php?c=getEditLists";
		
		var cs = document.getElementById("bodycontainer");
		cs.appendChild(dialogDiv);
		cs.appendChild(dialogDivOk);

		var selectObject = document.createElement("select");
			selectObject.id="pickCircle";
			$(selectObject).attr("name","pickCircle");
			/*
				var optionObj = document.createElement("option");
					$(optionObj).attr("value",follower);
					$(optionObj).text("My Home");					
					selectObject.appendChild(optionObj);	
*/							
		var dialogContent = document.getElementById("dialogContent");
			dialogContent.appendChild(selectObject);
			
		$("#dialog_"+followed).dialog(
			{
			width: 480,
			height: 300,
			buttons: { 
      		  "Ok": function() { 
					var followerId = $("#pickCircle").val();
					
					
  				var url = "/api/circle.php?c=listFollow&follower="+followerId+"&followed="+followed+"&duid="+duid;
    				$.getJSON(url,function(data){
						if(data.response.response == 1){
						
							$("#dialog_"+followed).dialog("close");
							$("#dialog_"+followed).remove();
							
							$("#dialogOk_"+followed).css("display","block");
							
							$("#dialogOk_"+followed).dialog({width: 480,
										height : 300,
										buttons : { "Ok": function(){$("#dialogOk_"+followed).dialog("close"); $("#dialogOk_"+followed).remove();}
										}
									});
										
						
							//$(".ui-dialog-buttonpane button").hide();
						
						
						
						}else if(data.response.response =-1){
							$("#dialogMessage").text("This Stream is " +data.response.errorMessage);
							
						}
						
					}); 

					},
				 "Cancel": function() { 
		          	  $(this).dialog("close"); 
        		  	  $(this).remove();
				}					
					
					}	      	

				});
	
			
			
			
			
			
	 $(".ui-dialog-buttonpane button").hide();
			
			
			
		$.getJSON(url,function(jsondata){
				$(jsondata.response).each(function(i,itemdata){
				
				var optionObj = document.createElement("option");
					$(optionObj).attr("value",itemdata.id);
					$(optionObj).text(itemdata.name);					
					selectObject.appendChild(optionObj);
				});
				
				$(".ui-dialog-buttonpane button").hide();
				$("#dialogWait").hide();
				$("#dialogContent").show();
				$(".ui-dialog-buttonpane button").show();

				
				
		});
}


/*
	TODO POSSIBLY NEEDED BY REDESIGN
*/
circle.removeTag = function(e){
	
	var foo = $(this).get(0);
	var closeImageId  = $(foo).attr("id");
	
	var idTag = closeImageId.substr(closeImageId.indexOf("_")+1);
	var bits = idTag.split("_");
	var itemId = bits[0];
	var tagValue = idTag.replace(itemId+"_","");

	

	var params = { cmd : "removeWordTag", value : unescape(tagValue), sid : itemId }
	$.getJSON("/api/tagging.php",params, function(json){
	
	var res = json.response;
		if(res == 1){								
			
			
			$("#adminTag_"+itemId+"_"+tagValue).parent(".posttag").remove();
	
	
		}
		
		});
	//rmtag_80178_Super
	
	   // rmtag_80178
	
	
	
		
}


/*
	TODO NEEDED BY REDESIGN
*/
circle.cancelComment = function(itemId){
		$("#comment_text_"+itemId).val("");
		$("#actions_"+itemId+" .postAddComment").click();

}

function getComments(listId, itemId){
			
				var circleStreamContent = $("#circleStreamItem_"+itemId).get(0);
				var currentComments = $("#commentWrap_"+itemId);
				if(typeof currentComments != "undefined"){
					currentComments.slideUp("fast")
					currentComments.remove();
				
				}

				
				var params = { l: listId, sid : itemId , children: true};
				
				$.getJSON("/api/getList.php",params, function(json){
					var items = json.items;
					var count =0;
					var commentWrap = document.createElement("div");
					commentWrap.className = "commentWrap";
					commentWrap.id = "commentWrap_"+itemId;
					
					var siteAdmin = json.siteAdmin;
					var editor = json.editor;
					var myDuid = json.myduid;
					
					if(siteAdmin > 0){
						editor = 1;
					}
					
					$(items).each(function(i,data){
						if(data.id !=itemId){
							var commentContent = document.createElement("div");
								commentContent.className = "commentContent";
								commentContent.id = "comment_"+data.id;
									
							if(count==0){
								var commentVerb = document.createElement("div");
									commentVerb.className = "commentHeading";
									$(commentVerb).html("<span style='font-weight: bold;'>COMMENTS</span> ( <a href='post.php?sid="+ itemId+"'>Read all "+items.length+" Comments</a> )");
								commentContent.appendChild(commentVerb);
							}
							
							count++;
	
							
							var headshot = document.createElement("div");
								headshot.className = "circleStreamHeadShot";
								headshot.id = "circleStreamHeadShot_"+data.duid+"_"+data.id;
							var headshotImg = document.createElement("img");
						
							if(data.headshotThumb == "" || data.headshotThumb == null){
								data.headshotThumb = "/images/MotherhoodAvatar.png";
							}
								
							var	headshotLink = document.createElement("a");
								headshotLink.href="/profile.php?u="+data.duid;
								
								
								headshotImg.src = data.headshotThumb;
								headshotLink.appendChild(headshotImg);
								headshot.appendChild(headshotLink);
								
								
								commentContent.appendChild(headshot);
							
							var commentAuthor = document.createElement("div");
								$(commentAuthor).css({"margin-bottom": "5px"});
								$(commentAuthor).html("<a href='/profile.php?u="+data.duid+"'>"+data.authorDisplayName+"</a> says:");
								
								commentContent.appendChild(commentAuthor);
							
							var commentDate = document.createElement("div");
								var datestr =data.created.split(".")[0];
								var dateObj = new Date(getDateFromFormat(datestr,"yyyy-MM-dd HH:mm:ss"));
								var createdDate =  formatDate(dateObj,"MMM dd, yyyy");
								commentDate.className="commentDate";
								$(commentDate).text(createdDate);
								commentContent.appendChild(commentDate);
								
							var commentBody = document.createElement("div");
								commentBody.className = "commentBody";
								commentBody.id = "commentBody_"+data.id;
								$(commentBody).text(data.description);								
								commentContent.appendChild(commentBody);
								
					//SPS -- DO STUFF HERE FOR DELETE COMMENT		
							
							
								if(editor > 0 || data.duid == myDuid){
								
									var deleteComment = document.createElement("div");
										deleteComment.className = "delComment";
									deleteComment.id = "delComment_"+data.id;
					$(deleteComment).html("<A id='deleteCommentClick_"+data.id+"' onclick='javascript:circle.deleteComment("+data.id+",\""+escape(data.description)+"\","+listId+","+itemId+");'>Delete Comment</a>");							
					commentContent.appendChild(deleteComment);
								
								}
								
								commentWrap.appendChild(commentContent);
						}
				});
				
						var commentVerb = document.createElement("div");
							commentVerb.className = "commentHeading";
							$(commentVerb).html("<a href='/post.php?sid="+itemId+"'>Read all "+items.length+" Comments</a>");
							commentWrap.appendChild(commentVerb);
						circleStreamContent.appendChild(commentWrap);
										$(commentWrap).slideDown("fast")


			});
	}




