function resize_images()
{
	// window info
	var w_width  = $(window).width();
	var w_height = $(window).height();
	var w_ratio  = w_width / w_height;
		
	$('#loader').css({'width' : w_width+'px', 'height' : w_height+'px'});
	$('#contact').css({'width' : w_width+'px', 'height' : w_height+'px'});
	$('#images').css({'width' : w_width+'px', 'height' : w_height+'px'});
	
	// Reposition loader text box
	var top_margin = (w_height-160)/2;
	top_margin = Math.round(top_margin);
	$('#loader_text').css({'marginTop' : top_margin+'px'});
	
	// Reposition contact text box
	top_margin = (w_height-$('#contact_text').height())/2;
	top_margin = Math.round(top_margin);
	$('#contact_text').css({'top' : top_margin+'px'});
	top_margin = (w_width-$('#contact_text').width())/2;
	top_margin = Math.round(top_margin);
	$('#contact_text').css({'left' : top_margin+'px'});
	
	// Reposition contact form
	top_margin = (w_height-$('#contact_form').height())/2;
	top_margin = Math.round(top_margin);
	$('#contact_form').css({'top' : top_margin+'px'});
	top_margin = (w_width-$('#contact_form').width())/2;
	top_margin = Math.round(top_margin);
	$('#contact_form').css({'left' : top_margin+'px'});
	
	$('#images').children().each(function(i){

		var i_ratio = bg_ratios[i];

		if( i_ratio < w_ratio )
		{
			var new_i_height = Math.round(w_width/i_ratio);
			var new_height = ((w_width/i_ratio)-w_height)/2;
			if( new_height < 0 ) { new_height = 0; }
			new_height = Math.floor(new_height);
			$(this).css({'width' : w_width+'px', 'height' : new_i_height+'px', top : '0px', left : '0px'});
			// $(this).css({'width' : w_width+'px', 'height' : new_i_height+'px', top : '-'+new_height+'px', left : '0px'});
		}
		else
		{
			var new_i_width = Math.round(w_height*i_ratio);
			var new_width = ((w_height*i_ratio)-w_width) /2;
			if( new_width < 0 ) { new_width = 0; }
			new_width = Math.floor(new_width);
			$(this).css({'width' : new_i_width+'px', 'height' : w_height+'px', top : '0px', left : '-'+new_width+'px'});
		}
	});
}


function fade_images()
{
	resize_images();
	
	// INITIALISATION
	// Number of images
	var img_count = $('#images').children('img').length;
	img_count = img_count - 1;
	
	// What is the current image
	var current_img = $('#images').attr('currentimg');
	current_img = current_img;
	if( current_img < 0 || current_img > img_count )
	{
		current_img = img_count;
		$('#images').children('img').fadeIn('fast');
	}
	$('#images').attr('currentimg',current_img);
	
	if( picture_loop == 1 )
	{
		if( current_img == 0 )
		{
			$('#images').children('img').eq(img_count).fadeIn(2500, function(){
				$('#images').children('img').show();
			});
			$('#images').attr('currentimg',img_count);
		}
		else if( current_img == img_count )
		{
			$('#images').children('img').show();
			$('#images').children('img').eq(current_img).fadeOut(2500);
			$('#images').attr('currentimg',(current_img-1));
		}
		else
		{
			$('#images').children('img').eq(current_img).fadeOut(2500);
			$('#images').attr('currentimg',(current_img-1));
		}

		fader = setTimeout("fade_images()", 8500);
	}
	
}

function timed_resize()
{
	resize_images();
	setTimeout("timed_resize()", 500);
}




