


$.fn.tmodal = function()
{
	$(this).click(function(){
			var href = this.href;
			//var modal = new $.tmodal(href);
			try
			{
				$.tmodal.show(href);
			}
			catch (e)
			{
				alert(e);
			}
			return $.tmodal.error;
		});
};


$(function(){
	$.tmodal = new Class_tmodal();
});

//if ($.browser.msie) return 'ie'+$.browser.version.match(/[0-9]+/));
//else $('body').addClass('std');

Class_tmodal = function() { };

Class_tmodal.prototype =
{
	url: false,
	error: false,
	buttons: null,	// holds the captions

	show: function(url)
	{
		try
		{
			this.shader.create();
			this.window.create();

			if (this.url_is_image(url))
			{
				$.tmodal.imageviewer.load(url);
			}
			else
			{
				$.tmodal.content.html("TModal ondersteunt alleen nog maar plaatjes.");
			}
		}
		catch (error)
		{
			alert("an error occurred\n"+error);
			this.error = error;
		}

	},

	close: function()
	{
		$.tmodal.window.destroy();
		$.tmodal.shader.destroy();
		//alert($('body').html());
	},


	url_is_image: function(url)		///  'static'
	{
		return /\.(jpg|gif|png)$/i.test(url);
	},


	shader:
	{
		create: function()
		{
			if (this.handle) { return; }	// shader already exists

			$('body').append('<div id="tmodal_shader"></div>');
			this.handle = $('#tmodal_shader');
			
			this.handle.css({opacity: 0, backgroundColor: 'black'});
			
			this.handle.fullscreen();
			
			//this.handle.css({opacity: 0.6});
			this.handle.animate({opacity: 0.6}, 300);
			
			this.attach_events();
			
			this.handle.css({zOrder: 999});
		},

		destroy: function()
		{
			if (this.handle)
			{
				//this.handle.uncenter();
				this.handle.remove();
				this.handle = null;
				$(document).unbind('keypress', this.keypress);
			}
		},


		attach_events: function()
		{
			this.handle.click($.tmodal.close);
			$(document).keypress(this.keypress);
		},
		
		keypress: function(event) { if (event.keyCode==27) $.tmodal.close();},

		handle: null
	},




	window:
	{
		show: function()		/// public
		{

			this.content.css({height:'auto', width: 'auto'});
			this.handle.center({click: $.tmodal.close});
			//this.handle.center();
			this.handle.css({zOrder: 1000});
			this.set_maxheight();
			
			
			
			//this.content.height(400);
			
			
			
			if (this.handle.css('opacity')!=1)
			{
				//this.handle.css({opacity: 1});
				this.handle.animate({opacity: 1.0}, 300);
				
				/*$.tmodal.shader.handle.queue(function()
					{
						$('#tmodal_window').css({opacity: 1});
						//$('#tmodal_window').animate({opacity: 1.0}, 400);
					});*/
			}
			
			
		},

		create: function()
		{
			if (this.handle) { return; }	// window already exists

			$('body').append('<div id="tmodal_window"><div id="tmodal_header"></div><div id="tmodal_content">content</div><div id="tmodal_footer">footer</div></div>');

			this.handle = $('#tmodal_window');
			this.header = $('#tmodal_header');
			this.content = $('#tmodal_content');
			this.footer = $('#tmodal_footer');
			
			this.handle.css({opacity: 0.5, zOrder: 1000});

		},

		destroy: function()
		{
			if (this.handle) 
			{
				this.handle.uncenter();
				this.handle.remove();
				this.handle = this.content = this.footer = 0;
				//alert('window destroyed');
			}
			
		},

		set_maxheight: function()
		{
			var height = this.handle.height();
			var width = this.handle.width();

			var window_height = $(window).height();
			
			var overflow = height - window_height + 100;
			
			if (overflow > 0) {
				this.content.height(this.content.height() - overflow);
				this.handle.center();
			}
		},

		handle: null,			/// public
		content: null,
		footer: null

	},

	imageviewer:
	{
		image: null,
		preload_image: null,
		image_url: null,
		//jq_image: null,
		//jq_footer: null,
		imagelist: null,
		image_index: -1,

		load: function(src)
		{
			this.image_url = src;
			this.load_window();
			this.load_imagelist();
			this.preload();
		},

		load_window: function()
		{
			
			$.tmodal.window.content.html('<img id="tmodal_image"/>');
			this.image = $('#tmodal_image');
			

			$.tmodal.window.footer.html('')
				.append('<a href="javascript:$.tmodal.close()" id="tmodal_close"><img src="pictures/24-em-cross.gif"/>Sluit dit venster</a>')
				.append(' <a href="javascript:$.tmodal.imageviewer.print()" id="tmodal_print"><img src="pictures/printer.gif"/> Afdrukken</a>')
				.append(' <a href="javascript:$.tmodal.imageviewer.prev()" id="tmodal_prev" style="visibility:hidden"><img src="pictures/24-arrow-previous.gif"/> Vorige</a>')
				.append(' <a href="javascript:$.tmodal.imageviewer.next()" id="tmodal_next" style="visibility:hidden">Volgende <img src="pictures/24-arrow-next.gif"/></a>');
		},

		preloaded: function()
		{
			this.image.attr('src', this.image_url);
			
			this.refresh_buttons();
			
			
			
			
			$.tmodal.window.show();
			
			window.setTimeout(function(){		// needed in FF 3.0
						$.tmodal.window.show();
					}, 50);
			
		},

		preload: function()
		{
			this.preload_image = new Image();
			var $this=this;
			this.preload_image.onload = function(){$this.preloaded()};
			//this.preload_image.onload = $.tmodal.imageviewer.preloaded;

			this.preload_image.src = this.image_url;
		},

		prev: function()
		{
			if (this.image_index>0)
			{
				this.image_url = this.imagelist[--this.image_index]
				this.preload();
			}
		},

		next: function()
		{
			if (this.image_index < this.imagelist.length-1)
			{
				this.image_url = this.imagelist[++this.image_index]; 
				this.preload();
			}
		},
		
		print: function()
		{
			//alert(this.image_url);
			var id = /\/([0-9]+)_[a-z]+\.[a-z]+$/.exec(this.image_url)[1];
			//alert(id);
			//location.href="voorraad/image/"+id+"?print=true";
			location.href="http://www.liberunits.nl/voorraad/image/"+id+"?print=true";
		},
		
		refresh_buttons: function()
		{
			$('#tmodal_prev, #tmodal_next').css({visibility: 'hidden'});

			if (this.image_index>0) $('#tmodal_prev').css({visibility: 'visible'});
			if (this.image_index<this.imagelist.length-1)  $('#tmodal_next').css({visibility: 'visible'});
		},

		load_imagelist: function()
		{
			var url = this.image_url;
			this.imagelist = new Array();

			$('a[href].popup').each(function(i){
					//var href = $(this).attr('href');
					var href = this.href;
					if (/\.(jpg|gif|jpeg|png)$/.test(href))
					{
						var iv = $.tmodal.imageviewer;
						if (url==href) iv.image_index = iv.imagelist.length;
						iv.imagelist.push(href);
					}
				});
	
			
		}






	}/*,


	*/


};
