	function preventDefaultAction(e)	{
		if (e)	{
   			if (typeof e.preventDefault != 'undefined')	{ 
				e.preventDefault(); 
				}               
    		else	{ 
				e.returnValue = false; 
				}                   
  			}
  		// safey for handling DOM Level 0
  		return false;
		}

	function cf_show_error(id, message) {
		if(message.length > 0) {
			if($('div.errors p.' + id).length > 0) {
				$('div.errors p.' + id).text(message);
				}
			else {
				$('div.errors').append('<p class="' + id + '">' + message + '</p>');
				}
			}
		else { /*no message, just clear the error if there is one*/
			$('div.errors p.' + id).remove();
			}
		if($('div.errors p').length > 0)
			$('div.errors').show();
		else
			$('div.errors').hide();
		}
	
	function bookmark_me(){
		var title = 'Matt Oatley';
		var url = 'http://www.mattoatley.com';
		var href = "http://www.mattoatley.com";
		if (window.sidebar) { // firefox
			window.sidebar.addPanel(title, url, href);
			}
		else if(window.opera && window.print) { // opera
			var elem = document.createElement('a');
			elem.setAttribute('href',url);
			elem.setAttribute('title',title);
			elem.setAttribute('rel','sidebar');
			elem.click();
			}
		else if(document.all) {// ie
			window.external.AddFavorite(url, title);
			}
		}
		
	function reset_bg_selected() {
		$("#bg_select > dd").removeClass('selected');
		}	

	function new_proj_id(which) {
		current = $("#projects div.current").attr("id");
		var num = current.substring(current.lastIndexOf('_') + 1, current.length);
		if(which == 'next')
			num++;
		else
			num--;
		var new_id = 'project_' + num;
		return new_id;
		}
	
	function get_last_project_id() {
		return $("#projects div.last").attr("id");
		}
	
	function set_current(id) {
		$("#projects .current").removeClass('current');
		$('#' + id).addClass('current');
		}
						
	function next_project(e) {
		if($('#projects div.current').hasClass('last')) {
			to_project('project_1', 1200);
			}
		else {
			var new_id = new_proj_id('next');
			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()
			to_project(last_id, 1200);	
			}
		else {
			var new_id = new_proj_id('previous');	
			to_project(new_id, 600);
			}
		return preventDefaultAction(e);
		}
		
	function to_project(new_id, speed) {
		if(speed) {
			$("#projects").animate({
					left: 70-$('#' + new_id).position().left
					}, speed);
				set_current(new_id);
			}
		else {
			$("#projects").animate({
					left: 70-$('#' + new_id).position().left
					}, 600);
				set_current(new_id);
			}
		}
	
	function init() {
		jQuery.easing.def = "easeOutQuint";
		$("#options").toggle();
		$("#leather_bg").click(function() {
			$("body").removeClass();
			$("body").addClass('leather');
			reset_bg_selected();
			$(this).addClass('selected');
			});
		$("#chalk_bg").click(function() {
			$("body").removeClass();
			$("body").addClass('chalk');
			reset_bg_selected();
			$(this).addClass('selected');
			});
		$("#wood_bg").click(function() {
			$("body").removeClass();
			$("body").addClass('wood');
			reset_bg_selected();
			$(this).addClass('selected');
			});	
		$("#next_project_btn").click(next_project);
		$("#previous_project_btn").click(previous_project);
		
		$('div.project_info, div.pi_background, div.pi_hitzone').fadeTo(5, 0);
		$('.fade').fadeTo(1, .50);
		$('div.project_info, div.pi_background').show();
		$('.fade').hover(
			function() {$(this).fadeTo('normal', 1);},
			function() {$(this).fadeTo('normal', .50);}
		);
		$('div.project_info').hover(
			function() {$(this).fadeTo('normal', 1); $(this).siblings('div.pi_background').fadeTo('normal', .93);},
			function() {$(this).fadeTo('normal', 0); $(this).siblings('div.pi_background').fadeTo('normal', 0);}
			);
		$('a.showcase_secondary, a.showcase_third').click(function() {
			$(this).siblings('a.showcase_link').children('img.showcase_main').attr('src', $(this).attr('href'));
			return preventDefaultAction();
			});	
		}
		
		
	//initiate when the document is ready
	$(document).ready(function(){
		init();	 
	});