$(document).ready(function(){
	
	fader = setTimeout("fade_images()", 7000);
	clearTimeout(fader);
	
	self.moveTo(1,1);
	window.resizeTo(screen.width,screen.height);
	
	// $(document).pngie({
	// blankgif:'/templates/redearth_holding/images/blank.gif',
	// sizingMethod:'crop'
	//  });
	$(document).pngFix(); 
	$('#contact').hide().css('visibility','visible');
	$('#contact_text').hide().css('visibility','visible');
	$('#contact_form').hide().css('visibility','visible');
	
	
	picture_loop = 1;
	
	$('#contact').hide();
		
	// IMAGE FADER
	// image info
	bg_ratios = new Array(1.6061, 1.5238, 1.5664, 1.4758);
	timed_resize();
	
	
	// ENTER
	$('#enter').click(function(){
		
		$('#loader').fadeOut(2000);
		// Start off fading
		fader = setTimeout("fade_images()", 7000);
		return false;
	});
	
	
	// SHOW CONTACT LAYER
	$('#images img, #logo').click(function(){
		//fade_images();
		picture_loop = 0;
		clearTimeout(fader);
		$('#logo').fadeOut(100);
		$('#contact').fadeIn(250);
		$('#contact_text').fadeIn(250);
	});
	
	
	// SHOW CONTACT FORM
	$('a.show_contact_form').click(function(){
		$('#contact_text').fadeOut(100, function(){
			$('#contact_form').fadeIn(100);
		});
		
		return false;
	});
	
	
	// CLOSE CONTACT LAYER & RESET
	$('.close_contact').click(function(){
		$('#contact_form').fadeOut(250);
		$('#contact_text').fadeOut(250);
		$('#contact').fadeOut(250);
		picture_loop = 1;
		fader = setTimeout("fade_images()", 4000);
		$('#logo').fadeIn(100);
		$('#sent_window').hide();
	});
	
	
	
	// CONTACT FORM VALIDATION & SUBMIT
	$('#contact_form').submit(function(){
		
		var this_id;
		var found_empty_fields = 0;
		$('.mssng_val').remove();
		
		$('#contact_form .required').each(function(){

			this_id = $(this).attr('id');
			
			
			if( $(this).hasClass('email') )
			{
				if(!$(this).val().match(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/))
				{
					found_empty_fields = 1;	
					// $('label[for='+this_id+']').append(' <span class="mssng_val">!</span>');
					// alert(this_id);
				}
			}
			else if( $(this).val() == '' )
			{
				// alert(this_id);
				found_empty_fields = 1;	
				// $('label[for='+this_id+']').append(' <span class="mssng_val">!</span>');
			}
			
		});
		
		
		if( found_empty_fields > 0 )
		{
			alert('Please fill out all mandatory fields.');
		}
		else
		{
			var form_values = $('#contact_form').serialize();
			var aj_action = $('#contact_form').attr('action') + '.json?callback=?';
			aj_action += "&"+form_values;
			
			$('#sent_window').fadeIn(50);
			// .animate({left : '100px'}, 1500, function(){
			// 	$('#sent_window').html('Information sent!<br>Thank you for your interest.').animate({left : '100px'}, 2000, function(){
			// 		$('#sent_window').fadeOut(300);
			// 	});
			// });

			
			$.getJSON(aj_action, function(){
				$('#sent_window').html('Information sent! Thank you for your interest.').delay(2000).fadeOut(300);;
			})
		}
		
		return false;
	});
	
	
	// CHECKBOX REPLACEMENT
	$('input[type=checkbox]').each(function(){
		this_id = $(this).attr('id');
		if( this_id != '')
		{
			$(this)
				.hide()
				.after('<div class="alt_check" tar="'+this_id+'"></div>');
		}
	});
	
	$('.alt_check').live('click', function(){
		for_id = $(this).attr('tar');
		
		if( $('#'+for_id).attr('checked')  )
		{
			// set to unchecked
			$('#'+for_id).removeAttr("checked");
			$(this).css('background-position','top left');
		}
		else
		{
			// set to checked
			$('#'+for_id).attr('checked', 'checked');
			$(this).css('background-position','top right');
		}
	});
	
	
	
	
	// ON WINDOW RESIZE
	$(window).resize(function() {
		resize_images();
	});	
});
