/*______________________________________

  Miz_ImageClip #071004
  
  Copyright (C) 1999-2007 Mizuyari All rights reserved.
  Script written by Mahiro Komura.
  http://mizuyari.jp/
______________________________________*/


var loading;
var imageClip;

// mizuyari.addOnLoadEvent(progressBarInit);
mizuyari.addOnLoadEvent(imageClipInit);
mizuyari.addCSS("/shared/styles/components/mizuyari.css");


function Miz_ImageClip(name) { // Miz_ImageClip #071004

 Miz_ImageClip.prototype.self			= name;
 Miz_ImageClip.prototype.fadeInOutTimer		= null;
 Miz_ImageClip.prototype.fadeInOutOpacity	= 0;
 Miz_ImageClip.prototype.fadeInOutBaseFrms	= 10;
 Miz_ImageClip.prototype.autoPlay		= false;
 Miz_ImageClip.prototype.autoPlayWeit		= 4000;
 Miz_ImageClip.prototype.autoPlayTimer		= null;
 Miz_ImageClip.prototype.showInfo		= false;
 Miz_ImageClip.prototype.imgs			= new Array();
 Miz_ImageClip.prototype.bgElement		= 'miz-photogallery-image';
 Miz_ImageClip.prototype.bgPosition		= '50% 0';
 Miz_ImageClip.prototype.imgElementId		= 'img';
 Miz_ImageClip.prototype.imgElementAlign	= 'center';
 Miz_ImageClip.prototype.currentImgId		= 0;
 Miz_ImageClip.prototype.prevImgId		= null;
 Miz_ImageClip.prototype.isExecutable		= false;
 Miz_ImageClip.prototype.paging			= null;
 Miz_ImageClip.prototype.pagingId		= 'miz-photogallery-paging';
 Miz_ImageClip.prototype.pagingItems		= null;
 Miz_ImageClip.prototype.stageTimer		= null;
 Miz_ImageClip.prototype.stageOpacity		= 0;
 Miz_ImageClip.prototype.stageBaseFrms		= 16;
 Miz_ImageClip.prototype.photoSetName		= null;
 Miz_ImageClip.prototype.photoSetTitle		= "----";
 Miz_ImageClip.prototype.commentElementId	= 'miz-photogallery-comment';
 Miz_ImageClip.prototype.xmlPath		= "./";
 Miz_ImageClip.prototype.loadTimerID		= null;
 Miz_ImageClip.prototype.typewriterTimer	= null;
 Miz_ImageClip.prototype.typewriterCount	= 0;
 Miz_ImageClip.prototype.typeContent		= new Array(2);
 Miz_ImageClip.prototype.typeCursor		= "|";
 Miz_ImageClip.prototype.typeSpeed		= 5;
 Miz_ImageClip.prototype.repositionTimer 	= null;

 Miz_ImageClip.prototype.init = function () {
  this.createInterface();
  this.reposition();
 }
 
 Miz_ImageClip.prototype.preloadImgs = function () {
  mizPreloadImgs(this.imgs);
 }

 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.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 (mizuyari.ua.isMac && mizuyari.ua.isGecko || mizuyari.ua.isWinIE60 ) {
   return this.fadeInOutBaseFrms / 3;
  } else {
   return this.fadeInOutBaseFrms;
  }
 }

 Miz_ImageClip.prototype.fadeInOut = function () {
  if (!this.isExecutable) return;
  if (this.fadeInOutTimer) return;
  if (this.autoPlayTimer) clearTimeout(this.autoPlayTimer);

   mizSetBackgroundRepeat(this.bgElement,'no-repeat');
   mizSetImgSrc(this.imgElementId,this.imgs[this.currentImgId].image.src);
   miz$('miz-photogallery-title').innerHTML = '【' + this.imgs[this.currentImgId].title + '】';
   miz$('miz-photogallery-print').innerHTML = '<a href="' + this.imgs[this.currentImgId].pdfsrc + '"><img src="/shared/images/button-print.gif" alt="PRINT" class="swapImg" /></a>';
   mizSetTextAlign(this.imgElementId,this.imgElementAlign);
   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); 
    mizSetOpacity(this.imgElementId,this.fadeInOutOpacity);
  } else {
   this.fadeInOutTimer = null;
   return;
  }
 }

 Miz_ImageClip.prototype.stageFrms = function () {
  if (mizuyari.ua.isMac) {
   return this.stageBaseFrms / 4;
  } else {
   return this.stageBaseFrms;
  }
 }

 Miz_ImageClip.prototype.loadImages = function() {
  loading.showProgressBar();
  loading.slowstart();
  this.loadImages_main();
 }

 Miz_ImageClip.prototype.loadImages_main = function() {
  clearInterval(this.loadTimerID);
  if (this.isImgsLoaded()) {
   // trace("load complete");
   loading.fadeOut();
   this.loadComplete();
  } else {
   // trace("loading");
   this.loadTimerID = setInterval(this.self+".loadImages_main()", 250);
  }
 }

 Miz_ImageClip.prototype.loadComplete = function() {
  this.isExecutable = true;
  this.gotoAndPlay(this.currentImgId);
 }

 Miz_ImageClip.prototype.isImgsLoaded = function() {
  var complete = true;
  var totalItems = this.imgs.length;
  var downloadedItems = 0;
  for (i=0; i<totalItems; i++) {
   if (!this.imgs[i].image.complete) {
    complete = false;
    // trace(i + ": " + complete);
   } else {
    downloadedItems++;
   }
  }
  var completePercent = Math.floor(downloadedItems / totalItems * 100);
  // trace(completePercent);
  loading.setProgressBarWidthByPercent(completePercent);
  loading.writeText('<p>preloading data: ' + downloadedItems + '/' + totalItems + ' images loaded.<\/p>');
  loading.start(completePercent);
  return complete;
 }

 Miz_ImageClip.prototype.setClipInfo = function(xdoc) {
  var imgElmt = xdoc.getElementsByTagName("img");
  var albumImages = new Array();
  for (i=0; i<imgElmt.length; i++) {
   albumImages[i] = {
    "image" : new Image(),
    "title" : imgElmt[i].getAttribute('title'),
    "pdfsrc" : imgElmt[i].getAttribute('pdfsrc'),
    "loaded" : false
   };
   albumImages[i].image.src = imgElmt[i].getAttribute('src');
  }
  this.imgs = albumImages;
 }

 Miz_ImageClip.prototype.reposition = function() { // reposition #070927
  if (mizuyari.ua.isWinIE60) return;
  imageClip.repositionTimer = setTimeout('imageClip.reposition_main()', 250);
 }

 Miz_ImageClip.prototype.reposition_main = function() { // reposition_main #070927
  if (this.repositionTimer) clearTimeout(this.repositionTimer);
  var stage = miz$('miz-photogallery-image');
  var stageHeight = 500;
  var loading = miz$('miz-loading');
  var innerHeight = getWindowInnerHeight();
   if ((innerHeight >= stageHeight)) {
    loading.style.top = ((innerHeight - loading.clientHeight - 20) / 2) + "px";
    stage.style.top = ((innerHeight - stageHeight) / 2) + "px";
   } else {
    loading.style.top = "300px";
    stage.style.top = "0px";
   }
 }

 Miz_ImageClip.prototype.open = function(xml, currentImgId) {
  if (!this.isExecutable) return;
  this.isExecutable = false;
  var stage = miz$('miz-photogallery');
  var overlay = miz$('miz-overlay');
  hideObjects();
  hideSelectBoxes();
  overlay.style.opacity = '0';
  mizuyari.removeClass(miz$('miz-overlay'),'hidden');
  this.photoSetName = xml;
  this.stageOpacity = 0;
  this.currentImgId = currentImgId;
  this.stageTimer = setTimeout(this.self + '.open_main1()',16);
 }

 Miz_ImageClip.prototype.open_main1 = function() {
  if (this.stageTimer) clearTimeout(this.stageTimer);
  var stage = miz$('miz-photogallery');
  var overlay = miz$('miz-overlay');
  if (this.stageOpacity < 80) {
   this.stageOpacity += Math.floor(100/this.stageFrms()); if (this.stageOpacity > 80 ) this.stageOpacity = 80;
   this.stageTimer = setTimeout(this.self + '.open_main1()',16); 
   mizSetOpacity(overlay.id,this.stageOpacity);
  } else {
   this.stageTimer = null;
   overlay.style.opacity = null;
   mizuyari.removeClass(miz$('miz-photogallery'),'hidden');
   var xmlurl = this.xmlPath + this.photoSetName + '.xml';
   sendRequest(imageClip_xmlLoaded,'','GET',xmlurl,true,true);
  }
 }

 Miz_ImageClip.prototype.open_main2 = function(res) {
  imageClip.setClipInfo(res);
  imageClip.loadImages();
  // mizuyari.addCSS("print_accessmap.css", "print_accessmap");
 }

 Miz_ImageClip.prototype.close = function() {
  var stage = miz$('miz-photogallery');
  mizuyari.addClass(miz$('miz-photogallery'),'hidden');
  this.stageOpacity = 80;
  this.stageTimer = setTimeout(this.self + '.close_main()',100);
 }

 Miz_ImageClip.prototype.close_main = function() {
  if (this.stageTimer) clearTimeout(this.stageTimer);
  var overlay = miz$('miz-overlay');
  if (this.stageOpacity > 0) {
   this.stageOpacity -= Math.floor(100/this.stageFrms()); if (this.stageOpacity < 0 ) this.stageOpacity = 0;
   this.stageTimer = setTimeout(this.self + '.close_main()',16); 
   mizSetOpacity(overlay.id,this.stageOpacity);
  } else {
   this.stageTimer = null;
   overlay.style.opacity = null;
   mizuyari.addClass(miz$('miz-overlay'),'hidden');
   // mizuyari.removeCSS("print_accessmap");
   showSelectBoxes();
   showObjects();
   // mizSetBackgroundImage(this.bgElement,"/shared/images/clear.gif");
   // mizSetImgSrc(this.imgElementId,"/shared/images/clear.gif");
   return;
  }
 }
 
 Miz_ImageClip.prototype.createInterface = function() {
  var pageBody = miz$('page-body');

  var mizOverlay = document.createElement('div');
  mizOverlay.setAttribute("id","miz-overlay");
  mizOverlay.setAttribute("class","hidden");
  mizOverlay.setAttribute("className","hidden");

  var mizLoading = document.createElement('div');
  mizLoading.setAttribute("id","miz-loading");
  mizLoading.innerHTML =
   '<div id="miz-loading-text"></div>' +
   '<div id="miz-loading-progressbar"><img src="/shared/images/progress_bar.gif" alt="" id="miz-loading-img" /></div>';

  var mizPhotogallery = document.createElement('div');
  mizPhotogallery.setAttribute("id","miz-photogallery");
  mizPhotogallery.setAttribute("class","hidden");
  mizPhotogallery.setAttribute("className","hidden");

  var mizPhotogalleryStage = document.createElement('div');
  mizPhotogalleryStage.setAttribute("id","miz-photogallery-stage");
  if (mizuyari.ua.isWinIE80) {mizPhotogalleryStage.setAttribute("onclick", new Function("imageClip.close();"));}
  mizPhotogalleryStage.setAttribute("onclick","imageClip.close();");
  if (mizuyari.ua.isWinIE70) {mizPhotogalleryStage.setAttribute("onclick", new Function("imageClip.close();"));}
  if (mizuyari.ua.isWinIE60) {mizPhotogalleryStage.setAttribute("onclick", new Function("imageClip.close();"));}
  if (mizuyari.ua.isWinIE55) {mizPhotogalleryStage.setAttribute("onclick", new Function("imageClip.close();"));}
  if (mizuyari.ua.isWinIE50) {mizPhotogalleryStage.setAttribute("onclick", new Function("imageClip.close();"));}

  var mizPhotogalleryTitle = document.createElement('div');
  mizPhotogalleryTitle.setAttribute("id","miz-photogallery-title");

  var mizPhotogalleryCloseButton = document.createElement('div');
  mizPhotogalleryCloseButton.setAttribute("id","miz-photogallery-close");
  if (mizuyari.ua.isWinIE80) {mizPhotogalleryCloseButton.setAttribute("onclick", new Function("imageClip.close();"));}
  mizPhotogalleryCloseButton.setAttribute("onclick","imageClip.close();");
  if (mizuyari.ua.isWinIE70) {mizPhotogalleryStage.setAttribute("onclick", new Function("imageClip.close();"));}
  if (mizuyari.ua.isWinIE60) {mizPhotogalleryStage.setAttribute("onclick", new Function("imageClip.close();"));}
  if (mizuyari.ua.isWinIE55) {mizPhotogalleryStage.setAttribute("onclick", new Function("imageClip.close();"));}
  if (mizuyari.ua.isWinIE50) {mizPhotogalleryStage.setAttribute("onclick", new Function("imageClip.close();"));}
  mizPhotogalleryCloseButton.innerHTML = '<img src="/shared/images/button-close.gif" alt="CLOSE" class="swapImg" />';

  var mizPhotogalleryPrintButton = document.createElement('div');
  mizPhotogalleryPrintButton.setAttribute("id","miz-photogallery-print");
  mizPhotogalleryPrintButton.innerHTML = '<img src="/shared/images/button-print.gif" alt="PRINT" class="swapImg" />';

  var mizPhotogalleryImage = document.createElement('div');
  mizPhotogalleryImage.setAttribute("id","miz-photogallery-image");
  mizPhotogalleryImage.innerHTML = '<img src="/shared/images/clear.gif" alt="" id="img" class="map" />';

  mizPhotogalleryImage.appendChild(mizPhotogalleryTitle);
  mizPhotogalleryImage.appendChild(mizPhotogalleryPrintButton);
  mizPhotogalleryImage.appendChild(mizPhotogalleryCloseButton);

  mizPhotogalleryStage.appendChild(mizPhotogalleryImage);
  mizPhotogallery.appendChild(mizPhotogalleryStage);

  pageBody.appendChild(mizOverlay);
  pageBody.appendChild(mizLoading);
  pageBody.appendChild(mizPhotogallery);

  progressBarInit();
  mizSwapImgs();  

  this.paging = miz$(this.pagingId);
  this.pagingItems = null;

  this.isExecutable = true;
 }

 return this;
}

