var disciplines = new Array();
var projects = new Array();


	$(document).ready(function(){

		
		$(".project").each(function(i){
		
			// add ID to project
			$(this).attr("id","project"+i);
								
			$(this).find(".disciplines li").each(function(){
				// add cursor type to replicate link
				$(this).css("cursor","pointer");	
				// add title
				$(this).attr("title","Show projects where I worked on the " + $(this).text() );	
				
				buildDisciplines($(this).text(),i);
			});
		});
		
		$(".disciplines li, #disciplines li").click(function(){
											
			showDisciplines($(this).text());	
			
			return false;
											
		});
		
		if($.browser.msie && $.browser.version=="6.0"){
			// do nothing for IE
			return true;
		} else {
			
			$("#hire-me a").click(function(){
				$("#hire-me").fadeOut("fast");
				$("#contact").animate({ top: '0px' }, 500);
				$("#wrapper").animate({ paddingTop: '162px' }, 500);
				$("#contact input[name=name]").focus();
				
				return false;
			});
		
		}
		
		
		$("#contact-form").validate({
				errorClass: "invalid",
				errorElement: "div",
				rules: {
					name: "required", // NOTE: linkselects must be validated against their ID rather than name
					email: {
						email: true,
						required: true
					},
					message: "required"
				},
				messages: {
					name: "",
					email: "",
					message: ""
				}
		});
		
		$("#contact-form").submit(function(){
										   
			if($(this).valid()){
				
				$(this).hide();
				$("#contact .sending").show();
				
				var myName = $(this).find("input[name=name]").val();
				var myEmail = $(this).find("input[name=email]").val();
				var myValidEmail = $(this).find("input[name=valid_email]").val();
				var myTelephone = $(this).find("input[name=telephone]").val();
				var myMessage = $(this).find("textarea[name=message]").val();
				var sendType = "AJAX";
				
				$.post("/send.php", { name: myName, email: myEmail, valid_email: myValidEmail, telephone: myTelephone, message: myMessage, send_type: sendType }, function(result){
																																				
					if(result == "true"){
						$("#contact .sending").hide();
						$("#contact .sent").show();
						
						//clear fields
						$("#contact-form input[type=text]").val("");
						$("#contact-form textarea").val("");
						
					} else {
						$("#contact .sending").hide();
						$("#contact-form").show();
						alert("Oops, for some reason your message didn't send - please try again");	
					}
																																				
				});
			}
			
			return false;
		});
		
		$("#contact a.close").click(function(){
			
			$("#contact").animate({ top: '-170px' }, 500);
			$("#wrapper").animate({ paddingTop: '0px' }, 500, function(){
																	   
				$("#hire-me").fadeIn("fast");
				$("#contact .sending").hide();
				$("#contact .sent").hide();	
				$("#contact #contact-form").show();
			});

			return false;								 
		});


		//$("img").lazyload();

	}); // end doc ready
	
	function buildDisciplines(discipline, projectID){
		
		if( disciplines.length < 1 ){
			
			//add discipline to array
			disciplines.push(discipline);
			//create project array
			var projectArr = new Array(disciplines.length-1, projectID);
			//add project array to project array
			projects.push(projectArr);
			
		} else {
			
			var alreadyAdded = false;
			var currentIndex = -1;
			
			for(i=0; i<disciplines.length; i++){
				if(disciplines[i] == discipline){
					alreadyAdded = true;
					currentIndex = i;
					break;	
				}
			}
			
			if(!alreadyAdded){
				//add discipline to array
				disciplines.push(discipline);
				//create project array
				var projectArr = new Array(disciplines.length-1, projectID);
				//add project array to project array
				projects.push(projectArr);
			} else {
				//create project array
				var projectArr = new Array(currentIndex, projectID);
				//add project array to project array
				projects.push(projectArr);
			}
			
		}
		
		$("#discipline #disciplines ul").children().remove();
		
		for(i=0; i<disciplines.length; i++){
			
			$("#discipline #disciplines ul").append("<li>" + disciplines[i] + "</li>");
		}
		
	}
	
	function showDisciplines(discipline){
		
		var matches = new Array();
		var matchIndex = -1;
		
		for(i=0; i<disciplines.length; i++){
			if(disciplines[i] == discipline){
				 // we have a match
				 matchIndex = i;
			}
		}
		
		for(n=0; n<projects.length; n++){
		
			if(projects[n][0] == matchIndex){
				//add all projects that have the same discipline
				matches.push(projects[n]);
			}
			
		}
						
		$("#discipline h2").html("All <em class=\"colour\">" + discipline + "</em> projects");
		// clear existing
		$("#discipline #matches ul").children().remove();
		
		for(x=0; x<matches.length; x++){
			
			var HTML = $("#project" + matches[x][1] + " .thumbnail").html();
			//console.log(matches[x][1]);
			$("#discipline #matches ul").append("<li id=\"goto"+matches[x][1]+"\">" + HTML + "</li>");
			
		}
		
		$("#discipline #matches ul li").click(function(){
											
			gotoProject($(this).attr("id"));				
											
		});
		
		$.fn.colorbox({inline:true, href:"#discipline", open: true});
		
	}
	
	function gotoProject(id){
		// parse ID
		var target = id.replace("goto","#project");
		// close window
		$.fn.colorbox.close();
		//scroll window to project
		$.scrollTo(target, 1000, { offset: {top:-15, left:0}, easing:'easeOutQuad', onAfter: glowProject(target) } );
		
	}
	
	function glowProject(id){
		
		setTimeout(function() { $(id).glow('#FFE400',1000,100) }, 500);
	}

