/* ########################################################################### *
/* ***** INDEX *************************************************************** *
/* ########################################################################### *
/* ##### GLOBAL JS CONFIGURATION
/* ########################################################################### */

/* ########################################################################### *
/* ##### GLOBAL JS CONFIGURATION
/* ########################################################################### */

/* ########################################################################### *
/* ##### INITIALISATION
/* ########################################################################### */

$(document).ready(ecl_init_global);

function ecl_init_global()
{
  // Navigation
  ecl_navigation();
  
  //Expand clickable areas
  ecl_homepage();
  
  // Hide Show Member fieldsets;
  ecl_init_hide_show();
  
  // Form example text
  ecl_example_txt();
  
  // handle scrolling images in right panel
  scrollingImages();
  
  //Add Google Analytics Advertising tracking
  //analytics_tracking();

  //Add yoxview
  yoxview();

  // Disable right click context menus from elements
  disablecontextmenu();
  
  showBaseHover();
  
    // -------------------------------------------------------------------------------------------------------
    // Cufon - font replacement
    // -------------------------------------------------------------------------------------------------------
    
    if ( ! ( $.browser.msie && ($.browser.version == 6) ) ){ // only apply cufon on modern browsers
    
        Cufon.replace('#nav-global a.overrideFont', {hover: true});
    }
  
}



/* ########################################################################### *
/* ##### PRESENTATION
/* ########################################################################### */

function ecl_navigation (){
  $("ul.sf-menu").superfish({speed: 1});

  $(".nav_primary ul li.active, .nav_primary ul li.trail").next().addClass('next');
  $(".nav_primary ul li").hover(function () {
    $(this).next().addClass("next");
    },
    function () {
      if(!$(this).hasClass("active") && !$(this).hasClass("trail")){
      $(this).next().removeClass("next");
      }
    }
  )
}

function ecl_example_txt(){
  $('#form-search-01').example(function() {
    return $(this).attr('title');
  });  
}

function scrollingImages() {
  
  //Randomly cycle through scrolling images.
  if($('.scrollingImages').length){
    $('.scrollingImages').cycle({
                 fx: 'fade',
                 timeout: 4000,
                 pause: true
                 });  
  }

}

function analytics_tracking(){
  
  //advert links have rel="advert" on them, each time it clicks, sends info to google
  $("a[rel='advert']").each(function(){
       $(this).click(function(){
          var href = $(this).attr("href");
          _gaq.push(['_trackEvent', 'Advert', 'Clicked', href]);
          return true;
       });
    });
  
  //links that are surrounded with a downloadLink div, each time it clicks, sends info to google
  $(".downloadLink a").each(function(){
       $(this).click(function(){
          var href = $(this).attr("href");
          _gaq.push(['_trackEvent', 'Download Links', 'Clicked', href]);
          return true;
       });
    });
  $(".downloadLinkIgnoreImg a").each(function(){
       $(this).click(function(){
          var href = $(this).attr("href");
          _gaq.push(['_trackEvent', 'Download Links', 'Clicked', href]);
          return true;
       });
    });

  //links that are surrounded with a extLink div, each time it clicks, sends info to google
  $(".extLink a").each(function(){
       $(this).click(function(){
          var href = $(this).attr("href");
          _gaq.push(['_trackEvent', 'Offsite Links', 'Clicked', href]);
          return true;
       });
    });
  $(".extLinkIgnoreImg a").each(function(){
       $(this).click(function(){
          var href = $(this).attr("href");
          _gaq.push(['_trackEvent', 'Offsite Links', 'Clicked', href]);
          return true;
       });
    });

}

function ecl_homepage() {
  //Expand clickable area
//  $('.newsItem').css('cursor','pointer')
//          .hover(function(){
//            $(this).find("a").css('text-decoration','underline');
//          },function(){
//            $(this).find("a").css('text-decoration','none');
//          })
//          .click(function(){
//              window.location = $(this).find("a").attr("href");
//          });
  
  //Randomly cycle through images on the home page.
  if($('#homeBannerImages').length){
    $('#homeBannerImages').cycle({
                 fx: 'fade',
                 timeout: 8000
                 });  
  }
  
  if($.browser.msie){
    //Fade images in and out on hover
    $('.logos a img').css("opacity", 0.5);
    $('.logos a img')  
    .hover(function(){
      $(this).css("opacity", 1);
    },function(){
      $(this).css("opacity", 0.5);
    });
  }else{
    //Fade images in and out on hover
    $('.logos a img').fadeTo("fast", 0.5);
    $('.logos a img')  
    .hover(function(){
      $(this).fadeTo("fast", 1);
    },function(){
      $(this).fadeTo("fast", 0.5);
    });
  }

  
  if (!((jQuery.browser.msie)&&(parseInt(jQuery.browser.version)<=6))) {
  if($('#scroller').length>0){
    $('#scroller').jcarousel({
      wrap: 'last',
      initCallback: mycarousel_initCallback
    });
  }
  }
  
  
}

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


