// JavaScript Document
jQuery(function($){

  /* handheld devices don't get the bandwidth-intensive stuff */
  if($(document).width() > 700)
  {

    // immediately replace the low res banner image with the hires one
    if($('#banner-fade').is('*'))
    {
      $('#banner-fade img').attr('src', $('#banner-fade img').attr('src').replace('/mobile_', '/'));
    }
    if($('#headImage').is('img'))
    {
      $('#headImage').attr('src', $('#headImage').attr('src').replace('/mobile_', '/'));
    }

    // load in advanced/enhanced scripts
    $.getScriptCached('/min/b=js&f=superfish.js,supersubs.js,jquery.innerfade.js,jquery.prettyPhoto.js,jquery.hoverIntent.minified.js,video.js,global.advanced.js', function(){

      // only load the rotaing banners after everything else has finished loading
      if($('#banner').is('*'))
      {
        setTimeout(function(){
          $.doBannerfade();
        }, 3000);
      }

      // attach prettyphoto where required
      $("a.prettyPhotoMe").prettyPhoto();

      // now that everything else is loaded, pull in the client-side form validation library
      $.getScriptCached('https://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js', function(){
        $("#newsletterForm").validate();
      });

    });

  }

  // adjust min-height of sidecolumn when absolutely positioned (ie on tabs)
  if($('#mainContentSideColumn').css('position') == 'absolute')
  {
    $(window).load(function(){
      $.adjustSideColumns();
    }).resize(function(){
      $.adjustSideColumns();
    });
  }

  // handle the plan your trip drop down selector for mobile layout
  $('#frmTravelBetween')
    .submit(function(){
      var location1 = $('select:eq(0)', this).val();
      var location2 = $('select:eq(1)', this).val();
      if(location1 != location2)
      {
        $('#theMatrixWrap .matrixPop').attr('style', '').css('display', 'none !important');
        $('#' + location1 + '-' + location2).css('display', 'block !important');
        $('#' + location2 + '-' + location1).css('display', 'block !important');
      }
      return false;
    })
    .find('select')
    .change(function(){
      $(this).closest('form').submit();
    });


  // handle changing the video on the homepage
  if($('#mainContent div.homevideo').is('div'))
  {
    $('#mainContent form.videoSelect select').change(function(){
      if($(this).val() != '')
      {
        $('#mainContent div.homevideo').html(alt_videos[$(this).val()]);
        if($(document).width() > 700)
        {
          VideoJS.setupAllWhenReady();
        }
      }
    });
  }

  // show PDF links
  $('#pdfLink').click(function(){
    $(this).siblings('ul').show('fast');
    return false;
  });


  // add link tracking behaviors
  $.trackLinkClicks();

});

jQuery.adjustSideColumns = function(){
  $('#sideContent').css('padding-bottom', $('#mainContentSideColumn').height() + ($('#sidemap').height() == 0 ? 20 : 0) + 'px');
}

jQuery.trackLinkClicks = function() {

    var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;

    $('video').bind('play', function(){
        _gaq.push(['_trackEvent', 'Video', 'Watch', $('source[type="video/mp4"]', this).attr('src')]);
    });

    $('a').each(function(){
        var href = $(this).attr('href');

        if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){

            $(this).click(function() {
                var extLink = href.replace(/^https?\:\/\//i, '');
                _gaq.push(['_trackEvent', 'External', 'Click', extLink]);
            });

            // if this is a link to be tracked as a conversion in Adwords, the URL
            // will be altered to pass it through the forwarding page.
            var link_text = $(this).text().toLowerCase();
            if(link_text.indexOf('visit our website') > -1 || link_text.indexOf('check rates') > -1 || $(this).hasClass('track'))
            {
              var this_href = $(this).attr('href');
              $(this).attr('href', '/forwarder?iframe=true&width=700&height=100&url=' + this_href).addClass('prettyPhotoMe');
              $(this).click(function(){
                  setTimeout(function(){
                      document.location.href = $.trim(this_href);
                    }, 3000);
              });
            }

        }
        else if (href.match(/^mailto\:/i)){
            $(this).click(function() {
                var mailLink = href.replace(/^mailto\:/i, '');
                _gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
            });
        }
        else if (href.match(filetypes)){
            $(this).click(function() {
                var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                var filePath = href.replace(/^https?\:\/\/(www.)luxurylodgesofaustralia.com\.com\.au\//i, '');
                _gaq.push(['_trackEvent', 'Download', 'Click - ' + extension, filePath]);
            });
        }
    });
}

jQuery.doBannerfade = function()
{
  if(typeof alt_banners == 'undefined') return;
  var container_height = $('#banner').height();
  $.each(alt_banners.banners, function()
  {
    $('#banner-fade')
      .append('<img src="' + this.src + '" alt="' + this.alt + '" title="' + this.alt + '" width="100%" style="display: none;" />');
  });
  $('#banner-fade')
    .find('script')
    .remove()
    .end()
    .innerfade({ speed: 800, timeout: 5000, type: 'sequence', containerheight: container_height});

  // if the window is resized, resize the banners
  $(window).resize(function(){
    var container_width = $('#banner-fade img:eq(0)').width();
    if(container_width < 768)
    {
      container_height = 234;
    }
    else if(container_width <= 1024)
    {
      container_height = 235;
    }
    else
    {
      container_height = 350;
    }
    $('#banner-fade').css('height', container_height);
  });
}

jQuery.getScriptCached = function(url, callback){
$.ajax({
   type: "GET",
   url: url,
   success: callback,
   dataType: "script",
   cache: true
});
};


