;(function($){$.fn.shuffle=function(){var allElems=this.get(),getRandom=function(max){return Math.floor(Math.random()*max)},shuffled=$.map(allElems,function(){var random=getRandom(allElems.length),randEl=$(allElems[random]).clone(true)[0];allElems.splice(random,1);return randEl});this.each(function(i){$(this).replaceWith($(shuffled[i]))});return $(shuffled)}})(jQuery);
function formatText(index,panel){
  return index + "";
}  

function displayError(){        
  //this.insertMessage( this.createMessageSpan() ); this.addFieldClass(); 
  html = "<div class=\"form-errors\"><ul class=\"errorlist\"><li>Please fill in the requied fields(*)</li></ul></div>"             
  if($(".form-errors").size() == 0)
    $(".form-container").addClass("error-in-form"); 
    $(".form-container form").addClass("error-in-form");
    $(".form-container form").append(html);
}

$(function(){
  $('#feature').anythingSlider({
    easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
    autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
    delay: 5000,                   // How long between slide transitions in AutoPlay mode
    startStopped: false,            // If autoPlay is on, this can force it to start stopped
    animationTime: 600,             // How long the slide transition takes
    hashTags: false,                // Should links change the hashtag in the URL?
    buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
    pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
    startText: "Start",             // Start text
    stopText: "Stop",               // Stop text
    navigationFormatter: formatText // Details at the top of the file on this use (advanced use)
  });
  $('#navigation li.current').prev().css('background','none');
  
  Cufon.replace('blockquote');
  
  // Font replacement for Why Research Mental Health page
  if ($('body').hasClass('why-research-mental-health')){
    Cufon.replace('.side-stats li');
  }
  
  // Truncate long comments on homepage
  if ($('body').hasClass('home')){
    $("#signups li p").truncate(140, { chars: /\s/, trail: [ " ( <a href='#' class='truncate_show'>more ...</a> )", " ( <a href='#' class='truncate_hide'>... less</a> )" ] });
  }

  // Truncate long comments on signups tables
  if ($('.table-container').length){
    $(".table-container li p").truncate(250, { chars: /\s/, trail: [ " ( <a href='#' class='truncate_show'>more ...</a> )", " ( <a href='#' class='truncate_hide'>... less</a> )" ] });
  }  
  
  // Shuffle and remove the last quote from the Side Quote sidebar
  $("#page-quotes-list li").shuffle();
  $("#page-quotes-list li:last-child,#page-quotes-list li:nth-child(4)").css('display','none');
  
  // Sign Up form
  if ($('form:not(.error-in-form)').length){
    $("label.helper").inFieldLabels({fadeOpacity:0.1});
  }
  if ($('form.error-in-form').length){
    $("label.helper").remove();
  }
  if ($('form').length){
    $(":checkbox").uniform();
    $("input[type='text']").addClass('required');
    $("input[type='text']").change(function(){
      $(this).removeClass('required');
    }); 
                      
    $.each($("input[type='text']"), function(){     
      if($(this).val() != ""){
        $(this).removeClass('required');    
        // $(this).addClass('LV_validation_message');
        $(this).addClass('LV_valid_field');
      }    
    })   
    
    f1 = new LiveValidation('id_first_name');
    f2 = new LiveValidation('id_surname');
    f3 = new LiveValidation('id_location');
    f4 = new LiveValidation('id_email');
    f1.add( Validate.Presence );
    f2.add( Validate.Presence );
    f3.add( Validate.Presence );
    f4.add( Validate.Presence );
    f4.add( Validate.Email );
  }   
  
  $("input[type='text']").blur(function(){
    var empty = false;
    jQuery(this).each(function(){
      empty = (jQuery(this).val() == "") ? true : empty;
    });
    if (empty){
      $(this).addClass('required');
    }
    return false;
  });
  
  
});