/*****************************/
/* Importing 2.0 			*/
/* Author: Simon Ilett 	   */
/**************************/

/* import CSS */
function cssImport(src) {
	var head = document.getElementsByTagName('head')[0];
	if($(src))
		return;
	var newCSS = new Element('link', {
		rel		: 'stylesheet',
		type	: 'text/css',
		href	: BASE_URL+src,
		id		: src
	});
	head.insertBefore( newCSS, head.lastChild );
}


/*****************************/
/* Ajax calling methods 3.0 */
/* Author: Simon Ilett 	   */
/**************************/
//Obj to update, path to ajax, params to send, onSuccess function
function ajaxProcess(oUpdateObj, oURL, oParams, oCallback) {
	var tmp;
	return tmp = new Request.HTML({  
			 method: 'post',  
			 url: oURL,
			 data: oParams,  
			 onRequest: function() { new ajax_overlay(oUpdateObj); },  
			 update: oUpdateObj,  
			 onSuccess: function() {  if(oCallback) oCallback(); }  
	}).send();
}

// Send and retrieve 
function ajaxStore(oURL, oParams, oCallback) {
	var tmp;
	return tmp = new Request({  
			 method: 'post',  
			 url: oURL,  
			 data: oParams,  
			 onSuccess: function(response) { if(oCallback) oCallback(response); }  
	}).send();
}


/*************************/
/* target grabber       */
/* Author: Simon Ilett */ 
/**********************/
function getTarget(e){
	if (typeof(e)=='object') {
		var evt = new Event(e); 
		var target = $(evt.target);	
	} else {
		var target = $(e);	
	}
	return (target ? target : false);
}

/*************************/
/* prevent behaviours 1.0      */
/* Author: Simon Ilett */
/**********************/
function preventEvent(e) {
	if (e) {
		var evt = new Event(e); 
		evt.preventDefault();
		evt.stop();
	}	
}


/* is empty object */
/* json checks */

function isEmpty(object) {
	for(var i in object) { return true; }
	return false;
}


/* Menu for IE 
function startList(){$("nav").getChildren().each(function(el,key){if(el.nodeName=="LI"){el.addEvents({'mouseover': function(){this.className+=" over";},'mouseout': function(){this.className=this.className.replace(" over","");}});}});}

*/





/* Big A+ */
var aPlus = new Class({
	Implements: [Options, Events],
	options: {
		display: true
	},
	initialize: function(element, options){
		this.setOptions(options);
		this.update = $('aplusdesign');
		this.header = $$('header'); // HTML5 selector
		
		window.addEvents({
			//'scroll': this.replaceBox.bind(this),
			'resize': this.replaceBox.bind(this)
		});
		this.replaceBox(true);
	},
	replaceBox: function(flag) {
		if(this.options.display) {
			var tmp = this.header[0];
			var tmpCoords = tmp.getCoordinates();
			var top = (tmpCoords.left + 138) - 325;
			if (flag) {
				this.update.set('morph', {duration: 500, transition: 'linear'});	
				//this.update.morph({left: top+'px', opacity: [0,1]});
				
				this.update.setStyle('left', top+'px');
				this.update.morph({opacity: [0,1]});
			} else {
				this.update.setStyle('left', top+'px');
			}
		} 
	}
	
});


/* Twitter ajax to stop page lag */
var twitterUpdate = new Class({
	Implements: [Options, Events],
	options: {
		display: true,
		count: 2
	},
	initialize: function(options){
		this.setOptions(options);
		this.userName = this.options.user_id;
		this.update = $(this.options.id);
		if (this.userName && this.update)
			this.getTweets();
	},
	getTweets: function() {
		
		new Request.JSONP({
		  url: 'http://twitter.com/statuses/user_timeline/' + this.userName + '.json',
			data: {
				count: this.options.count
			},
			onSuccess: function(tweets){
				tweets.each(function(tweet,i) {
					var tmp = this.tweetify(tweet.text)
					new Element('li',{
						html: '<p><a href="http://twitter.com/' + this.userName + '" title="Follow Simon Ilett" target="_blank">#</a> ' + tmp + '</p>'	
					}).inject('twitter-updates');
				}, this);
			}.bind(this)
		}).send();
	},
	/* Credit to David Walsh here */
	tweetify: function(str) {
		return str.replace(/(https?:\/\/\S+)/gi,'<a href="$1">$1</a>').replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>').replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>');
	}
});



/* Booking form */
var contactForm = new Class({
							
	Implements: [Options, Events],
	
	options: {},
	
	initialize: function(element, options){
		this.setOptions(options);
		this.setEvents();
		this.setInputs();
		this.thanks = new Fx.Slide('contact-thanks').hide();
		
	},
	
	setEvents: function() {
		this.submitBtn = $('contact-submit');
		this.submitBtn.addEvent('click', function(e) {
			this.validateForm();
		}.bind(this));
	},
	
	doError: function(o) {
		o.tween('color', '#f00');
		
	},
	
	unError: function(o) {
		if(o.getStyle('color')!='#444444') {
			o.tween('color', '#444444');
		}
	},
	
	setInputs: function() {
		
		this.inputName = $('contact-name');
		this.inputEmail = $('contact-email');
		this.inputPhone = $('contact-phone');
		this.inputMessage = $('contact-message');
	
		this.namelabel = this.inputName.getPrevious();
		this.emaillabel = this.inputEmail.getPrevious();
		this.phonelabel = this.inputPhone.getPrevious();
		this.messagelabel = this.inputMessage.getPrevious();
		
	},
	
	validateForm: function() {
		
		this.submitBtn.removeEvents();
		
		this.nameValid = false;
		this.emailValid = false;
		this.phoneValid = false;
		this.messageValid = false;
		
		// valid name
		if(!this.inputName.value=='') { 
			this.nameValid = true;
			this.unError(this.namelabel);	
		} else {
			this.doError(this.namelabel);
		}
	
		// valid email
		regExp = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
		if(this.inputEmail.value!='') {
			this.emailValid = regExp.test(this.inputEmail.value);
		}
		if (!this.emailValid){
			this.doError(this.emaillabel);
		} else {
			this.unError(this.emaillabel);	
		}
		
		// valid phone
		var regExp = /^([0-9\s]{6,15})+$/;
		if(this.inputPhone.value!='') {
			this.phoneValid = regExp.test(this.inputPhone.value);
		}
		if (!this.phoneValid){
			this.doError(this.phonelabel);
		} else {
			this.unError(this.phonelabel);	
		}
		
		
		// valid message
		if(!this.inputMessage.value=='') { 
			this.messageValid = true;
			this.unError(this.messagelabel);	
		} else {
			this.doError(this.messagelabel);
		}
	
		
		if (this.nameValid && this.emailValid && this.messageValid) {
			this.submitContact({
				'type' : 'contact',			   
				'name': this.inputName.value,
				'phone': this.inputPhone.value,
				'email': this.inputEmail.value,
				'message': this.inputMessage.value
			});		
		} else {
			this.setEvents();	
		}
		
	},
	
	submitContact: function(params) {
		this.submitBtn.fade('out');
		//$('contact-thanks').slideOut();
		new Fx.Slide('form-contact').slideOut();
		$('contact-thanks').setStyle('display', 'block');
		this.thanks.slideIn();
		var theCallback = function (response) {
			this.inputMessage.value ='';
		}.bind(this); 
		ajaxStore(BASE_URL + '_code/ajax.php', params, theCallback);
	}
	
});

/* Final call */
window.addEvent('domready',function(){setTimeout("init()",100);});
