/**

ImageMagic.js v1.0, 17.11.2008
Copyright (c) 2007-2008 Roland Kujundzic (http://www.web-to-print.eu)

@desc
Image functions. 

 */


function ImageMagic() {
}


/**

@desc
Prepare preview images (onMouseOver/onMouseOut).

@example
<div _preview="big.gif"><img src="small.gif"/></div>

 */
ImageMagic.prototype.preparePreview = function(conf) {

window.onload = function() {

  var omodiv = document.getElementsByTagName('div');

  for (var i = 0; i < omodiv.length; i++) {
    var preview = omodiv[i].attributes.getNamedItem('_preview');

    if (preview) {
      omodiv[i].style.position = 'relative';

      var img_src = conf.wait_picture ? conf.wait_picture : preview.value;
      omodiv[i].innerHTML = omodiv[i].innerHTML + '<div class="omo_preview"><img src="' + 
        img_src + '" style="margin:0;" /></div>';

      var tag  = omodiv[i].getElementsByTagName(conf.tag)[0];
      tag._div = omodiv[i].getElementsByTagName('div')[0];

      if (conf.wait_picture) {
        tag._load = preview.value;

        if (conf.has_picture) {
          tag._icon = conf.has_picture;
        }
      }

      tag.onmouseover = function () {

        if (this._load) {
          var preview_img = this._div.getElementsByTagName('img')[0];

          if (this._icon) {
            preview_img.onload = function () {
              this._icon.src = this._icon._icon;
            }
          }

          preview_img._icon = this;
          preview_img.src = this._load; 
          this._load = null;
        }

        this._div.style.visibility = "visible";
      }

      tag.onmouseout = function () {
        this._div.style.visibility = "hidden";
      }
    }
  }
}

}


/**

@desc
Prepare swappable images (onMouseOver/onMouseOut).

@example
<img src="a.gif" _swap="b.gif" />

 */
ImageMagic.prototype.prepareSwap = function() {

window.onload = function() {

  var images = document.getElementsByTagName('img');

  for (var i = 0; i < images.length; i++) {
    var swap = images[i].attributes.getNamedItem('_swap');

    if (swap) {
      images[i]._swap_over = new Image();
      images[i]._swap_over.src = swap.value;

      images[i]._swap_out = new Image();
      images[i]._swap_out.src = images[i].src;

      images[i].onmouseover = function () {
        this.src = this._swap_over.src;
      }

      images[i].onmouseout = function () {
        this.src = this._swap_out.src;
      }
    }
  }
}

}


/**

@desc
Show popup with image. Popup size is image size.

 */
ImageMagic.prototype.popup = function(img_url) {
  var img = new Image();

  img.onload = function() {

  var iw = window.open('', 'ImagePopup', 'toolbar=no,width=' + this.width +
    ',height=' + this.height);

  var title = this.src.split('/').pop();

  iw.document.write('<html><head><title>' + title + '<\/title><\/head>');
  iw.document.write('<body><img src="' + this.src + '"><\/body><\/html>');
  iw.document.close();

  iw.focus();
  }

  // when loading is finished ImageMagic::_popup() is called 
  img.src = img_url;
}


var image_magic = new ImageMagic();

