var url_docroot = '/';
var slideshow_time = 1000;

/* --------------------------------------------------------------------------------
 *  Slideshow
 * -------------------------------------------------------------------------------- */

var Slideshow = function() {
  var data;
  var current;
  var transitioning;
  var timer;
  
  function init() {
    left = $('.c_home .left');
    current = 0;
    transitioning = false;
    timer = false;
    
    $.getJSON(url_docroot + 'assets/data/slideshow.json', function(d) {
      data = d;
      $('.widget a', left).bind('click', clickHandler);
      setTimer();
    });
  }
  
  function setTimer() {
    if (timer) clearTimeout(timer);
    timer = setTimeout(loadNextImage, slideshow_time);
  }  
  
  function clickHandler(e) {
    if (transitioning) return;
    transitioning = true;
    
    current = $('.widget div a', left).index(e.currentTarget);
    loadImage();
    
    return false;
  }
  
  function loadNextImage() {
    if (transitioning) return;
    transitioning = true;
    
    current = (current + 1) % data.length;
    loadImage();
  }
  
  function loadImage()
  {      
    var div = $('<div class="loading image_holder image_' + (current + 1) + '" />'); 
    
    var img = $('<img />');
    
    div.append(img);
    // div.append('<p>' + data[current].caption + '</p>');    
    
    
    left.append(div);
    
    img.bind('load', imageLoaded);
    img.attr('src', url_docroot + 'assets/images/home/' + data[current].image);     
  }
  
  function imageLoaded() {
    var o_image = $('div:not([class~=loading])[class~=image_holder]', left);
    var n_image = $('div[class~=loading]', left);    
    
    o_image.fadeOut(1000, function() {
      o_image.remove();
      n_image.removeClass('loading');
      transitioning = false;
      setTimer();
    });
  }
  
  return {
    init: init
  }
};

/* --------------------------------------------------------------------------------
 *  Key times
 * -------------------------------------------------------------------------------- */
var KeyTimesHelper = function() {
  var backpanel = null;
  var ipad = null;
  var ipad_frame = null;
  var ipad_loaded = false;
  var ipad_content = null;
  
  function init() {
    $('a.travel_times').bind('click', open_times);
  }
  
  function open_times() {
    var body = $('body');    
    
    if (!backpanel) {
      backpanel = $('<div />').addClass('key_times_backpanel').height($(document).height()).css('opacity', 0);
      backpanel.bind('click', close_times);
      body.append(backpanel);
    }
    
    if (!ipad) {
      ipad = $('<div />').addClass('key_times_ipad').bind('click', close_times).hide();
      var ipad_img = $('<img />');
      ipad_frame = $('<div />').addClass('frame');
      ipad_frame.append(ipad_img);  
      ipad.append(ipad_frame);
      ipad_img.bind('load', function() {
        ipad_loaded = true;
        display_times();
      });
      
      body.append(ipad);
      ipad_img.attr('src', url_docroot + 'assets/images/ipad_frame.png');
    }
    
    backpanel.fadeTo(400, 0.7);
    display_times();
  }
  
  function display_times() {
    if (!ipad_loaded) return;
    
    if (!ipad_content) {
      ipad_content = $('<img />').addClass('times').hide();
      ipad_content.bind('load', function() {
        ipad_content.fadeIn(400);
      });
      
      ipad_frame.append(ipad_content);
      ipad_content.attr('src', url_docroot + 'assets/images/ipad_content.jpg');      
    }
    
    ipad.fadeIn(400);
  }
  
  function close_times() {
    ipad.fadeOut(400);
    backpanel.fadeOut(400);
  }
  
  return {
    init: init
  }
};

/* --------------------------------------------------------------------------------
 *  Registration text rotation
 * -------------------------------------------------------------------------------- */
 
var RegistrationMagic = function() {
  var text = ["Register for pre-sales", "Next phase coming soon"];
  
  var current = 0;
  var elem = false;
  
  function init() {
    elem = $('.register_magic .bot'); // .css({'paddingTop': '16px'});
    
    var timerDing = function() {
      elem.fadeOut('slow', function() {updateText(); elem.fadeIn();});
      current = (current + 1) % text.length;
    };
    
    updateText();
    setInterval(timerDing, 5000);
  }
    
  function updateText() {
    elem.text(text[current]);
  }
  
  return {
    init: init
  };
};

/* --------------------------------------------------------------------------------
 *  Ready?
 * -------------------------------------------------------------------------------- */
$(function() {
  if ($('#container.c_home')) Slideshow().init();
  if ($('#container.c_gallery')) {
    $('#content .box_white .gallery li a').fancybox();
  }
  
  KeyTimesHelper().init();
  
  if ($('#container.c_location')) {
    $('.maps li a').fancybox();
  }
  
  $('body').addClass('has-js');
  
  var checks = $('.label_check');
  
  function checks_update() {
  	$('input:checked', checks).each(function(){$(this).parent('label').addClass('check_on');});
  }
  
  checks.bind('click', function(e) {
    checks.each(function(){$(this).removeClass('check_on');});
    checks_update();  
  });
  checks_update();
  
  RegistrationMagic().init();
  
  var video_filename = 'rq_hd_8';
  var vt = '<div class="video_frame"><video controls="controls" width="854" height="480" autoplay>';
  
  // mp4 version needs to go first otherwise iPad won't work...
  vt += '<source type="video/mp4" src="' + url_docroot + 'assets/videos/' + video_filename + '.m4v" />';
  vt += '<source type="video/webm" src="' + url_docroot + 'assets/videos/' + video_filename + '.webm" />';
  vt += '<source type="video/ogg" src="' + url_docroot + 'assets/videos/' + video_filename + '.ogvid" />';
  
  // flash fallback...
  vt += '<object width="854" height="480" type="application/x-shockwave-flash" data="' + url_docroot + 'assets/flowplayer/flowplayer-3.2.5.swf">';
  vt += '<param name="movie" value="' + url_docroot + 'assets/flowplayer/flowplayer-3.2.5.swf" />'; 
  vt += '<param name="allowfullscreen" value="true" />'; 
  vt += '<param name="flashvars" value=\'config={"clip": {"url": "' + url_docroot + 'assets/videos/' + video_filename + '.m4v", "autoPlay":true, "autoBuffering":true}}\' />';      
  vt += '</object>';
  
  vt += '</video></div>'; 

  $('a.video').fancybox({
  	'autoDimensions': true,
  	'content': vt
  });
  
  $('.c_home .right .latest_post').ellipsis();
});

