$cwt = namco.clockworkTower = {
  teaser: {
    selectedWallpaper: -1
  }
};

namco.clockworkTower.teaser.createSWF = function() {
  swfobject.embedSWF("swf/teaser.swf", "clockworkTeaser", "100%", "100%", "9",
  {},
  {
    wmode: "transparent"
  });
};

namco.clockworkTower.teaser.createiOSPage = function() {
  var teaserContainer = $("#clockworkTeaser");
  var teaserDlgBox = $("#iosTeaserDlgBox");
  var teaserObject = $("#iosTeaserObject");
  var teaserVideoBox = $("#iosTeaserVideoBox");
  var teaserTitle = $("#iosTitle");

  teaserVideoBox.html('<object data="ios-mov/clock_2.mov" type="video/quicktime" height="297" width="500"><param value="true" name="controller"><param value="true" name="autoplay"><param value="black" name="bgcolor"><param value="aspect" name="scale"><param value="true" name="postdomevents"></object>')

  $(window).load(function() {
    teaserVideoBox.fadeIn();
    teaserTitle.fadeIn();
  });

  teaserObject.click(function() {
    // Turn off overflow for the clock 'slide-out' animation
    $("#wrapper").css({
      overflow: "hidden"
    });

    clearInterval($cwt.teaser.glowTimer);
    teaserDlgBox.hide();
    teaserObject.animate({
      left: $(window).width(),
      opacity: 0
    }, "slow", function() {
      document.location.href = "download.html";
    });
  });
  teaserObject.show();

  teaserContainer.css({
    background: "url('images/ios-background.jpg') no-repeat 0 0",
    width: "1024px",
    height: "768px"
  });

  // Setup 'glow' periodic fadein/out
  var interval = 10000;
  $cwt.teaser.dlgBoxTimer = setInterval(function() {
    teaserDlgBox.fadeIn("fast", function() {
      setTimeout(function() {
        teaserDlgBox.fadeOut("slow");
      }, interval / 4);
    });
  }, interval);
};

var showWallpaper = namco.clockworkTower.teaser.showWallpaper = function(index) {
  if ($cwt.teaser.selectedWallpaper == index) return;

  var wallpaperIcon = $($(".wallpaperThumbnail").get(index - 1));
  var wallpaperPreviewPrev = $("#wallpaperPreview img.active");
  var wallpaperPreview = $("#wallpaperPreview img").filter(
    "[rel=" + wallpaperIcon.attr("rel") + "]"
    );

  $cwt.teaser.selectedWallpaper = index;

  // Setup wallpaper size
  $("#wallpaperPreview").unbind("hover");
  $("#wallpaperPreview").hover(
    function() {
      $("#wallpaperSizes .wallpaperSize").unbind("click");
      $("#wallpaperSizes .wallpaperSize").click(
        function() {
          var selectedWallpaper = $(".wallpaperThumbnail.active");
          window.open(
            "images/wallpaper/" + selectedWallpaper.attr("rel")
            + "-" + $(this).attr("rel") + ".jpg"
            );
        });
      $("#wallpaperSizes").fadeIn("fast");
    }, function() {
      $("#wallpaperSizes .wallpaperSize").unbind();
      $("#wallpaperSizes").fadeOut("fast");
    });

  $("#wallpaperPreview img").stop();
  $(".wallpaperThumbnail").removeClass("active");
  $("#wallpaperPreview img").removeClass("active");

  wallpaperIcon.addClass("active");
  wallpaperPreview.addClass("active");

  // Stack new active image on top of preview for cross-fade
  $("#wallpaperPreview img").css( {
    zIndex: 0
  });
  wallpaperPreviewPrev.css( {
    zIndex: 1000
  });
  wallpaperPreview.css( {
    zIndex: 5000
  });

  $("#wallpaperPreview img").css( {
    opacity: 1
  });

  wallpaperPreview.fadeIn(function() {
    wallpaperPreviewPrev.hide();
  });
};

namco.clockworkTower.teaser.showDownloadBox = function() {
  /* Setup mouse events for wallpaper elements */
  $("#wallpaperThumbnails .wallpaperThumbnail").click(function() {
    showWallpaper($(this).index());
  });

  $("#wallpaperThumbnails #downloadWallpaperLeftArrow").click(function() {
    var selectedWallpaperIndex = $(".wallpaperThumbnail.active").index();
		
    if (selectedWallpaperIndex > 1)
      showWallpaper(selectedWallpaperIndex - 1)
    else
      showWallpaper($(".wallpaperThumbnail").size())
  });

  $("#wallpaperThumbnails #downloadWallpaperRightArrow").click(function() {
    var selectedWallpaperIndex = $(".wallpaperThumbnail.active").index();

    if (selectedWallpaperIndex < $(".wallpaperThumbnail").size()) {
      showWallpaper(selectedWallpaperIndex + 1);
    }else{
      showWallpaper(1);
    }
  });

  $("#downloadPages").fadeIn();
  $cwt.teaser.showWallpaper(1);
};

namco.clockworkTower.teaser.hideDownloadBox = function() {
  /* Unbind mouse events */
  $("#wallpaperThumbnails .wallpaperThumbnail").unbind("click");
  $("#wallpaperThumbnails .arrow").unbind("click");

  $("#downloadPages").fadeOut();
};
