var totalFeatured;
var switcher;
$(document).ready(function() {
	$('body').addClass('hasJS');
	$('.hasJS #header h3').css({
		width: Math.ceil($(window).width()/2)-160
	});
	$(window).resize(function(){
		$('.hasJS #header h3').css({
			width: Math.ceil($(window).width()/2)-160
		});
	})
	
	$('.post:first').addClass('first_post');
	
	//	Featured Project Image Rotation
		//	Build Stops
			$('#featured_image li').each(function(){
				$('#featured_footer .stops').append('<li><a href="'+$(this).find('a').attr('href')+'">'+$(this).find('img').attr('alt')+'</a></li>');
			})
			$('#featured_footer .stops li:first-child').addClass('active');
	
		$('#featured_description li').not('.active').hide();
		
		totalFeatured = $('#featured_image li').size();
		if (totalFeatured > 1) {
			switcher = setInterval('switchFeatured()', 10000);
		}
		
		$('#featured_footer .stops li').click(function(e){
			e.preventDefault();
			switchFeatured($(this).index());
			clearInterval(switcher);
			switcher = setInterval('switchFeatured()', 10000);
		})
		
		$('#featured_footer .readmore').attr('href', $('#featured_description .active a').attr('href'));
		
	//	Work Page
		$('#work #projects .project:nth-child(3n+1)').not(':first-child').addClass('clear');
		$('#work #projects .project:nth-child(3n-1)').addClass('middle');
		
		//	Project Type Switcher
			// $('#work #work_nav a').click(function(e){
			// 	e.preventDefault();
			// 	var newlist = $(this).text().toLowerCase();
			// 	$(this).text($('#work_selector h2').text());
			// 	$('#work_selector h2').text(newlist.substr(0,1).toUpperCase()+newlist.substr(1));
			// 	$('#project_list').slideUp('fast', function(){
			// 		$('#projects').addClass('loading');
			// 		$('#project_list').load('/includes/'+newlist+'.php', function(){
			// 			$('#work #projects .project:nth-child(3n+1)').not(':first-child').addClass('clear');
			// 			$('#work #projects .project:nth-child(3n-1)').addClass('middle');
			// 			$('#projects').removeClass('loading');
			// 			$('#project_list').slideDown('fast');
			// 		})
			// 	})
			// 	
			// })
		
		
	//	ClearInput
		var fields = new Array();
		var values = new Array();

		$('.ci').each(function(i){
			$(this).removeClass('ci').addClass('ci'+i);
			fields.push('ci'+i);
			values.push($(this).val());

			$(this).focus(function(){
				if($(this).val() == values[i]) {
					$(this).val('');
				}
			});
			$(this).blur(function(){
				var valStore = values[i];
				if($(this).val()==''){
					$(this).val(valStore);
				}
			})
		});

// Blog -------------------------------------------

	//	Post Navigation
		if(!$('.single .postnavigation .next').is(':empty') && !$('.single .postnavigation .prev').is(':empty')){
			setTimeout('navFade("prev", "next")', 5000);
			$('.single .postnavigation .next').hide();
		}
	//	Search focus?
		if($('div.noposts').size() > 0){
			$('#searchform #s').select();
		}
		
	
});

function navFade(fadeout, fadein) {
	$('.single .postnavigation .'+fadeout).fadeOut('slow', function(){
		$('.single .postnavigation .'+fadein).fadeIn('slow', function(){
			if(fadein == "next"){
				setTimeout('navFade("next", "prev")', 5000);
			} else {
				setTimeout('navFade("prev", "next")', 5000);
			}
			
		})
	})
}



function switchFeatured(newImage) {
	if (newImage != $('#featured_image .active').index()) {
		if(newImage >= 0){
			var nextImage = $('#featured_image li:eq('+newImage+')');
			var nextDescription = $('#featured_description li:eq('+newImage+')');
		} else {
			if($('#featured_image .active').index() == totalFeatured-1) {
				var nextImage = $('#featured_image li:first-child');
				var nextDescription = $('#featured_description li:first-child');
			} else {
				var nextImage = $('#featured_image .active').next('li');
				var nextDescription = $('#featured_description .active').next('li');
			}
		}
		nextImage.addClass('next').show();
		nextDescription.fadeIn('slow').addClass('next');
		$('#featured_image .active').fadeOut('slow', function(){
			$(this).removeClass('active')
			nextImage.removeClass('next').addClass('active');
			$('#featured_footer .stops .active').removeClass('active');
			$('#featured_footer .stops li:eq('+nextImage.index()+')').addClass('active');
		});
		$('#featured_description .active').fadeOut('slow', function(){
			$(this).removeClass('active')
			nextDescription.removeClass('next').addClass('active');
			$('#featured_footer .readmore').attr('href', $('#featured_description .active a').attr('href'));
		});
	}
}
