/*
Background rotator script
June 2010
By Bram de Leeuw
www.bramdeleeuw.nl
*/

(function($){


$(document).ready(function() {
			$("#backgrounds > img").each(function(){

				$(this).resizenow();
			});
	
	$(window).bind("resize", function(){
	   		//$("#backgrounds > img").resizenow(); 
			$("#backgrounds > img").each(function(){
				$(this).resizenow();
			});
	});
	
    $('#backgrounds').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeout: 1000,
		random: 1
	});

});

$.fn.resizenow = function() {
	return this.each(function() {
		var imagewidth = $(this).width();
		var imageheight = $(this).height();
		
		
		if(imageheight<1){
			imageheight=900;
		}
		if(imagewidth<1){
			imagewidth=1200;
		}
		
		//$('#feed > p').html(imagewidth+" - "+imageheight);
		var width = $(window).width();
		var height = $(window).height();
		
		
		
		var w = width;
	  	var h = width * (imageheight / imagewidth);
  		var x = 0;
	  	var y = (height - h) / 10;
  		
		//alert("imageheight ="+imageheight+"imagewidth ="+imagewidth+" h ="+h+" w ="+w+" height ="+height+" width ="+width);
		
  		if (h < height) { // y < 0
  			h = height;
	  		w = height * (imagewidth / imageheight);
  			y = 0;
  			x = (width - w) / 2;
	  	}
		
		
		
		// set everything
	  	$(this).css("marginTop",y);
  		$(this).css("marginLeft",x);
	  	$(this).css("width",w);
    	$(this).css("height",h);
	  	$(this).attr("width",w);
    	$(this).attr("height",h);
	});
		
};



})(jQuery);

