function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) {v=args[i+2];
    if (obj.style) {obj=obj.style;v=(v=='show')?'visible':(v=='hide')?'hidden':v;}
    obj.visibility=v;}
}

/** stuff for the carousel **/

var images;
var rotate = true;
var currImg;

function rotateImage(index){
			if(!index || index >= images.length ) index = 0;
  		img = $(images[index]);
			currImg = img;
  		img.fadeIn(700);
  		// this one just pauses
  		img.animate({opacity: 1}, 4000)
  		if(rotate){
  			img.fadeOut(700,	function(){rotateImage(index + 1)});
  		}
}

function setUpLinks(){
	images = $('.carousel img');
	images.each(function(){
		$('.carousel .buttons').prepend('<a href="#"></a>');
	});
	$('.carousel .buttons a').each(function(index){
		$(this).click(function(){selectImage(index)});
	});
}

function selectImage(index){
	rotate = false;
	if(currImg) currImg.hide();
	currImg = $(images[index]);
	currImg.show();
	$('.carousel .buttons a').each(function(span_ind){
		if(span_ind == index){
			$(this).addClass('selected');
		}else{
			$(this).removeClass('selected');
		}
	});
}

$(document).ready(function(){

	// set up the nav elements
    $('.top_nav_elem').hover(function(){
        $(this).find('.subnav').slideDown('fast');
    }, function(){
		$(this).find('.subnav').slideUp('fast');
    });


	// on the home page, set up the carousel
	if($('.carousel').length > 0){
			setUpLinks();
			rotateImage();
	}
});

function home_tweets(user){
	$.getJSON("http://twitter.com/status/user_timeline/"+user+".json?callback=?",
	  function(data){
		$.each(data, function(i, item) {
			//text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ullamcorper tempor ornare. Etiam congue, tellus a pharetra faucibus, quam to";
			text = item.text;
			if(i < 2){
				tweet = "<div class='tweet'>" +
					text + "<span class='controls'><a href='http://twitter.com/#!/"+user+"/status/"
					+ item.id_str + "'>" + prettyDate(item.created_at) + "</a><a href='http://twitter.com/intent/tweet?in_reply_to="
					+ item.id_str + "'>reply</a><a href='http://twitter.com/intent/retweet?tweet_id="
					+ item.id_str + "'>retweet</a><a href='http://twitter.com/intent/favorite?tweet_id="
					+ item.id_str + "'>favourite</a></span></div>";
				$("#twitter_feed").append(tweet);
			}
		});
	  });
}
/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.

function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ").replace(/\+\d\d\d\d/,"")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);
	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;

	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
	jQuery.fn.prettyDate = function(){
		return this.each(function(){
			var date = prettyDate(this.title);
			if ( date )
				jQuery(this).text( date );
		});
	};
