
function mizPreloadImages(srcs) {
  this.self = this;
  this.imgs = [];
  this.loadTimerID = null;
  this.complete = false;
  if (srcs) this.addImages(srcs);
}

mizPreloadImages.prototype = {

  addImage : function(src) {
    var imgId = this.imgs.length;
    this.imgs[imgId] = new Image();
    this.imgs[imgId].src = src;
    this.imgs[imgId].loaded = false;
  },
  
  addImages : function(srcs) {
    if (document.images && srcs) {
      for (i=0; i<srcs.length; i++) this.addImage(srcs[i]);
    }
  },

  loadImages : function() {
    // loading.showProgressBar();
    // loading.slowstart();
    this.loadTimerID = setInterval(function() {
      var self = contentViewer.preloadImages;
      if (self.isImgsLoaded()) {
        clearInterval(self.loadTimerID);
        // trace("load complete");
        //loading.fadeOut();
      } else {
        // trace("loading");
      }
    }, 250);
  },

  isImgsLoaded : function() {
    var complete = true;
    var totalItems = this.imgs.length;
    var downloadedItems = 0;
    for (i=0; i<totalItems; i++) {
      if (!this.imgs[i].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);
    this.complete = complete;
    return complete;
  }
}


function contentViewer (self) {
 this.self = 'window.' + self;
 this.contents = [];
 this.parentElmtId = 'content';
 this.autoWait = 4000;
 this.pageNum = 0;
 this.nextPageNum = null;
 this.prevPageNum = null;
 this.autoPlayTimer = null;
 this.faderTimer = null;
 this.faderOpacity = 100;
 this.fadeInOutBaseFrms = 16;
 this.preloadImages = null;
 // this.ready = false;
 // this.init();
}

contentViewer.prototype = {

setPageParams : function(num) {
 this.pageNum = num;
 this.nextPageNum = (this.pageNum == this.contents.length-2) ? 0 : this.pageNum+1;
 this.prevPageNum = (this.pageNum <= 0) ? this.contents.length-2 : this.pageNum-1;
 // trace(this.pageNum + ", " + this.nextPageNum + "," + this.prevPageNum)
},

clearTimer : function() {
 clearTimeout(this.autoPlayTimer);
},

init : function() {
  // trace(this.contents);
},

getXML : function(num) {
 this.clearTimer();
 this.setPageParams(num);
 miz$("photogallery-title").innerHTML = this.title;
 // miz$(this.parentElmtId).innerHTML = '';
 // miz$(this.parentElmtId).appendChild(content);
 miz$("photogallery-image").src = this.contents[num].url;
 // trace(miz$("photogallery-image").naturalWidth);

 miz$("photogallery-interface").style.top =
  (( (miz$("photogallery-image").naturalHeight || miz$("photogallery-image").clientHeight) - miz$("photogallery-prev").clientHeight) / 2) + 'px';

 // miz$("photogallery-interface").style.width = miz$("photogallery-image").naturalWidth + "px";
 miz$("photogallery-prev").style.left = "10px";
 miz$("photogallery-next").style.right ="10px";



 if (this.contents[num].auto != false) {
  this.autoPlayTimer = setTimeout(this.self + '.nextPage()', this.autoWait);
 }
},

startPage : function() {
  this.loading();
},

loading : function() {

 var content = mizuyari.elementFactory('img', {
  'id'  : "photogallery-image",
  'src' : "/gallery/images/ajax-loader.gif",
  'alt' : ""
 });
 miz$(this.parentElmtId).appendChild(content);

  var preloadImgArrs = []; for (i=0; i<this.contents.length; i++) {
    if (this.contents[i].url == undefined) continue;
    preloadImgArrs[i] = this.contents[i].url;
  }
  this.preloadImages = new mizPreloadImages(preloadImgArrs);
  this.preloadImages.loadImages();

  this.loadingTimer = setInterval(function() {
    var self = contentViewer;
    if (self.preloadImages.complete) {
      clearInterval(self.loadingTimer);
      self.loadComplete();
      // trace("ok");
    }
  }, 250, this);

},

loadComplete : function() {
 this.setPageParams(0);
 var thumbs = mizuyari.elementFactory('ul', {});
 var thumbsHTML = '';
 for (var i=0, j=contentViewer.contents.length-1; i<j; i++) {
	thumbsHTML += '<li><a href="javascript:contentViewer.goPage(' + i + ')"><img src="' + contentViewer.contents[i].thumb + '" alt="' + contentViewer.contents[i].title + '" /></a></li>';
 }
 thumbs.innerHTML = thumbsHTML;
 miz$('photogallery-thumbs').appendChild(thumbs);
 this.goPage(0);
 miz$("photogallery-caption").innerHTML = "<h3>" + this.contents[this.pageNum].title + "<\/h3><p>" + this.contents[this.pageNum].caption + "<\/p>";
},

nextPage : function() {
 this.fadeOut(this.nextPageNum);
 miz$("photogallery-caption").innerHTML = "<h3>" + this.contents[this.nextPageNum].title + "<\/h3><p>" + this.contents[this.nextPageNum].caption + "<\/p>";
},

prevPage : function() {
 this.fadeOut(this.prevPageNum);
 miz$("photogallery-caption").innerHTML = "<h3>" + this.contents[this.prevPageNum].title + "<\/h3><p>" + this.contents[this.prevPageNum].caption + "<\/p>";
},

goPage : function(pageNum) {
 this.fadeOut(pageNum);
 miz$("photogallery-caption").innerHTML = "<h3>" + this.contents[pageNum].title + "<\/h3><p>" + this.contents[pageNum].caption + "<\/p>";
},

faderFrms : function () {
  if (mizuyari.ua.isMac && mizuyari.ua.isGecko || mizuyari.ua.isWinIE ) {
   return this.fadeInOutBaseFrms / 3;
  } else {
   return this.fadeInOutBaseFrms;
  }
},

fadeOut : function (param) {
 if (this.faderTimer) return;
 this.clearTimer();
 if (this.autoPlayTimer) clearTimeout(this.autoPlayTimer);
 var thumbsElmt = miz$('photogallery-thumbs').getElementsByTagName('LI');
 mizuyari.removeClass(thumbsElmt[contentViewer.pageNum], 'current');
 mizuyari.addClass(thumbsElmt[param], 'current');
 mizSetOpacity("photogallery-image", 100);
 this.faderOpacity = 100;
 this.faderTimer = setTimeout(this.self + '.fadeOut_main('+param+')',16);
},

fadeOut_main : function (param) {
 if (this.faderTimer) clearTimeout(this.faderTimer);
 if (this.faderOpacity > 0) {
  this.faderOpacity -= Math.floor(100/this.faderFrms()); if (this.faderOpacity < 0 ) this.faderOpacity = 0;
  this.faderTimer = setTimeout(this.self + '.fadeOut_main('+param+')',16);
  mizSetOpacity("photogallery-image", this.faderOpacity);
 } else {
  this.faderTimer = null;
  this.getXML(param);
  this.fadeIn();
  return;
 }
},

fadeIn : function () {
 if (this.faderTimer) return;
 mizSetOpacity("photogallery-image", 0);
 this.faderOpacity = 0;
 this.faderTimer = setTimeout(this.self + '.fadeIn_main()',16);
 },

fadeIn_main : function () {
 if (this.faderTimer) clearTimeout(this.faderTimer);
 if (this.faderOpacity < 100) {
  this.faderOpacity += Math.floor(100/this.faderFrms()); if (this.faderOpacity > 100 ) this.faderOpacity = 100;
  this.faderTimer = setTimeout(this.self + '.fadeIn_main()',16); 
  mizSetOpacity("photogallery-image", this.faderOpacity);
 } else {
  this.faderTimer = null;
  this.clearTimer();
  if (this.contents[this.pageNum].auto != false) 
   this.autoPlayTimer = setTimeout(this.self + '.nextPage()', this.autoWait);
  return;
 }
}

}

function getUrlParam( url, name, value ) {
 var pos_s = url.indexOf( name + "=" );
 if( pos_s < 0 ) return value;
 var url = url.substring( pos_s + name.length + 1 );
 var pos_e = url.indexOf( '&' );
 if( pos_e < 0 ) return decodeURIComponent( url );
 return decodeURIComponent( url.substring( 0, pos_e ));
}
// via: http://yamachan-cs.blogspot.com/2007/01/js-url.html

function getUrlHash() {
 return location.hash.substring(1);
}

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);
}


// var trace = function() { void(0); }
