var videos = [
  {
    id: "beachsunset",
    loopTime: 32,
    fadeTime: 31,
    h: 1080,
    w: 1920
  },
  {
    id: "beachws",
    loopTime: 26,
    fadeTime: 25,
    h: 1080,
    w: 1920
  },
  {
    id: "beachrocks",
    loopTime: 32,
    fadeTime: 31,
    h: 1080,
    w: 1920
  },
  {
    id: "beachreflection",
    loopTime: 29,
    fadeTime: 28,
    h: 1080,
    w: 1920
  }
];

var currentVideo = 0;
var video = undefined;
var fadeTimer = undefined;

function setup() {
  setupInterface();
  $(window).bind("resize", scale);
  playVideo();
}

function setupInterface() {
  setupForm();
  initFadeOut();

  $(".navRight").bind("click", function() {
    nextVideo();
  })

  $(".navLeft").bind("click", function() {
    prevVideo();
  })

}

function initFadeOut() {
  var fadeOut = function() {
    // $(".controls").animate({
    //      opacity: 0
    //    }, 1500);
    $(".controls").removeClass("show");
  };

  var fadeIn = function() {
    // $(".controls").animate({
    //    opacity: 1
    //     }, 500);
    $(".controls").addClass("show");
  };

  fadeTimer = setTimeout(fadeOut, 3000);

  $(document).bind("mousemove", function() {
    fadeIn();
    clearTimeout(fadeTimer);
    fadeTimer = setTimeout(fadeOut, 3000);
  });
}

function setupForm() {
  var $email = $("#email");
  var defaultText = "enter e-mail for updates";

  $email.val(defaultText);

  $email.bind("focus", function() {
    if ($email.val() == defaultText) {
      $email.val("");
    }
  });

  $email.bind("blur", function() {

    if ($email.val().trim() == "") {
      $email.val(defaultText);
    }
  });

  $email.bind("keyup", function(e) {
    if (e.keyCode != 13) {
      return;
    }

    var email = $email.val();

    if (!/\S+@\S+\.\S+/.test(email)) {
      return;
    }

    $(".controls .complete").load("/register.php?email=" + escape(email), function() {
      $("#email").css("display", "none");
      $(".controls .complete").css("display", "block");
    });
  });
}


function scale() {

  if (!video) {
    return;
  }

  var w = video.w,
      h = video.h,
      winW = $(window).width(),
      winH = $(window).height(),
      pos = {};

  var ratio = w/h;
  var winRatio = winW/winH;
  if (winRatio < ratio) {
    pos.height = winH;
    pos.width = winH * ratio;
    pos.top = 0;
    pos.left = (pos.width - winW) / 2;
  } else {
    pos.width = winW;
    pos.height = winW / ratio;
    pos.left = 0;
    pos.top = (pos.height - winH) / 2;
  }

  $("#video").css(pos);
  return pos;
}

function playVideo() {
  video = getVideo();
  var pos = scale();

  if (pos) {
    insertVideo(video, pos);
    if (video.youtubeId) {
      insertYoutube(video.youtubeId);
    }
  }
}

function getVideo() {
  var video = videos[currentVideo];

  if (video) {
    //store loop time
    loopTime = video.loopTime;
    return video;
  }
}

function insertVideo(video, pos) {
  var h = $(window).height();
  var w = $(window).width();

  var flashvars = {
    id: video.id,
    loopTime: video.loopTime,
    fadeTime: video.fadeTime
  };
  var params = {
    wmode: "transparent"
  };
  var attributes = {};

  swfobject.embedSWF("Calm.swf", "video", w, h, "9.0.0", "expressInstall.swf", flashvars, params, attributes);
}

function insertYoutube(id) {
  var embed = '<embed src="http://www.youtube.com/v/' + id + '&loop=1&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" style="width: 1px; height: 1px; position: absolute; left: 0; top: 0;"></embed>';
  $("#yt").append(embed);
}

function removeVideo() {
  $("#video").empty();
  $("#yt").empty();
}

function nextVideo() {
  currentVideo++;

  if (currentVideo >= videos.length) {
    currentVideo = 0;
  }

  removeVideo();
  playVideo();
}

function prevVideo() {
 currentVideo--;

  if (currentVideo < 0) {
    currentVideo = videos.length - 1;
  }

  removeVideo();
  playVideo();
}


setup();
