
function loadImageDialogs(){
	
	$('a[rel=imagedialog]').click(function(){
		
		var scrolltop = $(document).scrollTop()+300;
		
		var loader = $('<div style="display: none; position: absolute; top: ' + scrolltop + 'px; left: 50%; margin-left: -30px; width: 60px; height: 60px; opacity: 0.8; text-align: center; padding-top: 0; padding-right: 5px; margin: auto; box-shadow: 2px 2px 2px #000; -moz-border-radius: 4px; border-radius: 4px; -webkit-border-radius: 4px; background: #FFF; z-index: 100000;"><img src="plugins/imagedialog/ajax-loader.gif" alt="Loading..." /></div>').appendTo('body').fadeIn();
		
		var src= $(this).attr('href');
		var title= $(this).attr('title');
		
		var image = $('<img src="' + src + '" style="position: absolute; visibility: hidden;" />').appendTo('body');
		image.bind('load',function(){
			
			var imgw = image.width();
			var imgh = image.height();
			
			var window_height = $(window).height();
			var window_width = $(window).width();
			
			if((window_height-60)<image.height()){
				var height = window_height-100;
				var newimgh = height-70;
				var ratio = imgh/newimgh;
				var newimgw = imgw/ratio;
			}
			
						/*
			if(image.width()>900){
				var imgw = 900;
				var imgh = "auto";
			}
			*/
			
			if(!newimgw) newimgw = imgw;
			if(!newimgh) newimgh = imgh;
			
			var width = newimgw+23;
			var height = newimgh+50;
			
			//alert(imgh + ' ' + imgw + ' ' + height + ' ' + width);
			
			var imageDialog = '<div class="imagedialog" style="position: relative;"><img src="' + src + '" width="' + newimgw + '" height="' + newimgh + '" /><span style="padding: 10px; bottom: 8px; left: 5px; position: absolute; background: #FFF; opacity: 0.9;">' + title + '</span></div>';
			$(imageDialog).dialog({
				resizable: false,
				open: function(){
					image.remove();
					loader.html('Click Esc to close').css('width','150').css('height','auto').css('padding','10px').css('marginLeft','-75px');
					setTimeout(function(){
						$(loader).fadeOut('fast');
					},2000);
					var _self = $(this);
					$('.ui-widget-overlay').hide().fadeIn().click(function(){
						$(_self).dialog('close');
					});
				},
				show: "fade",
				height: height,
				width: width,
				modal: true,
				close: function() {
					$(loader).fadeOut('fast');
				}
			});

		});
		
		return false;
	}).attr('rel','imagedialogset');

}

$(document).ready(function() {
	loadImageDialogs();
});

