function extender(sel){
	if(sel.selectedVal == "other"){
		console.log(sel.selectbox);
		var moreinfo = $('<input type="text" name="'+sel.selectbox+'_more" value="" />');
		$("#"+sel.selectbox+"_input").after(moreinfo);
	}
}

function showcasify(){
	$("#show_nav a").not(".portfolio").removeAttr("href");
	//for JS-browsers, all projects are hidden by default; show the first
	$("#showcase article:first").css("display","block");
	$("#show_nav li:first").addClass("active");
	$("#show_nav a").click(function(){
		//hide the visible project and show the project with the same index as the button
		var index = $("#show_nav a").index($(this));
		casify(index);
	});
	autocasify();

}

function casify(index){
	$("#showcase article:visible").fadeOut(600);
	$("#showcase article").eq(index).fadeIn(600);
	$("#show_nav li.active").removeClass("active");
	$("#show_nav li").eq(index).addClass("active");
}

function autocasify(){
	function delayTimer(delay){
		var timer;
		return function(fn){
			timer = clearTimeout(timer);
			if(fn){timer = setTimeout(function(){fn();},delay);};
			return timer;	
			}
	};
	//start the timer	
	var timer = delayTimer(5000);
	timer(function(){next();});

	function next(){
		//index of currently shown project
		var current = $("#show_nav li.active").index();
	 	//index starts at 0; exclude portfolio link
		if ((current + 1) >= ($("#show_nav li").size() - 1)){casify(0);}
		else{casify(current+1);}
		timer(function(){next();});
	};

	$("#show_nav").hover(
		function(){timer();},
		function(){timer(function(){next()});}
	);

	$("#showcase article").hover(
		function(){timer();},
		function(){timer(function(){next()});}
	);
}

function formify(){
	//jQueryify elements
	$('input[name=due_date]').datepicker({dateFormat:'MM d, yy'});
	$('#purpose').selectbox({onChangeCallback:extender});
	$('#personality').selectbox();
	$('textarea').elastic(); //with thanks to Jan Jarfalk http://www.unwrongest.com/projects/elastic/
	//remove the placeholders on focus; restore if the field hasn't been changed
	$("input,textarea")
		.focusin(function(){
			if($(this).html()){
				$(this).data("default",$(this).html());		
				$(this).html("");
				}
			else{
				$(this).data("default",$(this).val());
				$(this).val("");
				}
			})
		.focusout(function(){
			var restore = $(this).data("default");
			if($(this).is('textarea')){$(this).html(restore)}
			else{$(this).val(restore);};
			});
	//if the user writes something, unbind the above
	$("input,textarea").keydown(function(){$(this).css("color","#000"); $(this).unbind('focusin focusout');});
	}

$(document).ready(function(){
	var page = $("body").addClass("snazzy").attr("id");
	switch(page){
		case "home":
			showcasify();
			break;
		case "contact":
			formify();
			break;
	}
})
