// progressive-enhancement.js
//
// Responsible JavaScript enhancements.
// The aim of this file is to provide functionality niceties in the event that JavaScript is enabled on the client.
// In no case should JavaScript be required for the website to work. In no case should any JavaScript be in-line or embeded in HTML.
//
// NOTES:
// This file relies upon jQuery and numerous plugins. Both the plugins and this file are loaded dynamically by lazy-loader.js
// DO NOT manually include this file in any HTML

$(document).ready(function(){
  
  /* Function to highlite a target after the anchor has been clicked */
  function targetHighlight(target) {
    var target_id = $(target).attr("id"); // get the id of the target element
    if(target_id != 'content_main') { // don't apply the highlight to this list of targets
      $(target).effect("pulsate", { times: 1 }, 500);
    }
  }

  /* Open links to external websites in a new window using Event Delegation.
       Event Delegation ensures the function will work inside AJAX loads
       REFERENCE:[http://docs.jquery.com/Tutorials:AJAX_and_Events] */
//  $("body").click(function(event) {
//    if ($(event.target).is("a[@href*=http://]")) {
//      $(event.target).attr('target','_blank');
//    }
//  });
  
  // TEST: if we have an internal page anchor use the scroller plugins to get smooth animation.
  if($("a[href*=#]").length > 0) {
    $.localScroll({
      axis:'xy', // 'x', 'y', 'xy' or 'yx'
      onAfter: targetHighlight,
      offset: {top:-20, left:-20}
    });
  }
  
  // TEST: if we have a form on a page then we want to perform a number of enhancements.
  if($("form").length > 0) {
    if ($("form input.input-date").length > 0) {
      /* Attach Datepicker to inputs with a date expected */
      $("input.input-date").datepicker({
        dateFormat: 'dd/mm/yy',
        minDate: 0, maxDate: 365,
        closeAtTop: false,
        showStatus: true
      });
    }
    
    /* Validate forms
    $("#example-form").validate(); */
  }
  
  /* TEST: If there's a Fancybox element on the page, attach fancybox */
  if($("a.fancybox").length > 0) {
    $("a.fancybox").fancybox({
      'zoomSpeedIn': 500,
      'zoomSpeedOut': 500,
      'overlayShow': true,
      'overlayOpacity': .5
    }); 
  }
  
  /* let the CSS know that we have jQuery available */
  $("#container").addClass("jquery"); /* not added to body because of IE6's inability to deal with multiple classes properly */

  /* member login form */
  $("input#mal_username").InputHelper();
  $("input#mal_password").InputHelper();
  $("input#mal_password").parent("label").addClass("enshorten");


});

