/*
 * Golden Mile page initialisation - clouds, random header image
 */

  // don't wait for DOM load to complete before fixing EDP logo etc!

  $(document).ready(function() {  // jQuery init
    // before we do *anything* else, fix GM logo if EDP version required
    if (document.domain.match(/edp./)) {
      // make GM logo (img direct descendant of #header) the EDP version
      $('#header > img').attr('src','../img/gm-logo-edp.png');
    }
    
    // note: 'moving clouds' code breaks something in IE6 which throws the  
    // nav menu to the right, so we have to hide it from ie6. Also, clouds 
    // show through #container, so display upgrade notice below menu bar...
    var msie6 = $.browser.msie && Number($.browser.version)<7;
    
    // limit margin below <body> to avoid page floating in the air...
    var hDocument = $(document).height();
    var hContent = $('#content').height();
    var hRest = 40 + 200 + 40 + 20;  // top margin + header + footer + bottom margin
    if (hContent < hDocument-hRest) $('#content').height(hDocument-hRest);    
    
    // move clouds across background by 1px/2sec
    // - stores current background-position x-value in cookie 'clouds-x'
    // - depends on jQuery cookie plugin
    movebg();  // reset bg to current cookie-recorded position
    setInterval(movebg, 2000);  // move clouds 1px right every 2 seconds
    function movebg() {
      if ($.cookie('clouds-x')==null) $.cookie('clouds-x', 0, { path: '/' });
      var x = Number($.cookie('clouds-x'))+1;
      $.cookie('clouds-x', x, { path: '/' });
      if (!msie6) $('html').css('backgroundPosition', x+'px 0px');
    }
    
    //generate random image for header bar
    var nImages=3;
    var imageNo = Math.ceil(Math.random()*nImages);
    $('#header').css('background-image', 'url(/img/hdr-img-'+imageNo+'.png)');
    
    // add ie6 warning!
    if (msie6) {
      $('#menu').append('<p class="ie6-warn">We note you are using Internet Explorer 6.0. ' +
        'This is no longer supported by Golden Mile, you will have a much ' +
        'better browsing experience by ' + 
        '<a href="http://www.microsoft.com/windows/internet-explorer/">updating</a> ' +
        'to a more recent version.</p>');
    }
    
    // signal busy while fetching ajax data 
    //  - http://postpostmodern.com/instructional/global-ajax-cursor-change/
    $("html").bind("ajaxStart", function(){  
       $(this).addClass('busy');  
     }).bind("ajaxStop", function(){  
       $(this).removeClass('busy');  
     });      
    
    // google analytics tracking code - use jQuery getScript() to load tracking code after DOM load is complete
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");  
    jQuery.getScript(gaJsHost + "google-analytics.com/ga.js", function(){  
      try {
        var ua;
        if (document.domain.match(/edp./)) {
          ua = 'UA-9649500-2';
        } else {
          ua = 'UA-9649500-1';
        }
        var pageTracker = _gat._getTracker(ua);
        pageTracker._trackPageview();
      } catch(err) {}
    });  
    
  });