/*----------------------------------------------
mooZoom v1.0

mooZoom copyright: copyright (c) 2008 R'born development  - http://rborn.info

mooZoom license: 

You can use the script for personal or commercial projects, as long as this creditentials remains intact.
You CANNOT sell the script or parts of its code under your brand.

*/

var mooZoom = new Class({
  options: {},
  initialize: function(options) {
    this.x_fact = options.x_fact || 1.2;
    this.initial_position = options.image.getCoordinates();

    this.image = options.image.clone().injectAfter(options.image);
    options.image.setStyle('visibility', 'hidden');

    this.resetToOrigin();
    this.initial_size = options.image.getSize().size;
    this.container = new Element('div').setStyles({ width: this.initial_size.x, height: this.initial_size.y, position: 'absolute', left: this.initial_position.left, top: this.initial_position.top }).injectInside(document.body).adopt(this.image);

    this.container.addEvent('mousemove', function(ev) {
      var ev = new Event(ev);
      this.mouse_x = ev.client.x + window.getScrollLeft();
      this.mouse_y = ev.client.y + window.getScrollTop();

    } .bind(this));

    this.image.makeDraggable({
      wait: true
    });


    this.busy = false;
    this.zoom();

  },

  resetToOrigin: function() {
    this.image.setStyles({ position: 'relative', left: 0, top: 0, margin: 0 });
  },

  zoomToFit: function() {
    this.resetToOrigin();
    // set to size of window
    var w = document.documentElement.clientWidth;
    var h = document.documentElement.clientHeight;
    var zoom_w = w / this.image.width;
    var zoom_h = h / this.image.height;
    this.x_fact = Math.min(zoom_w, zoom_h);
    var zoom_out = this.createZoom();
    zoom_out.start({ 'left': 0, 'top': 0,
      'height': this.image.height * this.x_fact,
      'width': this.image.width * this.x_fact
    });
    this.zoomMap(this.x_fact);
  },

  zoomMap: function(amount) {
    // resize image map
    var map = document.getElementById(this.image.useMap.substring(1));
    if (map != null) {
      for (var i = 0; i < map.areas.length; i++) {
        var area = map.areas[i];
        var coords = area.coords.split(',');
        for (var j = 0; j < coords.length; j++) {
          coords[j] = coords[j] * amount;
        }
        area.coords = coords[0] + ',' + coords[1] + ',' + coords[2] + ',' + coords[3];
      }
    }
  },

  createZoom: function() {
    return new Fx.Styles(this.image,
		{
		  onComplete: function() { this.busy = false; } .bind(this),
		  duration: 100, transition: Fx.Transitions.linear, wait: true
		});
  },

  zoom: function() {

    this.image.addEvent('mousewheel',
		function(e) {

		  var e = new Event(e).stop();

		  if (!this.busy) {
		    if (!this.mouse_x) {
		      coord = e.target.getCoordinates();
		      this.mouse_x = coord.left + coord.width / 2;
		      this.mouse_y = coord.top + coord.height / 2;
		    }
		  }

		  var coord = this.image.getCoordinates();

		  rapx = (this.mouse_x - coord.left) / coord.width;
		  rapy = (this.mouse_y - coord.top) / coord.height;

		  // zoom out ---------------
		  if (e.wheel < 0) {
		    if (!this.busy) {
		      this.busy = true;

		      xo = parseInt(this.image.getStyle('left'))
		      yo = parseInt(this.image.getStyle('top'))

		      new_h = (coord.height / this.x_fact);
		      new_w = (coord.width / this.x_fact);

		      var zoom_out = this.createZoom();

		      zoom_out.start({ 'left': xo, 'top': yo, 'height': new_h, 'width': new_w });
		      this.zoomMap(1 / this.x_fact);
		    }
		  }

		  // zoom in -----------
		  else if (e.wheel > 0) {

		    if (!this.busy) {
		      this.busy = true;

		      xi = parseInt(this.image.getStyle('left'))
		      yi = parseInt(this.image.getStyle('top'))

		      hdiff = -((this.x_fact - 1) * coord.height * rapy + this.container.getCoordinates().top - coord.top);
		      wdiff = -((this.x_fact - 1) * coord.width * rapx + this.container.getCoordinates().left - coord.left);

		      if (hdiff < 0)
		        hdiff = 0;
		      if (wdiff < 0)
		        wdiff = 0;

		      new_h = (coord.height * this.x_fact);
		      new_w = (coord.width * this.x_fact);

		      var zoom_in = this.createZoom();

		      zoom_in.start({
		        'left': xi,
		        'top': yi,
		        'height': new_h,
		        'width': new_w
		      });

		      this.zoomMap(this.x_fact);
		    }
		  }

		} .bind(this));
  }
})

window.addEvent('load', function() {
  $$('.moozoom').each(function(item) { new mooZoom({ image: item }); });
});