	//$(document).ready(function() {
	$(function() {
	
		// Params
		var previousWidth 	= "320";
		var activeWidth 	= "";
		var loading			= "no";
		
		// Detect device and ios
		function isDevice()
		{
			var device = "normal";
			
			if(navigator.platform.indexOf("iPhone") != -1){
				device = "iphone";
			}
			
			if(navigator.platform.indexOf("iPad") != -1){
				device = "ipad";
			}
			
			return device;
		}
		
		function isiOS()
		{
			var device = "ios4";

			if (navigator.userAgent.match(/(iPad|iPhone);.*CPU.*OS 5_\d/i)){ 
			   device = "ios5";
			}
			
			return device;
		}
	
		/* Listen to browser Resize */
		function browserResize() 
		{
			var browserWidth = $(window).width();
			
			if(browserWidth >= '1300' && activeWidth != '1300')
			{
				// Show
				$('.logo').show();
				
				activeWidth = "1300";
				
				// 1300
				$('#grid').toggleClass('grid-'+activeWidth).toggleClass('grid-'+previousWidth);
				$('.row').toggleClass('row-'+activeWidth).toggleClass('row-'+previousWidth);
				swapImages('1300');
				
				previousWidth = "1300";
			}
			if(browserWidth <= '1299' && browserWidth >= '960' && activeWidth != '960')
			{
				// Show
				$('.logo').show();
				
				activeWidth = "960";
				
				// 960
				$('#grid').toggleClass('grid-'+activeWidth).toggleClass('grid-'+previousWidth);
				$('.row').toggleClass('row-'+activeWidth).toggleClass('row-'+previousWidth);
				swapImages('960');
				
				previousWidth = "960";
			}
			if(browserWidth <= '959' && browserWidth >= '640' && activeWidth != '640')
			{
				// Show
				$('.logo').show();
				
				activeWidth = "640";
				
				// 640
				$('#grid').toggleClass('grid-'+activeWidth).toggleClass('grid-'+previousWidth);
				$('.row').toggleClass('row-'+activeWidth).toggleClass('row-'+previousWidth);
				swapImages('640');
				
				previousWidth = "640";
			}
			if(browserWidth < '639' && activeWidth != '320')
			{
				// Hide
				$('.logo').hide();
				
				activeWidth = "320";
				
				// 300
				$('#grid').toggleClass('grid-'+activeWidth).toggleClass('grid-'+previousWidth);
				$('.row').toggleClass('row-'+activeWidth).toggleClass('row-'+previousWidth);
				swapImages('320');
				
				previousWidth = "320";
			}
		}

		function swapImages(width) 
		{

			$("#grid .row .cell img").each(function(i)
			{ 
				var rel = $(this).attr("rel");
				$(this).attr("src", "http://pjloughran.com/illustration/uploads/_"+ width +"/"+ rel);
			});
		}
	
		browserResize();
		$('#grid').show();		
	
		$(window).bind("resize", function(){
			browserResize();
		});

		
		// ------------------------------------------------------------------------
		// Entry
		$('.cell img').click(function(){
		
			if(loading == "no")
			{
				// Variables
				loading				= "yes";
				var url_title 		= $(this).attr("data-url");
				var margin_top		= ($(this).height()/2)-17;
				var margin_left		= ($(this).width()/2)-17;
				var middleStage		= $(window).scrollTop()+($(window).height()/2);
				
				//alert(margin_top);
				//alert(margin_left);
				//alert(middleStage);
				
				$(this).parent().prepend('<div id="loader" style="margin-top: '+margin_top+'px; margin-left: '+margin_left+'px;"><img src="http://pjloughran.com/illustration/images/pj/ajax-loader2.gif"></div>');
				
				// Prep box
				var device = isDevice();
				if(device == "iphone" || device == "ipad")
				{
					if(isiOS() == "ios4")
					{
						$('.box').css('top', middleStage+"px");
					}
				} else {
					$('.box').css('top', "50%");
				}
				
				$.get('/illustration/index.php/entry/'+url_title+'/'+activeWidth, function(data)
				{
					// Data
					$('.box').removeClass('box-1300');
					$('.box').removeClass('box-960');
					$('.box').removeClass('box-640');
					$('.box').removeClass('box-320');
					$('.box').addClass('box-'+activeWidth);
					$('.box .wrap').html(data);
					
					// Variables
					
					// Display
					$("#loader").remove();
					$('#overlay').show(0, function()
					{
						$('#box').show('fast', function()
						{
							loading	= "no";
						});
					});
					
				});
			}
		});
		
		$('#box #close').click(function()
		{
			$('#box').hide(0,function()
			{
				$('#overlay').hide();
				$('#box .entry').remove();
			});
		});
		
		$('#box .wrap').click(function()
		{
			$('#box').hide(0,function()
			{
				$('#overlay').hide();
				$('#box .entry').remove();
			});
		});
		
		// ------------------------------------------------------------------------
		// Page
		
			// About
			$('.nav-about').click(function(){

				// Variables
				var middleStage		= $(window).scrollTop()+($(window).height()/2);
					
				// Prep page
				var device = isDevice();
				if(device == "iphone" || device == "ipad")
				{
					if(isiOS() == "ios4")
					{
						$('.page-about').css('top', middleStage+"px");
					}
				} else {
					$('.page-about').css('top', "50%");
				}
				
				// Data
				$('.page-about').removeClass('page-about-1300');
				$('.page-about').removeClass('page-about-960');
				$('.page-about').removeClass('page-about-640');
				$('.page-about').removeClass('page-about-320');
				$('.page-about').addClass('page-about-'+activeWidth);
						
				// Display
				$('#overlay').show(0, function()
				{
					$('.page-about').show('fast', function()
					{
						// Show alert?
						if(device == "iphone") $(".alert").delay(1500).fadeIn().delay(5000).fadeOut();
					});
				});
			});
			
			$('.page-about #close').click(function()
			{
				$('.page-about').hide(0,function()
				{
					$('#overlay').hide();
					$('.alert').hide();
				});
			});
			
			// Contact
			$('.nav-contact').click(function(){

				// Variables
				var middleStage		= $(window).scrollTop()+($(window).height()/2);
					
				// Prep page
				var device = isDevice();
				if(device == "iphone" || device == "ipad")
				{
					if(isiOS() == "ios4")
					{
						$('.page-contact').css('top', middleStage+"px");
					}
				} else {
					$('.page-contact').css('top', "50%");
				}
				
				// Data
				$('.page-contact').removeClass('page-contact-1300');
				$('.page-contact').removeClass('page-contact-960');
				$('.page-contact').removeClass('page-contact-640');
				$('.page-contact').removeClass('page-contact-320');
				$('.page-contact').addClass('page-contact-'+activeWidth);
						
				// Display
				$('#overlay').show(0, function()
				{
					$('.page-contact').show('fast');
				});
			});
			
			$('.page-contact #close').click(function()
			{
				$('.page-contact').hide(0,function()
				{
					$('#overlay').hide();
					$('.alert').hide();
				});
			});
			
			// Client
			$('.nav-client').click(function(){

				// Variables
				var middleStage		= $(window).scrollTop()+($(window).height()/2);
					
				// Prep page
				var device = isDevice();
				if(device == "iphone" || device == "ipad")
				{
					if(isiOS() == "ios4")
					{
						$('.page-client').css('top', middleStage+"px");
					}
				} else {
					$('.page-client').css('top', "50%");
				}
				
				// Prep visual
				if(device == "iphone")
				{
					$('.client-full').hide();
					$('.client-iphone').show();
				} else {
					$('.client-full').show();
					$('.client-iphone').hide();
				}

				// Data
				$('.page-client').removeClass('page-client-1300');
				$('.page-client').removeClass('page-client-960');
				$('.page-client').removeClass('page-client-640');
				$('.page-client').removeClass('page-client-320');
				$('.page-client').addClass('page-client-'+activeWidth);
						
				// Display
				$('#overlay').show(0, function()
				{
					$('.page-client').show('fast', function()
					{
						// Show alert?
						if(device == "iphone" || device == "ipad") $(".alert").delay(1500).fadeIn().delay(5000).fadeOut();
					});
				});
			});
			
			$('.page-client #close').click(function()
			{
				$('.page-client').hide(0,function()
				{
					$('#overlay').hide();
					$('.alert').hide();
				});
			});
			
			
		// ------------------------------------------------------------------------
		// Overlay close
			
		$('#overlay').click(function()
		{
			$('.page-about').hide();
			$('.page-contact').hide();
			$('.page-client').hide();
			$('#box').hide(0, function()
			{
				$('#overlay').hide();
				$('#box .entry').remove();
			});
		});
		
		// ------------------------------------------------------------------------
		// Alert close
			
		$('.alert').click(function()
		{
			$('.alert').hide();
		});

	});
