
// Script written by Sky Apperley (skyapperley.co.uk) March 2010

$(document).ready(function() {
 // =========== Roll my own FUNCTIONS  =========== 
	function processText(text) {
		var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
		text = text.replace(exp,'<a href="$1">$1</a>');
		exp = /\B#(\w+)/ig;
		text = text.replace(exp,'<span class="hashtag">#$1</span>');
		exp = /\B@([_a-z0-9]+)/ig;
		text = text.replace(exp, function(reply) {
			return '<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(0)+'</a>'
			});
		return text;
	}
	var changeTweet = function(tweet) {
		$('.twitpanel').animate({top: 70}, 750, function() {
		  $(this).empty().html(tweet);
		}).delay(100).animate({top: 0}, 750, function() {
			  $(this).delay(100);
			})
	};

	var changeTime = function(currenttime) { 
		$('.timepanel').animate({top: -30}, 750, function() {
		  $(this).empty().text(currenttime);
		}).delay(100).animate({top: 0}, 750, function() {
			  $(this).delay(100);
			})
	};

	function processDate(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
		var day=values[0];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);

	  if (delta < 60) {
	    return day+' - '+'less than a minute ago';
	  } else if(delta < 120) {
	    return day+' - '+'about a minute ago';
	  } else if(delta < (60*60)) {
	    return day+' - '+(parseInt(delta / 60)).toString() + ' minutes ago';
	  } else if(delta < (120*60)) {
	    return day+' - '+'about an hour ago';
	  } else if(delta < (24*60*60)) {
	    return day+' - '+'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
	  } else if(delta < (48*60*60)) {
	    return day+' - '+'1 day ago';
	  } else {
	    return day+' - '+(parseInt(delta / 86400)).toString() + ' days ago';
	  }
	}
 // =========== VOODOO  =========== 
	var navAnim=false;
	
	$('div.navbar1 a').click(function() {
		if (!navAnim) {
			navAnim=true;
			$("div.longbox").animate({ left: 0}, 750, "expoinout", function() {
				navAnim=false;
			})
		};
		return false;
	});
	
	$('div.navbar2 a').click(function() {
		if (!navAnim) {
			navAnim=true;
			$("div.longbox").animate({ left: 560}, 750, "expoinout", function() {
				navAnim=false;
			})
		};
		return false;
	});

	var tweetMotion = function(tweet,tweettime) {
		changeTweet(tweet);
		changeTime(tweettime);
	};
	
	var tweetstore=[];
	var tweettimestore=[];
	var tweet="", tm=1;
	skymookURL = 'http://twitter.com/statuses/user_timeline/skymook.json?count=6';
	//skymookURL = 'includes/js/skymook.json'; // < local for testing only
	$('.twitpanel').html('<img src="images/loader.gif" alt="Spining loading circle" /> Loading ...');
	$('.timepanel').html('Loading ...');
	$.ajax({
		async: false,
		url: skymookURL,
		dataType: 'jsonp', // jsonp for remote server
		success: function(json) {
				var ntweets=json.length;
				for (var i = ntweets - 1; i >= 0; i--){
					tweetstore[i]=processText(json[i].text);
					tweettimestore[i]=processDate(json[i].created_at);
				};
				tweetMotion(tweetstore[0],tweettimestore[0]); // fire this once immediately
				setInterval(function () { tweetMotion(tweetstore[tm],tweettimestore[tm]);tm++;if(tm==5){tm=0}; }, 6750);
	    }
	});
	
	$('input').css('color', '#7C7C7C');
	$('textarea').css('color', '#7C7C7C');
	$('#send').css('color', '#000')
	$('input').focus(function() {
		if ($(this).val() == $(this).attr("title")) { $(this).removeAttr("value").css('color', '#000').removeClass('red-input');}
	});
	$('input').focus(function() {
		if ($(this).val() == $(this).attr("title")) { $(this).removeAttr("value").css('color', '#000').removeClass('red-input');}
	});
	$('textarea').focus(function() {
		if ($(this).val() == $(this).attr("title")) { $(this).removeAttr("value").css('color', '#000').removeClass('red-input');}
	});
	$('input').blur(function() {
		$(this).removeClass('red-input');
		$('.form_output').fadeOut('slow');
	});
	$('textarea').blur(function() {
		$(this).removeClass('red-input');
		$('.form_output').fadeOut('slow');
	});
	
	function displayError() {
		$('.form_output').hide().addClass('error').html("Oops, please fill out the fields marked by red.").fadeIn('slow');
	}
	
	$('.infoicon').click(function() {
		$("#dialog").html('<p>The code for this custom twitter loader widget was written by Sky using Javascript and Query without the use of any jQuery plug-ins.</p><p>With Ajax, it gets a JSON object from the twitter servers, parses the required data and displays it using custom formating on any links or hash tags found in the tweet.</p>')
		$("#dialog").dialog({ draggable: true, title: 'About this twitter loader', buttons: { "Ok": function() { $(this).dialog("close"); } }, resizable: false, modal: true });
	});
	
	$('#send').click(function() { 
		// Validate before sending
		var validated = true;
		if ($('#name').val() == "" || $('#name').val() == "name *" ) {
			$('#name').addClass('red-input');
			validated = false;
			displayError()
		};
		if ($('#email').val() == "" || $('#email').val() == "email *" ) {
			$('#email').addClass('red-input');
			validated = false;
			displayError()
		};
		if ($('#message').val() == "" || $('#message').val() == "message" ) {
			$('#message').addClass('red-input');
			validated = false;
			displayError()
		};
		// code for captcha for later!
/*		if ($('#security_code').val() == "" || $('#security_code').val() == "Enter Security Code*" ) {
			$('#security_code').addClass('red-input');
			validated = false;
			displayError()
		};*/
		if (validated) {
			$.post('includes/php/sky_post.php', {'send': 'OK', 'name': $('#name').val(), 'email': $('#email').val(), 'message': $('#message').val(), 'security_code': $('#security_code').val() }, function(data) { 
				//$('.form_output').hide().html(data).fadeIn('slow'); // for testing only
				if (data) {
					$('.form_output').hide().removeClass('error').html("Thank you for your message. I will contact you shortly.").fadeIn('slow').delay(6000).fadeOut('slow', function() {
						$('#name').val('')
					});
					//$('#security_code').val("");
				} else {
					$('.form_output').hide().html("Oops, there seems to be a server error with sending your email.").fadeIn('slow')
				};
			});
		};
		return false;
	});
	$('.form_output').hide();

}); // end document.ready

