$(document).ready(function() {
	var original_ratio = null, new_src= null;
	if (!$.browser.msie)
		{
			$('.resize').hide();
		}
	
	pre = $('#preload');
	$('#preload').attr('src',$('.resize').attr('src'));
	var original_ratio = $(pre).width() / $(pre).height();
	
	var _image = new Image();
	_image.src = $('.resize').attr('src');
	
	$(_image).load( function() {
		$('.resize').fadeIn('100',function(){
			img_init(original_ratio);
		});
	});

	var images = $('a[rel="civico"]').click(
	function (event) {
		$('.main-img').addClass('loading');
		
		var img  = $('.resize');
		img.w = $('.resize').width();
		img.h = $('.resize').height();
		img.pos = img.position();
		img.left = img.pos.left;
		
		var new_image = new Image();
		if ($.browser.msie) { 
			new_src = this+'?'+ Math.random()*9999;
		}
			else new_src = this;
		new_image.src = new_src;
		
		$('.resize').hide();
		$(new_image).load( function() {
			$('.resize').attr('src',new_src).fadeIn('1000');
			
			$('#preload').attr('src',new_image.src);
			original_ratio = $('#preload').width() / $('#preload').height();
			img_init(original_ratio);
			
			
			/*
			$('.resize').fadeIn('100',function(){
				img_init(original_ratio);
			});
			*/
			
			
		});
		
		return false;
	});
	
	$('a[rel="civico"]').mouseover(
			function () {
				// PRELOAD IMMAGINI
				var preload_image = $('<img />').attr('src', this);
				return false;
	});
	
	function img_init (ratio_c) {
			left= 260 ;
			
			var img = $('.resize');
			img['h'] = img.height();
			img['w'] = img.width();
			img['r'] =img['w']/img['h'];
			
			var view = $(window);
			view['h'] = view.height();
			view['w'] = view.width() - left;
			view['r'] = view['w']/view['h'];
			
			var diff = new Array();
			diff['w'] = img['w'] - view['w'];
			
			diff['h'] = img['h'] - view['h'];
			
			switch (true) {
			
			case (view['r'] == img ['r']):
				img.css('top',0);
				img.css('left',left);
				break;
			
			
			case (img['h']>img['w']): //immagine verticale
				img.css('left',left);
				img.height(view['h']);
				img.width(img.height()*ratio_c);
					if ( Math.abs(img.height() - view.height()) < 5)
						{
						img.css('top','0');
						}
				var new_x = 260 - ((img['w'] - view['w'])/2);
				
				img.css('left',new_x+'px');
				
				break;
			
			case (view['r']>img['r']): //finestra stretta in altezza
				img.css('left',left);
				img.width(view['w']);
				img.height(img.width()/ratio_c);
					if ( Math.abs(img.height() - view.height()) < 5)
						{
						img.css('top','0');
						}
				
					var new_y = Math.ceil ((view.height() - img.height()) / 2);
					if (new_y < 0)
						{
					img.css('top',new_y+'px');
						}
				
				break;
				
			case (view['r']<img['r']): //finestra stretta in larghezza
				img.css('top','0');
				img.height(view['h']);
				img.width(img.height()*ratio_c);
				var img_w = img.width();
				var new_x = 260 - ((img['w'] - view['w'])/2);
				
				img.css('left',new_x+'px');
				
				break;
			}
		}

		$(window).bind('resize', function() {
			img_init($(pre).width() / $(pre).height());	
		});

		setInterval(function() {
			img_init($(pre).width() / $(pre).height());
		}, 100);
	
		
});
