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

var mooSizer = new Class({

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

	initialize: function(options){													//if(DEBUG==1)console.log("class initialised");
        this.setOptions(options);

		//Define image ratio & minimum dimensions
		var minwidth	= this.options.minsize*(this.options.startwidth);			//if(DEBUG==1)console.log("minwidth	"+minwidth	);
		var minheight	= this.options.minsize*(this.options.startheight);			//if(DEBUG==1)console.log("minheight	"+minheight	);
		var ratio		= this.options.startheight/this.options.startwidth;		//if(DEBUG==1)console.log("ratio		"+ratio		);

		this.resizenow(minwidth,minheight,ratio);
        

        window.addEvent('resize', function(){										//if(DEBUG==1)console.log("resizenow event fired");
			this.resizenow(minwidth,minheight,ratio);

		}.bind(this));

				
		document.addEvent('orientationchange', function(){
		    this.resizenow(minwidth,minheight,ratio);
		    initTouchDevice(); // Ruft die FUnktion aus der site.js auf, um die aktuelle Orientation abzufragen
		    iPadFunctions(); // iPad Funktionen aufrufen
		}.bind(this));
		
		window.addEvent('orientationchange', function(){
		    this.resizenow(minwidth,minheight,ratio);
		    initTouchDevice(); // Ruft die FUnktion aus der site.js auf, um die aktuelle Orientation abzufragen
		    iPadFunctions(); // iPad Funktionen aufrufen
		}.bind(this));

	},
	
	resizenow: function(minwidth,minheight,ratio) {								// if(DEBUG==1)console.log("resizenow called");

		//Gather browser and current image size
		var imagesize		= $(this.options.bgElement).getSize();
		var imagewidth		= imagesize.x;											//if(DEBUG==1)console.log("imagewidth		"+imagewidth	);
		var imageheight		= imagesize.y;											//if(DEBUG==1)console.log("imageheight	"+imageheight	);
		var clientsize		= window.getSize();
		var scrollsize      = window.getScrollSize();
		
		if(isiDevice && !isiOS5)
		{
		   	var browserwidth	= scrollsize.x;											//if(DEBUG==1)console.log("browserwidth	"+browserwidth	);
    		var browserheight	= scrollsize.y;											//if(DEBUG==1)console.log("browserheight	"+browserheight	); 
		} else {
		    var browserwidth	= clientsize.x;											//if(DEBUG==1)console.log("browserwidth	"+browserwidth	);
    		var browserheight	= clientsize.y;											//if(DEBUG==1)console.log("browserheight	"+browserheight	);
		}
		
		if(isiPad && !isiOS5){
		    $$('#navi_wrapper li').each(function(item, index){
		        item.setStyle('height',scrollsize.y);
		        if(!naviSetLeft)
		        {
		            item.setStyle('left',window.getSize().x/2-180);
		        }
		        
		    });
		}

		
 		//Check for minimum dimensions
		if ((browserheight < minheight) && (browserwidth < minwidth)){				//if(DEBUG==1)console.log("within minimum dimensions");
				//$(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){										//if(DEBUG==1)console.log("browserheight > browserwidth");
				imageheight = browserheight;
					$(this.options.bgElement).setStyle('height',browserheight);
				imagewidth = browserheight/ratio;									//if(DEBUG==1)console.log("imagewidth		"+imagewidth	);
					$(this.options.bgElement).setStyle('width',imagewidth);
				
				if (browserwidth > imagewidth){										//if(DEBUG==1)console.log("browserheight > imagewidth");
					imagewidth = browserwidth;										//if(DEBUG==1)console.log("imagewidth		"+imagewidth	);
						$(this.options.bgElement).setStyle('width',browserwidth);
					imageheight = browserwidth * ratio;								//if(DEBUG==1)console.log("imageheight	"+imageheight	);
						$(this.options.bgElement).setStyle('height',imageheight);
				}
			}			
			//When browser is wider
			if (browserwidth >= browserheight){										//if(DEBUG==1)console.log("browserwidth >= browserheight");
				imagewidth = browserwidth;											//if(DEBUG==1)console.log("imagewidth		"+imagewidth	);
					$(this.options.bgElement).setStyle('width',browserwidth);
				imageheight = browserwidth * ratio;									//if(DEBUG==1)console.log("imageheight	"+imageheight	);
					$(this.options.bgElement).setStyle('height',imageheight);
				
				if (browserheight > imageheight){									//if(DEBUG==1)console.log("browserheight > imageheight");
					imageheight = browserheight;									//if(DEBUG==1)console.log("imageheight	"+imageheight	);
						$(this.options.bgElement).setStyle('height',browserheight);
					imagewidth = browserheight/ratio;								//if(DEBUG==1)console.log("imagewidth		"+imagewidth	);
						$(this.options.bgElement).setStyle('width',imagewidth);
				}
			}
		}
	}

});

/*************************************************************/
var moosizer = null;
document.addEvent('domready', function(){
  	moosizer = new mooSizer({ bgElement:'fadeIt' });       
});

window.addEvent('domready', function(){
  	var background_image = $('fadeIt').getElement('img');

	var img = new Image();
	img.src = background_image.get('src');
	var img_height = img.height;
	var img_width = img.width;
	
	
	new Asset.images([background_image.src], {
		onComplete: function(){
		//	background_image.setStyle('height', 'auto'); // durch css angaben ist das bild in der regel verzerrt, script liest GrÃ¶ÃŸen falsch ein
			//var background_image_size = {'x': $('backgroundimage_width').get('value') * 1, 'y': $('backgroundimage_height').get('value') * 1}; // background_image.getSize()
		//	background_image.setStyle('height', '100%');
			moosizer = new mooSizer(
				{
					bgElement: 'fadeIt',
					startwidth: img_width,
					startheight: img_height
				}
			);
		}
	});      
});



