$(document).ready(function() {	
	

						$("#discusstoggle").bind("click",
												  		function()
														{
															$(this).stop(true,false);
															$("div#member-comments").slideToggle('medium', function() { });
															$("div#member-share").slideUp('medium', function() {  });
															this.blur();
															return false;
														}
												  );
	

						$("#sharetoggle").bind("click",
												  		function()
														{
															$(this).stop(true,false);
															$("div#member-share").slideToggle('medium', function() { });
															$("div#member-comments").slideUp('medium', function() {  });
															this.blur();
															return false;
														}
												  );
						
						

						
						$("a.closeup").bind("click",
												  		function()
														{
															$("div#member-comments").slideUp('medium', function() {  });
															$("div#member-share").slideUp('medium', function() {  });
															this.blur();
															return false;
														}
												  );
						$("div#member-comments").hide();
						$("div#member-share").hide();
						
						// share form
						$("div#member-share form").bind("submit",
														function()
														{
															var body = "From ";
															var recipient = $("input#ffriendaddress").attr('value');
															var message = $("#femailmessage").attr('value');
															var URL = $("#shareurl").attr('value');
															
															body += $("input#fyourname").attr('value');
															body += " - Check this out! "+URL+" ";
															body += message;
															
															window.location = "mailto:"+recipient+"?subject=Little Weeds&body="+body;
															return false;
														}
													)
						
						
						
						/* RANKING */
						
						$('.stabUp').click(function(e){
							
							e.preventDefault();
							ArticleID = $(this).parent().find("input.id").val();
							
							var data = "ArticleID=" + ArticleID + "&value=1";  
							t = $(this);
							$.ajax({
							  type: "POST",
							  url: "/littleweeds/profile/articlerank",
							  data: data,
							  cache: false,
							  success: function(score)
							  {
							  	if(score.indexOf('error') != -1)
							  	{
							  		alert('You have already voted on this item');
							  	} else
							  	{
							  		t.parent().find("p.votes").html(score);
							        //console.log( "Rank retrieved: " + score );
							    }
							  }
							});
						});
						
						$('.stabDown').click(function(e){
							
							e.preventDefault();
							ArticleID = $(this).parent().find("input.id").val();
							
							var data = "ArticleID=" + ArticleID + "&value=-1";  
							t = $(this);
							
							$.ajax({
							  type: "POST",
							  url: "/littleweeds/profile/articlerank",
							  data: data,
							  cache: false,
							  success: function(score)
							  {
							  	if(score.indexOf('error') != -1)
							  	{
							  		alert('You have already voted on this item');
							  	} else
							  	{
							  		t.parent().find("p.votes").html(score);
							        //console.log( "Rank retrieved: " + score );
							    }
							  }
							});
						});
						
						/* COMMENTS */
						var curPage = 1;
						
						function reloadComments(page)
						{
								 ArticleID = $("#body").find("input.id").val();

								 var data = "ArticleID=" + ArticleID + "&page=" + page;  

					 			 $.ajax({
									   type: "GET",
									   url: "/littleweeds/profile/comments",
									   data: data ,
									   success: function(html){
								           //console.log( "Comment Reloaded: " + html );
								           
								           $("ul.comments").html(html);
								           $("#comment").val('');
								           
								           if($('#commentsTotalPages').val() > 1)
								           {
									           if(curPage == 1)
									           {
									           		$('.commentsPagerPrev').hide();
									           		if($('#commentsTotalPages').val() > 1)
									           		{
									           			$('.commentsPagerNext').show();
									           		}
									           }
									           else
									           {
									           		if(curPage == $('#commentsTotalPages').val())
									           		{
									           			$('.commentsPagerNext').hide();
									           			$('.commentsPagerPrev').show();
									           		}
									           		else
									           		{
									           			$('.commentsPagerNext').show();
									           			$('.commentsPagerPrev').show();
									           		}
									           }
								           }
								           
							       	   }
							 	});
						
						}
						
						$('#commentSubmit').click(function(e){
							
							e.preventDefault();
							if($("#comment").val() != '') {
								
								//post request
								
								 ArticleID = $("#body").find("input.id").val();

								 var data = "ArticleID=" + ArticleID + "&CommentBody=" + escape($("#comment").val());  

					 			 $.ajax({
									   type: "POST",
									   url: "/littleweeds/profile/comments",
									   data: data ,
									   success: function(msg){
								           //console.log( "Comment Posted: " + msg );
								           
								           //reload comments & go to first page
								           reloadComments(1);
							       	   }
							 	});
							 	$("#comment").val('');

							}
							else
							{
								//flash red
								//console.log( "Empty Comment");

							}
							
							return 0;
							
						});
						
						$('.commentsPager').click(function(e){
							
							e.preventDefault();
							
							page = ($(this).attr('title')).split(' ');
							page = page[1];
							
							curPage = page;
							
							reloadComments(page);

						});
						
						$('.commentsPagerNext').click(function(e){
							
							e.preventDefault();
							
							curPage++;
							
							reloadComments(curPage);
							
						
						});
						$('.commentsPagerPrev').click(function(e){
							
							e.preventDefault();
							
							curPage--;
							
							reloadComments(curPage);
							
						});
						
					

});