function div_visibility(strWhichTab)
{
	
	$('#newsbox_feeds > div').each(function(){$(this).hide();});
	$('ul#togglemenu > li').each(function(){
			if($(this).hasClass('active_menu_item'))
			{
				$(this).removeClass('active_menu_item');
				$(this).animate({'background-position': '0 0'}, 0);
				$(this).mouseleave();
			}
	});
	
	$('li#'+strWhichTab).addClass('active_menu_item');
	

	
	switch(strWhichTab.replace('menu_',''))
	{
		case "redblog":
			$('div#redblog').show();
			$('div#news_title span').html("News<a class='previous_news_link' href='http://www.rrc.mb.ca/news'>Previous Items</a>");
			break;
		case "blogroll": 
			$('div#blogroll').show();
			$('div#news_title span').text("Blog Roll");
			break;
		case "twitter":
			$('div#twitter').show();
			$('div#news_title span').html("Twitter<a class='twitter_profile_link' href='http://twitter.com/RRC'>Follow Us</a>");
			break;
		case "facebook":
			$('div#facebook').show();
			$('div#news_title span').text("Facebook");
			break;
		case "youtube":
			$('div#youtube').show();
			$('div#news_title span').text("YouTube Videos");
			break;
		case "flickr":
			$('div#flickr').show();
			$('div#news_title span').text("Flickr Photos");
			break;
	}
}
$(function(){
	slideShow();
	
  $.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?callback=?', {count: 4, screen_name: 'RRC', include_rts: 'true'}, function(data){
    
    var source = $('ul#twitter-posts').html();
    $('ul#twitter-posts').empty();

    // loop through and append to twitter block
    $.each(data, function(index,post) {
      post.text = post.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>';
      });
      $('ul#twitter-posts').append("<li><span>" + post.text + "</span><span class='time_wrapper'><a class='twitter_time_link' href='http://twitter.com/" + post.user.screen_name + "/statuses/" + post.id_str +"'> "+relative_time(post.created_at)+"</a></span></li>");
    });
  });
  
  $('ul#togglemenu > li').each(function(){
		$(this).click(function()
		{
			if(!$(this).hasClass('active_menu_item'))
			{
				$(this).animate({'background-position': '0 -30'}, 0);
				div_visibility($(this).attr('id'));
			}
		});
		
		$(this).mouseover(function()
		{
			if(!$(this).hasClass('active_menu_item'))
			{
				$(this).animate({'background-position': '0 0'}, 250);
			}
		});
		
		$(this).mouseleave(function()
		{
			if(!$(this).hasClass('active_menu_item'))
			{
				$(this).animate({'background-position': '0 7'}, 250);
			}
		});
		
	});
	
	$("#menu_redblog").animate({'background-position': '0 -30'}, 0);
	div_visibility("menu_redblog");
	
	$('ul#togglemenu > li').each(function(){
		if(!$(this).hasClass('active_menu_item'))
		{
			$(this).mouseleave();
		}
	});
	
	$("#youtube").flash({
		swf:"http://www.youtube.com/p/1FA81333177E5809?hl=en_US&fs=1",
		width:430,
		height:230
	});
	$("#flickr").flash({
		swf:"http://www.flickr.com/apps/slideshow/show.swf?v=71649&amp;offsite=true&amp;lang=en-us&amp;page_show_url=%2Fphotos%2Fredrivercollege%2Fshow%2F&amp;page_show_back_url=%2Fphotos%2Fredrivercollege%2F&amp;user_id=40170705@N05&amp;jump_to=",
		width:430,
		height:230
	}); 
	

	
});


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';
  }
}

function slideShow() {
 
	//Set the opacity of all images to 0
	$('#gallery a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#gallery a:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	$('#gallery .caption').css({opacity: 0.7});
 
	//Resize the width of the caption according to the image width
	$('#gallery .caption').css({width: $('#gallery a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
	$('#gallery .content').html($('#gallery a:first').find('img').attr('rel'))
	.animate({opacity: 1}, 400);
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',6000);
	
}
 
function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));
 
	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
 
	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:100 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '70px'},500 );
	
	//Display the content
	$('#gallery .content').html(caption);
}
