// portfolio item swap for home page
if (typeof(swap_items) != "undefined" && swap_items == 1) {
	var hinck_item = "<img id='folioImg' src='/media/photos/showcase-hinck.jpg' alt='' />"
	  + "<p id='folioCaption'>"
		+ "<span id='captionText'>A State Rep. trusted me to design his campaign site, and it paid off&#8202;&mdash;&#8202;twice.</span>"
	  + "</p>"
		+ "<a id='showMore' href='/portfolio' rel='internal' title='See this and other projects in my portfolio.'>See more!</a>";
					
	var ncdc_item = "<img id='folioImg' src='/media/photos/showcase-ncdc.jpg' alt='' />"
	  + "<p id='folioCaption'>"
		+ "<span id='captionText'>An inspiring non&#45;profit chose twelve8 to refresh its web presence.</span>"
	  + "</p>"
		+ "<a id='showMore' href='/portfolio' rel='internal' title='See this and other projects in my portfolio.'>See more!</a>";
					
	var current_item = ncdc_item;
	function swapItems() {
	  // fade out current picture
		$("#folioImg").animate({opacity: 0},"slow");
		// switch image and caption using innerHTML
		if (arguments[0] == current_item) {
		  $("#folioFrame").html(arguments[1]);
			current_item = arguments[1];
		} else {
		  $("#folioFrame").html(arguments[0]);
			current_item = arguments[0];
		}
	  $("#folioImg").css("opacity",0);
		// fade in new image
		$("#folioImg").animate({opacity: 1},"slow");
	}
}
// send email without refresh using AJAX;
function sendEmail() {
  // gather form data
	var the_message = document.getElementById("cMessage").value,
		the_name = document.getElementById("cName").value,
		the_email = document.getElementById("cEmail").value,
		the_phone = document.getElementById("cPhone").value,
		the_math = document.getElementById("cMath").value;
	// assemble query string
	var stuff = "c_message=" + escape(the_message) + "&c_name=" + escape(the_name) + "&c_email=" + escape(the_email) + "&c_phone=" + escape(the_phone) + "&c_math=" + escape(the_math);
  var page = "/php/ajax_send_email.php";
  // pass query string to php email script
	loadXMLPosDoc(page,stuff)
	return false;
}

function getKeyCode(e) {
	var evt = e || window.event;
	var char_code;
	if (e.keyCode) { char_code = e.keyCode; } else if (e.which) { char_code = e.which; }
	return char_code;
}

function domMoveDown() {
  // clone contact form "header"
  var clone = $("#footerWrap").clone();
	// remove original "header"
	$("#footerWrap").remove();
	clone.hide();
	// place clone at original footer position
	clone.insertAfter($("#contentWrap"));
	clone.css("background-color","#84A92A");
	$("#finePrint").show();
	clone.show();
	$("#navMenu a.contact").css("color","");
	$("#navMenu a.contact").unbind("click");
	// restore original onclick events
	$("#navMenu a.contact").click(domMoveUp);
	$("#contactForm").submit(function(){
	  return false;
	});
	$("#cSubmit").click(sendEmail);
	$("#cCancel").removeAttr("title");
  $("#closeContact").remove();
	return false;
}
function domMoveUp() {
  // clone contact form footer
  var clone = $("#footerWrap").clone();
	// remove contact form footer
	$("#footerWrap").remove();
	clone.hide();
	// place cloned footer at top of page
	clone.insertBefore($("#headerWrap"));
	clone.css("background-color","#52691A");
	$("#finePrint").hide();
	clone.show();
	$("#navMenu a.contact").css("color","#52691A");
	// change onclick events to trigger replacement of footer at bottom of page
	$("#navMenu a.contact").unbind("click");
	$("#navMenu a.contact").click(domMoveDown);
	$("#cCancel").click(domMoveDown);
	// focus message field in form
	$("#cMessage").focus();
	// prevent submit triggering (in order to use AJAX)
	$("#contactForm").submit(function(){
	  return false;
	});
	// attach sendEmail function to submit
	$("#cSubmit").click(sendEmail);
	// allow domMoveDown to be triggered (replacing contact form at bottom of page)
	$("#cCancel").attr("title","Hide this contact form");
	$("<a href='#' id='closeContact' title='Hide this contact form'>X</a>").insertBefore("#contact h1");
	$("#closeContact").click(function(){
	  domMoveDown();
		$("#closeContact").remove();
		return false;
	});
	return false;
}

// removes link titles which are redundant when tooltips are active
function removeTitles() {
  var current_title = undefined;	 
	$("#navMenu a:not(.home)").bind("mouseover", function(){
	  var current_link = this.parentNode.childNodes[1];
	  current_title = current_link.title;
	  current_link.title = "";
	});
	$("#navMenu a:not(.home)").bind("mouseout", function(){
	  var current_link = this.parentNode.childNodes[1];
	  current_link.title = current_title;
	  current_title = undefined;
	});
}

$(document).ready(function(){
  // attach domMoveUp function to "contact" click and enter key (if not IE)
	if (!/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)){
	  $("#navMenu a.contact").click(domMoveUp);
	}
	
	removeTitles();
	
	$("#contactForm").submit(function(){
	  return false;
	});
	
	// attach sendEmail function to enter key and submit button click (if not IE)
	if (!/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)){
	  $("#contactForm").keypress(function(e) {
	    var key_code = getKeyCode(e);
		  var evt = e || window.event;
		  if (evt.target) var evt_target = evt.target;
		  else evt_target = evt.srcElement;
		  if (evt_target.id != "cMessage") {
		    if (key_code == 13) {
		      sendEmail();
		    }
		  }
	  });
	  $("#cSubmit").click(sendEmail);
	}
	// if current page has a portfolio slideshow, initiate it
	if (typeof(swap_items) != "undefined" && swap_items == 1) {
	  var second_img = new Image();
		second_img.src = "/media/photos/showcase-hinck.jpg";
	  var swap_timer = setInterval("swapItems(hinck_item,ncdc_item)",5000);
		$("#folioImg").animate({opacity: 1},"fast");
	}
});