<!--

/**
 * ImageAjax (image_ajax.js)
 * Copyright (c) 2009 Jack Szwergold. All rights reserved.
 *
 * w: http://www.runwork.com
 * e: me@runwork.com
 *
 * Created: 2008-10-08
 * Version: 2009-10-10
 *
 */

  Event.observe(window, 'load', function () {
    var toggle = new toggleImageClass();
  });

  var toggleImageClass = Class.create();

  toggleImageClass.prototype = {

    initialize: function() {
      // Sets a container that will catch the boxes.
      this.magicbucket();
      this.bucket = ($('Bucket') != null) ? $('Bucket') : $$('body')[0];

      toggleImage = this.toggle.bind(this);
    },

    // Coding for fading the elements.
    toggle: function(paramsRAW) {
      var params = typeof(paramsRAW) != 'undefined' ? Object.toJSON(paramsRAW).evalJSON() : "{ 'null' : 'void' }";
      this.item_id = typeof(params.item_id) != 'undefined' ? params.item_id : '';
      this.type = typeof(params.type) != 'undefined' ? params.type : '';
      this.img_name = typeof(params.img_name) != 'undefined' ? params.img_name : '';
      this.url = typeof(params.url) != 'undefined' ? params.url : '';
      this.geometry = typeof(params.geometry) != 'undefined' ? params.geometry : '';
      this.position = typeof(params.position) != 'undefined' ? params.position : '';
      this.caption = typeof(params.caption) != 'undefined' ? params.caption : 0;
      this.caption = this.caption == true ? 1 : 0;

      var parent = this;
      if ($('zoom'+this.item_id) == null) {
        this.zoom_box = new Element('div', { 'class': 'Zoom', 'id': 'zoom'+this.item_id });
        this.zoom_box.hide();
        new Element.insert(this.bucket, { bottom: parent.zoom_box });
        this.fetch();
      }
      else {
        $('zoom'+this.item_id).fade({ duration: 0.25,
          afterFinish: function() {
            new Element.remove($('zoom'+parent.item_id));
            new Event.stopObserving(window, 'resize', function () { parent.positioninfo(); });
          }
        });
      }
    },

    fetch: function() {
      $('zoom'+this.item_id).appear({ duration: 0.25 });
      var pars = 'pn=image&view=embed&item_id='+this.item_id+'&type='+this.type+'&img_name='+this.img_name+'&geometry='+this.geometry+'&caption='+this.caption;
      var parent = this;
      new Ajax.Updater('zoom'+this.item_id, this.url, { method: 'get', parameters: pars,
        onComplete: function() {
          if (parent.caption == true && typeof($('zoom'+parent.item_id).down('div.Caption')) != "undefined" )
            $('zoom'+parent.item_id).down('div.Caption').setStyle({ 'width': $('zoom'+parent.item_id).down('img').getWidth()+'px' });
          parent.positioninfo();
          new Event.observe(window, 'resize', function () { parent.positioninfo(); });
        }
      });
    },

    positioninfo: function() {
      var offsetLeft = (this.position == 'R') ? -($('zoom'+this.item_id).down('div.Padding').getWidth()-$('click'+this.item_id).getWidth()) : 0;
      $('zoom'+this.item_id).clonePosition('click'+this.item_id, { 'setWidth': false, 'setHeight': false, 'offsetLeft': offsetLeft } );
    },

    // create the magic bucket
    magicbucket: function () {
      if ($('Bucket') == null) {
        var element = new Element('span', { 'id': 'Bucket' }).setStyle({ 'position': 'absolute', 'left': '0', 'bottom': '0' });
	    Element.insert($$('body')[0], { bottom: element } );
      }
    }

  }

// -->
