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


if (typeof(console) == "undefined") {

	console = {
		log: function(m) {
		
		}
	}
}



$(document).ready(function(){
  
  $('.utilities #menu').hide(); 
  $('#account-menu').live('click', function(){
    $('.utilities #menu').show();
    $('#account-menu').addClass('menu-close');    
  });
  
  $('.menu-close').live('click', function(){
    $('.utilities #menu').hide();
    $('#account-menu').removeClass('menu-close'); 
  });

  $('.post-button').live('click', function(){ 
    $(this).find('.get-share-prompt-spinner').show();
    var type = $(this).attr('posttype');
    if(type == 'Intervue'){
      var iid = $(this).attr('iid');
      var variables = {authenticity_token: authToken, iid: iid};
    }else if(type == 'Question'){
      var qid = $(this).attr('qid');
      var variables = {authenticity_token: authToken, qid: qid};
    }else if(type == 'Response'){
      var rid = $(this).attr('rid');
      var variables = {authenticity_token: authToken, rid: rid};
    }
    $.post('/intervue/get_post_partial', variables, function(data){
      if(data != 'uncool') {
        $('#dimmer').fadeIn('fast');
        $('#prompt-box').empty().append(data).css({top: $(window).scrollTop() + 40}).show();
        $('#tweet').focus();
        $('.spinner').hide();
      }
    });
    
	});
	
	$('#dimmer').hide();
	
	$('.cancel-prompt-box').live('click', function(){
    $('#prompt-box').empty().hide();
    $('#dimmer').fadeOut('fast');
    $('#post-error').hide();
	});
  
	$('.text-counter').live('keyup', function(evt){
    
    var thisElement = this;
    var maxCharacters = parseInt( $(thisElement).attr('maxtext') );
    var textString = $(thisElement).attr('value');
    
    if(textString.length == maxCharacters) {
        $(thisElement).css({backgroundColor: "#fffcd8"});
        if( $(thisElement).hasClass('show-counter') ) {
            $('.text-counter-feedback').empty().append("0 chars left");
        }
    }else if(textString.length > maxCharacters){
        $(thisElement).css({backgroundColor: "#fffcd8"});
        alert("You have exceeded the maximum character limit.");
        $(thisElement).attr('value', textString.slice(0, maxCharacters));
        return false;
    }else{
        if( $(thisElement).hasClass('show-counter') ) {
            $('.text-counter-feedback').empty().append( maxCharacters - textString.length + " chars left");
        }
        $(thisElement).css({backgroundColor: "white"});
    }
    
	});
	
	$('#post-to-twitter').live('click', function(){
    var post = $('#tweet').attr('value');
    var posttype = $(this).attr('posttype');
    $('.announcement-content').empty();
    $('.prompt-button').hide();
    $('#post-spinner').show();
    if(posttype == 'Intervue'){
      var iid = $(this).attr('iid');
      var variables = {authenticity_token: authToken, iid: iid, post: post};
    }else if(posttype == 'Question'){
      var qid = $(this).attr('qid');
      var variables = {authenticity_token: authToken, qid: qid, post: post};
    }else if(posttype == 'Response'){
      var rid = $(this).attr('rid');
      var variables = {authenticity_token: authToken, rid: rid, post: post};
    }
    $.post('/intervue/post_intervue_info_to_twitter', variables, function(data){
        if(data.response == 'cool'){
            //resetPromptBoxes();
            $('#prompt-box').empty().hide();
            $('#dimmer').fadeOut('fast');
            //$('.announcement-content').empty().append("A link to this Intervue has been posted to Twitter.");
        }else{
            //alert(data.message);
            $('#post-error').find('.container').empty().append(data.message).show();
        }
    }, 'json');
	});
	
	$('.delete-response').live('click', function(){
    var thisElement = this;
    var rid = $(this).attr('rid');
    var answer = confirm("Are you sure you want to delete this Response.  THIS CAN NOT BE UNDONE!");
    if(answer){
        $.post('/intervue/delete_response', {authenticity_token: authToken, rid: rid}, function(data){
          if(data.response == 'cool') {
              $(thisElement).closest('.response-row').fadeOut(200);
          }else{
              alert(data.message);
          }
        }, 'json');
    }
	});
	
	$('.more-button').live('click', function(){
    var fullText = $(this).attr('fulltext');
    $(this).parent().empty().append(fullText);
	});
	

});
