$(window).ready(function() {
	
	function init(){	
		
		rotateHomepageImages();		
		loadTypography();
		
		if ($('.comments').length >0) commentForm();
			
		$(window).load(function(){	
			
			if($('#imgs').length > 0){
				$('#imgs').css('display', 'none').fadeIn();
			};		

			if ($('.nav').length > 0){ $('.nav').css('display','block'); };

			// Fade in intros for each section.
			if($('#text').length > 0) { 		
				$('#text').css('opacity',0);

				$('#intro_background').fadeIn(750, function(){
					$('#text').fadeTo(750, 0.9, function(){ 
						$('#text p').fadeIn(750);
					});	
				});
			};
			
			if ($('body#p-publications').length != 0){
				if ($('ul#featured_publications').children().length > 1) { $('ul#featured_publications').cycle({speed:1300}); }
			};
						
			$('#service_img img').fadeOut(0, function(){ $('#service_img img').fadeIn(); });
			
			$('.project img').hide().fadeIn();
			$('.progress img').hide().fadeIn();
			$('.planning img').hide().fadeIn();
			$('.preservation img').hide().fadeIn();
			
			if($('.cycle').length > 0) {
				$('.cycle').hide().fadeIn();		
				$('.cycle').cycle({timeout: 0, speed:750, delay:400, next: '#next', prev: '#prev'});
			}
			
			if($('.cycle li').length == 1) $('.cycle li').fadeIn();
			
			if($('body#p-watercolor_gallery').length > 0) renderWatercolors();
			 
		});
		
				
	}
	
	function hideImg() { $(this).hide(); }
	function fadeInImg() { $(this).fadeIn(); }
	
	function renderWatercolors(){
		$('.watercolor_cycle li').each(function(){$(this).css({display: 'none'});});
		$('.watercolor_cycle li:first').fadeIn();
		
		$('.watercolor_list ol li a').each(function(){
			$(this).click(function(){			
				$('.watercolor_list ol li a.cur').removeClass();
				$(this).addClass('cur');			
				$('ul.watercolor_cycle').cycle({fx: 'fade', speed: 3300, timeout: 0, before: hideImg, after: fadeInImg, startingSlide: $('.watercolor_list ol li a.cur').attr('rel')-1 });
				return false;
			});
		});
	}
	
	function loadTypography(){
		Cufon.replace('#logo', { fontFamily: 'Centaur', hover: true, hoverables: {a:true }});
		Cufon.replace('.project_numbers a', { fontFamily:'CentaurItalic', hover: true, hoverables: {a:true } } );
		Cufon.replace('body#home #intro p', { fontFamily: 'Centaur', hover: true, hoverables: {a:true }});
		Cufon.replace('#intro #text p', { fontFamily: 'Centaur', hover: true, hoverables: {a:true }});
	}
	
	function rotateHomepageImages(){
		
		var homepageImage = ['home-01.jpg','home-02.jpg','home-03.jpg','home-04.jpg','home-05.jpg','home-06.jpg'];
		var random = Math.floor(Math.random()*6);
		
		 $(window).load(function(){			
			if($('body#home').length > 0) {	
				$('.homepageImages').css({'background-image': 'url(img/homepage/'+homepageImage[random]+')'}).hide().fadeIn(750);
				$('#intro p').css('display','block');
			};
		});
		
				
	
	}
		
	init();
	
	function commentForm(){
		$('.field').each(function(){
			var title = $(this).find('label').text();
			$(this).find('input').attr('placeholder', title);
			// $(this).find('textarea').attr('placeholder', title);
		});
		
		$('.field input').each(function(){
			$(this).focus(function(){
				if ($(this).attr('value') != "") $(this).attr('value', '').removeClass('unfocus');
			}).blur(function(){
				if ($(this).attr('value') == "") $(this).attr('value', $(this).attr('placeholder')).addClass('unfocus');					
			}).attr('value', $(this).attr('placeholder')).addClass('unfocus');
		});
	}
	
});

$(document).ready(function() {
	function preloadImages(){
		var homepageImage = ['home-01.jpg','home-02.jpg','home-03.jpg','home-04.jpg','home-05.jpg','home-06.jpg'];		
		for (var i=0; i < homepageImage.length; i++) {
			$("<img>").css({display: 'none'}).attr({
				src: homepageImage[i],
				style: 'display:none'
			});
		};
	}
	if ($('body#home').length > 0) preloadImages();
	
});
