$(document).ready(function() {
	var twitterAccount = "ukcontacts";

	$("#new-incorps").attr("checked", false);
	$("#terms").attr("checked", false);

	$(".content .section").hide();

	$("form.validate").validate();

	$("div.testimonials").scrollable({size:1, circular:true}).autoscroll(5000);

	$("#looking-for").click(function() { $(this).select(); }).autocomplete("/beta/lookups/classifications.php", {width: '581px'});
	$("#location").click(function() { $(this).select(); }).autocomplete("/beta/lookups/locations.php", {width: '581px'});

	$("#terms").click(function() {
		if ($(this).attr("checked")) {
			$(".register").slideDown();
		}
		else {
			$(".register").slideUp();
			$(".incorps-area").slideUp();
			$("#new-incorps").attr("checked", false);
		}
	});

	$("#new-incorps").click(function() {
		if ($(this).attr("checked")) {
			$(".incorps-area").slideDown();
		}
		else {
			$(".incorps-area").slideUp();
		}
	});

	$("span.caption").each(function() {
		var self = $(this);
		var input = $(this).prev("input");
		var container = input.parent();
		
		$(container).addClass('captioned');

		if ($.browser.msie) {
			self.css({background: '#fff'});
		}

		self.click(function() {
			self.fadeTo('slow', 0.2);
			$(input).focus();
		});
		
		$(input).click(function() {
			self.fadeTo('slow', 0.2);
		});

		$(input).keyup(function() {
			if ($(input).val() != '') {
				self.fadeTo('fast', 0);
			}
		});

		$(input).focus(function() {
			if ($(input).val() != '') {
				self.fadeTo('fast', 0);
			}
		});

		$(input).focusout(function() {
			if ($(this).val() == '') {
				self.fadeTo('slow', 1);
			}
			else {
				self.fadeTo('slow', 0);
			}
		});
	});

	/* Blogger feed */
	/*
	google.load("gdata", "1.x");
	google.setOnLoadCallback(function() {
		var service = new google.gdata.blogger.BloggerService();
	
		service.getBlogFeed("http://ukcontacts.blogspot.com/feeds/posts/default", function(feed) {
			var entries = feed.getEntries();

			alert(entries.length);
		}, function() { alert('error'); });
	});*/

	/* Add twitter feed */
	$(".sidebar").each(function() {
		var self = $(this);

		$.getJSON("http://search.twitter.com/search.json?q=from:" + twitterAccount + "&callback=?", function(data) {

			if (data.results.length > 0) {
				self.append("<h2 class='twitter'>Latest Tweets</h2><div class='tweets'><ul></ul></div>");

				var listOfTweets = $(".tweets ul");
				$.each(data.results, function(i, item) {
					var re = new RegExp("(http://bit\.ly/.[a-zA-Z0-9]*)");
					var tweet = item.text;
					var tweetWithLink = tweet;

					if (re.test(tweet)) {
						var bitlyURL = re.exec(tweet)[0];
						var url = '<a href="' + bitlyURL + '">' + bitlyURL + '</a>';
						tweetWithLink = tweet.replace(re, url);
					}

					listOfTweets.append("<li title='" + tweet + "'>" + tweetWithLink + "</li>");
				});
			}
		});
	});
});


