var progressPercentage = 65;			// The final percentage the bar should animate to

var progressAnimationSpeed = 2000;   	// The time to complete the bar animation in milliseconds, 1000 = 1 second

var twitterUsername = 'sabrewebdesign'; // Your twitter username

var tweetCount = 2;						// The number of tweets to show, set to 0 to prevent twitter loading



if (typeof(Cufon) != 'undefined') {

	// Cufon (font replacement) settings. See http://cufon.shoqolate.com/ for more information.

	Cufon.replace('h1, h2, .follow-button a');

	Cufon.replace('.dates-wrap div', { textShadow: '#EDEDED 1px 1px 0px' });

}



$(document).ready(function() {

	if ($.fn.tabs) {

		// Content area tabs settings.  See http://docs.jquery.com/UI/Tabs for more information.

		$('#tabs').tabs({ fx: { opacity: 'toggle', duration: 'slow' }});

	}

	

	if ($.fn.qtip) {

		// Content area tab tooltips.  See http://craigsworks.com/projects/qtip/docs/ for more information.

		$('.about-tab, .contact-tab, .add-tab').qtip({

			content: {

				text: false

			},

			style: {

				border: {

			      width: 1,

			      radius: 4,

			      color: '#3E3E3E'			      

			    },

				tip: {

			    	corner: 'bottomMiddle',

			    	size: {

			    		x: 10,

			    		y: 5

			    	}			    

			    },

			    background: '#3E3E3E',

			    color: '#FFFFFF',

			    paddingLeft: 8,

			    paddingRight: 8,

			    paddingTop: 1,

			    paddingBottom: 1,

			    fontSize: '11px',

				classes: { tooltip: 'tab-tooltips' }

			},

			position: {

				corner: {

					target: 'topMiddle',

					tooltip: 'bottomMiddle'

				},

				adjust: {

					y: -2

				}

			}

		});

	}

	

	// Switcher JS

	$('a.options-trigger').click(function() {

		$('.options-content').toggle();

		return false;

	});

}); // End document.ready()



$(window).load(function() {

	animateProgressBar();

	getTweets();

}); // End window.load()



// Animates the progress bar to the specified percentage at the specified speed.

// Calls showTweets() once the animation is completed

function animateProgressBar()

{

	// Normalise the percentage to 1-100

	if (progressPercentage < 1) {

		progressPercentage = 1;

	} else if (progressPercentage > 1) {

		progressPercentage = 80;

	}

    var progressBarWrap = $('#progress-bar-wrap');

    var progressAmount = $('#progress-amount');    

    var targetWidth = $('#progress-wrap').width() * (progressPercentage / 100);

    progressBarWrap.animate({

        width: targetWidth

    }, progressAnimationSpeed, function() {

		$('#moving-arrow').animate({height: 35}, 'slow', 'easeOutBack', function() {

			progressAmount.text(progressPercentage + '%').fadeIn('slow', showTweets);

		});		

    }).css('overflow', 'visible');

	$('#progress-indicator').fadeIn('slow');

}



var fetchedTweets = null;

var fetchWaitCount = 0;



// Get the latest Tweets

function getTweets()

{	

	if (twitterUsername && tweetCount > 0) {

		(function() {

			var t = document.createElement('script'); t.type = 'text/javascript'; t.src = 'http://twitter.com/statuses/user_timeline/' + twitterUsername + '.json?callback=setTweets&count=' + tweetCount;

			var h = document.getElementsByTagName('head')[0]; h.appendChild(t);

		})();

	}

}



// Set the Tweets (callback from getTweets())

function setTweets(twitters)

{

	fetchedTweets = twitters;

}



// Show the Tweets

function showTweets()

{	

	if (!(twitterUsername && tweetCount > 0)) {

		return;

	}

	

	var tweets = [];

	

	if (fetchedTweets == null) {

		if (fetchWaitCount < 50) {

			// The tweets haven't arrived yet try again in 200ms

			setTimeout(showTweets, 200);

			fetchWaitCount++;

			return;

		} else {

			// We've waited more than 10 seconds for the Tweets so bail out to stop an infinite loop

			tweets.push('<li><span>I spent too long looking for tweets so I\'m giving up</span></li>');

		}

	} else if (fetchedTweets.length > 0) {

		for (var i = 0; i < fetchedTweets.length; i++) {

			var username = fetchedTweets[i].user.screen_name;

			var status = fetchedTweets[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {

				return '<a href="'+url+'">'+url+'</a>';

			}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {

				return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';

			});

			tweets.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+fetchedTweets[i].id+'">'+relative_time(fetchedTweets[i].created_at)+'</a></li>');

		}

	} else {

		// The returned tweets were empty

		tweets.push('<li><span>I couldn\'t find any tweets</span></li>');

	}

	

	

	$('#twitter_div').css('height', $('#twitter_div').height());

	$('#twitter_update_list').html(tweets.join(''));

	$('#twitter_div').wrapInner('<div/>');

	$('#twitter_div').animate({height: $('div:first', $('#twitter_div')).height()}, 400);

}



// Get the age of the Tweet relative to now

function relative_time(time_value)

{

	var values = time_value.split(" ");

	time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];

	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 'less than a minute ago';

	} else if(delta < 120) {

		return 'about a minute ago';

	} else if(delta < (60*60)) {

		return (parseInt(delta / 60)).toString() + ' minutes ago';

	} else if(delta < (120*60)) {

		return 'about an hour ago';

	} else if(delta < (24*60*60)) {

		return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';

	} else if(delta < (48*60*60)) {

		return '1 day ago';

	} else {

		return (parseInt(delta / 86400)).toString() + ' days ago';

	}

}



// Image preloader

(function($) {

	var cache = [];

	$.preloadImages = function() {

    for (var i = 0; i < arguments.length; i++) {

      var cacheImage = document.createElement('img');

      cacheImage.src = arguments[i];

      cache.push(cacheImage);

    }

  }

})(jQuery);



$.preloadImages(

	'images/followme-hover.png',

	'contact-form/images/close.png',

	'contact-form/images/success.png',

	'images/tab-info-on.png',

	'images/tab-contact-on.png',

	'images/tab-add-on.png',

	'images/transp.png',

	'images/buynow1.png',

	'images/buynow2.png'

);
