/*
	Master Javascript file 
	
	version:   	1.4
	author:   	Bryan Lademann
	email:     	hi@bryanlademann.com
	website:   	http://www.bryanlademann.com    
	
*/


google.load("jquery", "1");
//google.load("jqueryui", "1");     


//Allows links to open in external window while maintaining pages validity.  
//------------------------------------------------------------
var ExternalLinks = {};      

ExternalLinks.setup = function ()    
{ 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && 
		   	anchor.getAttribute("rel") == "external") 
		 	anchor.target = "_blank"; 
	} 
};         


//creates smooth scrolling jump links  
//------------------------------------------------------------
var SmoothScroll = {};

SmoothScroll.setup = function()
{
	jQuery("a.anchorLink").click(function () {	
		elementClick = jQuery(this).attr("href")
		destination = jQuery(elementClick).offset().top;
		jQuery("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
		return false;
	}); 
}


// fade effect for project content
// ------------------------------------------------------------
var SlideShow = {};// creates an object container
SlideShow.setup = function()
{
	var tabs = jQuery('#content-featured ul a') // gathers all the project nav links            
	var projects = jQuery('#content-featured .project'); // gathers all the projects 
	
	// onClick event behavior
	jQuery(tabs).click(function()
	{	
		jQuery(projects).fadeOut(120) // when clicked previous images fade from view
		jQuery(tabs).removeClass('active');//removes 'you are here' indicator
		jQuery(this).addClass('active');//adds 'you are here' indicator
		
		var proj = jQuery(this).attr('href'); //gathers link destination
		jQuery(proj).fadeIn(350); // then the new image slowly appears in 800 milliseconds
		return false; // prevents the visual dissonance when a browser jumps to an anchor link
		
	});
};
       
  
// pulls lastest tweet and convert urls to links
//------------------------------------------------------------     
var Tweet = {};

Tweet.setup = function(username)
{
	jQuery.fn.lastTwitterMessage = function(username)
	{
		var twitter = jQuery('#twitter');

		var $base = this;
		if(!username || username == "") return this; // username required
		var url = "http://twitter.com/statuses/user_timeline.json?callback=?";
		jQuery.getJSON( url, { count: 10, screen_name: username },
		function(data){
		if(data && data.length >= 1){
		try{
		var item = null;
		for(var i = 0; i < data.length; i++){
		if(/^@/i.test(data[i].text)) continue;
		item = data[i]; break;
		}
		if(!item) return;
		var $tweet = $("<p></p> ").text(item.text);
		$tweet.html(
		$tweet.html()
		.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>')
		.replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>')
		.replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>')
		)
	    $base.empty().append($tweet).show();
		} catch (e) { };
		};
		});
		return this; // Don't break the chain	   
	}; 
}
           

google.setOnLoadCallback(function(){       
	
	SlideShow.setup();
	ExternalLinks.setup();
	SmoothScroll.setup();  
	Tweet.setup();    

	jQuery(function() {
		jQuery("<div id='tweet'></div>").hide().appendTo("#twitter")
		.lastTwitterMessage('bryanlademann');
	})
	
	jQuery(function(){
		
		
	})
     
 });