var SD = 
{
	Current : null,
	CurrentLink : null,
	CurrentAnimation : null,
	MORE_TEXT : "Read the rest",
	MAX_NUMBER_OF_IMAGES_PER_POST : 3,
	MAX_NUMBER_OF_PARAGRAHS_PER_POST : 1,
	POST_THUMBNAIL_SIZE_WIDTH : "160px",
	POST_THUMBNAIL_SIZE_HEIGHT : "120px",
	
	Go : function( state )
	{
		if( document.getElementById( state ) ) 
		{
			if( SD.Current )
			{
				SD.Current.style.display = "";
			}
			
			if( SD.CurrentLink )
			{
				SD.CurrentLink.className = "";
			}
			
			SD.CurrentLink = document.getElementById( state + "Link" );
			SD.Current = document.getElementById( state );
			SD.Current.style.display = "block";
			SD.CurrentLink.className  = "selected";
			
			document.title = "Studio Dynamic" + ( state !== "home" ? ( " - " + state.substr( 0, 1 ).toUpperCase() + state.substr( 1 ) ) : "" );
			
			YAHOO.util.History.navigate( "page", state );
		}
	},
	Init : function()
	{
		var state = SD.GetState();
		
		YAHOO.util.History.register( "page", state, SD.Go );	
		YAHOO.util.History.initialize( "yui-history-field", "yui-history-iframe" );
						
		SD.Go( state );
	},
	DrawVimeoPlayer : function( videoID, height, width )
	{
		var html = "<iframe src='http://player.vimeo.com/video/" + videoID + "?title=0&amp;byline=0&amp;portrait=0&amp;color=custom' width='" + width +"' height='" + height +"' frameborder='0'></iframe>";
		
		document.write( html );
	},
	GetState : function()
	{
		return YAHOO.util.History.getBookmarkedState( "page" ) || "home";
	},
	ToggleVideo : function()
	{		
		var player = document.getElementById( "player" )
		,flash_player_wrap = document.getElementById( "vimeo_wrap" )
		,flash_player = document.getElementById( "vimeo_wrap" ).getElementsByTagName( "iframe" )[ 0 ]
		,attributes = { height: { to: 292 } , opacity: { from: 0.1, to: 1 } }
		,attributes2 = { height: { to: 260 } , opacity: { from: 0.1, to: 1 } }
		,animation = YAHOO.util.Easing.easeIn
		,len = YAHOO.env.ua.gecko ? 0.6 : 0.9	
		,image = "minus.png"
		,anim
		,visibility = "visible";
		
		if( ! player.isHidden )
		{
			attributes = attributes2 = { height: { to: 0 } , opacity : { to: 0 } };
		
			animation = YAHOO.util.Easing.easeOut;
			image = "plus.png"; 
			visibility = "hidden";
			player.isHidden = false;
		}
		else
		{
			player.style.visibility = visibility;
		}
		
		player.isHidden = ! player.isHidden;

		SD.CurrentAnimation = new YAHOO.util.Anim( player ,attributes, len, animation );
		SD.CurrentAnimation.onComplete.subscribe( function() { if( this === SD.CurrentAnimation ) { player.style.visibility = visibility; } } );
		SD.CurrentAnimation.animate();	
		
		new YAHOO.util.Anim( flash_player ,attributes2, len, animation ).animate();
		new YAHOO.util.Anim( flash_player_wrap ,attributes2, len, animation ).animate();
		
		if( SD.timeout )
		{
			clearTimeout( SD.timeout );
		}
		
		SD.timeout = setTimeout( 
			function() 
			{ 
				document.getElementById( "toggleVideo" ).style.background = "url('images/" + image + "') no-repeat";
				SD.timeout = null;	
			},
			Math.round( len * 333 ) /* 1/3 of the length of the animation */  
		);
	},
	TruncatePosts : function()
	{
		var blogEntries = YAHOO.util.Dom.getElementsByClassName( "feedburnerFeedBlock", "div" )[ 0 ].getElementsByTagName( "li" ),
		i,
		j,
		entry,
		headline,
		images,
		paragrahs,
		addMoreLink,
		padding;
		
		for( i=0; i < blogEntries.length; i++ )
		{
			entry = blogEntries[ i ].getElementsByTagName( "div" )[ 0 ];
			headline = blogEntries[ i ].getElementsByTagName( "span" )[ 0 ].getElementsByTagName( "a" )[ 0 ];
			images = entry.getElementsByTagName( "img" );
			paragraphs = entry.getElementsByTagName( "div" );
			
			headline.title = "Read \"" + headline.innerHTML + "\"";
			
			for( j=0; j < images.length; j++ )
			{
					if( ( j >= SD.MAX_NUMBER_OF_IMAGES_PER_POST ) || ( YAHOO.util.Dom.hasClass( images[ j ], "sdHide" ) ) )
					{
						images[ j ].style.display = "none";
					}
					else
					{
						images[ j ].style.width =  SD.POST_THUMBNAIL_SIZE_WIDTH;
						images[ j ].style.height = SD.POST_THUMBNAIL_SIZE_HEIGHT;
						images[ j ].style.clear = images[ j ].parentNode.style.clear = "none";
						images[ j ].className += " sdThumb";
						images[ j ].parentNode.target = "_blank";
						images[ j ].parentNode.title = "View Fullsize";
						
						if( ! YAHOO.env.ua.ie )
						{
							images[ j ].style.cssFloat = "left";
						}
						else
						{
							images[ j ].style.float = "left";
						}
					}
			}
			
			for( j=0; j < paragraphs.length; j++ )
			{
				var text = paragraphs[ j ].textContent ? paragraphs[ j ].textContent : paragraphs[ j ].innerText ? paragraphs[ j ].innerText : "";
				
				if( j === 0 )
				{
					addMoreLink = false;
					padding = 0;
				}
				
				if( ( j - padding ) >= SD.MAX_NUMBER_OF_PARAGRAHS_PER_POST )
				{	
					if( YAHOO.lang.trim( text ).length > 0 || paragraphs[ j ].getElementsByTagName( "img" ).length === 0  )
					{
						paragraphs[ j ].style.display = "none";
						
						if( YAHOO.lang.trim( text ).length > 0 )
						{
							addMoreLink = true;
						}
					}
				}
				else if( YAHOO.lang.trim( text ).length === 0 && paragraphs[ j ].getElementsByTagName( "img" ).length === 0  )
				{
					paragraphs[ j ].style.display = "none";
					padding++;
				}
				else
				{
					paragraphs[ j ].style.textAlign = "left";
				}
				
				if( j === paragraphs.length - 1 && addMoreLink )
				{
 					if( j > 0 )
 					{
						var cssClass = "moreLink";
						if( i === blogEntries.length - 1 )
						{
							cssClass += " last";
						}
						
 						entry.innerHTML += "<a target='_blank' title='Read the rest of \"" + headline.innerHTML + "\"' href='" + headline.href + "' class='" + cssClass +"'>"+ SD.MORE_TEXT + "</a>";
 					}
				}
			}
		}
	},
	TwitterCallback : function( tweets )
	{
		var _relativeTime = 
			function( 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';
		  		}
		};
		
		var html = [];
		html.push( '<ul id="twitter_update_list">' );
		
		for ( var i=0; i<tweets.length; i++ )
		{
		    var username = tweets[ i ].user.screen_name;
		    var status = tweets[ i ].text.replace( /((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, 
				function( url ) 
				{
		      		return '<a target="_blank" href="' + url + '">' + url + '</a>';
		    	}
			).replace(/\B@([_a-z0-9]+)/ig, 
				function( reply ) 
				{
		      		return  reply.charAt( 0 ) + '<a target="_blank" href="http://twitter.com/' + reply.substring( 1 ) + '">' + reply.substring( 1 ) + '</a>';
		    	}
			);
		    
			html.push( '<li><span>'+ status +'</span> <a target="_blank" style="font-size:85%" href="http://twitter.com/' + username + '/statuses/' + tweets[ i ].id + '">' + _relativeTime( tweets[ i ].created_at ) +'</a></li>');
		}
		html.push('</ul>');
		
		document.write( html.join( "" ) );
	}
};

 var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17085111-1']);
 _gaq.push(['_setDomainName', 'none']);
 _gaq.push(['_setAllowLinker', true]);
 _gaq.push(['_trackPageview']);

  (function() {
   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


