//hold flag of whether we have paused the cycling
var pauseCycle = false ;

$(document).ready(function(){

  //initialise london clock
  var gmtoffset = $('#offset').attr( 'value' ) - 0 ;
  var timeStamp = $('#gmt').attr( 'value' ) ;

  var optionsLON = { utc: true, utc_offset: gmtoffset , baseDate: timeStamp }
  $('#clk_lon').jclock(optionsLON);

  //initialise abu dhabi clock
  var optionsABD = { utc: true, utc_offset: 4 , baseDate: timeStamp }
  $('#clk_abd').jclock(optionsABD);

  //preload our menu onhover images
  preloadImages( Array( 'media/menu/aboutus_off.gif' , 'media/menu/work_off.gif' , 'media/menu/clients_off.gif' , 'media/menu/contactus_off.gif' ) ) ;

  //set hoverintent on pause button
  var pauseconfig = {sensitivity:3,interval:10,over:pause_cycle,timeout:10,out:restart_cycle};
//  $('#ctrl').hoverIntent(pauseconfig);
});


function preloadImages( imgs ) {

  numImgs = imgs.length ;
  for ( i = 0 ; i < numImgs ; i++ ) {

    temp = new Image() ;
    temp.src = imgs[i] ;
  }
  return false ;
}

function pauseBtn( ) {

  if ( pauseCycle ) {
    restart_cycle();
  } else {
    pause_cycle();
  }
}

function pause_cycle( ) {

  pauseCycle = true ;
  var pulseconfig = {duration:500,waitTime:2000,targetOpacity:0.2};
  $('#ctrl #pause_btn').hide();
  $('#ctrl #play_btn').show();
  $('#splash #pause').fadeIn(100);
  $('#splash #pause').pulsate(pulseconfig);
}

function restart_cycle( ) {

  pauseCycle = false ;
  $('#ctrl #play_btn').hide();
  $('#ctrl #pause_btn').show();
  $('#splash #pause').fadeOut(100);
}

//cycle images if we're on a project page
function cyclePrjImg( numImg , pv , nx ) {

  spTo = window.setTimeout("switchprji("+numImg+","+pv+","+nx+")",4000);
}

//cycle images if we're on the homepage
function cycleHPImg( numImg , pv , nx ) {

  spTo = window.setTimeout("switchhpi("+numImg+","+pv+","+nx+")",4000);
}


function switchprji( numImg , pv , nx ) {

  window.clearTimeout(spTo);
  if ( !pauseCycle ) {

    $('#splash #si_'+pv).fadeOut(200);
    $('#splash #si_'+nx).fadeIn(1000);
    var pv = pv + 1 ;
    var nx = nx + 1 ;
    if ( pv == numImg ) {
      var pv = 0 ;
    } else if ( nx == numImg ) {
      var nx = 0 ;
    }
  }
  spTo = window.setTimeout("switchprji("+numImg+","+pv+","+nx+")",4000);
}


function switchhpi( numImg , pv , nx ) {

  window.clearTimeout(spTo);
  if ( !pauseCycle ) {

    $('#pover #tt_'+pv).css({display:'none'});
    $('#splash #si_'+pv).fadeOut(200);
    $('#pover #tt_'+nx).css({display:'block'});
    $('#splash #si_'+nx).fadeIn(1000);
    var pv = pv + 1 ;
    var nx = nx + 1 ;
    if ( pv == numImg ) {
      var pv = 0 ;
    } else if ( nx == numImg ) {
      var nx = 0 ;
    }
  }
  spTo = window.setTimeout("switchhpi("+numImg+","+pv+","+nx+")",4000);
}