// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

$(function() {

	$(".group_description").truncate({max_length: 200, more: "<strong>Show</strong> more", less: "<strong>Hide</strong> more"});

	$("a.thickbox").click( function(){
		$("#message_title").focus();
		var theClass = $(this).parent().attr('class');
		setThickboxFocus(theClass);
	});

    $(".closeThickbox").click(tb_remove);

    $("#fb_disconnect").click( function(){
        FB.Facebook.apiClient.revokeAuthorization(); 
    });

	$('#TB_nav > ul > li a').click( function(event) {
		event.preventDefault();
		$('#TB_nav > ul > li').removeClass('current');
		$(this).parent().addClass('current');
	});
	
	$('a.commentable').live('click', function(event){
		event.preventDefault();
		var link = $(event.target);
     	if (link.attr("clicked") != "true"){ 
      		link.attr("clicked", "true" );
      		commentList = $(link.parents()[1]).find(".commentList");
      		var url = link.attr("href");
      		$("li.commentTrigger a[href='"+url+"']").parent().remove();
      		commentForm = new CommentForm($('#post_comment_wrapper form'), link, commentList);
      		commentList.append($("<li/>").append(commentForm.build_form()));
      		
      		$('textarea#comment_body').autoResize({
              // On resize:
              onResize : function() {
                  $(this).css({opacity:0.8});
              },
              // After resize:
              animateCallback : function() {
                  $(this).css({opacity:1});
              },
              // Quite slow animation:
              animateDuration : 100,
              // More extra space:
              extraSpace : 10
          });
      		
      		commentForm.focusFirst();
    	}
	});
	
	$('li.commentTrigger a').live('click', function(event){
	  event.preventDefault();
		var link = $(event.target);
		var url = link.attr("href");
		link.parent().remove();
		$("a.commentable[href='"+url+"']").click();
	});
	
  
  if (typeof(validation_failed_for_a_post) != "undefined" && validation_failed_for_a_post) { tb_show("Posts", tb_dialogue_url) };

  $('input#blog_for_blogsherpa').click(function(event){
    $('div#for_blogsherpa').toggle();
  });

  $('#keep_account').each(function() {
    if (this.checked) {
	  $('div.keep_account_fields').show();	
    } else {
	  $('div.keep_account_fields').hide();
    }	
  });

  $('#keep_account').click(function () {
    if (this.checked) {
	  $('div.keep_account_fields').show();	  
		if(submitflag)
			{
				jQuery(".TB_modal").css({'height' : 464});
				}
		else{
				jQuery(".TB_modal").css({'height' : 378});
				
			}
			
    } else {
	  $('div.keep_account_fields').hide();  	
				jQuery(".TB_modal").css({'height' : 275});
    }	
  });

	$(".commentList ul li:first-child").prepend("<span class='firstListItem'></span>");
		
});

var setThickboxFocus = function(selected_class) {
	$('#TB_nav > ul > li').removeClass('current');
	$('#TB_nav > ul > li.' + selected_class).addClass('current');
};


function CommentForm(template_form, link, target_to_update, oldContents) {
	var form = {};
	
	this.handleResponse = function (responseText, statusText){
		if (statusText == "success"){
 			form.parent().remove();    
			$(target_to_update).append(responseText);
			link.attr("clicked",false);
			recordCommentEvent();
		}
	} 
	
	this.handleError = function(request, textStatus, errorThrown) {
		form = form.html($(request.responseText).find("> form").html());
		form.find(".commentable_cancel").click(function(){
			form.remove();
			$(link.parent()).show();
		})
		form.show();
	}
	
	this.build_form = function(){
		form = $(template_form.clone(true));
		url = link.attr("href")
		form.attr("action", url);
	
		$(form).ajaxForm({ 
					beforeSubmit: this.beforeSubmit, 
					success: this.handleResponse, 
					error: this.handleError
		});
    
		return form;
  }

	this.focusFirst = function(){
		var elem = $('textarea:visible', form).get(0);
		if (elem) {
			elem.focus();
		}
	}

}

function recordCommentEvent() {
  s.pageName="groups : ajax comment created";
  s.prop1="post comment";
  s.eVar3="post comment";
  s.events="";
  //Clear out the referrer
  s.referrer="http://www.lonelyplanet.com/";
  void(s.t());
  return true;
}

