$(document).ready(function(){

  $().hide_non_js_elements();

  $('div.logo img').center_logo();
  
  $('ul.award_listing li h4').toggle_award_category_description();
  
  $('div.tabs').tabs(); // jquery UI
  
  $('#entryform textarea').not('#entryform textarea.count_150').textarea_word_count( 300 );
  
  $('#entryform textarea.count_150').textarea_word_count( 150 );
  
  $('input[type="submit"]').saef_submit_validation( $('#entryform textarea').not('#entryform textarea.count_150'), $('#entryform textarea.count_150') );  
  
  $('#printemail ul').print_link();
  
  $('p.edit a').lost_data_warning();
  
});



/**
    * EMPLOYER LOGOS
    * Vertically centering the logo within container
    * ----
*/
$.fn.center_logo = function()
{
	$(this).each(function(i){
	
		var margin_height = $(this).parent().height() - $(this).height();
    	var final_height = margin_height / 2;
    	
    	$(this).css({marginTop: final_height});
	
	});
}



/**
* Lost Data Warning
* -----------------
* Displays a confirm box onClick that warns the user that
* they may lose data if the proceed.
*
* @return this
*/
$.fn.lost_data_warning = function()
{
  $(this).click(function( ){
      return confirm('Any unsaved information will be lost if you leave this page. Are you sure you want to proceed?');
  });
  return $(this);
}



/**
* Toggle award categry descriptions
* -----------------
* Show/hides the award category descrption when 'this' is clicked
*
* @returns this
*/
$.fn.toggle_award_category_description = function()
{
  $(this).click(
    function(){
     $(this).toggleClass('open').next().slideToggle('medium');
     return false;
   }
 );
 return $(this);
}



/**
* Hide non js elements
* -----------------
* Add a class to the body element i order to hide the
* elements not required when javascript is active.
*/
$.fn.hide_non_js_elements = function( )
{
   $('body').addClass( 'js-enabled' );
}



/**
* SAEF Submit Validation
* -----------------
* Checks word counts on textareas before submission is allowed
*
* @param textareas_300 - the textareas with a 300 word limit
* @param textareas_150 - the textareas with a 150 word limit
*/
$.fn.saef_submit_validation = function( textareas_300, textareas_150 )
{
  $(this).click(function(){
      wc_error = false;
      if($(this).val() == 'Complete'){
           $(textareas_300).each(function(){
               if( $(this).val().split(/[\s\.\?]+/).length - 1 > 300 ) {
                   wc_error = true
               }
           });
           $(textareas_150).each(function(){
                if( $(this).val().split(/[\s\.\?]+/).length - 1 > 150 ) {
                    wc_error = true
                }
            });
          if( wc_error === true ) {
              alert('The form cannot be submitted because you have used too many words for some answers. Please check the word counts for your responses and re-submit.');
              return false;
          }
          return confirm('This will complete your application, you will not be able to edit it further, please ensure that all details are correct.');
      }
  });
  
}



/**
* Textarea Word Counts
* -----------------
* Checks word counts on textareas before submission is allowed
*
* @param textareas_300 - the textareas with a 300 word limit
* @param textareas_150 - the textareas with a 150 word limit
*/
$.fn.textarea_word_count = function( max_count )
{

  this.each(function(){
  
    // get current count
    var cur_count = $(this).val().split(/[\s\.\?]+/).length;
    var word = 'word';
    if($(this).val() == '' || $(this).val() == ' '){
        cur_count = 0;
    }
    if( cur_count > 1 || cur_count == 0) {
        word = 'words';
    }
    $(this).after('You have used <span class="word_count">'+cur_count+' '+word+'</span> (max '+max_count+' words)');
    $(this).next('span.word_count').html(cur_count+' '+word);
    if( cur_count > max_count ){
        $(this).css({border:'1px solid #C41230'});
        $(this).next('span.word_count').css({background:'#C41230', color:'#fff', fontWeight:'bold'});
    } else {
        $(this).css({border:'1px solid #ccc'});
        $(this).next('span.word_count').css({background:'', color:'', fontWeight:''});
    }// end get current count
    
    
    // setup word count on keyup
    $(this).keyup(function(){
        cur_count = $(this).val().split(/[\s\.\?]+/).length;
        var word = 'word';
        if($(this).val() == '' || $(this).val() == ' '){
            cur_count = 0;
        }
        if( cur_count > 1 || cur_count == 0) {
            word = 'words';
        }
        $(this).next('span.word_count').html(cur_count+' '+word);
        if( cur_count > max_count ){
            $(this).css({border:'1px solid #C41230'});
            $(this).next('span.word_count').css({background:'#C41230', color:'#fff', fontWeight:'bold'});
        } else {
            $(this).css({border:'1px solid #ccc'});
            $(this).next('span.word_count').css({background:'', color:'', fontWeight:''});
        }
    });
    
  });
          
}



/**
* Print Link
* -----------------
* Write in a print link to print the current window
*/
$.fn.print_link = function()
{
  var new_li = $('<li class="print"><a href="#">Print this</a></li>');
  $('a',new_li).click(function(){
     window.print();
     return false;
  });
  return $(this).prepend(new_li);
}