jQuery(document).ready(function($) {
	
	
	/*
  *   Search Field Text
  *   =================
  *   Some simple javascript to handle the search input text. We first grab the default value and
  *   store it in a variable. Then, on 'click' or 'blur' we alter add some logic to decide if we
  *   show or remove the default text.
  *
  */
  var search_value = $('#signup_input').val();
  $('#signup_input').click(function() {
    if( $(this).val() == search_value ) {
      $(this).val('');
    }
  });
  $('#signup_input').blur(function() {
    if( $(this).val() == '' ) {
      $(this).val(search_value);
    }
  });
  
  /*
  *   Sidebar Newsletter Callout
  *   ==========================
  *   This needs to be refactored into a more modular approach.
  */
  var sidebar_search_value = $('#signup_input_callout').val();
  $('#signup_input_callout').click(function() {
    if( $(this).val() == search_value ) {
      $(this).val('');
    }
  });
  $('#signup_input_callout').blur(function() {
    if( $(this).val() == '' ) {
      $(this).val(search_value);
    }
  });
  
	
	// Login form toggle
	$('#view-toggler-login').click(function(e) {
	  $('.login_box').toggle();
		return false;
	});
	
	// Hide the login form if the body is clicked
	$('body').click(function(e) {
	  if($(e.target).closest('.login_box').length == 0 && $('.login_box').is(":visible")) {
	    $('.login_box').toggle();
	  }
	});
	
	
	/*
	*   Twitter Feed Integration
	*   ========================
	*   Using the tweet.jquery plugin to handle the twitter feed.
	*/
  $('#twitter_update_list').tweet({
      join_text: strings['auto'],
      username: strings['username'],
      avatar_size: 32,
      count: 3,
      auto_join_text_default: strings['auto_join_text_default'],
      auto_join_text_ed: strings['auto_join_text_ed'],
      auto_join_text_ing: strings['auto_join_text_ing'],
      auto_join_text_reply: strings['auto_join_text_reply'],
      auto_join_text_url: strings['auto_join_text_url'],
      loading_text: strings['loading_text']
    });
	
	/*
	*   Newsletter Signup Popup
	*   =======================
	*   When a user signs up for the newsletter they are presented with a popup box that has a small
	*   disclaimer. This effect done using jQuery-UI.
	*/
	$('#signup_button').click(function(e) {
	  e.preventDefault();
	  newsletter_signup();
	});
	
	$('#signup_button_callout').click(function(e) {
	  e.preventDefault();
	  $('#signup_input').val( $('#signup_input_callout').val() );
	  newsletter_signup();
	});
	
	/*
	*   This is the actual function that calls a jQuery UI popup.
	*/
	var newsletter_signup = function() {
	   var popup = "<div id='jquery-ui-popup'><p>"+strings['disclaimer']+"</p></div>";
  	  $(popup).dialog({
  	    resizeable: false,
  	    height: 250,
  	    modal: true,
  	    buttons: [
  	      {
  	        text: strings['continue'],
  	        click: function() {
  	          $('#form_newsletter_signup').submit();
  	        }
  	      },
  	      {
  	        text: strings['cancel'],
  	        click: function() {
  	          	$( this ).dialog( "close" );
  	        }
  	      }
  	    ]
  	  });
	}

	
	
	/*
	*   Dropdown Menu Javascript
	*   ========================
	*   The javascript that makes the dropdown function
	*/
	$("ul.dropdown li").hover(function(){
      $(this).addClass("hover");
      $('ul:first',this).css('visibility', 'visible');
  }, function(){
      $(this).removeClass("hover");
      $('ul:first',this).css('visibility', 'hidden');
  });
  $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
	
	
	
	/*
	*   Character Counter
	*   =================
	*   Ensures the characters are under the limit
	*/
	$('.charlimit').keyup(function() {
	  var chars = $(this).val().length;
	  var limit = $(this).data('charlimit');
	  $(this).next('span').html(limit - chars);
	});
	
	
});
