;jQuery.fn.editPlaceToEat = function(opt){
var $input = $('div.cabinet input',$(this));
var $name = $('h3',$(this)); var original_name = $name.html();
var $location = $('p.location a',$(this));var original_location = $location.html();
var $description = $('p.desc',$(this));var original_description = $description.html();
var $address = $('ul.address',$(this));var original_address = $address.html();
var $comments_anchor = $('a.comments',this); var $comments = $(this).next();
var $delete = $('a.delete',this);
var place = $(this).attr('place');
var $self = $(this);
if(opt.can_edit)
{

//<<<<<<<<NAME
$name.after('<input type="text" class="name" /><span class="inline-edit name"><a class="save">Save</a>  <a class="cancel">Cancel</a></span>');
var $input_name = $('input.name',$name.parent()); var $save_name = $('span.name a.save',$name.parent());var $cancel_edit_name = $('span.name a.cancel',$name.parent()); 
$cancel_edit_name.hide(); $save_name.hide(); $input_name.hide();
if($name.hasClass("default"))
	{$name.html('Click to add a title ...'); original_name = $name.html();}
$name.click(function(){
	$cancel_edit_name.show();$save_name.show();
	$(this).hide();
	if(!$(this).hasClass("default"))
		$input_name.val(original_name);
	$input_name.show();
});
$save_name.click(function(){
	$.post(opt.save_url,{place: place,camp: 'name', value: $input_name.val()},function(resp){
		if(resp.status == 'error')
		{
			$cancel_edit_name.hide();$save_name.hide();$name.html(original_name);//$input.remove();
		}
		if(resp.status == 'success')
		{
			$cancel_edit_name.hide();$save_name.hide();$name.html(resp.value);original_name = resp.value; $name.removeClass('default');
		}
		$input_name.hide(); $name.show();
	},'json');
});
$cancel_edit_name.click(function(){
	$input_name.hide();$name.html(original_name);$name.show();$cancel_edit_name.hide();$save_name.hide();
});
//<<<<<<<<<<<<< LOCATION
$location.parent().after('<input type="text" class="location" /><span class="inline-edit location"><a class="save">Save</a>  <a class="cancel">Cancel</a></span>');
var $save_location = $('a.save',$location.parent().next().next());var $cancel_edit_location = $('a.cancel',$location.parent().next().next()); var $input_location = $('input.location',$location.parent().parent());
$input_location.autocomplete({ url: opt.city_url });
$cancel_edit_location.hide();$save_location.hide();$input_location.hide();
if($location.hasClass("default"))
	{$location.html('Click to add the location ...'); original_location = $location.html();}
$location.parent().click(function(){
	$cancel_edit_location.show();$save_location.show();
	$location.parent().hide();
	if(!$location.hasClass("default"))
		$input_location.val(original_location);
	$input_location.show();
});
$save_location.click(function(){
	$.post(opt.save_url,{place: place,camp: 'location', value: $input_location.val()},function(resp){
		if(resp.status == 'error')
			$location.html(original_location);//$input.remove();
		if(resp.status == 'success')
		{
			$location.html(resp.value);original_location = resp.value; $location.removeClass('default');
		}
		$cancel_edit_location.hide();$save_location.hide();$input_location.hide(); $location.parent().show();
	},'json');
});
$cancel_edit_location.click(function(){
	$input_location.hide();$location.html(original_location);$location.parent().show();$cancel_edit_location.hide();$save_location.hide();
});
//<<<<<<<<<<<< DESCRIPTION
$description.after('<textarea rows="2" class="description"></textarea><span class="inline-edit description"><a class="save">Save</a> <a class="cancel">Cancel</a></span>');
var $input_description = $description.next(); var $save_description = $('a.save',$input_description.next());var $cancel_edit_description = $('a.cancel',$input_description.next()); 
$cancel_edit_description.hide();$save_description.hide();$input_description.hide();
if($description.hasClass("default"))
	{$description.html('Click to add the description ...'); original_description = $description.html();}
$description.click(function(){
	$cancel_edit_description.show();$save_description.show();
	$(this).hide();
	if(!$(this).hasClass("default"))
		$input_description.val(original_description);
	$input_description.show();
});
$save_description.click(function(){
	$.post(opt.save_url,{place: place,camp: 'description', value: $input_description.val()},function(resp){
		if(resp.status == 'error')
			$description.html(original_description);//$input.remove();
		if(resp.status == 'success')
		{
			$description.html(resp.value);original_description = resp.value; $description.removeClass('default');
		}
		$cancel_edit_description.hide();$save_description.hide();$input_description.hide(); $description.show();
	},'json');
});
$cancel_edit_description.click(function(){
	$input_description.hide();$description.html(original_description);$description.show();$cancel_edit_description.hide();$save_description.hide();
});
//<<<<<<<<<<<< ADDRESS
$address.after('<textarea rows="2" class="address"></textarea><span class="inline-edit address"><a class="save">Save</a> <a class="cancel">Cancel</a></span>');
var $input_address = $address.next() ;var $save_address = $('a.save',$input_address.next());var $cancel_edit_address = $('a.cancel',$input_address.next());
if($address.hasClass("default"))
	{$address.html('Click to add the address ...'); original_address = $address.html();}
$save_address.hide();$cancel_edit_address.hide();$input_address.hide();
$address.click(function(){
	$cancel_edit_address.show();$save_address.show();
	$(this).hide();
	if(!$(this).hasClass("default"))
		$input_address.val(original_address);
	$input_address.show();
});
$save_address.click(function(){
	$.post(opt.save_url,{place: place,camp: 'address', value: $input_address.val()},function(resp){
		if(resp.status == 'error')
			$address.html(original_address);//$input.remove();
		if(resp.status == 'success')
		{
			$address.html(resp.value);original_address = resp.value; $address.removeClass('default');
		}
		$cancel_edit_address.hide();$save_address.hide();$input_address.hide(); $address.show();
	},'json');
});
$cancel_edit_address.click(function(){
	$input_address.hide();$address.html(original_address);$address.show();$cancel_edit_address.hide();$save_address.hide();
});
//<<<<<<<<<<< PHOTO
$input.after('<iframe name="'+opt.type+'_'+place+'" style="display:none"></iframe>');
$iframe = $input.next();
$input.change(function(){
	$(this.form).attr('target',opt.type+'_'+place);
	$(this.form).attr('action',opt._change_photo_url+'?place='+place);
	$(this.form).submit();
	$(this).val('');
})
$iframe.load(function(){
	var src = $('img',$(this.contentWindow.document.body)).attr('src');
	if(src != '')
		$('img',$input.parent()).attr('src',src);
});
//<<<<<<<<<< DELETE PLACE
$delete.click(function(){
	$.post(opt.delete_url,{place: place},function(resp){
		if(resp.status == 'success')
			$self.next().remove();
			$self.remove();
	},'json');
});
}
// <<<<<<< COMMENTS
$comments_anchor.click(function(){
	this.stare = !this.stare; // true = open. initial true
	if(this.stare)
		$comments.show();
	else
		$comments.hide();
});
var $comment_input = $('input.comment',$comments); var $comment_add = $('a.comment',$comments);
$comment_add.click(function(){
	//this.can_click = !this.can_click;
	if($comment_input.val() == '') 
		return;
	$.post(opt.add_comment_url,{place: place, text: $comment_input.val()},function(resp){
		if(resp.status == 'error' || resp.status == 'invalid')
			{return}
		else
		{
			var comments_no = resp.comments_no;
			$.post(opt.get_comments_url,{place: place},function(resp){
				$('li',$comments).remove();
				$comments.prepend(resp);
				$comments_anchor.html('Comments &nbsp('+comments_no+')');
			},'html');
		}
		$comment_input.val('');
	},'json');
});
//<<<<<<<<< VOTE
var $vote_1 = $('ul.opinion li a.like',this); 
var $vote_2 = $('ul.opinion li a.dislike',this);var can_vote = true;
var $p1 = $('ul.opinion p:first',this);var $p2 = $('ul.opinion p:last',this);
$vote_1.click(function(){
	if(!can_vote) return;
	var vote = $(this).attr('vote');
	$.post(opt.vote_url,{place: place, vote: vote},function(resp){
		if(resp.status == 'success')
		{
			if(vote == 0)
			{
				$vote_1.attr('vote','1');$vote_1.html('Like');
				$vote_2.attr('vote','-1');$vote_2.html('Don\'t like');
			}
			if(vote == 1)
			{
				$vote_1.attr('vote','0');$vote_1.html('Unvote');
				$vote_2.attr('vote','-1');$vote_2.html('Don\'t like');
			}
			$p1.html(resp.votes_like);
			$p2.html(resp.votes_dislike);
		}
		can_vote = true;
	},'json');
});
$vote_2.click(function(){
	if(!can_vote) return;
	var vote = $(this).attr('vote');
	$.post(opt.vote_url,{place: place, vote: vote},function(resp){
		if(resp.status == 'success')
		{
			if(vote == 0)
			{
				$vote_1.attr('vote','1');$vote_1.html('Like');
				$vote_2.attr('vote','-1');$vote_2.html('Don\'t like');
			}
			if(vote == -1)
			{
				$vote_1.attr('vote','1');$vote_1.html('Like');
				$vote_2.attr('vote','0');$vote_2.html('Unvote');
			}
			$p1.html(resp.votes_like);
			$p2.html(resp.votes_dislike);
		}
		can_vote = true;
	},'json');
});
};

