// Global Variables:
var resizeTimer = '';
// var imageSrc    = [];
// var imageSizes  = [];


	function chooseSize () {

		windowSize = window.getSize();

		i = 0;
		
		while (imageSizes.length > i)
		{
			size = imageSizes[i].split('x');
			size[0] = (size[0]).toInt();
			size[1] = (size[1]).toInt();
			
			if((windowSize.x > size[0] && windowSize.y > size[1] )){
				if (i === 0){
					return i;
				}else{
					return (i-1);
				}
				
			}
			
			i++;
		}
		
		// If the window size is super, ultra small
		// fall back to the smallest size possible
		return (imageSizes.length-1);	
	}
	
	
	function scaleImage () {

		
		// You are now free to being the resize process:
		index        = chooseSize();						
		windowSize   = window.getSize();
		size         = imageSizes[index].split('x');
		// window_ratio = windowSize.x/windowSize.y;
		image_ratio  = size[0]/size[1];
		pad          = 0;
		
		// console.info(windowSize);
		
		// Add an extra 33 pixels of padding 
		// to the top, if share is enabled:
		if (share) {
			pad = 33;
		}


		// Determine the correct size:
		if( (windowSize.x > size[0] && windowSize.y > size[1]) ){
			
			// Use the maximum image size:
			new_width  = size[0];
			new_height = size[1];

		}else if(image_ratio > 1 || image_ratio == 1){
			
			// Width is more important
			new_width  = size[0]-(size[0]-windowSize.x+10);
			new_height = new_width/image_ratio;				
			
		}else{
			
			// height is more important:
			new_height = size[1]-(size[1]-windowSize.y+10+pad);
			new_width = new_height*image_ratio;
			
		}
		
		// Verify that the image isn't too large:
		if (new_width > windowSize.x) {
			
			new_width  = new_width-(new_width-windowSize.x+10);
			new_height = new_width/image_ratio;
			
		}
		
		if(new_height > windowSize.y){
			
			new_height = new_height-(new_height-windowSize.y+10+pad);
			new_width  = new_height*image_ratio;
		
		}
		
		
		if($('image').getProperty('width') == new_width &&  $('image').getProperty('height') == new_height){
			// console.log('skipping update');
			return false;
		}
		
		
		// Load the correct image:
		$('image').setProperty('src',imageSrc[index]);
		
		
		// Time to scale!
		$('image').setProperty('width',new_width.toInt());
		$('image').setProperty('height',new_height.toInt());


		$('container').setStyle('width', new_width.toInt()+'px');
		$('container').setStyle('height', new_height.toInt()+'px');

		$('distance').setStyle('margin-bottom', (-(new_height.toInt()+pad.toInt())/2).round()+'px');

	}
	
	
	function checkBrowser () {
		
		/*
			OPERA - GOOD
			SAFARI - GOOD
			CHROME - GOOD

			Netscape: - BAD
			Firefox: <3 - BAD
			Firefox: 3> - Fair

			IE: =<6 - BAD
			IE: 7 - GOOD
		*/
		
		
		// Sorry, but Linux's built in image resizing is just horible:
		if (Browser.Platform.linux === true || Browser.Platform.other === true){
			return false;
		}else if(Browser.Platform.win === true){
			
			// A version of IE older than IE7:
			if(Browser.Engine.trident && Browser.Engine.version < 5){
				return false;
			}
			
			// A version of Firefox older than Firefox 3:
			if(Browser.Engine.gecko && Browser.Engine.version < 19){
				return false;
			}
		}
		
		return true;
		
	}
	
	
	if(checkBrowser()){
		
		// Attach the events:
		window.addEvent('domready', function() {
			
			$('body').setStyle('overflow','hidden');
			
			scaleImage();
		});

		window.addEvent('resize', function() {
			// Wait before resizing the photo...
			resizeTimer = $clear(resizeTimer);
			resizeTimer = (function(){ scaleImage(); }).delay(500);
		});
	}

