/**
 * @author webinc
 */

function loadTwitter(){

	var username = 'franksegers'; // set user name
	var format = 'json'; // set format, you really don't have an option on this one
	var url = 'http://api.twitter.com/1/statuses/user_timeline/' + username + '.' + format + '?callback=?&count=1'; // make the url
	$.getJSON(url, function(tweet){ // get the tweets
		$(".what").html(formatTwitString(tweet[0].text)); // get the first tweet in the response and place it inside the div
		var tTime = new Date(Date.parse(tweet[0].created_at));
		var cTime = new Date();
		var sinceMin = Math.round((cTime - tTime) / 60000);
		if (sinceMin == 0) {
			var sinceSec = Math.round((cTime - tTime) / 1000);
			if (sinceSec < 10) 
				var since = 'less than 10 seconds ago';
			else 
				if (sinceSec < 20) 
					var since = 'less than 20 seconds ago';
				else 
					var since = 'half a minute ago';
		}
		else 
			if (sinceMin == 1) {
				var sinceSec = Math.round((cTime - tTime) / 1000);
				if (sinceSec == 30) 
					var since = 'half a minute ago';
				else 
					if (sinceSec < 60) 
						var since = 'less than a minute ago';
					else 
						var since = '1 minute ago';
			}
			else 
				if (sinceMin < 45) 
					var since = sinceMin + ' minutes ago';
				else 
					if (sinceMin > 44 && sinceMin < 60) 
						var since = 'about 1 hour ago';
					else 
						if (sinceMin < 1440) {
							var sinceHr = Math.round(sinceMin / 60);
							if (sinceHr == 1) 
								var since = 'about 1 hour ago';
							else 
								var since = 'about ' + sinceHr + ' hours ago';
						}
						else 
							if (sinceMin > 1439 && sinceMin < 2880) 
								var since = '1 day ago';
							else {
								var sinceDay = Math.round(sinceMin / 1440);
								var since = sinceDay + ' days ago';
							}
		
		$(".when").html(since);
		
	});
}

function formatTwitString(str)
{
	// This function formats the tweet body text

	str=' '+str;

	str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
	// The tweets arrive as plain text, so we replace all the textual URLs with hyperlinks

	str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
	// Replace the mentions

	str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	// Replace the hashtags

	return str;
}


function sendContactForm(){
	// reset captcha image
	if(!checkContactForm()) return false;
	$('#submit').attr("disabled",true);
	
	$.ajax({
		type: "POST",
		url: "securimage/securimage_segers.php",
		data: $("#contactform").serialize(),
		complete: function(msg){
			//alert( "responsetext = " + msg.responseText);
			if(msg.responseText.match(/^OK/) != null){
				//MESSAGE SENT!!
				// set visual
				setFormField($("#captcha"),true);
				$("#contactform")[0].reset();
				//$("#formsent").show();				
				$("#formsent").blink({
				    repeat: 3
				}, function(){setTimeout(function(){$("#formsent").fadeOut(500)},3000);});
				
				
				
			}
			else{
				//MESSAGE NOT SENT!!
				setFormField($("#captcha"),false);
			}
			
			$('#submit').attr("disabled",false);
			
			// captcha altijd weer leegmaken
			$("#captcha_img").attr("src","securimage/securimage_show.php?" + Math.random());
			$("#captcha").val("");
		}
	});

}

function checkContactForm(){
	var blnCheckEmail, blnCheckPhoneNumber, blnCheckName, blnCheckMessage;
	blnCheckEmail = checkEmail();
	blnCheckPhoneNumber = checkPhoneNumber();
	blnCheckName = checkName();
	blnCheckMessage = checkMessage();
	
	if(blnCheckEmail && blnCheckPhoneNumber && blnCheckName && blnCheckMessage) return true;
	else return false;
}

function checkEmail(){
	var $field = $("#email");
	var check = $field.isEmail();
	setFormField($field,check);
	return check;
}

function checkPhoneNumber(){
	var $field = $("#phone");
	var check = $field.isPhoneNumber();
	setFormField($field,check);
	return check;
}

function checkName(){
	var $field = $("#name");
	var check = $field.isName();
	setFormField($field,check);
	return check;
}

function checkMessage(){
	var $field = $("#message");
	var check = $field.minLength(10);
	setFormField($field,check);
	return check;
}

function emptyForm(){
	
}

//function to set the visual aspect of the form field
function setFormField($formfield,blnCheck){
	if(blnCheck){
		$formfield.removeClass("error");
		return true;
	}
	else{
		$formfield.addClass("error");
		return false;
	}
}