//<<<<<<<<<<<< ADD NEW PLACE
;jQuery.fn.addPlaceToEat = function(opt){
//var $add_new = $('li.new a',this);
$(this).click(function(){
	var $self = $(this);
	$.post(opt.add_url,{trip: opt.trip},function(resp){
		if(resp.status != 'success')
			return;
		var len = $('li[place]',$(this).parent().parent()).length;
		var html ='<li place="'+resp.id+'" ';
		if(len%2 == 1)
			html += 'class="alt"';
		html += '"<form enctype="multipart/form-data" method="post"><div style="width:100px; height:80px;margin-bottom:30px; margin-right:30px;" class="cabinet"><input type="file" class="file" name="photo"><img alt="click to upload" width="105" height="85" src="" /></div></form><h3 class="default"></h3><p class="location">in <a class="default"></a></p><p class="desc default"></p><a class="delete">Delete this place</a><ul class="opinion"><p>0</p><li><a class="like" vote="1">Like</a></li><li><a class="dislike" vote="-1">Don\'t like</a></li><p>0</p></ul><ul class="address default"></ul><hr /><a class="comments">Comments</a></li><ul class="comments" id="place_eat_comments"><input type="text" class="comment" /><a class="comment">Comment</a></ul>';
		$self.parent().before(html);
		$self.parent().prev().prev().editPlaceToEat(opt);
		SI.Files.stylize($('div input',$self.parent().prev().prev())[0]);
	},'json');
})
};

