$(function() {
  initialise_tabs();
    
  $('.banner #animation_region').each(function() {
    $(this).find('h3, p').css({ opacity: 0.75 }).hide();
    $(this).find('.segment').css({ width:'72px' }).click(function() {
      if (!($(this).hasClass('active'))) {
        var new_active_segment = $(this);
        $(this).closest('#animation_region').find('.segment.active').each(function() {
          $(this).find('p').fadeOut('fast', function() {
            $(this).closest('.segment').removeClass('active').animate({ width:'72px' }, 'fast', function() {
              new_active_segment.animate({ width:'168px' }, function() {
                $(this).addClass('active').find('p').css({ opacity:0.75 }).fadeIn('fast');
              });
            });
          });
        });
      }
    }).eq(0).addClass('active').css({ width:'168px' }).find('p').show();
  });
  
  $('.print-this-page').click(function() {
    window.print();
    return false;
  });
  
  $('#location_swatch').change(function(e) {
    if($(this).val() == "au") {
      $(location).attr('href', "http://tauruslogistics.com.au" + window.location.pathname);
    } else {
      $(location).attr('href', "http://tauruslogistics.co.nz" + window.location.pathname);
    }
  });
  
  if (!/quote/.test(window.location.path)) {
    $('a.glossary-term').hoverIntent({
      interval: 200,
      timeout: 500,
      over: function() {
        $('body').append('<div id="glossary-popup">' + $(this).attr('data-definition') + '</div>');
        var offset = $(this).offset();
        $('#glossary-popup').css({ left:offset.left + 'px', top:offset.top + 18 + 'px', opacity:0.9 }).show();
      },
      out: function() {
        $('#glossary-popup').remove();
      }
    });
  }
  
  $('.clickable-gallery').each(function() {
    $(this).find('img').closest('p').css({ display:'inline' });
    $(this).find('a').lightBox();
    $(this).append('<a class="activate" href="#">More images</a>');
    $(this).find('a.activate').click(function() {
      $(this).closest('.clickable-gallery').find('a').eq(0).click();
      return false;
    });
  });

  $('.quote-selector-button').click(function() {
    var selector = $('select', $(this).closest('div'));
    if ((v = selector.val()) == '') {
      window.location.href = '/shipping';
    } else {
      window.location.href = '/shipping/' + v + '/quote';
    }
    return false;
  });
});

function activate_tab(id) {
  var t = $('#' + id.replace(/^\#/, '')).closest('.tab-page');
  id = '#' + t[0].id;
  var container = t.closest('.tabbed-content');
  container.children('ul.tabs').children('li').removeClass('active').children('a').filter('[href$=' + id + ']').parent().addClass('active');
  container.children('.tab-page').hide();
  t.show();
}

function initialise_tabs() {
  $('.tabbed-content').each(function() {
    $(this).find('.tabs a').click(function() {
      activate_tab(this.href.replace(/^.*\#/, ''));
      return false;
    });
    $(this).find('.tab-page').hide();
    var q = window.location.hash;
    if (q && (q != '') && $(this).find(q).length > 0) {
      activate_tab(q);
    } else {
      activate_tab($(this).find('.tab-page').get(0).id);
    }
  });
}

/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

