// CONTENTS ====================================================================
/*
- DEFAULT               - common code that runs on every page.
- UTILS               - util functions
*/

// DEFAULT --------------------------------------------------------------
function InitDefault(){

    rotateHomePanels();

  // call other Init functions
  SetExternalLinks();
  SetUpJobApplicationDatePickers();

  //$(this).griddit({cols: 12, vertical: 20});

  eqHomepage();
  $(window).resize(eqHomepage);
  $(window).resize(resizeCampaign);
  resizeCampaign();
  
}

function eqHomepage() {
  
  var $story   = $('.homepage-expand-pods .story'),
    $img  = $story.find('img'),
    $div  = $story.find('.pod-content'),
    int_w  = $story.width() - $img.outerWidth() - 40;
  
  $div.attr('style', '');
  if($story.width() > ($img.width() * 2) ) $div.width(int_w);

    var currentTallest = 0;
  $('.homepage-expand-pods').eq(0).find('li .rotate').each(function (i) {
      
      $(this).find('.item').each(function (i) {
          if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
      });

  });
  $('.homepage-expand-pods').eq(0).find('li .rotate').css({ 'height': currentTallest });

  var currentTallest = 0;
  $('.homepage-expand-pods').eq(1).find('> li').each(function (i) {

      $(this).find('.rotate .item').each(function (i) {
          if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
      });

      if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }

  });
  $('.homepage-expand-pods').eq(1).find('> li').css({ 'height': currentTallest });
}

function rotateHomePanels() {
    var interval = 8000;
    var speed = 1000;
    $('li .rotate').cycle({ delay: interval, timeout: interval, speed:speed });
    $('#campaign-bar .rotate').cycle({ delay: interval, timeout: interval, speed: speed, before: function () { resizeCampaignOnRotate(speed); } });
}

var resizeCampaignOnRotateFirstRun = true;

function resizeCampaignOnRotate(speed) {

    if (!resizeCampaignOnRotateFirstRun) {
        var total = $('#campaign-bar .rotate .promo').length;
        var index = $('#campaign-bar .rotate .promo:visible').index();
        if (index + 1 >= total) {
            index = 0;
        } else {
            index = index + 1;
        }
        var height = $('#campaign-bar .rotate .promo').eq(index).height();
        $('#campaign-bar').animate({ 'height': height + "px" }, speed);
    }
    else resizeCampaignOnRotateFirstRun = false;
}

function resizeCampaign() {
    var height = $('#campaign-bar .rotate .promo:visible').height();
    $('#campaign-bar').css('height', height + "px");
}

// UTILS --------------------------------------------------------------

function ToggleGrid() {
  // keypress handler for grid - press esc to toggle
  $('html').keyup(function(event) {
    if(event.keyCode == 27){
      ( $('body').hasClass('grid') ) ? $('body').removeClass('grid') : $('body').addClass('grid');
    }
  });
}

(function($) {
jQuery.fn.griddit = function(options){
  
  var defaults = {  
    width: 940,
    height: $(window).height(),
    cols: 12,
    gutterwidth: 20,
    colColour: "rgba(255,0,0,0.2)",
    vertical: 20
  };
  var options = $.extend(defaults, options);
  
  var left    = ($(window).width() / 2) - (defaults.width / 2);
  var html     = '<canvas id="grid-cols" width="' + defaults.width + '" height="' + defaults.height + '" style="position: absolute; top: 0; left: ' + left + 'px"></canvas>';
  $('body').css('position', 'relative');
  $('body').parent().append(html);
  var canvas     = document.getElementById("grid-cols");
  var ctx     = canvas.getContext("2d");
  
  // vertical grid columns
  var col_w    = ( defaults.width - ((defaults.cols - 1) * defaults.gutterwidth) ) / defaults.cols;
  if( (col_w % 1) == 0) {
    ctx.fillStyle     = defaults.colColour;
    for (var i = 0; i < defaults.cols; i++) {
      ctx.fillRect (i * (col_w + defaults.gutterwidth), 0, col_w, defaults.height);
    };
  }else{
    alert('Your grid isn\'t a nice round number :/');
  }
  
  // horizontal grid lines
  ctx.strokeStyle   = defaults.colColour;
  ctx.lineWidth     = 1;
  ctx.beginPath();
  ctx.beginPath();
  for (var i = 0; i < (defaults.height / defaults.vertical); i++) {
    var y = (i * defaults.vertical) + 0.5;
    ctx.moveTo(0, y);
    ctx.lineTo(defaults.width, y);
  };
  
  ctx.stroke();
  
  $(canvas).hide();
  
  $('html').keyup(function(event) {
    if(event.keyCode == 27){
      if( $(canvas).is(':visible') ){
        $(canvas).hide();
      }else{
        $(canvas).show();
      }
    }
  });
  
}
})(jQuery);
    
(function($) {
jQuery.fn.eqByRow = function(){
  /*
  Purpose: equalise the heights of li's in a list by row
  usage: $('#eq-this-list').eqByRow();
  */
  var inRow   = Math.floor( $(this).width() / $(this).children("li:first").width() );
  var items   = $(this).children('li');
  for (var i = 0; i < ($(items).length / inRow); i++){
    var max   = 0;
    var start   = (i * inRow);
    for (var j = start; j < start + inRow; j++){
      if(items[j]){
        if( $($(items)[j]).height() > max ) max = $($(items)[j]).height();
      }
    };
    for (var k = start; k < start + inRow; k++){
      if(items[k]){
        $($(items)[k]).height(max);
      }
    };
  };
}
})(jQuery);

(function($) {
jQuery.fn.biggerClick = function(){
  /*
  Purpose: makes a container 'hot', and binds it click event to go to the location of the first <a>'s href it finds within it
  usage: $('#make-these-hot ul li').biggerClick();
  */
  $(this).hover(
    function(){
      $(this).addClass("hover");
    },function(){
      $(this).removeClass("hover");
    }
  )
  $(this).click(function(){
    window.location = $(this).find("a:first").attr("href");
  })
}
})(jQuery);

(function($) {
jQuery.fn.defaultValueInput = function(){
  for (var i=0; i < $(this).length; i++) {
    $(this[i]).data('defaultValue', $(this[i]).attr('value'));
    $(this[i]).addClass('default-value');
  };
  $(this).focus(function(){
    if( $(this).attr('value') == $(this).data('defaultValue') ){
      $(this).attr('value', '');
      $(this).removeClass('default-value');
    }
  });
  $(this).blur(function(){
    if( $(this).attr('value') == '' || $(this).attr('value') == $(this).data('defaultValue') ){
      $(this).attr('value', $(this).data('defaultValue'));
      $(this).addClass('default-value');
    }
  });
}
})(jQuery);

function SetExternalLinks(){
  /*
  Purpose: checks all <a>'s in the doc, if it has a rel attribute of 'external', sets to open in new window
  usage: <a href="http://www.google.com" rel="external">Google</a>
  */
  $('a[rel=external]').click(function(){ window.open(this.href); return false; });
}

function SetUpJobApplicationDatePickers() {
    if (jQuery.ui) {
        $("span.date-format").hide();
        $("img.date-icon").show();
        $("input.date-entry").datepicker({ dateFormat: "dd/mm/yy" });

        $("img.date-icon").click(function () {
            $(this).prev().focus();
        });
    }
}

// ======================================================================

