/*
MooSizer - a Mootools rewrite of Supersized - Full Screen Background/Slideshow jQuery Plugin
License: MIT-style license.
Credits: Original jQuery supersized script By Sam Dunn rewritten for Mootools 1.2 by Markus Timtner; 2009-03-27
*/

document.addEvent('domready', function(){
		moosizer = new mooSizer({ bgElement:'myGallery' });
});

var DEBUG;(typeof(window.console) != "undefined")?DEBUG=1:DEBUG=0;

var mooSizer = new Class({

	Implements: [Options, Events],
	options: {
		startwidth: 640,  
		startheight: 480,
		minsize: .5,
		slideshow: 1,
		slideinterval: 5000,
		bgElement: ''
	},

	initialize: function(options){													
        this.setOptions(options);

		//Define image ratio & minimum dimensions
		var minwidth	= this.options.minsize*(this.options.startwidth);			
		var minheight	= this.options.minsize*(this.options.startheight);			
		var ratio		= this.options.startheight/this.options.startwidth;		

		this.resizenow(minwidth,minheight,ratio);

 		window.addEvent('resize', function(){										
			this.resizenow(minwidth,minheight,ratio);
		}.bind(this));
	},
	
	resizenow: function(minwidth,minheight,ratio) {								
		//Gather browser and current image size
		var imagesize		= $(this.options.bgElement).getSize();
		var imagewidth		= imagesize.x;											
		var imageheight		= imagesize.y;											
		var clientsize		= window.getSize();
		var browserwidth	= clientsize.x;											
		var browserheight	= clientsize.y;											
		
 		//Check for minimum dimensions
		if ((browserheight < minheight) && (browserwidth < minwidth)){				
				$(this.options.bgElement).setStyle('height',minheight);
				$(this.options.bgElement).setStyle('width',minwidth);
		}
		else{	
			//When browser is taller	
			if (browserheight > browserwidth){
				imageheight = browserheight;
					$(this.options.bgElement).setStyle('height',browserheight);
				imagewidth = browserheight/ratio;								
					$(this.options.bgElement).setStyle('width',imagewidth);
				
				if (browserwidth > imagewidth){									
					imagewidth = browserwidth;										
						$(this.options.bgElement).setStyle('width',browserwidth);
					imageheight = browserwidth * ratio;							
						$(this.options.bgElement).setStyle('height',imageheight);
				}
			}			
			//When browser is wider
			if (browserwidth >= browserheight){							
				imagewidth = browserwidth;									
					$(this.options.bgElement).setStyle('width',browserwidth);
				imageheight = browserwidth * ratio;			
					$(this.options.bgElement).setStyle('height',imageheight);
				
				if (browserheight > imageheight){								
					imageheight = browserheight;							
						$(this.options.bgElement).setStyle('height',browserheight);
					imagewidth = browserheight/ratio;		
						$(this.options.bgElement).setStyle('width',imagewidth);
				}
			}
		}
	}

});

