/* 
 * Posterous Widget Gallery
 * 
 * Copyright 2008 posterous.com All Rights Reserved 
 * garry@posterous.com
 */

if (PWidgets == null) {

	var PWidgets = "loaded";
//	alert("this is run");
	
	Event.observe(window, 'load',
		function() { 
  		    var items = $$('div.post img');
			for (var i = 0; i < items.length; i++) {
				var img = items[i];
				if (img.width > 500) {
					new PosterousLargeExternal(img);
				}
			}
		}
	);
	
	var PosterousLargeExternal = Class.create({
		initialize: function(element){
			this.element = element;
			this.oldWidth = element.width;
			this.oldHeight = element.height;
			element.observe('click', this.handleClick.bindAsEventListener(this));
			
			var newdim = posterous_resize_image(element.width, element.height, 500);
			if (newdim != null) {
				element.width = newdim[0];
				element.height = newdim[1];
			}
			element.addClassName('pointer_cursor');	
		},
		handleClick: function() {
			var modal = Control.Modal.open(
				"<a href='#' onClick='Control.Modal.close();return false;'>"+
				"<img src='" + this.element.src + "'" +
				" width='" + this.oldWidth + "'" + " height='" + this.oldHeight + "'" + "></a>", 
				{ opacity: 0.6 });
		}
	});
	
	
	
	function posterous_resize_image(width, height, maxWidth) {
		if (width > maxWidth) {
			return [maxWidth, maxWidth * height / width];
		} else {
			return null;
		}
	}

	var pw_stopclick = function(e) {
	    Event.stop(e);
	};

}