// $Id: expandimage.js,v 1.43 2007/07/31 14:12:03 akira Exp $

var asablo = new Object;
asablo.option = new Object;
asablo.alert = function(msg) {
 alert(msg + "(error reported by $Id: expandimage.js,v 1.43 2007/07/31 14:12:03 akira Exp $)");
}
asablo.addevent = function(o, type, fn) {
 if (o.addEventListner) {
  o.addEventListner(type, fn, false);
  return true;
 } else if (o.attachEvent) {
  var r = o.attachEvent("on" + type, fn);
  return r;
 } else {
  return false;
 }
};
asablo.bodyonload = function(o) {
 try {
  if (! asablo.option.noimagewatermark) WatermarkImages();
 } catch(err) {}
 return true;
};

asablo.noHTMLElement = false;
try {
 HTMLElement;
} catch(err) { asablo.noHTMLElement = true; }

if (! (document.body && document.body.insertAdjacentHTML) || asablo.noHTMLElement) {
 try {
  if (asablo.noHTMLElement) {
   document.createElement('html');
   HTMLElement = { prototype: window["[[DOMElement.prototype]]"] || {}};
  }

  HTMLElement.prototype.insertAdjacentHTML = function(where, text) {
   var r = this.ownerDocument.createRange();
   if (where == 'afterBegin' || where == 'beforeEnd') {
    r.selectNodeContents(this);
    r.collapse(where == 'afterBegin');
   } else {
    r.selectNode(this);
    r.collapse(where == 'beforeBegin');
   }
   r.insertNode(r.createContextualFragment(text));
  }
  if (!navigator.userAgent.match (/WebKit/i)) {
   HTMLElement.prototype.addEventListener = function(name,func,dir) {
    this.attachEvent(name, func);
   }
  }
 } catch(err) {
  asablo.alert("Your browser is not supported: " + err);
 }
}

try {
 var ua = navigator.userAgent.toLowerCase();
 asablo.isOpera = ua.match('opera') ? true : false;
 // note: window.opera is available only during visualization
 asablo.isOpera7 = function() {
  return asablo.isOpera && typeof document.body.style.opacity == "undefined";
 };
 // note: body.style is available only during visualization
 asablo.isIE6 = function() {
  return document.all && typeof document.body.style.maxHeight == "undefined";
 };
 asablo.isSafari = ua.match('safari');
 asablo.isMacSafari = asablo.noHTMLElement;

 // DisplayImage - display image
 asablo.DisplayImage = function(o) {
  o.style.display = 'block';
  o.parentNode.style.backgroundPosition = '-100px -100px';
 };
} catch(err) {
 asablo.alert("Your browser is not supported: " + err);
}

