/* Author: Dominic Whittle */

window.scrollTo(0, 1); // iPhone toolbar hiding. this doesn't do shit!









jQuery(document).ready(function($) {



	if ( $(window).scrollTop() > 100 ){
		$("#nav .ir").css("visibility","visible");
	} else {
		$("#nav .ir").css("visibility","hidden");
	}		 

	$(window).scroll(function() {
		if ( $(this).scrollTop() > 100 ){
			$("#nav .ir").css("visibility","visible");
			//alert("tom is gay!");
		} else {
			$("#nav .ir").css("visibility","hidden");
		}		 
	});

	
	

	// Email confuserator
	//
	$(".format-mail").each(function(){
		
		var name = $(this).attr("data-name");
		var url = $(this).attr("data-url");
		
		$(this).html('<a href="mailto:'+name+'@'+url+'">'+name+'@'+url+'</a>');
		
	});









	// FULLSCREEN BACKGROUND IMAGES
	
	var transition_speed = 1000;	
	var auto_next = 'false'; //not ready yet
	
	
	
	function sizeFullscreenImage() {
		var window_height = $(window).height();
		var window_width = $(window).width();
		
		var $img = $(".SCALE");
		var img_height = $img.attr("data-height"); 
		var img_width = $img.attr("data-width");
		var ratio = $img.attr("data-ratio");
		
		//size based on width.
		var oh = window_width*(ratio);
		var ow = window_width;
		
		if ( oh < window_height ){
			//size based on height.
			var ow = window_height/ratio;
			var oh = window_height; 			
		} 
					
		$img.attr("height", oh).attr("width", ow); // Math.round?
		
		if ( $("body").hasClass("ie6") == true ){ 
			$("#fullscreen").css("width", window_width+"px").css("height", window_height+"px"); 
		}
	} 






	/* 
		SET UP IMAGES
		This runs the first time the page loads.
		It gets the first "image" and applies it's src, allowing the browser to load it.
		Once the first image is loaded and made visible, the other images are given their src so 
		the browser can load them into cache in the background, without them being seen.
	*/
	function setupFullscreenImage() {
		var $img = $(".FIRST");
		var src = $img.attr("data-src");

		$img.attr("src", src).css("opacity", 0).css("visibility", "visible").css("z-index", 5);

		$img.load(function(){ 
			$(this).animate({'opacity' : 1 }, transition_speed, function(){
					$("#fullscreen img").each(function(){
						var src = $(this).attr("data-src");
						$(this).attr("src", src);
						
						// Cleanup (not required)
						$(this).removeAttr("data-src");						
						// End Cleanup
						
						//$(this).removeClass("FIRST");
					});				
			}); //animate 
			//$(this).removeClass("FIRST");
		})
		.each(function(){
			if ( this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) ){
				$(this).trigger("load");
				//$("#nav").css("background","red");
			}
		}); 
		
		
		
		
	} //function
	setupFullscreenImage();













/*


		var $img = $(".FIRST");
		var src = $img.attr("data-src");

		$img.css("visibility", "visible").attr("src", src).css("opacity", 0).css("z-index", 5);

		$img.load(function(){ 
			
			$("#fullscreen").attr("data-status", "loaded");
			
			$(this).animate({'opacity' : 1 }, transition_speed, function(){
							
					$("#fullscreen img").each(function(){
						var src = $(this).attr("data-src");
						$(this).attr("src", src);
					});
					
					//stopCycle();
					//alert("worked 1st time");				
			
			}); //animate 
		});
	

	// Background images should have loaded by now...
	// But they sometimes don't.
	// So let's run a bit of code every 4 seconds to check whether the an attribute
	// has been applied by the load();
	// and if it hasn't, let's 



	var intervalID;  
	   
	function startCycle(){  
		intervalID = setInterval(checkLoad, 4000);  
	} 
	startCycle(); // do it... NOOOOOW!
	
	
	function stopCycle(){  
	   clearInterval(intervalID);  
	}  



		
		function checkLoad(){
			
			if ( $("#fullscreen").attr("data-status") == "loaded" ) {
	
				$("#nav").css("background", "red"); 
				stopCycle();
				//alert("cycle stopped");
	
			} else {
				
				$("#nav").css("background", "blue");
				
				$img.attr("src", src).css("opacity", 0).css("visibility", "visible").css("z-index", 5);
	
				$(this).animate({'opacity' : 1 }, transition_speed, function(){
				
						$("#fullscreen img").each(function(){
							var src = $(this).attr("data-src");
							$(this).attr("src", src);
						});			
				
				}); //animate 
	
			}
		} checkLoad();

*/

















	/*
		TRANSITION
		
	*/
	$("#microns a").click(function(e){
		e.preventDefault();
		
		// Prevent Double-Click
		if ( $(this).hasClass("current_micron") == true ) {
			return false; // stop
		}

		var target_image = '#whit_fullscreen_image-' + $(this).attr("rel");
		
		if ( $(target_image).hasClass("CURRENT") == true ) {
			return false; // stop
		}
		
		var outgoing_image = '#' + $(".CURRENT").attr("id");
		
		$("#microns a").removeClass("current_micron");

										
										
		
		// DO STUFF WITH THE IMAGES.
		
		$(outgoing_image).removeClass("SCALE").removeClass("CURRENT");
		
		$(target_image).addClass("SCALE").addClass("CURRENT").css("opacity",0).css("visibility", "visible").css("z-index", "6");
		
		sizeFullscreenImage(); // call this once we've given the new image the .SCALE class. 
		
		$(target_image).animate({'opacity' : 1 }, transition_speed, function(){	// Fade in. Then...
		
			$(outgoing_image).css("z-index", "").css("visibility", "").css("opacity", ""); //removeClass("CURRENT").
			$(target_image).css("z-index", "5"); 

		}); //animate 
		
		$(this).addClass("current_micron");
		
	});











	
	sizeFullscreenImage();
	
	$(window).resize(function() { 
		sizeFullscreenImage();
	});


	

	
		























}); //end document ready









/*
jQuery(window).bind("load", function() {

		var $img = jQuery(".FIRST");
		var src = $img.attr("data-src");
		
		if ( src != $img.attr("src") ){ // img hasn't loaded
			
			$img.attr("src", src).css("opacity", 0).css("visibility", "visible").css("z-index", 5);
	
			$img.load(function(){ 
				jQuery(this).animate({'opacity' : 1 }, transition_speed, function(){
						jQuery("#fullscreen img").each(function(){
							var src = $(this).attr("data-src");
							jQuery(this).attr("src", src);
							
						});				
				}); //animate 

			});
		
		}

}); //end
*/













