function add_toggle(field) {
	$("#"+field).focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
}

function sticky_nav(element) {
	$(".btns").children("li").each(function(count,ele){
		$(ele).children("a:first").css("background-position","left");
	});
	$(element).children("a:first").css("background-position","right");
}

function sticky_nav_portfolio(element) {
	$("#nav_port a").each(function(count,ele){
		$("#"+ele.id).removeClass('navbtn-selected');
	});
	$("#"+element).addClass('navbtn-selected');
}

function stick_nav(id) {
	var classes = $("#"+id).attr("class");
	var tmp = new Array();
	tmp = classes.split(' ');
	for(i=0;i<tmp.length;i++) {
		if(tmp[i].substring(0,3) == "cat") {
			cat = tmp[i].substring(3,tmp[i].length);
			if(!$("#"+cat).hasClass("navbtn-selected")) sticky_nav_portfolio(cat);
		}
	}
}

function next_project() {
	if($('#projects div.current').hasClass('last')) {
		var first_id = get_first_project_id();
		stick_nav(first_id);
		to_project(first_id, 1200);	
	} else {
		var new_id = new_proj_id('next');
		stick_nav(new_id);
		to_project(new_id, 600);		
	}
	return preventDefaultAction(e);
}

function previous_project(e) {
	if($('#projects div.current').hasClass('first')) {
		var last_id = get_last_project_id();
		stick_nav(last_id);
		to_project(last_id, 1200);	
	} else {
		var new_id = new_proj_id('previous');
		stick_nav(new_id);
		to_project(new_id, 600);
	}
	return preventDefaultAction(e);
}

function get_last_project_id() {
	return $("#projects div.last").attr("id");
}

function get_first_project_id() {
	return $("#projects div.first").attr("id");
}


function to_project(new_id, speed) {
	if(speed) {
		$("#projects").animate({
				left: 0-$('#' + new_id).position().left
		}, speed);
		set_current(new_id);
	} else {
		$("#projects").animate({
			left: 70-$('#' + new_id).position().left
		}, 600);
		set_current(new_id);
	}
}

function set_current(id) {
	$("#projects .current").removeClass('current');
	$('#' + id).addClass('current');
}

function new_proj_id(which) {
	current = $("#projects div.current").attr("id");
	if(which=='next') {
		new_id = $("#projects div.current").next().attr("id");
	} else {
		new_id = $("#projects div.current").prev().attr("id");
	}
	return new_id;
}

function preventDefaultAction(e) {
	if(e) {
		if (typeof e.preventDefault != 'undefined')	{ 
			e.preventDefault(); 
		} else {
			e.returnValue = false; 
		}                   
	}
	return false;
}

function jump_to(cat) {
	id = $("#projects div."+cat).attr("id");
	if(id != undefined) to_project(id, 600);
}

function port_init() {
	var left = 750;
	$('#projects').children().each(function(child,aab){
		
		$(aab).css('left',left);
		left+=750;
		
	});
	$("#go_left").click(previous_project);
	$("#go_right").click(next_project);
	
	$('div.project_info, div.pi_background').fadeTo(5, 0);
	$('div.project_info, div.pi_background').show();
	
	
	$('div.project_info').hover(
		function() {
			$(this).stop(true,true).fadeTo('normal', 1); 
			$(this).siblings('div.pi_background').stop(true,true).fadeTo('normal', .93);
		},
		function() {
			$(this).fadeTo('normal', 0); 
			$(this).siblings('div.pi_background').fadeTo('normal', 0);
		}
	);
	
	$("#has_loaded").attr("value","yes");
	 jump_to("cat_websites");
	 $(".gallery a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded'});
}

$(document).ready(function() {  
	port_init();
});
