// FilmStrip --- //
// var slider = new fs({fsFrameId: 'showcase', crtFrameId: 'thumbs'}); //
function fs(x){
	var self = this;
	this.motion = null;
	this.obj = {fsFrame:document.getElementById(x.fsFrameId),crtFrame:document.getElementById(x.crtFrameId),links:null,slides:null}
	this.val = {time:0,duration:100,curPos:0,newPos:0,ulWidth:0,liWidth:0}
	this.obj.links = this.obj.crtFrame.getElementsByTagName('a');
	this.obj.slides = this.obj.fsFrame.getElementsByTagName('li');
	this.val.curPos = this.obj.fsFrame.scrollLeft;
	this.val.liWidth = this.obj.slides[0].offsetWidth;
	this.val.ulWidth = this.val.liWidth*this.obj.slides.length;	
	this.obj.fsFrame.getElementsByTagName('ul')[0].style.width = this.val.ulWidth + 'px';	
	for(var i=0;i<this.obj.links.length;i++){
		this.obj.links[i].rel = i;
		this.obj.links[i].onclick = function() {
			if(self.motion == null){
				for(var j=0;j<self.obj.links.length;j++){self.obj.links[j].className = '';}
				self.setMove(this.rel);
				this.className = 'selected';
				return false;
			}
			else{return false;}
		};
	}	
	this.setMove = function(num){
		self.val.time=0;
		self.val.newPos = self.val.liWidth*num;
		self.motion = setInterval(function(){self.move();}, 20);
	};
	this.move = function() {
		self.val.curPos = self.obj.fsFrame.scrollLeft;
		var dist = self.val.newPos - self.val.curPos;
		var move = self.ease(self.val.time, self.val.curPos, dist, self.val.duration); 
		if(self.val.newPos > self.val.curPos){
			move = Math.ceil(move);
			this.obj.fsFrame.scrollLeft = move;
			if(move == self.val.newPos){
				clearInterval(self.motion);
				self.motion = null;
			}
		}
		else{
			move = Math.floor(move);
			this.obj.fsFrame.scrollLeft = move;
			if(move == self.val.newPos){
				clearInterval(self.motion);
				self.motion = null;
			}
		}
		self.val.time++;
	};
	this.ease = function (t, b, c, d){return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;};
}	


// Validation --- //
// Pass form ID to validate, along with added class='required' to required inputs and id='e_mail' to required e-mail
function validate(formId){
	var inputsWithErrors = [];
	var reqFields = [];
	var form = document.getElementById(formId);
	var formElems = form.getElementsByTagName('*');
	for(var i=0;i<formElems.length;i++){
		if(formElems[i].className.match('required')){
			reqFields.push(formElems[i]);
		}
	}
	for(var i=0;i<reqFields.length;i++){
		var tag = reqFields[i].tagName.toLowerCase();
		if(tag == 'input' || tag == 'textarea'){
			if(reqFields[i].value == '' || reqFields[i].value == null){
				inputsWithErrors.push(reqFields[i]);
			}
			else if(reqFields[i].id == 'e_mail'){
				var emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/ig;
				if(!reqFields[i].value.match(emailRegex)){
					inputsWithErrors.push(reqFields[i]);
				}
			}
			else if(reqFields[i].id == 'turing'){
				var turingVal = reqFields[i].value;
				if(turingVal.toLowerCase() == 'four'){
					turingVal = '4';
				}
				if(turingVal != '4'){
					inputsWithErrors.push(reqFields[i]);
				}
			}
		}
	}
	for(var i=0;i<reqFields.length;i++){
			if(reqFields[i].parentNode.className.match('highlight')){
				var clearClass = reqFields[i].parentNode.className.replace('highlight','');
				reqFields[i].parentNode.className = clearClass;
			}
		}
	if(inputsWithErrors.length > 0){
		for(var i=0;i<inputsWithErrors.length;i++){
			inputsWithErrors[i].parentNode.className += ' highlight';
		}
		return false;
	}
	else{
		var notification = document.createElement('span');
		var sendButton = document.getElementById('send');
		var sendContain = sendButton.parentNode;
		if(document.getElementById('sending')){
			var notify = document.getElementById('sending');
			if(notify.innerHTML == 'Message Sent!'){
				notify.setAttribute('style','background: url(styles/images/loading.gif) left 7px no-repeat;');
				notify.innerHTML = 'Sending another message...';
			}
		}
		else{
			notification.id = 'sending';
			notification.innerHTML = 'Sending Message...';
			sendContain.appendChild(notification);
		}
		submission = true;
		sendMessage();		
		return false;
	}
}

// Sweet AJAX stuff... --- //
function getHTTPObject() {
	var xhr = false;
	if (window.XMLHttpRequest){xhr = new XMLHttpRequest();}
	else if (window.ActiveXObject) {
		try{xhr = new ActiveXObject('Msxml2.XMLHTTP');}
		catch(e){
			try{xhr = new ActiveXObject('Microsoft.XMLHTTP');}
			catch(e){xhr = false;}
		}
	}
	return xhr;
}
function sendMessage(){
	var sendReq = getHTTPObject();
	var data = queryData();
	var file = 'includes/contact.php';
	if(sendReq){
		sendReq.onreadystatechange = function(){
			confirm(sendReq);
		}
		sendReq.open('POST', file, true);
		sendReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
		sendReq.send(data);
	}
}
function confirm(sendReq){
	var notify = document.getElementById('sending');
	if (sendReq.readyState == 4) {
		if (sendReq.status == 200 || sendReq.status == 304){
			if(sendReq.responseText == 'sent'){
				setTimeout(function(){
					notify.style.backgroundImage = 'none';
					notify.style.paddingLeft = '0px';
					notify.innerHTML = 'Message Sent!';
					submission = false;
				}, 3000);
				
			}
			else{
				notify.innerHTML = 'Sorry, there was a problem with your message, try again later...';
				notify.style.backgroundImage = 'none';
			}
		}
		else{
			notify.innerHTML = 'Sorry, there was a problem with your message.';
			notify.style.backgroundImage = 'none';
		}
	}
}
function queryData(){
	var data = '';
	var nameVal = document.getElementById('name').value;
	var emailVal = document.getElementById('e_mail').value;
	var message = document.getElementById('message').value;
	var turing = document.getElementById('turing').value;
	data = 'name=' + encodeURIComponent(nameVal);
	data += '&e_mail=' + encodeURIComponent(emailVal);
	data += '&message=' + encodeURIComponent(message);
	data += '&turing=' + encodeURIComponent(turing);
	data += '&js=on';
	data += '&send=send';
	return data;
}
function resetForm(){
	var form = document.getElementById('contactMe');
	var inputs = form.getElementsByTagName('input');
	var text = form.getElementsByTagName('textarea')[0];
	for(var i=0;i<3;i++){
		inputs[i].value = '';
	}
	text.value = '';
	if(document.getElementById('sending')){
		var sending = document.getElementById('sending');
		var sendPar = sending.parentNode;
		sendPar.removeChild(sending);
	}
	submission = false;
}

var submission = false;
window.onload = function() {	
	if(document.getElementById('showcase')){
		var slider = new fs({fsFrameId: 'showcase', crtFrameId: 'thumbs'});
	}
	var form = document.getElementById('contactMe');
	resetForm();
	form.onsubmit = function(){
		if(submission === false){
			return validate('contactMe');
		}
		else {
			return false;
		}
	}
}