////////////////
// Easing.1.3 //
////////////////

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});

//////////////////////////
// Easing Compatibility //
//////////////////////////

jQuery.extend( jQuery.easing,
{
	easeIn: function (x, t, b, c, d) {
		return jQuery.easing.easeInQuad(x, t, b, c, d);
	},
	easeOut: function (x, t, b, c, d) {
		return jQuery.easing.easeOutQuad(x, t, b, c, d);
	},
	easeInOut: function (x, t, b, c, d) {
		return jQuery.easing.easeInOutQuad(x, t, b, c, d);
	},
	expoin: function(x, t, b, c, d) {
		return jQuery.easing.easeInExpo(x, t, b, c, d);
	},
	expoout: function(x, t, b, c, d) {
		return jQuery.easing.easeOutExpo(x, t, b, c, d);
	},
	expoinout: function(x, t, b, c, d) {
		return jQuery.easing.easeInOutExpo(x, t, b, c, d);
	},
	bouncein: function(x, t, b, c, d) {
		return jQuery.easing.easeInBounce(x, t, b, c, d);
	},
	bounceout: function(x, t, b, c, d) {
		return jQuery.easing.easeOutBounce(x, t, b, c, d);
	},
	bounceinout: function(x, t, b, c, d) {
		return jQuery.easing.easeInOutBounce(x, t, b, c, d);
	},
	elasin: function(x, t, b, c, d) {
		return jQuery.easing.easeInElastic(x, t, b, c, d);
	},
	elasout: function(x, t, b, c, d) {
		return jQuery.easing.easeOutElastic(x, t, b, c, d);
	},
	elasinout: function(x, t, b, c, d) {
		return jQuery.easing.easeInOutElastic(x, t, b, c, d);
	},
	backin: function(x, t, b, c, d) {
		return jQuery.easing.easeInBack(x, t, b, c, d);
	},
	backout: function(x, t, b, c, d) {
		return jQuery.easing.easeOutBack(x, t, b, c, d);
	},
	backinout: function(x, t, b, c, d) {
		return jQuery.easing.easeInOutBack(x, t, b, c, d);
	}
});

