/**

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
Re-try image load (3x) if image is broken.

@example
<img src="test.png" onError="image_magick.reload(this[, 500])" >

 */
ImageMagic.prototype.reload = function(img_obj, wait) {

  if (this._is_reload) {
    this._is_reload++;
  }
  else {
    this._is_reload = 1;
    this._img = img_obj;
    this._wait = (wait > 0) ? wait : 500;
  }

  var new_img = new Image();
  new_img._im = this;

  setTimeout(function() {
        
    new_img.onload = function() {
      this._im._img.src = this.src;
    }

    new_img.onerror = function() {
      alert('reload try ' + this._im._is_reload + ' failed');
      if (this._im._is_reload < 3) {
        this._im.reload();
      }
    }

    alert('reload: ' + new_img._im._img.src);
    new_img.src = new_img._im._img.src;
  }, this._wait);
}


/**

@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 offset = '';
  if (this.width < screen.availWidth && this.height < screen.availHeight) {
    var x = screen.availWidth / 2 - this.width / 2;
    var y = screen.availHeight / 2 - this.height / 2;
    offset = ',left=' + x + ',top=' + y + ',screenX=' + x + ',screenY=' + y;
  }

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

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

  iw.document.write('<html><head><title>' + title + '</title></head>');
  iw.document.write('<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">');
  iw.document.write('<a href="javascript:window.close();">');
  iw.document.write('<img src="' + this.src + '" border="0">');
  iw.document.write('</a></body></html>');
  iw.document.close();

  iw.focus();
}

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


var image_magic = new ImageMagic();

