// JavaScript Document
$(document).ready(function(){	

  /***********************************************************/
  //social function
  $('li a>img').click(function(){
		var network=$(this).attr('title');
		var w = '575';
		var h = '475';
		switch(network){
			case 'facebook':
			window.open('http://www.facebook.com/home.php?#!/pages/Adams-Fifteen/170210816329686','height='+h+',width='+w+',top='+((screen.height-h)/2)+',left='+((screen.width-w)/2)+',scrollbars=no,resizable=yes');
			break;
			case 'twitter':
			window.open('http://www.twitter.com/AdamsFifteen','height='+h+',width='+w+',top='+((screen.height-h)/2)+',left='+((screen.width-w)/2)+',scrollbars=no,resizable=yes');
			break;
			case 'linkedin':
			alert('Sorry, page coming soon');
			break;
		}
		return false;
	});//end social function
  
  /*************************works page************************/
		$('#scroll li img').mouseover(function(){
				$(this).attr('src','images/scrollon.png')
		});
		$('#scroll li img').mouseout(function(){
				$(this).attr('src','images/scrolloff.png')
		});
  
  		$('div.works').addClass('hidden_div');
		$('#mini-content .works:first-child').removeClass('hidden_div');
		$(function() {
		  timer=setInterval(auto_rotate, 5000);
		});
		
		$('#scroll li img').click(function(){
				clearInterval(timer);
				var job = '#' + $(this).attr('title');
			 	var oldDiv=$('.works:not(.hidden_div)');
				var newDiv=$(job);
				oldDiv.fadeOut('medium', function(){oldDiv.addClass('hidden_div'); newDiv.fadeIn('medium').removeClass('hidden_div');});
				timer=setInterval(auto_rotate,5000);
			});
		
		function auto_rotate(){
		  var oldDiv=$('.works:not(.hidden_div)').attr('id');
		  oldDiv='#'+oldDiv;
		  switch(oldDiv){
			case '#BeSpoken_Spaces':
			$(oldDiv).fadeOut('medium', function(){$(oldDiv).addClass('hidden_div');$('#Genesis_Financial_Freedom').fadeIn('medium').removeClass('hidden_div');});
	  		break;
			case '#Genesis_Financial_Freedom':
			$(oldDiv).fadeOut('medium', function(){$(oldDiv).addClass('hidden_div');$('#Seven64').fadeIn('medium').removeClass('hidden_div');});
	  		break;
			case '#Seven64':
			$(oldDiv).fadeOut('medium', function(){$(oldDiv).addClass('hidden_div');$('#redvelvet_cupcakes').fadeIn('medium').removeClass('hidden_div');});
	  		break;
			case '#redvelvet_cupcakes':
			$(oldDiv).fadeOut('medium', function(){$(oldDiv).addClass('hidden_div');$('#projectq').fadeIn('medium').removeClass('hidden_div');});
	  		break;
			case '#projectq':
			$(oldDiv).fadeOut('medium', function(){$(oldDiv).addClass('hidden_div');$('#BeSpoken_Spaces').fadeIn('medium').removeClass('hidden_div');});
	  		break;												
		  }
		};/*end of rotate*/ 
		
		
/***********************************************************/ 
//function to call last tweet on twitter using API and JQUERY getJSON						   
  var username='AdamsFifteen'; // set user name
  var format='json'; // set format, you really don't have an option on this one
  var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url
  
	  $.getJSON(url,function(tweets){ // get the tweets
		  //$("#address p.smallfont").html(tweets[0].text)
		  var tweet=anchorText(tweets[0].text+' ');
		  
		  }); // get the first tweet in the response and place it inside the div
});//end of ready function  	   
  
/***********************************************************/ 
//function to add anchor to any tweet	
function anchorText(tweet){			   
	var combinedRegex = /@\w+|((\bhttps?:\/\/)|(\bwww\.))\S*\..*?(?=\W?\s)/gi,
		container = $('#tweet-container');
	
	var result, prevLastIndex = 0;
	combinedRegex.lastIndex = 0;
	while((result = combinedRegex.exec(tweet))) {
		// Append the text coming before the matched entity
		container.append($('<span/>').text(tweet.slice(prevLastIndex, result.index)));
		if(result[0].slice(0, 1) == "@") {
			// Twitter username was matched
			container.append($('<a/>')
				// .slice(1) cuts off the first character (i.e. "@")
				.attr({
					href: 'http://twitter.com/' + encodeURIComponent(result[0].slice(1)),
					target: '_blank'
					})
				.text(result[0])
			);
		} else {
			// URL was matched
			container.append($('<a/>')
				.attr({
					href: result[0],
					target: '_blank'
					})
				.text(result[0])
			);
		}
		// prevLastIndex will point to the next plain text character to be added
		prevLastIndex = combinedRegex.lastIndex;
	}
	// Append last plain text part of tweet
	container.append($('<span/>').text(tweet.slice(prevLastIndex)));
}

