/*******************************************************************************

AgencyCollective movie.js
(c) 2009 BuzaMoto. All rights reserved.
Author: mud(tm) - Takashi Okamoto.

*******************************************************************************/
var loadedImages = [];
var imageSwitching = false;
var selectedImages = {};

var currentMovie = null;
var flashvars = {
  videoAutoPlay: "false",
  fullSizeView: "false",
  oneClick: "true",
  doubleClick: "false",
  viewBigMiddleButton: "true",
  viewTooltip: "true",
  mouseHide: "true",
  mouseHideTime: "1",
  soundVolume: "0.8",
  spaceKey: "true"
};
var params = {
  scale: "noscale",
  allowfullscreen: "true",
  bgcolor: "000000"
};
var attributes = {};

function showNextImage(target) {
  var currentImage = selectedImages[target.id];
  var nextImage = $(currentImage).next('div');
  if (nextImage === undefined) {
    nextImage = $(currentImage).siblings().first();
  }
  nextImage.down('a').onclick();
}

function showImage(image_id, image_url, target) {
  if (imageSwitching) return;
  
  imageSwitching = true;
  if (target != 'main-gallery') {
    if (selectedImages[target]) {
      $(selectedImages[target]).removeClassName('selected');
    }
  } else {
    if (selectedImages['main-image']) {
      $(selectedImages['main-image']).removeClassName('selected');
    }
  }
  
  if (target == 'main-gallery') {
    if ($('project-movie-download-links') && $('project-movie-download-links').visible())
      hideDownloadLinks();
    if (showingMovie()) {
      removeFlashMovie();
      if (!loadedImages[image_id]) {
        loadedImages[image_id] = new Image();
        loadedImages[image_id].src = image_url;
      }
      if ($('main-image').down("img"))
        $('main-image').down("img").src = "";
      else {
        var imageElement = new Element('img');
        $('main-image').appendChild(imageElement);
      }
    }
    $('main-image').show();
  }
  
  $(target).down('img').fade({
    duration: 0.2,
    afterFinish: function() {
      getStatus(target).update('loading...').setStyle({visibility: 'visible'});
      if (!loadedImages[image_id]) {
        loadedImages[image_id] = new Image();
        loadedImages[image_id].src = image_url;
      }
      setImage(image_id, target);
    }
  });
}

function setImage(image_id, target) {
  if (loadedImages[image_id].complete) {
    var statusObject = getStatus(target);
    
    if (statusObject.getStyle('visibility') == 'visible') {
      statusObject.update('click to go next').setStyle({visibility: 'hidden'});
    }
    if (target != 'main-gallery') {
      selectedImages[target] = image_id;
    } else {
      selectedImages['main-image'] = image_id;
    }
    
    $(image_id).addClassName('selected');
    $(target).down('img').src = loadedImages[image_id].src;
    $(target).down('img').appear({duration: 0.2, afterFinish: function() { imageSwitching = false; }});
  } else {
    window.setTimeout(function() {
      setImage(image_id, target);
    }, 100);
  }
}

function getStatus(target) {
  return $(target).next('.status') || $(target).down('.status') || $(target).next('div').down('.status');
}

function initMovie(id, movieName, imagePath, videoPath, iphonePath, reel) {
  removeFlashMovie();
  if (selectedImages['main-gallery']) {
    $(selectedImages['main-gallery']).removeClassName('selected');
    selectedImages['main-gallery'] = null;
  }
  currentMovie = id;
  if ($(id)) $(id).addClassName('selected');

  // show download links
  if ($('project-movie-download-links').getStyle('visibility') == 'hidden')
    showDownloadLinks(movieName, imagePath, videoPath, iphonePath);
  else
    attachFlashMovie(movieName, imagePath, videoPath, iphonePath, reel);
}

function showMovie(id, movieName, imagePath, videoPath, iphonePath, reel) {
  flashvars.videoAutoPlay = "true";
  removeFlashMovie();
  if (selectedImages['main-gallery']) {
    $(selectedImages['main-gallery']).removeClassName('selected');
    selectedImages['main-gallery'] = null;
  }
  currentMovie = id;
  if ($(id)) $(id).addClassName('selected');
  
  // show download links
  if ($('project-movie-download-links').getStyle('visibility') == 'hidden')
    showDownloadLinks(movieName, imagePath, videoPath, iphonePath);
  else
    attachFlashMovie(movieName, imagePath, videoPath, iphonePath, reel);
}

function showingMovie() {
  if ($('main-gallery'))
    return $('main-gallery').childElements()[0].tagName.toUpperCase() == 'OBJECT';
  return false;
}

function removeFlashMovie() {
  if (showingMovie()) {
    $('main-gallery').childElements()[0].remove();
    if (currentMovie) {
      $(currentMovie).removeClassName('selected');
      currentMovie = null;
    }
  }
}

function hideDownloadLinks() {
  new Effect.Opacity('project-movie-download-links', {
    duration: 0.4,
    from: 1.0,
    to: 0.0,
    afterFinish: function() {
      $('project-movie-download-links').setStyle({visibility: 'hidden'});
    }
  });
}

function showDownloadLinks(movieName, imagePath, videoPath, iphonePath) {
  $('project-movie-download-links').setStyle({visibility: 'visible'});
  new Effect.Opacity('project-movie-download-links', {
    duration: 0.4,
    from: 0.0,
    to: 1.0,
    afterFinish: function() {
      attachFlashMovie(movieName, imagePath, videoPath, iphonePath);
    }
  });
}

function attachFlashMovie(movieName, imagePath, videoPath, iphonePath, reel) {
  if (!reel && $('main-image').visible()) {
    $('main-image').hide();
  }
  if (!$('flash-movie')) {
    var flashContainer = new Element('div', { id: 'flash-movie', style: 'background: red; width: 640px; height: 360px;' });
    if (!reel) Element.insert('main-gallery', {top: flashContainer});
    else Element.insert('reel', {top: flashContainer});
  }
  $('movie-name').update(movieName);
  $('download-iphone').href = iphonePath;
  $('download-quicktime').href = videoPath;
  flashvars.imagePath = imagePath;
  flashvars.videoPath = videoPath;
  if (!reel) {
    swfobject.embedSWF("/player/player.swf", "flash-movie", "640", "360", "9.0.0", false, flashvars, params, attributes);
  } else {
    flashvars.videoAutoPlay = "false";
    swfobject.embedSWF("/player/player_reel.swf", "flash-movie", "920", "518", "9.0.0", false, flashvars, params, attributes);
  }
}
