/*
MooSizer - a Mootools rewrite of: Supersized - Full Screen Background/Slideshow jQuery Plugin

License:
	MIT-style license.

Credits:
	Original jQuery supersized script By Sam Dunn ( <http://buildinternet.com> / <http://onemightyroar.com>	 )
	found here: <http://buildinternet.com/2009/02/supersized-full-screen-backgroundslideshow-jquery-plugin/>
	rewritten for Mootools 1.2 by Markus Timtner ( <http://mtness.net> ) 2009-03-27 1100-1500 GMT+1
*/

var mooSizer = new Class({

	Implements: [Options, Events],
	options: {
		startwidth: 1280,
		startheight: 600,
		minsize: .5,
		slideshow: 1,
		slideinterval: 5000,
		bgElement: '',
		imgElement: '',
		crop_enter: true
	},

	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.imgElement).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).height(minheight);
			$(this.options.bgElement).setStyle('height', minheight);
			//$(this).width(minwidth);
			$(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);
				}
			}
		}
		// CROP_CENTER
		if(this.options.crop_enter == true) {
			// CENTRAGE HORIZONTAL
			var decalwidth = imagewidth - browserwidth;
			decalwidth /= 2;
			$(this.options.bgElement).setStyle('margin-left', -decalwidth);
			// CENTRAGE VERTICAL
			var decalheight = imageheight - browserheight;
			decalheight /= 2;
			$(this.options.bgElement).setStyle('margin-top', -decalheight);
		}
	}
});
