$(function (){
	var mouse_is_inside;
	
	$(document).ajaxStart(function(){
		$('#loader').show();
	}).ajaxStop(function(){
		$('#loader').hide();
	});
	
	//post comment
	function postComment(feedid,comment,div){
		$.ajax({
			type: "get",
			url: "ajax/useractions.php",
			data: "action=postcomment&feedid="+feedid+"&comment="+comment,
			success: function(data){
				div.append(data);
			}
		});
	}
	
	//post like
	function postLike(postid){
	
		$.ajax({
			type: "get",
			url: "ajax/useractions.php",
			data: "action=postlike&postid="+postid,
			success: function(data){
				$('#status').html(data);
				alert(data);
			}
		});
	}
	
	//become a fan
	function becomeFan(rid){
		$.ajax({
			type: "get",
			url: "ajax/useractions.php",
			data: "action=becomefan&rid="+rid,
			success: function(data){
				$('#fantxt').html("You are a fan<p>"+data+"</p>");
			}
		});
	}
	
	
	//********Buttons****************//
	//Post Comment
	$(".comment_txt").click(function(){
		$(this).addClass("write");
		$(this).parent().parent().find('.postcomment').show();
	});
	$('.commentsdiv').hover(function(){ 
        mouse_is_inside=true; 
    }, function(){ 
        mouse_is_inside=false; 
    });

    $('#wrapper').mouseup(function(){ 
        if(! mouse_is_inside){
			$('.postcomment').hide();
			$(".comment_txt").removeClass("write")
		}
    });

	$(".postcomment").click(function(){
		var comment=$(this).parent().find('.comment_txt').val();
		var feedid=$(this).parent().find('.feedid').html();
		var div=$(this).parent().find('.comments');		
		postComment(feedid,comment,div);
		$(this).parent().find('.comment_txt').val('');
		
		return false;
	});
	
	//Like a post
	$(".postlike").click(function(){		
		var postid=$(this).parent().parent().find('.postid').html();		
		postLike(postid);
		return false;
	});
	
	//Like a post
	$(".post_deal_like").click(function(){	
		var likediv=$(this).parent();
		var dealid=likediv.find('.dealid').html();		
		$.ajax({
			type: "get",
			url: "ajax/useractions.php",
			data: "action=deal_like&dealid="+dealid,
			success: function(data){
				//$('#status').html(data).fadeOut(9000);
				likediv.html("<img src='images/thumb-up.png'/> You like this deal");
			}
		});
		
		return false;
	});
	
	//Become fan of an Eatout
	$(".bfan").click(function(){		
		var rid=$(this).parent().find('.rid').html();
		alert(rid);
		becomeFan(rid);
		return false;
	});
	
	//Follow a flogger
	$(".flogfollow").click(function(){		
		alert('hello');
		//var uid=$(this).parent().find('.uid').html();
		//alert(uid);
		return false;
	});
	
	
	//Post a Flog
	$(".postflog").unbind("click").click(function(){
		//alert('Clicked');
		//form values
		var rid=$("#rid").val();
		var title=$("#title").val();
		var comment=$("#comment").val();
		var f=$("#food").val();
		var a=$("#ambience").val();
		var s=$("#service").val();
		var ate=$("#ate").val();
		var visit=$("#visit").val();
		
		if(rid=='' || comment=='' || f=='' || a=='' || s=='' ){
			alert('Please fill in all required fields');
			return false;
		}else{
			//post data
			$.ajax({
				type: "post",
				url: "post/postflog.php",
				data: "rid="+rid+"&title="+title+"&comment="+comment+"&food="+f+"&ambience="+a+"&service="+s+"&ate="+ate+"&visit="+visit,
				success: function(data){
					
					 //$("#status").html(data).fadeOut(9000);
					 var reviewdata = data.split('|');
					 $("#reviews").prepend('<div style="padding-right: 4px; padding-top: 15px; float: left; width: 110px; height: 110px;"><a href="flog.php?uid='+reviewdata[1]+'"><img src="http://user.tossdown.com/user/avatars/'+reviewdata[11]+'" align="left"></a><div class="clear"></div><p>By:<a href="flog.php?uid='+reviewdata[1]+'">'+reviewdata[2]+'</a></p><div class="clear"></div><a href="flog.php?uid='+reviewdata[1]+'" target="_blank"><span style="font-size: 10px;">'+reviewdata[10]+' more Post(s)</span></a><div class="clear"></div><span id="flogfollow1" style="font-size: 10px;"></span></div><!--User Reviews--><div id="reviewpost"><div class="reviewtitle"><h2><a style="font-weight: bold;" href="review.php?id=13"><img src="images/quotes.png" align="left">'+reviewdata[3]+'</a></h2><p class="reviewpostdate"><span style="color: rgb(204, 204, 204);">'+reviewdata[8]+'</span></p><br></div><div class="entry"><div style="width: 525px;"><div style="float: left;"><div style="float: none; background: url(&quot;http://tossdown.com/images/revratingbg2.png&quot;) no-repeat scroll 0% 0% transparent; height: 77px; width: 76px; margin: 0pt 7px 0pt 0pt;"><div style="font-size: 11px; line-height: 170%; padding: 2px; color: rgb(151, 190, 21);"><b style="font-size: 23px; font-weight: normal; height: 20px; width: 76px; float: left; text-align: center;">'+reviewdata[9]+'</b><br><b>Food:</b> '+reviewdata[5]+'<br><b>Ambience:</b> '+reviewdata[6]+'<br><b>Service:</b> '+reviewdata[7]+'<br></div></div></div><div style="color: rgb(67, 67, 67); font-size: 1em;">'+reviewdata[4]+'</div></div><div class="clear"></div><span id="postoptions13"><a href="#" class="postlike">Like</a>&nbsp;•&nbsp;<a href="#postcomments13" title="Read Comments">Comment</a></span><div class="clear"></div><div id="commentsdiv13" style="background-color: rgb(243, 243, 243); width: 400px; float: left; padding: 5px;"><span class="postid">13</span><div class="commbox"><label>Comment</label><input class="comment_txt" name="comment" style="width: 400px; height: 30px;" type="text"><button style="margin: 2px 0pt 0pt 274px;" class="postcomment">Comment</button></div></div></div></div><div class="clear"></div>');
					 $('.tab').eq(4).click();
					 $("#postflogform").hide();
					$("#status").html("<p class='response'>Thank you for Reviewing Us! Please read it <a href='eoreviews.php?rid="+rid+"'>here</a>.</p>");
				}
			});
		}
		
		return false;
	});
	
	
});