Object.extend(String.prototype, {
  // if a string doesn't end with str it appends it
  ensureEndsWith: function(str) {
    return this.endsWith(str) ? this : this + str;
  },
  
  // makes sure that string ends with px (for setting widths and heights)
  px: function() {
    return this.ensureEndsWith('px');
  }
});

Object.extend(Number.prototype, {
  // makes sure that number ends with px (for setting widths and heights)
  px: function() {
    return this.toString().px();
  }
});

var Window = {
  // returns correct dimensions for window, had issues with prototype's sometimes. this was ganked from apple.
  size: function() {
var width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
var height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
var x = window.pageXOffset || (window.document.documentElement.scrollLeft || window.document.body.scrollLeft);
var y = window.pageYOffset || (window.document.documentElement.scrollTop || window.document.body.scrollTop);
return {'width':width, 'height':height, 'x':x, 'y':y}
}
}

var TollpostFancyZoomBox = {
  zooming : false,
  setup : false,
  
  init: function() {
    if (TollpostFancyZoomBox.setup) return;
    TollpostFancyZoomBox.setup = true;
    
    var ie = navigator.userAgent.match(/MSIE\s(\d)+/);
    if (ie) {
      var version = parseInt(ie[1]);
      Prototype.Browser['IE' + version.toString()] = true;
      Prototype.Browser.ltIE7 = (version < 7) ? true : false;
    }
    
    var html = '<div id="zoom" style="display:none;z-index:3000"> \
<table id="zoom_table" style="border-collapse:collapse; width:100%; height:100%;"> \
<tbody> \
<tr> \
<td class="tl" /> \
<td class="tm" /> \
<td class="tr" /> \
</tr> \
<tr> \
<td class="ml" /> \
<td class="mm"> \
<div id="zoom_content"> \
</div> \
</td> \
<td class="mr" /> \
</tr> \
<tr> \
<td class="bl" /> \
<td class="bm" /> \
<td class="br" /> \
</tr> \
</tbody> \
</table> \
<a href="#" title="Close" id="zoom_close" style="position:absolute; top:0; left:0;"> \
<div class="close_button"></div> \
</a> \
</div>';
    
    var body = $$('body').first();
    body.insert(html);
    
    TollpostFancyZoomBox.zoom = $('zoom');
    TollpostFancyZoomBox.zoom_table = $('zoom_table');
    TollpostFancyZoomBox.zoom_close = $('zoom_close');
    TollpostFancyZoomBox.zoom_content = $('zoom_content');
    TollpostFancyZoomBox.zoom_close.observe('click', TollpostFancyZoomBox.hide);
    TollpostFancyZoomBox.middle_row = $A([$$('td.ml'), $$('td.mm'), $$('td.mr')]).flatten();
    TollpostFancyZoomBox.cells = TollpostFancyZoomBox.zoom_table.select('td');
    
    // hide zoom if click fired is not inside zoom
    $$('html').first().observe('click', function(e) {
      var click_in_zoom = e.findElement('#zoom'),
          zoom_display = TollpostFancyZoomBox.zoom.getStyle('display');
      if (zoom_display == 'block' && !click_in_zoom) {
        TollpostFancyZoomBox.hide(e);
      }
    });

    // esc to close zoom box
    $(document).observe('keyup', function(e) {
      var zoom_display = TollpostFancyZoomBox.zoom.getStyle('display');
      if (e.keyCode == Event.KEY_ESC && zoom_display == 'block') {
        TollpostFancyZoomBox.hide(e);
      }
    });
    
    // just use gifs as ie6 and below suck
    if (Prototype.Browser.ltIE7) {
      TollpostFancyZoomBox.switchBackgroundImagesTo('gif');
    }
  },
  
  show: function(e) {
    e.stop();
if (TollpostFancyZoomBox.zooming) return;
TollpostFancyZoomBox.zooming = true;
var related_div = $('lightBox');
var width = 660;
var height = 500;
var d = Window.size();
var yOffset = document.viewport.getScrollOffsets()[1];
// ensure that newTop is at least 0 so it doesn't hide close button
var newTop = Math.max((d.height/2) - (height/2) + yOffset, 0);
var newLeft = (d.width/2) - (width/2);
//var newTop = 100;
//var newLeft = 100;
TollpostFancyZoomBox.curTop = e.pointerY();
TollpostFancyZoomBox.curLeft = e.pointerX();
TollpostFancyZoomBox.moveX = -(TollpostFancyZoomBox.curLeft - newLeft);
TollpostFancyZoomBox.moveY = -(TollpostFancyZoomBox.curTop - newTop);
    TollpostFancyZoomBox.zoom.hide().setStyle({
position : 'absolute',
top : TollpostFancyZoomBox.curTop.px(),
left : TollpostFancyZoomBox.curLeft.px()
});
    
new Effect.Parallel([
new Effect.Appear(TollpostFancyZoomBox.zoom, {sync:true}),
new Effect.Move(TollpostFancyZoomBox.zoom, {x: TollpostFancyZoomBox.moveX, y: TollpostFancyZoomBox.moveY, sync: true}),
new Effect.Morph(TollpostFancyZoomBox.zoom, {
style: {
width: width.px(),
height: height.px()
},
sync: true,
beforeStart: function(effect) {
// middle row height must be set for IE otherwise it tries to be "logical" with the height
     if (Prototype.Browser.IE) {
     TollpostFancyZoomBox.middle_row.invoke('setStyle', {height:(height-40).px()});
     }
TollpostFancyZoomBox.fixBackgroundsForIE();
},
afterFinish: function(effect) {
TollpostFancyZoomBox.zoom_content.innerHTML = related_div.innerHTML;
TollpostFancyZoomBox.unfixBackgroundsForIE();
TollpostFancyZoomBox.zoom_close.show();
TollpostFancyZoomBox.zooming = false;
}
})
], { duration: 1});
  },
  
  hide: function(e) {
    e.stop();

if (TollpostFancyZoomBox.zooming) return;
TollpostFancyZoomBox.zooming = true;
new Effect.Parallel([
new Effect.Move(TollpostFancyZoomBox.zoom, {x: TollpostFancyZoomBox.moveX*-1, y: TollpostFancyZoomBox.moveY*-1, sync: true}),
new Effect.Morph(TollpostFancyZoomBox.zoom, {
style: {
width: '1'.px(),
height: '1'.px()
},
sync : true,
beforeStart: function(effect) {
TollpostFancyZoomBox.fixBackgroundsForIE();
TollpostFancyZoomBox.zoom_content.innerHTML = '';
TollpostFancyZoomBox.zoom_close.hide();
},
afterFinish: function(effect) {
TollpostFancyZoomBox.unfixBackgroundsForIE();
TollpostFancyZoomBox.zooming = false;
}
}),
new Effect.Fade(TollpostFancyZoomBox.zoom, {sync:true})
], { duration: 1 });
  },
  
  // switches the backgrounds of the cells and the close image to png's or gif's
  // fixes ie's issues with fading and appearing transparent png's with
  // no background and ie6's craptacular handling of transparent png's
  switchBackgroundImagesTo: function(to) {
    TollpostFancyZoomBox.cells.each(function(td) {
      var bg = td.getStyle('background-image').gsub(/\.(png|gif|none)\)$/, '.' + to + ')');
      td.setStyle('background-image: ' + bg);
    });


	var close_img = TollpostFancyZoomBox.zoom_close.firstDescendant();
	var bg = close_img.getStyle('background-image').gsub(/\.(png|gif|none)\)$/, '.' + to + ')');
	close_img.setStyle('background-image: ' + bg);
/*
	var new_img = close_img.readAttribute('src').gsub(/\.(png|gif|none)$/, '.' + to);
    close_img.writeAttribute('src', new_img);
*/
  },
  
  // prevents the thick black border that happens when appearing or fading png in IE
fixBackgroundsForIE: function() {
    if (Prototype.Browser.IE7) { TollpostFancyZoomBox.switchBackgroundImagesTo('gif'); }
},

// swaps back to png's for prettier shadows
unfixBackgroundsForIE: function() {
    if (Prototype.Browser.IE7) { TollpostFancyZoomBox.switchBackgroundImagesTo('png'); }
}
}