// ExpandImage - expand image
function ExpandImage(o, imgwidth, imgheight, origimgurl) {
 // o: A element that contains IMG element as its first child (thumbnail)
 var imgscr = document.getElementById('imagescreen');
 var height = document.height || document.documentElement.scrollHeight || document.body.scrollHeight;
 var width = document.documentElement.scrollWidth || document.body.scrollWidth;
 var yoffset = document.documentElement.scrollTop || document.body.scrollTop;
 if (! imgscr) {
  var fc = o.firstChild;
  if (fc.tagName != 'IMG') {
   // not IMG, this is not supported
   return true;
  }
  var htmlfragment = "<div id='imagescreen'><div class='imagebackground'><div></div></div><div class='imagefull'><img class='imagedata'><div class=imagecomment>クリックで閉じる</div></div></div>";

  document.body.style.width = null;
  document.body.insertAdjacentHTML("AfterBegin", htmlfragment);
  imgscr = document.getElementById('imagescreen');
 }

 var imgbg = imgscr.firstChild;
 var imgfull = imgbg.nextSibling;
 var imgdata = imgfull.firstChild;
 try {
  imgdata.owidth = imgwidth;
  imgdata.oheight = imgheight;
  if (0) {  //- change this if you want to have wheel action
   imgdata.scale = 100;
   document.body.onmousewheel = function() {
    var x = document.getElementById('imagescreen');
    if (x) {
     x = x.firstChild.nextSibling.firstChild;
     x.scale += event.wheelDelta / 60;
     if (x.scale < 1) x.scale = 2;
     if (x.scale > 200) x.scale = 200;
     x.style.width = x.scale * x.owidth / 100;
     x.style.height = x.scale * x.oheight / 100;
    }
    return false;
   };
  }
 } catch(err) {
 }
 var clwidth = document.documentElement.clientWidth;
 if (clwidth > width || asablo.isIE6() || asablo.isOpera7()) {
  width = clwidth;
  imgscr.style.width = imgbg.style.width = imgfull.style.width = width + 'px';
 }
 var winheight = document.all ? document.documentElement.clientHeight || document.body.clientHeight : window.innerHeight;
 var winwidth = document.all ? document.documentElement.clientWidth || document.body.clientWidth : window.innerWidth;
 var minheight = height + yoffset;
 if (minheight < winheight) minheight = winheight; 
 imgscr.style.height = imgbg.style.height = minheight + 'px';
 imgfull.style.height = imgheight + 'px';
 imgfull.style.top = yoffset + 'px';
 imgdata.style.width = imgwidth + 'px';
 imgdata.style.height = imgheight + 'px';
 imgfull.style.backgroundPosition =
  (winwidth / 2) + 'px ' + (winheight / 2) + 'px';
 imgbg.onclick = imgfull.onclick = function(e) {
  var imgscr = document.getElementById('imagescreen');
  // hide imagescreen
  imgscr.style.display = 'none';
  // and imgdata
  imgscr.firstChild.nextSibling.firstChild.style.display = 'none';
  if (document.all) { event.cancelBubble = true; } else if (e) { e.stopPropagation(); }
  try {
   document.body.onmousewheel = null;
  } catch(err) {}
  return false;
 };
 imgdata.style.display = 'none';
 imgdata.onload = function() { asablo.DisplayImage(this); };

 var samedata = (imgdata.src == origimgurl);
 if (samedata) {
  imgdata.src = origimgurl;
  if (asablo.isMacSafari || asablo.isOpera || asablo.isSafari && imgdata.complete) {
   asablo.DisplayImage(imgdata);
  }
 } else {
  imgdata.src = origimgurl;
  if (asablo.isOpera7()) asablo.DisplayImage(imgdata);
 }

 imgscr.style.display = 'block';

 return false;
}

asablo.expandimage = function(o, imgwidth, imgheight, origimgurl) {
 return ExpandImage(o, imgwidth, imgheight, origimgurl);
};

// WatermarkImages() - image watermark
function WatermarkImages() {
 try {
  var e = document.getElementsByTagName('div');
  var elen = e.length;
  for (var i = 0; i < elen;  i++) {
   var o = e[i];
   if (o.className == 'msg-pict' && o.firstChild.tagName == 'A') {
    var im = o.firstChild.firstChild;
    if (im.tagName != 'IMG') im = im.nextSibling;
    var il = 4;
    if (im.offsetLeft > 100) {
     //- assume right aligned picture (IE's weird behavior)
     var ro = o.offsetWidth - im.offsetWidth;
     if (ro < 0) ro = 0;
     if (asablo.isIE6()) ro = 0;
     il += ro;
    } else {
     if (asablo.isIE6()) {
      il = 4;
     } else {
      il += im.offsetLeft;
     }
    }
    var ih = im.height - 18;
	if (ih < 0) ih = 0;
    var htmlfragment = '<div class="msg-pict-overlay" onClick="this.nextSibling.onclick()" style="position: absolute; top: ' + ih + 'px; left: ' + il + 'px;"><span></span></div>';
    o.insertAdjacentHTML('afterBegin', htmlfragment);
   }
  }
  return true;
 } catch(err) {
  alert("WatermarkImages failed (" + err + ")");
  return false;
 }
}