function scrollingWidth(){
  $('#scroller')
  .css('width','30000px');
  
  $('#logoContainer')
  .css('position','relative')
  .css('overflow','hidden')
  .css('height','40px')
  .css('top','2px');
}

function scrollDivLeft(id){
  clearTimeout(timerRight) 
  document.getElementById(id).scrollLeft-=scrollStep
  timerRight=setTimeout("scrollDivLeft('"+id+"')",10)
}

function scrollDivRight(id){

  clearTimeout(timerLeft)
  document.getElementById(id).scrollLeft+=scrollStep
  timerLeft=setTimeout("scrollDivRight('"+id+"')",10)
}

function stopMe(){
  clearTimeout(timerRight) 
  clearTimeout(timerLeft)
}

function ecl_init_hide_show(){
  $('.form-container .memberHeading h2')  .css('cursor','pointer')
                      .each(function(){
                        if(!$(this).hasClass('active')){       
                          $(this).addClass('collapsed');
                          $(this).parent().next('fieldset').hide();
                        }
                      })
                      .hover(function(){
                        $(this).css('text-decoration','underline');
                      },function(){
                        $(this).css('text-decoration','none');
                      })
                      .click(function(){
                        if($(this).hasClass('collapsed')){  
                          $(this).parent().next('fieldset').show();
                          $(this).removeClass('collapsed');
                        }else{
                          $(this).parent().next('fieldset').hide();
                          $(this).addClass('collapsed');
                        }
                      });    
}


function yoxview() {
  $(".yoxview").yoxview({
      videoSize: {maxwidth: 720, maxheight: 560}
  });
}

/* ########################################################################### *
/* ##### DISABLE RIGHT CLICK CONTEXT MENU
/* ########################################################################### */

function disablecontextmenu() {
  $(".disablecontextmenu").bind("contextmenu",function(e){
        return false;
    });
}

function showBaseHover()
{
  if ($('#baseHover').length > 0)  {
    var cookieName = $('#baseHover').attr('data-uuid');
    // Check whether the hover should be hidden
    if (cookieName && readCookie(cookieName)) {
      $('#baseHover').hide();
    }
    else {
      $('#baseHover').show();
      
      var popup = $('#baseHover')
        .css({"bottom": -1 * $('#baseHover').height()})
        .hide();
  
      popup.show().animate( { bottom: 5 }, 750);
  
      $('#baseHoverSpacer').height($('#baseHover').height());

      $('#closeBaseHover').click(function(){
        $('#baseHover').hide();
        $('#baseHoverSpacer').height(1);
        if (cookieName) {
          createCookie(cookieName, true, 365);
        }
      });
    }
    
  }
}

/***************************************************************************** *
 **** UTIL FUNCTIONS
/***************************************************************************** */

function isIE(version, lessThan){
  version = (version==undefined) ? 6 : version;
  lessThan = (lessThan==undefined) ? false : lessThan;
  
  if(lessThan){
    if (($.browser.msie)&&(parseInt($.browser.version)<=version)){
      return true;
    }
  } else {
    if (($.browser.msie)&&(parseInt($.browser.version)==version)){
      return true;
    }
  }
  
  return false;  
}

function createCookie( name,value,days) 
{
    if ( days) 
    {
            var date = new Date( );
            date.setTime( date.getTime( )+( days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString( );
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie( name) 
{
    var nameEQ = name + "=";
    var ca = document.cookie.split( ';');
    for( var i=0;i < ca.length;i++) 
    {
            var c = ca[i];
            while ( c.charAt( 0)==' ') c = c.substring( 1,c.length);
            if ( c.indexOf( nameEQ) == 0) return c.substring( nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie( name) 
{
    createCookie( name,"",-1);
}


/* ########################################################################### *
/* ##### Recaptcha Options
/* ########################################################################### */
var RecaptchaOptions = {
  theme : 'white'
};

/* FIN */
