if(!window.console)
{
	console = {};
	console.log = function(){};}

Header = {};

(function(app) {
	
	app.duration = 0;
	app.loaded = 0;
	app.len = 0;
	app.index = 0;
	
	app.init = function(duration)
	{		
		$('#header #image img').each(function(i){
			var url = this.src + "?r=" + Math.random( );
			$(this).data('index', i).fadeOut(0).load(app.loadHandler).attr('src', url );
		});
		
		this.duration = duration;
		this.len = $('#header #image img').length;
	}
	
	app.rotate = function()
	{
		var previous = $('#header #image img')[app.index];
		
		/*app.index++;
		
		if(app.index == app.len)
			app.index = 0;*/
			
		app.index = Math.round(Math.random() * (app.len - 1));

		var image = $('#header #image img')[app.index];
		$(image).fadeIn(1000);
		$(previous).fadeOut(1000);
	}
	
	app.loadHandler = function()
	{	
		app.loaded++;
			
		if(app.loaded == app.len) {
			var first = $('#header #image img')[Math.round(Math.random() * (app.len - 1))];
			$(first).fadeIn(1000);
			$(document).everyTime(app.duration, app.rotate);
		}
	}
		
})(Header);

$(document).ready(function(){Header.init(10000);});