function hideSelectBoxes() {
 var selectElmts = document.getElementsByTagName('select');
 for (i=0; i<selectElmts.length; i++) {
  selectElmts[i].style.visibility = 'hidden';
 }
}
function showSelectBoxes() {
 var selectElmts = document.getElementsByTagName('select');
 for (i=0; i<selectElmts.length; i++) {
  selectElmts[i].style.visibility = 'true';
 }
}


function hideObjects() {
 var objectElmts = document.getElementsByTagName('object');
 for (i=0; i<objectElmts.length; i++) {
  objectElmts[i].style.visibility = 'hidden';
 }

 var embedElmts = document.getElementsByTagName('embed');
 for (i=0; i<embedElmts.length; i++) {
  embedElmts[i].style.visibility = 'hidden';
 }
}
function showObjects() {
 var objectElmts = document.getElementsByTagName('object');
 for (i=0; i<objectElmts.length; i++) {
  objectElmts[i].style.visibility = 'true';
 }

 var embedElmts = document.getElementsByTagName('embed');
 for (i=0; i<embedElmts.length; i++) {
  embedElmts[i].style.visibility = 'true';
 }
}


function Miz_ProgressBar(objName) { // Miz_ProgressBar #070911
 this.self = objName;
}

Miz_ProgressBar.prototype = {
 "self" : this,
 "progressBarTimer" : null,
 "progressBarElmt" : null,
 "progressBarImage" : null,
 "progressBarWidth" : 1,
 "progressBarCurrentPercent" : 0,
 "progressBarCurrentWidth" : 0,
 "progressBarCurrentOpacity" : 0,
 "getProgressBarWidth" : function() {
  return this.progressBarElmt.clientWidth;
 },
 "setProgressBarWidthByPercent" : function(percent) {
  this.progressBarCurrentPercent = percent;
  this.progressBarCurrentWidth = eval(this.progressBarElmt.clientWidth / 100 * percent);
 },
 "getProgressBarCurrentWidth" : function(percent) {
  return eval(this.progressBarImage.clientWidth);
 },
 "getProgressBarCurrentPercent" : function() {
  return eval(this.progressBarImage.clientWidth / this.getProgressBarWidth() * 100);
 },

 "showProgressBar" : function() {
  this.progressBarElmt.style.visibility = 'visible';
  this.progressBarElmt.style.zIndex = '';
  this.progressBarCurrentOpacity = 100;
  mizSetOpacity(this.progressBarElmt.id, this.progressBarCurrentOpacity);
 },

 "hideProgressBar" : function() {
  this.progressBarElmt.style.visibility = 'hidden';
  this.progressBarElmt.style.zIndex = '0';
  this.progressBarCurrentOpacity = 0;
  mizSetOpacity(this.progressBarElmt.id, this.progressBarCurrentOpacity);
 },

 "slowstart" : function() {
  clearInterval(this.progressBarTimer);
  this.progressBarCurrentPercent += 0.125;
  this.drawProgressBar();
  if (this.progressBarCurrentPercent < 100) {
   this.setProgressBarWidthByPercent(this.progressBarCurrentPercent);
   this.progressBarTimer = setInterval(this.self+".slowstart()", 16);
  }
 },

 "start" : function(percent) {
  clearInterval(this.progressBarTimer);
  this.progressBarCurrentPercent++;
  this.drawProgressBar();
  if (this.progressBarCurrentPercent < percent) {
   this.progressBarTimer = setInterval(this.self+".start("+percent+")", 16);
  }
 },

 "drawProgressBar" : function() {
  if (this.progressBarCurrentPercent > 100) this.progressBarCurrentPercent = 100;
  this.setProgressBarWidthByPercent(this.progressBarCurrentPercent);
  this.progressBarImage.style.width = this.progressBarCurrentWidth + "px";
  this.progressBarImage.style.height = "1px";
 },

 "writeText" : function(html) {
  this.progressBarText.innerHTML = html;
 },
 
 "fadeIn" : function() {
  clearInterval(this.progressBarTimer);
  this.progressBarCurrentOpacity += 20;
  if (this.progressBarCurrentOpacity > 100) this.progressBarCurrentOpacity = 100;
  mizSetOpacity(this.progressBarElmt.id, this.progressBarCurrentOpacity);
  if (this.progressBarCurrentOpacity < 100) {
   this.progressBarTimer = setInterval(this.self+".fadeIn()", 16);
  }
 },

 "fadeOut" : function() {
  clearInterval(this.progressBarTimer);
  this.progressBarCurrentOpacity = 100;
  this.progressBarTimer = setInterval(this.self+".fadeOut_main()", 16);
 },

 "fadeOut_main" : function() {
  clearInterval(this.progressBarTimer);
  this.progressBarCurrentOpacity -= 20;
  if (this.progressBarCurrentOpacity < 0) this.progressBarCurrentOpacity = 0;
  mizSetOpacity(this.progressBarElmt.id, this.progressBarCurrentOpacity);
  if (this.progressBarCurrentOpacity > 0) {
   this.progressBarTimer = setInterval(this.self+".fadeOut_main()", 16);
  } else {
   this.hideProgressBar();
  }
 }

}


