/* javascript.js */

var aSlides = new Array();
var openProject = "";
var slideshow;
var slideIndex;
// aKeys is an array of project anchors, set in a javascript tag on the bottom of the html page, from the database:
var aKeys = new Array();

// animate the window when jumping to an anchor:
function scrollWin(id, oldid){
	$('html').animate({
		scrollTop: $(id).offset().top+30
	}, 500,
	function(){
		location = oldid;
	});
}

// open project, set url anchor and start slideshow:
function gotoId(id){
	var num = 0;
	// next and previous buttons link to '#goto_' where _ is a project number. Parse that and goto the correct project.
	if(id.indexOf('#goto')>-1){
		num = parseFloat(id.substring(5,id.length));
		id = aKeys[num];
	} else {
		// else we have an actual project id to goto:
		for(i=0; i<aKeys.length; i++){
			if(id == aKeys[i]){
				num = i;
				break;
			}
		}
	}
	slideIndex = num;
	var aS = aSlides[num];
	var sshtml = '';
	var nav = '#nav'+num;
	slideshow = '#slideshow'+num;
	if(aS.length>1){
		for(var i=0; i<aS.length; i++){
			sshtml += '<div><img src="'+aS[i]['url']+'" alt="'+aS[i]['caption']+'" /></div><!-- end slide -->';
		}
		$(slideshow).html(sshtml);
	}
	
	$(id).parents().next(".project").slideDown("fast", function(){
		$('.slideshow').cycle('stop');
		if(aS.length>1){
			$('.slideplay').html('<a href="#" class="pause">&nbsp;</a>');
			setPlayPause();
			$(nav).html('');
			$(slideshow).cycle({
				fx:'scrollLeft',
				timeout:5000,
				speed:800,
				pager:nav,
				before:hideCaption,
				after:showCaption
			});
		}
		num--;
		newId = aKeys[num];
		if(num<0){
			newId = "#projects";
		}
		scrollWin(newId, id);
		openProject = id;
	});
}

// for next and previous buttons and project heads
$(function(){
	$(".p_link a").click(function(event) {
		var aTemp = this.href.split("#");
		var id = '#'+aTemp[1];
		event.preventDefault(); 
		$(".project").slideUp();
		gotoId(id);
	});
	$("#projects h3 a").click(function(event) {
		var aTemp = this.href.split("#");
		var id = '#'+aTemp[1];
		event.preventDefault();
		$(".project").slideUp();
		// project heads toggle projects open and closed when clicked:
		if(openProject!=id){
			gotoId(id);
		} else {
			openProject = "";
			scrollWin("#projects", "#");
		}
	});
});

// for the slideshow play/pause buttons:
function setPlayPause(){
	$('.pause').unbind('click');
	$('.pause').click(function(event) {
		event.preventDefault();
		if(slideshow){
			$(slideshow).cycle('pause');
			$(this).parent().html('<a href="#" class="play">&nbsp;</a>');
			setPlayPause();
		}
	});
	$('.play').unbind('click');
	$('.play').click(function(event) {
		event.preventDefault();
		if(slideshow){
			$(slideshow).cycle('resume');
			$(this).parent().html('<a href="#" class="pause">&nbsp;</a>');
			setPlayPause();
		}
	});
}

$(document).ready(function() {
	// this button will open all projects to print
	$("#printprojects").click(function(event){
		event.preventDefault();
		printall();
    });
	
	// open project if anchor exists in url
	var id = jQuery.url.attr("anchor");
	$(".project").slideUp();
	if(id){
		id = "#"+id;
		gotoId(id);
	}

	// prepare contact form
	$("#submit").click(function(){
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var nameVal = $("#name").val();
		if(nameVal == '[ name ]') {
			$("#name").after('<p class="error">Please enter your name.</p>');
			hasError = true;
		}
		
		var emailVal = $("#email").val();
		if(emailVal == '[ email ]') {
			$("#email").after('<p class="error">Please enter your email.</p>');
			hasError = true;
		} else if(!emailReg.test(emailVal)) {	
			$("#email").after('<p class="error">Enter a valid email address to send from.</p>');
			hasError = true;
		}
		
		var subjectVal = $("#subject").val();
		if(subjectVal == '[ subject ]') {
			$("#subject").after('<p class="error">Please enter a subject.</p>');
			hasError = true;
		}
		
		var messageVal = $("#message").val();
		if(messageVal == '[ message ]') {
			$("#message").after('<p class="error">Please enter a message.</p>');
			hasError = true;
		}

		if(hasError == false) {
			$(this).hide();
			$("#sendemail").append('<p id="loading"><img src="http://www.flydaddy.net/img/loading.gif" alt="loading" /></p>');
			$.post("../modules/sendemail.php",
   				{ name: nameVal, email: emailVal, subject: subjectVal, message: messageVal },
   				function(data){
					$("#sendemail").slideUp("normal", function() {				   
						$("#sendemail").after('<p>Thanks. Message received.</p>');											
					});
   				}
			);
		}
		
		return false;
	});
	
	// setup form
	setupForm($("#sendemail").find("input"));
	setupForm($("#sendemail").find("textarea"));
});

function hideCaption(){
	var cname = "#caption"+slideIndex;
	$(cname).hide('fast');
}
function showCaption(){
	var caption = "";
	if(this.firstChild.alt){
		caption = this.firstChild.alt;
	}
	var cname = "#caption"+slideIndex;
	$(cname).html(caption);
	$(cname).show('fast');
}


// for forms with no labels:
function clearField(obj){
	obj.value="";
}
function resetField(obj){
	// make the form field read '[ field name ]' when unfilled:
	if(obj.value=="" || obj.value==undefined){
		obj.value="[ "+obj.id+" ]";
	}
}
function setupForm(aFields){
	for(i=0; i<aFields.length; i++){
		var obj = aFields[i];
		resetField(obj);
	}
}

// printing all projects
var printcounter = 0;
function printall(){
	printcounter = 0;
	$('.slideshow').cycle('pause');
	$('.slideshow').cycle(0);
	$('.project').slideDown(function(){
		printcounter++;
		// if all projects have opened print the page:
		if(printcounter==$('.project').length){
			setTimeout("printpage();", 10);
		}
	});
}
function printpage(){
	// set the caption for the open project:
	var caption = aSlides[slideIndex][0]['caption'];
	var cname = "#caption"+slideIndex;
	$(cname).html(caption);
	$(cname).show();
	
	// print the page with all projects open then close all projects and goto the previously opened project:
	window.print();
	$(".project").slideUp("fast");
	var id = jQuery.url.attr("anchor");
	if(id){
		id = "#"+id;
		gotoId(id);
	}
}