/*---------------------------------------------------------------------------*/
	
	$(document).ready(function() {
		var href = $('base').attr('href');
		
		$('.external').live("click", function() {
			window.open($(this).attr('href'));			
			return false;
		})		
		
		// To smooth scroll the #anchors
		var h = window.location.hash;
		if(h == "#last") {
			var e = $("#last-comment");
			doScroll(e);
			e.css({borderLeft: "5px solid #FCC80F", paddingLeft: "5px"});					
		} else if (h == "#first") {
			var e = $("#first-comment");
			doScroll(e);
		} else if (h == "#comment") {
			var e = $(".comment");
			doScroll(e);			
		}
		
		function doScroll(el) {
			var offset = e.offset().top;
			$('html, body').animate({scrollTop: offset}, 1000);
			return false;			
		}
		
		// Show newsletter form:
		$('#button-newsletter').click(function() {
			var box = null;
			var attach_form = function() {
				$('#newsletter .close a, .boxy-modal-blackout').click(function() {
					box.hideAndUnload(); return false;
				});
				
				$('#newsletter').ajaxForm({
					target:		'#newsletter-container',
					url:		href + '/newsletter/',
					success:	attach_form
				});
			}
			
			// Load form:
			$.get(href + '/newsletter/', {}, function(data) {
				// Load box:
				box = new Boxy(
					'<div id="newsletter-container" class="formatted">' + data + '</div>',
					{
						title:		"Subscribe",
						modal:		true,
						show:		true,
						afterShow:	attach_form
					}
				);
			});
			
			return false;
		});//.click();
		
		/* Check if there is a video */
		if($("#video").length) {
			/* Hide Image Fader */
			$("#image-fader, #logo-fader").hide();
			
		} else {
			/* No Video */
			$("#flashVideo").hide();
			if($("#image-fader, #logo-fader").length == 1) {
				$("#image-fader").imageFader(3000, 0.2);
				$("#logo-fader").imageFader(3000, 0.2);
			}
		}		

		$("#live-chat-button").popupWindow({
				height: 390,
				width: 610,
				centerBrowser: 1
			});
		
		/* Twitter */
		$.getJSON("http://twitter.com/statuses/user_timeline/22470271.json?count=1&callback=?",function(tweet) {
			
			var status = "<strong><a href='http://twitter.com/randbcc/' class='external' title='Follow Us on Twitter'>Twitter...</a></strong>";
				status += "<a class='external' href='http://twitter.com/randbcc/statuses/" + tweet[0].id + "/'> ";
				status += "<span class='tweet'>" + tweet[0].text + "</span></a>";
				status += "<span class='ago'>" + relative_time(tweet[0].created_at) + " from " + tweet[0].source + "</span>";		
			
			$('#twitterFeed').html(status);
		});
		//
	});
	
	/* From Twitter (blogger.js) */
	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';
	  }
	}

	
/*---------------------------------------------------------------------------*/