/* START Twitter */
(function($) {
	/*
		jquery.twitter.js v1.5
		Last updated: 08 July 2009

		Created by Damien du Toit
		http://coda.co.za/blog/2008/10/26/jquery-plugin-for-twitter

		Licensed under a Creative Commons Attribution-Non-Commercial 3.0 Unported License
		http://creativecommons.org/licenses/by-nc/3.0/
	*/

	$.fn.getTwitter = function(options) {

		$.fn.getTwitter.defaults = {
			userName: null,
			numTweets: 5,
			loaderText: "Loading tweets...",
			slideIn: true,
			slideDuration: 750,
			showHeading: true,
			headingText: "Latest Tweets",
			showProfileLink: true,
			showTimestamp: true
		};

		var o = $.extend({}, $.fn.getTwitter.defaults, options);

		return this.each(function() {
			var c = $(this);

			// hide container element, remove alternative content, and add class
			c.hide().empty().addClass("twitted");

			// add heading to container element
			if (o.showHeading) {
				c.append("<h2>"+o.headingText+"</h2>");
			}

			// add twitter list to container element
			var twitterListHTML = "<ul id=\"twitter_update_list\"><li></li></ul>";
			c.append(twitterListHTML);

			var tl = $("#twitter_update_list");

			// hide twitter list
			tl.hide();

			// add preLoader to container element
			var preLoaderHTML = $("<p class=\"preLoader\">"+o.loaderText+"</p>");
			c.append(preLoaderHTML);

			// add Twitter profile link to container element
			if (o.showProfileLink) {
				var profileLinkHTML = "<p class=\"profileLink\"><a href=\"http://twitter.com/"+o.userName+"\">http://twitter.com/"+o.userName+"</a></p>";
				c.append(profileLinkHTML);
			}

			// show container element
			c.show();

			// load blogger.js
			// (today's date is added to the url to cache it for today only)
			var today = new Date();
			var datestamp = (today.getYear() + 1900) + "-" + (today.getMonth() + 1) + "-" + today.getDate();
			$.ajax({
				type: "GET",
				url: "http://twitter.com/javascripts/blogger.js?_=" + datestamp,
				dataType: "script",
				cache: true,
				success: function() {
					// populate #twitter_update_list
					//$.getScript("http://twitter.com/statuses/user_timeline/"+o.userName+".json?callback=twitterCallback2&count="+o.numTweets, function() {
					$.getScript("http://api.twitter.com/1/statuses/user_timeline/"+o.userName+".json?callback=twitterCallback2&include_rts=true&count="+o.numTweets, function() {
						// remove preLoader from container element
						$(preLoaderHTML).remove();

						// remove timestamp and move to title of list item
						if (!o.showTimestamp) {
							tl.find("li").each(function() {
								var timestampHTML = $(this).children("a");
								var timestamp = timestampHTML.html();
								timestampHTML.remove();
								$(this).attr("title", timestamp);
							});
						}

						// show twitter list
						if (o.slideIn) {
							// a fix for the jQuery slide effect
							// Hat-tip: http://blog.pengoworks.com/index.cfm/2009/4/21/Fixing-jQuerys-slideDown-effect-ie-Jumpy-Animation
							var tlHeight = tl.data("originalHeight");

							// get the original height
							if (!tlHeight) {
								tlHeight = tl.show().height();
								tl.data("originalHeight", tlHeight);
								tl.hide().css({height: 0});
							}

							tl.show().animate({height: tlHeight}, o.slideDuration);
						}
						else {
							tl.show();
						}

						// add unique class to first list item
						tl.find("li:first").addClass("firstTweet");

						// add unique class to last list item
						tl.find("li:last").addClass("lastTweet");
						
						// open links in a new window
						tl.find('a').attr('target','_blank');
					});
				}
			});
		});
	};
})(jQuery);
/* END Twitter */



/* START document.ready */
$(document).ready(function() {

	// open external links in new window
	$("a").filter(function() {
	return this.hostname && this.hostname !== location.hostname;
	}).attr('target', '_blank');

	
	// twitter feed
	$("#twitter").getTwitter({
		userName:			"BHFamiliesUK",
		numTweets:			2,
		loaderText:			"Loading tweets...",
		slideIn:			false,
		slideDuration:		0,
		showHeading:		false,
		showProfileLink:	false,
		showTimestamp:		true
	});


	// homepage rotator
	var homepageBanner = $('#banner div#slides');
	if(homepageBanner.length > 0){
		$(homepageBanner).cycle({ 
			fx:				'fade', 
			speed:			200,
			timeout:		8000,
			pager: 			'#slide-nav',
			next: 			'#slide-next', 
			prev: 			'#slide-prev',
			cleartypeNoBg:	true
		});
	}

	
	// IE6 and IE7 don't support CSS :after so we need to do this in jQuery for these 2 browsers only
	if ($.browser.msie && $.browser.version <= 8) {
		$('#banner #slide-menu').after('<div id="banner-after"><img src="/template-assets/toys_boy.png" alt=""/></div>');
	}
	
	
	// set default values on text boxes (only if they are empty)
	$('input[type="text"]').each(function() {
		var textbox = $(this);
		var placeholderText = textbox.attr('placeholder');
		if(placeholderText !== undefined && placeholderText.length > 0 && textbox.val().length == 0) {
			textbox.val(placeholderText);
		}
	});


	// clear default values on focus
	$('input[type="text"]').focus(function() {
		var textbox = $(this);
		var placeholderText = textbox.attr('placeholder');
		if(placeholderText !== undefined && placeholderText.length > 0 && textbox.val() == placeholderText) {
			textbox.val('');
		}
	});


	// set default values on blur (only if the box is empty)
	$('input[type="text"]').blur(function() {
		var textbox = $(this);
		var placeholderText = textbox.attr('placeholder');
		if(placeholderText !== undefined && placeholderText.length > 0 && textbox.val().length == 0) {
			textbox.val(placeholderText);
		}
	});


	// add to favourites link
	if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) { 
		// chrome does not permit addToFavorites() function by design 
		$('.add-to-favourites').attr({ 
			title: 'This function is not available in Google Chrome. Click the star symbol at the end of the address-bar or type Ctrl-D to create a bookmark.', 
			href: 'javascript:return false' 
		})
		.css({opacity: .5}); // dim the button/link 
	} else {
		$('.add-to-favourites').click(
			function() {
				var url = window.location.href;
				var title = document.title;

				if(window.sidebar) { // Mozilla Firefox
					window.sidebar.addPanel(title, url, "");
				} else if(window.external) { // IE
					window.external.AddFavorite(url, title);
				} else if(window.opera && window.print) {
					window.external.AddFavorite(url, title);
				} else {
					alert("Sorry, your browser does not appear to support this function.");
				}

				preventDefault();
			}
		);
	}
	
	// click handler for postcode fields..
	/*
	alert('postcodes');
	$('#your_postcode').each(
		function(index, element){
			alert('start');
			element = $(element);
			element.click(
				function(){
					alert('here');
				}
			);
		}
	);
	*/
});
/* END document.ready */