function mizSetImgSrc(id, src) {
 var obj = miz$(id);
 obj.src = src;
}

function mizSetBackgroundImage(id, src) {
 var obj = miz$(id).style;
 obj.backgroundImage = 'url(' + src + ')';
}

function mizSetBackgroundPosition(id, pos) {
 var obj = miz$(id).style;
 obj.backgroundPosition = pos;
}

function mizSetTextAlign(id, align) {
 var obj = miz$(id).style;
 obj.textAlign = align;
}

function mizSetBackgroundRepeat(id, method) {
 var obj = miz$(id).style;
 obj.backgroundRepeat = method;
}

function mizSetOpacity(id, opacity) { 
 var obj = miz$(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]);
 }
}

function csAutoMarkup(str) {
 if (mizuyari.ua.isSafari) {
  str = str.replace('&#38;','<span class="amp">&<\/span>');
 } else {
  str = str.replace('&','<span class="amp">&<\/span>');
 }
 str = str.replace('ISSUE','<span class="issue">ISSUE<\/span>')
 return str;
}

function imageClipInit() {
 imageClip = new Miz_ImageClip("imageClip");
 imageClip.init();
 mizuyari.addEvent(window, "resize", imageClip.reposition);
}
function imageClip_xmlLoaded(oj) {
 var res = oj.responseXML;
 imageClip.open_main2(res);
}

function progressBarInit() {
 loading = new Miz_ProgressBar("loading");
 loading.progressBarElmt = miz$("miz-loading");
 loading.progressBarImage = miz$("miz-loading-img");
 loading.progressBarText = miz$("miz-loading-text");
}
