/*______________________________________

  Miz_ImageClip #060612
  
  Copyright (C) 1999-2006 Mizuyari All rights reserved.
  Script written by Mami Komura.
  http://mizuyari.jp/
______________________________________*/


function Miz_ImageClip(name) { // Miz_ImageClip #060612

 this.self 		= name;
 this.fadeInOutTimer	= null;
 this.fadeInOutOpacity	= 0;
 this.fadeInOutBaseFrms	= 16;
 this.autoPlay		= true;
 this.autoPlayWeit	= 6000;
 this.autoPlayTimer	= null;
 this.imgs		= [];
 this.imgCaches		= null;
 this.bgElement		= null;
 this.bgPosition	= '0% 0%';
 this.imgElementId	= null;
 this.imgElementAlign	= 'left';
 this.currentImgId	= 0;
 this.prevImgId		= null;
 this.isExecutable	= false;
 
 Miz_ImageClip.prototype.preloadImgs = function () {
  this.imgCaches = [];
  for (i=0; i<this.imgs.length; i++) {
   this.imgCaches[i] = new Image();
   this.imgCaches[i].src = this.imgs[i];
  }
 }

 Miz_ImageClip.prototype.gotoAndPlay = function (id) {
  if (!this.isExecutable) return;
  if (this.fadeInOutTimer) return;
  if (id != null) {
   this.prevImgId = this.currentImgId;
   this.currentImgId = id;
  }
  this.fadeInOut();
 }

 Miz_ImageClip.prototype.setAutoPlay = function (mode) {
  if (!this.isExecutable) return;
  Miz_ImageClip.prototype.autoPlay = mode;
  if (mode != false) this.nextImg();
 }

 Miz_ImageClip.prototype.nextImg = function () {
  if (!this.isExecutable) return;
  if (this.fadeInOutTimer) return;
  if (this.autoPlayTimer) clearTimeout(this.autoPlayTimer);
  this.prevImgId = this.currentImgId;
  this.currentImgId = (this.currentImgId < this.imgs.length-1) ? this.currentImgId+1 : 0;
  this.fadeInOut();
 }

 Miz_ImageClip.prototype.prevImg = function () {
  if (!this.isExecutable) return;
  if (this.fadeInOutTimer) return;
  if (this.autoPlayTimer) clearTimeout(this.autoPlayTimer);
  this.prevImgId = this.currentImgId;
  this.currentImgId = (this.currentImgId > 0) ? this.currentImgId-1 : this.imgs.length-1;
  this.fadeInOut();
 }

 Miz_ImageClip.prototype.fadeInOutFrms = function () {
  if (navigator.userAgent.indexOf("Firefox") != -1) {
   return this.fadeInOutBaseFrms / 1.6;
  } else if (navigator.appVersion.indexOf('Win',0) != -1 &&
   navigator.appName.indexOf("Microsoft Internet Explorer",0) != -1) {
   return this.fadeInOutBaseFrms / 1.6;
  } else {
   return this.fadeInOutBaseFrms;
  }
 }

 Miz_ImageClip.prototype.init = function () {
  if (this.imgs.length < 1) return;
  this.preloadImgs();
  this.isExecutable = true;
 }

 Miz_ImageClip.prototype.fadeInOut = function () {
  if (!this.isExecutable) return;
  if (this.fadeInOutTimer) return;
  if (this.autoPlayTimer) clearTimeout(this.autoPlayTimer);
  mizSetBackgroundRepeat(this.bgElement,'no-repeat');

  // debug
  // document.forms[0].elements['status1'].value = this.currentImgId;
  // document.forms[0].elements['status2'].value = this.prevImgId;

  if (navigator.userAgent.indexOf("Firefox") != -1) {
   mizSetImgSrc(this.imgElementId,this.imgCaches[this.prevImgId]);
   mizSetTextAlign(this.imgElementId,this.imgElementAlign);
   mizSetBackgroundImage(this.bgElement,this.imgs[this.currentImgId]);
   mizSetBackgroundPosition(this.bgElement,this.bgPosition);
   mizSetOpacity(this.imgElementId,100);
  } else {
   mizSetImgSrc(this.imgElementId,this.imgCaches[this.currentImgId]);
   mizSetTextAlign(this.imgElementId,this.imgElementAlign);
   mizSetBackgroundImage(this.bgElement,this.imgs[this.prevImgId]);
   mizSetBackgroundPosition(this.bgElement,this.bgPosition);
   mizSetOpacity(this.imgElementId,0);
  }
  this.fadeInOutOpacity = 0;
  this.fadeInOutTimer = setTimeout(this.self + '.fadeInOut_main()',16);
 }

 Miz_ImageClip.prototype.fadeInOut_main = function () {
  if (!this.isExecutable) return;
  if (this.fadeInOutTimer) clearTimeout(this.fadeInOutTimer);
  if (this.fadeInOutOpacity < 100) {
   this.fadeInOutOpacity += Math.floor(100/this.fadeInOutFrms()); if (this.fadeInOutOpacity > 100 ) this.fadeInOutOpacity = 100;
   this.fadeInOutTimer = setTimeout(this.self + '.fadeInOut_main()',16); 
   if (navigator.userAgent.indexOf("Firefox") != -1) {
    mizSetOpacity(this.imgElementId,100-this.fadeInOutOpacity);
   } else {
    mizSetOpacity(this.imgElementId,this.fadeInOutOpacity);
   }
  } else {
   if (navigator.userAgent.indexOf("Firefox") != -1) {
    mizSetImgSrc(this.imgElementId,this.imgCaches[this.prevImgId]);
   }
   this.fadeInOutTimer = null;
   if (this.autoPlay) this.autoPlayTimer = setTimeout(this.self + '.nextImg()', this.autoPlayWeit);
   return;
  }
 }

 Miz_ImageClip.prototype.debug_printImgList = function () {
  document.open();
  document.write('<ul>');
  for (i=0; i<imageClip.imgs.length; i++) {
   document.write('<li><a href="javascript:void(0);" onclick="imageClip.gotoAndPlay('+i+'); return false;">' + imageClip.imgs[i] + '<\/a><\/li>');
  }
  document.write('<\/ul>');
  document.close();
 }

 return this;
}


function mizSetImgSrc(id, img) {
 var obj = document.getElementById(id);
 obj.src = img.src;
}

function mizSetBackgroundImage(id, src) {
 var obj = document.getElementById(id).style;
 obj.backgroundImage = 'url(' + src + ')';
}

function mizSetBackgroundPosition(id, pos) {
 var obj = document.getElementById(id).style;
 obj.backgroundPosition = pos;
}

function mizSetTextAlign(id, align) {
 var obj = document.getElementById(id).style;
 obj.textAlign = align;
}

function mizSetBackgroundRepeat(id, method) {
 var obj = document.getElementById(id).style;
 obj.backgroundRepeat = method;
}

function mizSetOpacity(id, opacity) { 
 var obj = document.getElementById(id).style;
 obj.filter = "alpha(opacity=" + opacity + ")";
 obj.KhtmlOpacity = (opacity / 100);
 obj.MozOpacity = (opacity / 100);
 obj.opacity = (opacity / 100);
}

function mizPreloadImg(src) {
 if (document.images && src) (new Image()).src = src;
}

function mizPreloadImgs(srcs) {
 if (document.images && srcs) {
  for (i=0; i<srcs.length; i++) mizPreloadImg(srcs[i]);
 }
}

