/*
  Horrible IE Detection stuff from Microsoft's MSDN
  Needed because old IE won't work with our banner
  rotator
*/

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

/*
 *  Make and AJAX call to the application to get the list of banners that
 *  aren't the generic GPC Hero banner to be displayed. Shows a spinner
 *  while the load is happening.
 */
function loadBannerRota(){
  $('.bannerImage').append('<div id="spinnerLoading">Loading...</div>');
  jQuery.ajax({
    url: '/banners/rota_list',
    dataType: 'json',
    error: function(data){ $('#spinnerLoading').remove();  },
    success: populateDOMCallback
  });
}

/*
 *  Callback processes the response back from the
 *  server, which should contain JSON objects
 *  representing the different slides we want to
 *  show.
 *
 */
function populateDOMCallback(data, state, request){
  for(var i in data){
    var html = '<div class="bannerImage" style="background-image:url(\'' + data[i].image  + '\');">' + data[i].content + '</div>';
    $(html).appendTo('#bannerRota');
  }
  startBannerRota();
}

/*
 *  This function is called after the AJAX callback
 *  populates the bannerRota div with the rest of the
 *  content for the banner roatation. 
 *
 *  Will start sliding through all of the child divs
 *  of the bannerRota and give some navigation controls.
 *
 */
function startBannerRota(){ 
  $('#spinnerLoading').remove();
  $('#bannerRota').cycle({
    pager: '#slidePager',   
    speed: 'slow',
    pause: true,
    slideExpr: 'div',
    fx: 'scrollLeft'
  });
  $('#slidePager').show();
//  $('.videoPopup').each(function (i, element) {
//      $(element).attr('href', $(element).attr('rev'));
//      $(element).prettyPhoto(); //attr('href', $(element).attr('rev'));
//  });
}

/*
 *  This function is called when the page is 'ready', IE when 
 *  all of the DOM objects are present and the page has been
 *  rendered
 */
$(document).ready(function() {
  $('#videoLink').attr('href','/case-studies/videos/p/enfield-council-saves-400000-pounds-with-gpc-visa/?iframe=true&width=780&height=600');
  $('#galleryLink').attr('href','/case-studies/videos/p/gallery?iframe=true&width=780&height=600');
  $('#civilServiceVideo').attr('href','/videos/p/visa-at-civil-service-live-2010?iframe=true&width=780&height=600');

  /*
   *  For the Videos plugin, when genreating a gallery it uses the 'rev' attribute to store the
   *  popup URL. Use JS to swap this for the href for all objects with class .videoPopup to
   *  enable the popup gallery.
   */
  $('.videoPopup').each(function (i, element) {
    $(element).attr('href', $(element).attr('rev'));
  });

  /*
   *  Use the lightbox where it's needed
   */
//  $("a[rel^='prettyPhoto']").prettyPhoto();


  /*
   *  Load the banner roating script, which has an id of 'bannerRota'
   *  but only if we're not using an old and broken version of IE.
   */
  var ver = getInternetExplorerVersion();
  if(ver == -1 || ver >= 7.0){
    loadBannerRota();
  }
});

