$(document).ready(function () {
    
    //Home Page Carousel 
	var num_slides = $("#main-carousel ul").children().length;
	var cur_slide = 1; 
    
    //add the slide nav
    for(var i=1; i<=num_slides; i++) {
    	$("nav.carousel-nav ul").append("<li id='menu" + i + "'>" + i + "</li>");
    }
    
	var nav_width = num_slides * 20;
	$("#main-carousel nav.carousel-nav").width(nav_width);
    $("nav.carousel-nav ul li:first-child").addClass('active');
    $("#main-carousel ul.slideshow li:first-child").removeClass('slide').addClass('current-slide');
    
    //auto scroll through slides
    var t = setInterval(autoScroll, 8000);
    
    //make slide nav control which slide is shown
    $("nav.carousel-nav ul li").click(function(){
    	//check to see if this item is already active
    	var is_active = $(this).hasClass('active');

    	//get the id number and associate it with a slide
    	var slide_id = $(this).attr('id').replace('menu', '');
    	
    	if(!is_active){
			$("#slide" + slide_id).fadeIn('normal', function(){
				$("nav.carousel-nav ul li.active").removeClass('active');
				$("#menu" + slide_id).addClass('active');
				$("#slide" + cur_slide).fadeOut();
				cur_slide = slide_id;
				clearInterval(t);
				t = setInterval(autoScroll, 8000);
			}); 
		}
    });
    
	//this is the function to call in timeout to scroll through slides automatically every 8000ms
	function autoScroll(){
		
		var this_slide = cur_slide;
		//check to see if this is the last slide, if it is, start over...
		if(cur_slide == num_slides) {
			this_slide = 1;
			$("#slide" + this_slide).fadeIn('normal', function(){
				$("nav.carousel-nav ul li.active").removeClass('active');
				$("#menu" + this_slide).addClass('active');
				$("#slide" + cur_slide).fadeOut();
				cur_slide = this_slide;
			});
		} 
		//otherwise, go on to the next slide
		else {
			this_slide++;
			$("#slide" + this_slide).fadeIn('normal', function(){
				$("nav.carousel-nav ul li.active").removeClass('active');
				$("#menu" + this_slide).addClass('active');
				$("#slide" + cur_slide).fadeOut();
				cur_slide = this_slide;
			});
		} 
				
	}
	//end of home carousel
	
	//intitiate twitter stream! engage!
	tweets('potatoh');
	//scroll through twitter statuses
	var tweet_count = 0;
	var current_tweet = 0; 
	$("div.twitter-widget div.arrows div.right-arrow").click(function(){
		//if there are more tweets to the right, move it right
		tweet_count = $("ul.the-tweets li").length;

		if( current_tweet != (tweet_count - 1) ) {
			$("div.twitter-widget div.arrows div.left-arrow").css({backgroundPosition: '0 -20px'});
			$("ul.the-tweets li#tweet_id" + current_tweet).fadeOut('normal', function(){
				current_tweet++;
				$("ul.the-tweets li#tweet_id" + current_tweet).fadeIn();	
			});
		} else {
			$(this).css({backgroundPosition: '-16px -20px'});
		}
	});
	
	$("div.twitter-widget div.arrows div.left-arrow").click(function(){
		//if there are more tweets to the left, move it left
		tweet_count = $("ul.the-tweets li").length;

		if( current_tweet != 0 ) {
			$("div.twitter-widget div.arrows div.right-arrow").css({backgroundPosition: '-16px 0'});
			$("ul.the-tweets li#tweet_id" + current_tweet).fadeOut('normal', function(){
				current_tweet--;
				$("ul.the-tweets li#tweet_id" + current_tweet).fadeIn();	
			});
		} else {
			$(this).css({backgroundPosition: '0 0'});
		}
	});
});//end document ready


//Utitlity functions

//turns urls into clickable links in Twitter posts
String.prototype.linkify = function() {
  return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:% \?\/.=]+[^\.,\)\s*$]/g, function(m) {
    return m.link(m);
  });
};

//turns @mentions in Twitter into clickable links
String.prototype.atify = function() {
    return this.replace(/@[\w]+/g, function(m) {
        return "<a href='http://www.twitter.com/"+m.replace('@','')+"'><strong>"+m+"</strong></a>";
    });
};

//format the date
function formatDate(date) {

  var m_names = new Array("January", "February", "March", 
  "April", "May", "June", "July", "August", "September", 
  "October", "November", "December");
  var v=date.split(' ');
  var d = new Date(Date.parse(v[1]+" "+v[2]+", "+v[5]+" "+v[3]+" UTC"));
  
  var curr_date = d.getDate();
  var curr_month = d.getMonth();
  var curr_year = d.getFullYear();
  return  m_names[curr_month] + " " + curr_date + ", " + curr_year;
}

function tweets(handle) {
    
    var url = "/twitter.php?screenname=" + handle;
    $.getJSON(url, function(data){
        $.each(data, function(i, item) {
            $('ul.the-tweets').append('<li id="tweet_id' + i + '">' + item.text.linkify().atify() + 
            						  '<br /><span class="tweeted-at">' + formatDate(item.created_at) + '</span></li>');
            						  

        });
    });
}

