window.addEvent('domready', function () {
	
	
	var w = window.getSize();
	var dim;
	var diff;
	var fx;
	var dir = 0;
	var timer;	
	
	var div = new Element ('div', {
		'id': 'loading',
		'styles': {
				'top': 0,
				'left': 0,
		        'width': w.x,
		        'height':  w.y,
				'opacity': 0
		    }
	});
	
	div.inject($$('body')[0]);
		
	var data = new Request.JSON({
		url: "./resize.php", 
		onSuccess: function(image, t){
			image = JSON.decode(t);
			
			diff = {
				'height': image.height - w.y, 
				'width': image.width - w.x
			};
					
			
			var bg = new Asset.images(['./images/spinner.gif'/*, './images/logo.png', './images/logo-transparent.png'*/, image.url], {
				onComplete: function () {

					$$('body').setStyle('background-image',	'url(' + image.url + ')');

					fx = new Fx.Tween($$('body')[0], {duration: 2000, wait:false, transition: Fx.Transitions.Quad.easeOut});
					fx.set('background-position', -parseInt(diff.width / 2) + 'px ' + -parseInt(diff.height / 2) + 'px');
				
					(function() {
						div.fade('out');
					}).delay(1000);
					
					(function() {
						div.setStyle('display', 'none');
					}).delay(1500);
					
				
					
					window.addEvent('click', function () {
						window.location = $('logo').get('href');
					});
					
					window.removeEvents('mousemove');

					window.addEvent('mousemove', function (e) {
						e = new Event(e);

						var pos =(e.client.x - (w.x/2)) / (w.x/2);

						if (pos > .6) {
							if (dir != -1) {
								fx.start('background-position', -parseInt( (diff.width / 2) * 1.3) + 'px ' + -parseInt(diff.height / 2) + 'px');
								dir = -1;
							}
						}

						if (pos < -.7) {
							if (dir != 1) {
								fx.start('background-position', -parseInt( (diff.width / 2) * .7) + 'px ' + -parseInt(diff.height / 2) + 'px');
								dir = 1;
							}			
						}
					});
				}
			});
			
			
	  	  	
			
		}
	});
	
	
	window.addEvent('init-dimensions', function () {
		
		$$('body').setStyle('background-image',	'none');
		
		w = window.getSize();
		dim = {
			'height': (Math.ceil( w.y / 100 ) + 1)*100, 
			'width': (Math.ceil( w.x / 100 ) + 1)*100
		};
		
		data.get({
			'height': dim.height,
			'width': dim.width
		});
		
		div.setStyles({
			'display': 'block',
			'width': w.x,
	        'height':  w.y,
			'opacity': 1
		});
		
	});
	
	window.fireEvent('init-dimensions');
	

	window.addEvent('resize', function(){
	
		
	  $clear(timer);
	  timer = (function () {
		window.fireEvent('init-dimensions');
	  }).delay(750);
	});
	
	var fx = new Fx.Tween($$('body')[0], {duration: 2000, wait:false, transition: Fx.Transitions.Quad.easeOut});
	fx.set('background-position', '-50px 0px');
	
	
	var dir = 0;
	
});
