<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stephen McIntyre</title>
	<atom:link href="http://stephenmcintyre.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://stephenmcintyre.net</link>
	<description>Young Scottish Web Designer</description>
	<lastBuildDate>Thu, 10 Jun 2010 15:25:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Untold Entertainment Made in jQuery Part 3 &#8211; The Project Slider</title>
		<link>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-project-slider/</link>
		<comments>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-project-slider/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 15:00:46 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=577</guid>
		<description><![CDATA[
Being a showcase for the Flash work of Untold Entertainment, the image slider is used as the main focus of the home page and displays his latest projects. Continuing from the first two tutorials, I&#8217;ll now show you how to create this slider with jQuery.
We&#8217;ll be treating the slider as a separate element from our [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/06/untold-slider.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/06/untold-slider.png" alt="" title="untold-slider" width="400" height="308" class="aligncenter size-full wp-image-584" /></a></p>
<p>Being a showcase for the Flash work of <a href="http://untoldentertainment.com">Untold Entertainment</a>, the image slider is used as the main focus of the home page and displays his latest projects. Continuing from the <a href="/blog/untold-entertainment-in-jquery-animating-twitter-text/">first</a> <a href="/blog/untold-entertainment-in-jquery-styling-the-twitter-bar/">two</a> tutorials, I&#8217;ll now show you how to create this slider with jQuery.</p>
<p>We&#8217;ll be treating the slider as a separate element from our Twitter bar before, so no need to use old code here.</p>
<p>With the abundance of jQuery image gallery plugins and scripts it&#8217;s safe to say that jQuery makes this sort of thing in particular run a lot more smoothly than with plain JavaScript. This section will be the easiest part of the series.</p>
<p>Let&#8217;s dive in.<span id="more-577"></span></p>
<p>First have a look at the <a href="/examples/untold/part-3.html">slider demo</a>.</p>
<h3>The Markup</h3>
<p>We&#8217;ll start with the HTML tags.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;slider&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;prev&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;next&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;content&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p><strong>#slider:</strong> The container element that wraps everything.</p>
<p><strong>#image:</strong> This contains the large image and navigation buttons (<code>#prev</code> and <code>#next</code>). The image is actually a background, but more on that later.</p>
<p><strong>#content:</strong> Holds elements for the project title (<code>&lt;h3&gt;</code>) and excerpt (<code>&lt;p&gt;</code>). The <code>&lt;a&gt;</code> inside the title creates a link to the page that the project is on.</p>
<h3>Setting Up jQuery and the Projects Array</h3>
<p>If you&#8217;ve followed the previous tutorials, including jQuery is still as straight-forward as it was before. Again, we&#8217;ll be drawing it in from the Google Code repository.</p>
<p>I&#8217;ve also added an extra empty tag that we can add our own code into.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>JavaScript should go above the main HTML content (at the start of the <code>&lt;body&gt;</code> if your document has it).</p>
<p>Inside the empty script tag, set up our &#8220;projects&#8221; array like so.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> projects <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/img/projects/bestEd/hedgeYourBets/thumb.jpg&quot;</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Best Ed – Hedge Your Bets!&quot;</span><span style="color: #339933;">,</span>
    excerpt<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/2009/09/17/best-ed-hedge-your-bets/&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/img/projects/bestEd/crossingDog/thumb.jpg&quot;</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Best Ed – Crossing Dog&quot;</span><span style="color: #339933;">,</span>
    excerpt<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/2009/09/17/best-ed-crossing-dog/&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/img/projects/bloat/bloat.jpg&quot;</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Bloat.&quot;</span><span style="color: #339933;">,</span>
    excerpt<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Our company president created Bloat. in roughly twenty-two hours at TOJam 4, a weekend-long game development expo. Bloat. is a true [...]&quot;</span><span style="color: #339933;">,</span>
    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/2009/05/05/bloat/&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/img/projects/AWTY/eyeInTheSky/EyeInTheSky.jpg&quot;</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Eye in the Sky&quot;</span><span style="color: #339933;">,</span>
    excerpt<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;We created this spot-the-differences game for Sinking Ship Entertainment’s Are We There Yet? World Adventure kids’ travel teevee [...]&quot;</span><span style="color: #339933;">,</span>
    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/2008/11/28/eye-in-the-sky/&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/img/projects/AWTY/trainTrack/trainTrack.jpg&quot;</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Train Track!&quot;</span><span style="color: #339933;">,</span>
    excerpt<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;We were approached by Sinking Ship Entertainment to create an eye-spy game for their kids’ travel teevee show Are We There Yet? World [...]&quot;</span><span style="color: #339933;">,</span>
    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/2008/11/28/train-track/&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> project_id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The script uses JSON notation to create an array of objects, each with the same set of attributes: &#8220;image&#8221;, &#8220;title&#8221;, &#8220;excerpt&#8221; and &#8220;link&#8221;.</p>
<p>We would print these onto the page using PHP, looping through database rows in a WordPress setup. This won&#8217;t be covered as it&#8217;s irrelevant how the information gets to the page. This set of example project data will work on its own for now.</p>
<p>The <code>project_id</code> variable holds the array key for our current project data object. This means we can change this number (with the navigation buttons) and call a whole new set of data when we want it to load.</p>
<h3>Attaching Clicks to the Navigation Buttons</h3>
<p>To allow ourselves to reference our HTML elements, we have to wait for them to load before we call our JavaScript to interact with them. For this we can either place the JavaScript after our HTML, or use jQuery&#8217;s nice &#8220;document ready&#8221; check to do this for us, and still keep our JavaScript at the top.</p>
<p>Here I&#8217;ll use the jQuery method. Generally it lets us to load all of our JavaScript first, allowing a smoother page load and cuts out some jerky effects.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  $<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#prev'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      project_id<span style="color: #339933;">--;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>project_id <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        project_id <span style="color: #339933;">=</span> projects.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      load_project<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#next'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      project_id<span style="color: #339933;">++;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>project_id <span style="color: #339933;">&gt;</span> projects.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        project_id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      load_project<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    load_project<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In each case, clicking one of the buttons adds or removes one from <code>project_id</code>. If it is less than zero or greater than our maximum index in <code>projects</code> (five) then it is reset to the other side of the scale.</p>
<p>For example, pressing the previous button when you are looking at the first project (ID of zero), the slider skips to the fifth project in the list. Clicking next from here takes you back to the first element, and so on.</p>
<h3>load_project()</h3>
<p>This is what is called when the page is loaded and when a navigation button is clicked. It takes the <code>project_id</code> and populates the slider with all information attached to that array key inside <code>projects</code>.</p>
<p>I&#8217;ll go through this one a bit at a time.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> load_project<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> project_data <span style="color: #339933;">=</span> projects<span style="color: #009900;">&#91;</span>project_id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#image'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'backgroundImage'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content h3 a'</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span> project_data.<span style="color: #660066;">link</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>project_data.<span style="color: #660066;">title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content p'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>project_data.<span style="color: #660066;">excerpt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>project_data.<span style="color: #660066;">excerpt</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content p'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' &lt;a href=&quot;'</span> <span style="color: #339933;">+</span> project_data.<span style="color: #660066;">link</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;keep reading&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;img src=&quot;'</span> <span style="color: #339933;">+</span> project_data.<span style="color: #660066;">image</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; /&gt;'</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>responseText<span style="color: #339933;">,</span> textStatus<span style="color: #339933;">,</span> XMLHttpRequest<span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#image'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'backgroundImage'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;'</span> <span style="color: #339933;">+</span> project_data.<span style="color: #660066;">image</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 5:</strong> Removes any overridden background image styles on &#8220;#image&#8221; to display the &#8220;loading&#8221; spinner.</p>
<p><strong>Lines 7-9:</strong> Adds an <code>href</code> to the title allowing it to link to an external page, and fills in the title text. These are both taken from the current item in the list of projects (referenced here as <code>project_data</code>).</p>
<p><strong>Lines 11-16:</strong> The first line fills in the <code>&lt;p&gt;</code> tag with the excerpt text. If the excerpt is not an empty string, an extra &#8220;keep reading&#8221; link is also added to the end of the text linking to the project page. This is the same link used for the title.</p>
<p><strong>Lines 18 and 19:</strong> jQuery creates a DOM element of an image, with its source linking to the current project&#8217;s image URL. We can then use this to track how long it takes to load, as done here with the built-in <code>.load()</code> method.</p>
<p>When the element has loaded (read: the image has been fully downloaded into your browser), it issues a callback to JavaScript. Anything we put inside of this callback will only be run once the requested image is available to be displayed. The main reason here being to switch out the &#8220;loading&#8221; spinner.</p>
<p><strong>Line 21:</strong> Here we edit the background of the &#8220;#image&#8221; container again, this time changing it to the image for the current project and overriding the spinner.</p>
<h3>Adding Style</h3>
<p>If you&#8217;re familiar with basic CSS, these will be pretty straight-forward.</p>
<p>However, pay particular attention to the <code>:hover</code> pseudo on the navigation buttons. Since we want the buttons to animate when we hover over them and not on its own or on a timer, it&#8217;s useful to do this with CSS here instead of JavaScript.</p>
<p>Again, this part goes right at the top before everything else (in the <code>&lt;head&gt;</code> section if your document has one).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
  <span style="color: #00AA00;">*</span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#slider</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">466px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #3333ff;">:Verdana</span><span style="color: #00AA00;">,</span> Geneva<span style="color: #00AA00;">,</span> Arial<span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#image</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'loader.gif'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">340px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#content</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#eee</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#image</span> a
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">280px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:<span style="color: #993333;">none</span> </span>url<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'prev.png'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#222</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  a<span style="color: #cc00cc;">#prev</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'prev.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  a<span style="color: #cc00cc;">#prev</span><span style="color: #3333ff;"><span style="color: #00AA00;">:</span>hover
  </span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'prev-hover.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  a<span style="color: #cc00cc;">#next</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'next.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  a<span style="color: #cc00cc;">#next</span><span style="color: #3333ff;"><span style="color: #00AA00;">:</span>hover
  </span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'next-hover.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></td></tr></table></div>

<p>And that&#8217;s it. Here is what your final slider will look like.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/06/untold-js-slider.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/06/untold-js-slider.png" alt="" title="untold-js-slider" width="400" height="381" class="aligncenter size-full wp-image-590" /></a></p>
<p>Now comes the fun part. Next week I&#8217;ll be showing you how to make that roll-out tongue on the menu bar, all without a bit of Flash.</p>
<p>Connect with my blog&#8217;s <a href="http://feeds.feedburner.com/stephenmcintyre?format=html">news stream</a> to get notified as soon as it&#8217;s up.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-project-slider/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Untold Entertainment Made in jQuery Part 2 – Styling the Twitter Bar</title>
		<link>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-styling-the-twitter-bar/</link>
		<comments>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-styling-the-twitter-bar/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 00:50:17 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=556</guid>
		<description><![CDATA[Carrying on from the first part of creating a Flash-less home page on Untold Entertainment, we add to the tutorial on animating Twitter text with some more usability and style.
We start off by generating links (to URLs and Twitter pages) inside the text, make a couple of buttons to skip the tweets backwards and forwards, [...]]]></description>
			<content:encoded><![CDATA[<p>Carrying on from the first part of creating a Flash-less home page on <a href="http://untoldentertainment.com">Untold Entertainment</a>, we add to the tutorial on <a href="/blog/untold-entertainment-in-jquery-animating-twitter-text/">animating Twitter text</a> with some more usability and style.</p>
<p>We start off by generating links (to URLs and Twitter pages) inside the text, make a couple of buttons to skip the tweets backwards and forwards, then animate the Twitter bird and speech bubble as a background.<span id="more-556"></span></p>
<p>Try out the new <a href="/examples/untold/part-2.html">styled Twitter bar demo</a>.</p>
<h3>The New HTML Structure</h3>
<p>To add in our new elements, we&#8217;ll have to mess about with the layout of our HTML tags.</p>
<p>The container still holds the text as before, but we add in the bird image and navigation buttons inside a div. The holder is there to keep the buttons beside the text, and the outer wrapper collects all of it including the bird image.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;wrapper&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;holder&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tweet_buttons&quot;</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;prev&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;#9668;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;next&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;#9658;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;container&quot;</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bird&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bird.gif&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bird&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>The HTML entities are the forward and backward pointing arrowheads you see. These can be images, but the entities will do for now to make things simpler.</p>
<h3>Adding Links to the Text</h3>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-static.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-static.png" alt="" title="bird-static" width="400" height="39" class="aligncenter size-full wp-image-570" /></a></p>
<p>To find URLs and Twitter usernames, we have to do a check on each word. Here we&#8217;re looking for anything starting with &#8220;http://&#8221; and &#8220;@&#8221; respectively.</p>
<p>We change our <code>add_text()</code> method from last time with the following lines of code. This should replace the line beginning with <code>var new_text = ...</code>.</p>
<p>The first three sections change text inside <code>add_text()</code>. For the full replacement code, use the new function at the end of the tutorial.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> word <span style="color: #339933;">=</span> updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>word_key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'@'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  word <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;a href=&quot;http://twitter.com/'</span> <span style="color: #339933;">+</span> word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'http://'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  word <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;a href=&quot;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> new_text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &lt;span class=&quot;rot'</span> <span style="color: #339933;">+</span> rand <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This wraps any usernames and URLs inside an anchor <code>&lt;a&gt;</code> tag and turns them into clickable links.</p>
<h3>Animating the bird</h3>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-chirp.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-chirp.png" alt="" title="bird-chirp" width="400" height="38" class="aligncenter size-full wp-image-569" /></a></p>
<p>These few lines go after <code>$('#container').html(new_text);</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'bird_shout.gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>chirp_time<span style="color: #339933;">,</span> <span style="color: #3366CC;">'blabel_'</span> <span style="color: #339933;">+</span> update_key<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'bird.gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The variable <code>chirp_time</code> should be added to our list of Variables at the top of the JavaScript section. Here I&#8217;ve set it to 100 as it works well for the GIF animation. And while you&#8217;re there add in a delay that we&#8217;ll use later.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">...
<span style="color: #003366; font-weight: bold;">var</span> length_min <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> length_max <span style="color: #339933;">=</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> chirp_time <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> delay <span style="color: #339933;">=</span> <span style="color: #CC0000;">2000</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is what makes the bird&#8217;s mouth move, held open by the time we&#8217;ve specified in <code>chirp_time</code> as 100 milliseconds.</p>
<h3>Animating the Speech Bubble</h3>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-transition.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-transition.png" alt="" title="bird-transition" width="400" height="37" class="aligncenter size-full wp-image-571" /></a></p>
<p>Look for the &#8220;if&#8221; statement of <code>if(word_key < updates...</code>, add this line just inside.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble.gif&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This sets the background to be the non-animated image of a speech bubble.</p>
<p>In the "else" of this statement, look for the first timer event (the first line inside). In here we add the code to make the background animate as the text transitions into a new tweet. It's similar to the line above, but instead links to an animated image.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble_animated.gif&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Setting Up the Controls</h3>
<p>To add JavaScript "onclick" events to our navigational buttons, we call jQuery on page load. This is to allow the elements to be created before we bind events to them.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#prev'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">stopTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    word_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    update_key<span style="color: #339933;">--;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>update_key <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      update_key <span style="color: #339933;">=</span> updates.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble_animated.gif&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>delay<span style="color: #339933;">,</span> <span style="color: #3366CC;">'label_'</span> <span style="color: #339933;">+</span> word_key<span style="color: #339933;">,</span> add_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#next'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">stopTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    word_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    update_key<span style="color: #339933;">++;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>update_key <span style="color: #339933;">&gt;</span> updates.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      update_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble_animated.gif&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>delay<span style="color: #339933;">,</span> <span style="color: #3366CC;">'label_'</span> <span style="color: #339933;">+</span> word_key<span style="color: #339933;">,</span> add_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The code should look very familiar to what's inside <code>add_text()</code>. Simply put, the <code>#previous</code> code skips to the previous tweet and <code>#next</code> goes to the one after, so this changes the increment or decrement of the <code>update_key</code> variable.</p>
<h3>Updating the Stylesheet</h3>
<p>The new styles for these elements are also as follows.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#bubble</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">700px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">60px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #3333ff;">:inline-</span>block<span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'bubble.gif'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#buttons</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">40px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#buttons</span> a
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#999</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#bird</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>And that's it for now.</p>
<p>Next we'll make the image slider.</p>
<p>(Here's the new code for <code>add_text()</code>)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> add_text<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> rand <span style="color: #339933;">=</span> Math.<span style="color: #660066;">ceil</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> word <span style="color: #339933;">=</span> updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>word_key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'@'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    word <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;a href=&quot;http://twitter.com/'</span> <span style="color: #339933;">+</span> word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'http://'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    word <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;a href=&quot;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> new_text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &lt;span class=&quot;rot'</span> <span style="color: #339933;">+</span> rand <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>new_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'bird_shout.gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>chirp_time<span style="color: #339933;">,</span> <span style="color: #3366CC;">'blabel_'</span> <span style="color: #339933;">+</span> update_key<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'bird.gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  word_key<span style="color: #339933;">++;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>word_key <span style="color: #339933;">&lt;</span> updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble.jpg&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    set_timer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">else</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>wait<span style="color: #339933;">,</span> <span style="color: #3366CC;">'reset'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      word_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
      update_key<span style="color: #339933;">++;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>update_key <span style="color: #339933;">&gt;</span> updates.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        update_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble_animated.gif&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>delay<span style="color: #339933;">,</span> <span style="color: #3366CC;">'label_'</span> <span style="color: #339933;">+</span> word_key<span style="color: #339933;">,</span> add_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-styling-the-twitter-bar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Untold Entertainment Made in jQuery Part 1 &#8211; Animating Twitter Text</title>
		<link>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-animating-twitter-text/</link>
		<comments>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-animating-twitter-text/#comments</comments>
		<pubDate>Thu, 27 May 2010 23:14:55 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=531</guid>
		<description><![CDATA[
Ryan Creighton&#8217;s Flash development site Untold Entertainment has a quirky home page with plenty of animation and things to click on (complete with roll-out tongue) and of course is built in Flash. Thing is, I&#8217;m against any kind of Flash interface. It&#8217;s great for games, but otherwise it&#8217;s unnecessary (which includes videos).
With the whole Adobe [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold.png" alt="" title="untold" width="400" height="244" class="aligncenter size-full wp-image-552" /></a></p>
<p>Ryan Creighton&#8217;s Flash development site <a href="http://untoldentertainment.com">Untold Entertainment</a> has a quirky home page with plenty of animation and things to click on (complete with roll-out tongue) and of course is built in Flash. Thing is, I&#8217;m against any kind of Flash interface. It&#8217;s great for games, but otherwise it&#8217;s unnecessary (which includes videos).</p>
<p>With the whole Adobe v Apple debate that&#8217;s going on right now (yes, I said I wouldn&#8217;t mention it but it applies here &#8216;kay), it&#8217;s safe to say you&#8217;d be better placing your chips on the JavaScript and HTML5 end of the table.</p>
<p>The discouraging part is that his website could be done without Flash as most of the scrolling and animating could be done with straight JavaScript, and jQuery will help see things along smoothly.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold-twbar.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold-twbar.png" alt="" title="untold-twbar" width="400" height="38" class="aligncenter size-full wp-image-553" /></a></p>
<p>Today I&#8217;ll show you how to animate the text at the top, feeding in from <a href="http://twitter.com/untoldent">his Twitter feed</a>, complete with jerky timings, slanted text and collecting feed data.<span id="more-531"></span></p>
<p>Check out the <a href="/examples/untold/part-1.html">Untold Twitter bar</a> demo.</p>
<h3>Including jQuery, Timer plugin, and Google AJAX Feed API</h3>
<p>We&#8217;ll start off by including our libraries and plugins. Here I use jQuery, the jQuery Timers plugin, and the Google AJAX Feed API &#8211; all sourced from their Google Code hosts.</p>
<p>Admittedly we won&#8217;t be using much jQuery here as the code changed quite a bit as it got added to, but it might come in handy in later tutorials. If you know how to change the scripts into plain JavaScript, by all means change those parts over.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stikked.googlecode.com/svn/trunk/static/js/jquery.timers.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.com/jsapi?key=YOURAPIKEY&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>More information on the API key can be found in my earlier post on <a href="/blog/guide-to-google-ajax-feed-api/">Google AJAX Feeds</a>.</p>
<p>This code goes inside the <code>&lt;body&gt;</code> element, right at the top so it loads first. There&#8217;s an extra element in there &#8211; this is where we&#8217;ll add our own JavaScript.</p>
<h3>Settings and Variables</h3>
<p>To allow us to fine tune the feel of the page later, we&#8217;ll set up a list of variables. This bit of code is the first to go inside the empty <code>&lt;script&gt;</code> tag above.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Variables - tweak as you like */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> username <span style="color: #339933;">=</span> <span style="color: #3366CC;">'UntoldEnt'</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> feed_url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://twitter.com/statuses/user_timeline/21834006.rss'</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> entry_fetches <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> interval <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> wait <span style="color: #339933;">=</span> <span style="color: #CC0000;">6000</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> length_min <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> length_max <span style="color: #339933;">=</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>These should be reasonable to understand, and what isn&#8217;t obvious will be covered later as the script develops.</p>
<h3>Collecting Feed Data</h3>
<p>In this example we want to collect the data from Twitter, or more specifically <a href="http://twitter.com/untoldent">@untoldent</a>&#8217;s account. More information on the use of the <code>google</code> object can be found on the same link as above (the <a href="/blog/guide-to-google-ajax-feed-api/">Google Feed</a> one).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Google Feed API stuff */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> updates <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> feed <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">feeds</span>.<span style="color: #660066;">Feed</span><span style="color: #009900;">&#40;</span>feed_url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  feed.<span style="color: #660066;">setNumEntries</span><span style="color: #009900;">&#40;</span>entry_fetches<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  feed.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>result.<span style="color: #660066;">error</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> result.<span style="color: #660066;">feed</span>.<span style="color: #660066;">entries</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> entry <span style="color: #339933;">=</span> result.<span style="color: #660066;">feed</span>.<span style="color: #660066;">entries</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">title</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span>username.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>entry.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'@'</span> <span style="color: #339933;">&amp;&amp;</span> entry.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'RT'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          updates.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>entry.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    set_timer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
google.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;feeds&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
google.<span style="color: #660066;">setOnLoadCallback</span><span style="color: #009900;">&#40;</span>Initialize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We start off by collecting the RSS feed from the <code>feed_url</code> and check there are no errors like I did before.</p>
<p>The script then loops through each feed element once it has been loaded in and removes replies and retweets. What&#8217;s left is added to our array of <code>updates</code>, split word by word into another array inside of it.</p>
<p>When this is done, <code>set_timer()</code> is called to start the actual stuff that makes it look like Untold&#8217;s Twitter bar.</p>
<h3>Creating Animated Text</h3>
<p>Last bit of JavaScript &#8211; the fancy part.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Animated Tweet stuff */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> clamp<span style="color: #009900;">&#40;</span>val<span style="color: #339933;">,</span> min<span style="color: #339933;">,</span> max<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> Math.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span>min<span style="color: #339933;">,</span> Math.<span style="color: #660066;">min</span><span style="color: #009900;">&#40;</span>max<span style="color: #339933;">,</span> val<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> word_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> update_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> set_timer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> length <span style="color: #339933;">=</span> clamp<span style="color: #009900;">&#40;</span>updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>word_key<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> length_min<span style="color: #339933;">,</span> length_max<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> timing <span style="color: #339933;">=</span> length <span style="color: #339933;">*</span> interval<span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>timing<span style="color: #339933;">,</span> <span style="color: #3366CC;">'label_'</span> <span style="color: #339933;">+</span> word_key<span style="color: #339933;">,</span> add_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> add_text<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> rand <span style="color: #339933;">=</span> Math.<span style="color: #660066;">ceil</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> new_text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &lt;span class=&quot;rot'</span> <span style="color: #339933;">+</span> rand <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>word_key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>new_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  word_key<span style="color: #339933;">++;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>word_key <span style="color: #339933;">&lt;</span> updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    set_timer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">else</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>wait<span style="color: #339933;">,</span> <span style="color: #3366CC;">'reset'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      word_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
      update_key<span style="color: #339933;">++;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>update_key <span style="color: #339933;">&gt;</span> updates.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        update_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      set_timer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><b>clamp:</b> In other languages, <code>clamp</code> is usually available inside a <code>Math</code> library, but not this one. So I&#8217;ve added this to make the code cleaner later.</p>
<p><b>word_key and update_key:</b> These are indexes for the <code>updates</code> array and the words inside each element. These increment by one as they are used, and then reset to zero when moving to a new element. They&#8217;re kept outside the functions so that they can be called from wherever.</p>
<p><b>set_timer:</b> First calculates a timing based on the length of the word. To avoid certain words appearing too quickly or slowly, the <code>length_min</code> and <code>length_max</code> are used from earlier to add a constrained limit on either side. It is then multiplied by <code>interval</code> &#8211; also set earlier &#8211; to work out our timer.</p>
<p>The Timers plugin is then used to add a timer to the container element. This will call <code>add_text()</code> after the set time from our previous word length is up.</p>
<p><b>add_text:</b> Starts by setting a random number. This sets our rotation later, based on four classes in a stylesheet. Each word is contained within a <code>&lt;span&gt;</code> element to treat each rotation separately.</p>
<p>The <code>.html()</code> method inside jQuery allows us to add HTML markup inside of our container. Here we add our new element (the <code>&lt;span&gt;</code> with a new word) onto the existing DOM.</p>
<p>When the word has been added, the index increases by one to access the next word. A timer is then set to add this word to the queue, which then adds it on the same way as before.</p>
<p>If there are no words left (i.e., the tweet has ended), the script stops for a period defined by <code>wait</code> and some variables are reset to prepare for a new word. This includes <code>word_key</code>, <code>update_key</code> (if needed), and resets the HTML inside the container back to being empty.</p>
<p>Again, the timer is reset to prepare for new words to be added in, once the script has come out of its pause.</p>
<h3>Rotation Styles</h3>
<p>To keep with the quirky feel of Untold&#8217;s header, the text in the tweet bar also tilts word-by-word. As we have already set up our span elements with rotation classes, all that&#8217;s left is to style them.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
<span style="color: #cc00cc;">#container</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">1.4em</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #3333ff;">:Verdana</span><span style="color: #00AA00;">,</span> Geneva<span style="color: #00AA00;">,</span> Arial<span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.rot1</span>
<span style="color: #00AA00;">&#123;</span>
  -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>1deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>1deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  rotation<span style="color: #00AA00;">:</span> 1deg<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.rot2</span>
<span style="color: #00AA00;">&#123;</span>
  -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>2deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>2deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  rotation<span style="color: #00AA00;">:</span> 2deg<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.rot3</span>
<span style="color: #00AA00;">&#123;</span>
  -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>-1deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>-1deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  rotation<span style="color: #00AA00;">:</span> -1deg<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.rot4</span>
<span style="color: #00AA00;">&#123;</span>
  -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>-2deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>-2deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  rotation<span style="color: #00AA00;">:</span> -2deg<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></td></tr></table></div>

<p>As you can see, these styles use WebKit and Mozilla transforms so they&#8217;re not native to all browsers. The <code>rotation</code> attribute is also only available as part of CSS3, so again not all browsers will be using this.</p>
<p>The latest version of Firefox runs it fine, although I found problems with the latest Chrome and Safari strangley, even though they use WebKit. Basically, if the text doesn&#8217;t look slanted your browser doesn&#8217;t support rotation <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold-js-twbar.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold-js-twbar.png" alt="" title="untold-js-twbar" width="400" height="19" class="aligncenter size-full wp-image-554" /></a></p>
<p>And that&#8217;s it. I&#8217;ll be seeing how far I can go with these scripts into a pure Flash-less home page.</p>
<p>Next I&#8217;ll be adding to this Twitter bar a bit more and taking it from there.</p>
<p>I&#8217;d like to know what you think of this project. Should JavaScript be used as a preference over Flash for interface design? Are there parts of Ryan&#8217;s home page that can&#8217;t possibly be made in this way?</p>
<p>Let me know in the comments, or rant at me on <a href="http://twitter.com/_bigSteve">Twitter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-animating-twitter-text/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Copying Images with allow_url_fopen Switched Off</title>
		<link>http://stephenmcintyre.net/blog/copy-images-allow_url_fopen-off/</link>
		<comments>http://stephenmcintyre.net/blog/copy-images-allow_url_fopen-off/#comments</comments>
		<pubDate>Thu, 20 May 2010 16:06:56 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=506</guid>
		<description><![CDATA[I&#8217;m working on a Facebook app that needs to collect images from an external server. Normally I could use a function like copy to do this, but this requires allow_url_fopen to be on and a lot of providers like mine have this turned off to tighten server security.
Eventually I came across file_get_contents to do the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a Facebook app that needs to collect images from an external server. Normally I could use a function like <a href="http://php.net/copy">copy</a> to do this, but this requires <code>allow_url_fopen</code> to be on and a lot of providers like mine have this turned off to tighten server security.</p>
<p>Eventually I came across <code>file_get_contents</code> to do the job. Since this also needs <code>allow_url_fopen</code> to be on, my post on <a href="/blog/fetch-content-using-php/">fetching page content with cURL</a> came to mind.</p>
<p>This does the same thing without restrictions on privacy and is available on most current setups.<span id="more-506"></span></p>
<h3>Fetching Raw Data with get_page_content</h3>
<p>We start off with our <code>get_page_content</code> method from the last post, which we use to collect page data. We&#8217;re looking to collect the raw data of an image, and here I&#8217;ve used the Google logo as an example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.google.co.uk/intl/en_com/images/srpr/logo1w.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'logo.png'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The use of <code>get_page_content</code> is the same as in the last post.</p>
<p>For the last three lines we use a few functions for creating a new file.</p>
<p>The first line creates a handle to a file (which doesn&#8217;t have to exist), which here we&#8217;ve called &#8220;logo.png&#8221;. This is where our image will be copied to. The &#8220;w&#8221; parameter tells the script we are opening the file for it to be directly written to, and we save the resource into <code>$fp</code>.</p>
<p>The second line writes the content that we collected earlier into our image. The raw data will make it display properly since it&#8217;s being saved as an image file.</p>
<p>Calling <code>fclose</code> on the resource closes the handle to it and frees up memory.</p>
<h3>Mimicking the PHP Copy</h3>
<p>Now the script will work fine if you use it as-is, but for a more robust way of handling file copying we&#8217;re going to create a wrapper around it to be called similarly to the <code>copy</code> function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> copy_file<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source</span><span style="color: #339933;">,</span> <span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$source</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here we have a new function with a <code>$source</code> and <code>$destination</code> parameter, identical to <code>copy</code>.</p>
<h3>Handling Failed Requests</h3>
<p>To add even further similarity to the <code>allow_url_fopen</code> method I&#8217;ll add in some boolean return values.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> copy_the_file<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source</span><span style="color: #339933;">,</span> <span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$source</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$fw</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fw</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So now if there is no content in the external file or the new file can&#8217;t be written to your server, the function will return false.</p>
<p>You can add to this with some cURL error checking and a handler on <code>fopen</code>, but it depends how robust it would need to be on your own website.</p>
<p>The great thing is, this script doesn&#8217;t just work with images. Just change the destination file to anything else with a change of extension and it&#8217;ll work exactly the same.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/copy-images-allow_url_fopen-off/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Messing with Background-Attachment</title>
		<link>http://stephenmcintyre.net/blog/messing-with-background-attachment/</link>
		<comments>http://stephenmcintyre.net/blog/messing-with-background-attachment/#comments</comments>
		<pubDate>Thu, 13 May 2010 21:17:00 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=489</guid>
		<description><![CDATA[
Earlier this week I came across an interesting Tumblr theme by Jarred Bishop. The site played with the background-attachment CSS property to show a unique background for each Tumblr post on his main page (depending on which post image he used) and stays static as you scroll.
I started to play around with some code to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/05/parrots.jpg"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/05/parrots.jpg" alt="" title="parrots" width="400" height="164" class="aligncenter size-full wp-image-500" /></a></p>
<p>Earlier this week I <a href="http://twitter.com/_bigSteve/status/13877279858">came across</a> an interesting Tumblr theme by <a href="http://jarredbishop.tumblr.com/">Jarred Bishop</a>. The site played with the <code>background-attachment</code> CSS property to show a unique background for each Tumblr post on his main page (depending on which post image he used) and stays static as you scroll.</p>
<p>I started to play around with some code to replicate this effect and instead came up with some different examples, while still manipulating <code>background-attachment</code>. Hit the jump for some demos.<span id="more-489"></span></p>
<p>Before we start coding, <a href="/examples/bg-attach/">check these out</a>!</p>
<p>Like them?</p>
<p>Moving the scroll bar on each box changes the effect in which the background moves inside it. You either get a simple scroller (nothing special), two garage-door-style shutters, or a really nice smooth-in animation that desaturates our parrot.</p>
<p>It&#8217;s all down to changing the <code>background-attachment</code> from &#8220;scroll&#8221; to &#8220;fixed&#8221;.</p>
<h3>Styling the Containers</h3>
<p>Here&#8217;s an example. This is from the last box in the set &#8211; the one that looks like it changes into black and white as you scroll. First we&#8217;ll set up the styles. These do most of the work.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">&lt;style<span style="color: #00AA00;">&gt;</span>
  <span style="color: #00AA00;">*</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.container</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.container</span> div
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.container</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
    overflow-x<span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> 
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.container</span> div
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #3333ff;">:no-</span>repeat<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'parrot.jpg'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.container</span> div<span style="color: #6666ff;">.grey</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'parrot-grey.jpg'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.container</span> div<span style="color: #6666ff;">.fixed</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-attachment</span><span style="color: #00AA00;">:</span><span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></td></tr></table></div>

<p>The first part sets all margins to zero (if you have a <code>&lt;body&gt;</code> tag it&#8217;s better to use that instead of the <code>*</code> &#8220;all&#8221; pseudo). This means we don&#8217;t get weird spacing issues with the background being in the top-left corner.</p>
<p>We set each container and the <code>&lt;div&gt;</code>s inside of them to have the same height and width, both matching your images. Means they won&#8217;t overlap each other with weird sizes.</p>
<p>The container class code gives the right side a scroll bar and removes it from the bottom (we don&#8217;t need the bottom one).</p>
<p>The rest should be okay.</p>
<h3>Adding the Containers</h3>
<p>There&#8217;s a tiny bit of HTML to add in.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;container&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fixed&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;grey fixed&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>That&#8217;s all it takes, your CSS will do the rest.</p>
<p>We give our outer div the &#8220;container&#8221; class that we set earlier. The inside elements then can be given a &#8220;fixed&#8221; class to set its background-attachment.</p>
<p>Make sure to give the second one a &#8220;grey&#8221; class to change its image if you want to. For this you definitely do, but for the shutter-style examples it depends how you want it to look.</p>
<p>So now if you want to play around with the different effects, all you have to do is add or remove the &#8220;fixed&#8221; class from the HTML above.</p>
<p>Be sure to check the source of the example page to see how I did it. Just bear in mind that there are some styles in there that are purely to demonstrate them inside the grid.</p>
<p>Things like the &#8220;bottom&#8221; and &#8220;right&#8221; classes and the &#8220;position&#8221; attribute in the container class were only created for the example, but feel free to play with them and see what you get.</p>
<p>Tracing back to the start of this post, I&#8217;ll definitely be putting something else up based on Jarred&#8217;s theme, this being the first bit of insight into how it all works.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/messing-with-background-attachment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Load WordPress Gravatars on the Fly</title>
		<link>http://stephenmcintyre.net/blog/load-gravatars-on-the-fly/</link>
		<comments>http://stephenmcintyre.net/blog/load-gravatars-on-the-fly/#comments</comments>
		<pubDate>Thu, 06 May 2010 23:22:36 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[The Web]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=475</guid>
		<description><![CDATA[Globally Recognised Avatar (or Gravatar) is a service from the WordPress team that allows you to improve your web presence across blogs and specialist websites. It&#8217;s one of those &#8220;why didn&#8217;t I think of that?&#8221; products that are so simple but yet have so much opportunity to be integrated into virtually everything. You can see [...]]]></description>
			<content:encoded><![CDATA[<p>Globally Recognised Avatar (or <a href="http://gravatar.com" target="_blank">Gravatar</a>) is a service from the WordPress team that allows you to improve your web presence across blogs and specialist websites. It&#8217;s one of those &#8220;why didn&#8217;t I think of that?&#8221; products that are so simple but yet have so much opportunity to be integrated into virtually everything. You can see examples of this from the comments below and in my sidebar.</p>
<p>The problem with Gravatars that I find a lot of people mentioning is that they don&#8217;t get to see their Gravatar before they post a comment &#8211; like the <a href="http://www.shamusyoung.com/twentysidedtale/?p=1462#comments">comments section</a> on Shamus Young&#8217;s Wavatar post.</p>
<p>You may not think that&#8217;s too big of a deal, or perhaps from a marketing point of view that&#8217;s your way of driving comments (nasty people), but allowing them to play about with their image in the form will improve their relationship with your site and increase traffic anyway. Saves people deleting and re-commenting just because their Gravatar didn&#8217;t show up right too.</p>
<p>So this week I&#8217;ve put together a little script that generates these little Gravatars as you type. It&#8217;s even got little <a href="http://www.shamusyoung.com/twentysidedtale/?p=1462" target="_blank">Wavatar</a> faces on them that change when you make a new address, so you&#8217;ll never get bored!<span id="more-475"></span></p>
<p>Here&#8217;s the <a href="/examples/gravatar/">demo</a>.</p>
<p>So for this, we need some kind of an MD5 encryption algorithm, as the Gravatar API needs this to secure its e-mail addresses.</p>
<p>I&#8217;ve used a script from <a href="http://www.webtoolkit.info/javascript-md5.html">webtoolkit</a> that does the job perfectly.</p>
<p>The script&#8217;s over 200 lines long and you can find it <a href="/examples/gravatar/webtoolkit-md5.js">here</a>. I&#8217;ve included it in the script as an external file for easiness.</p>
<h2>Setting Up</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gravatar&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Gravatar&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;webtoolkit-md5.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>Here we have our image, the input box, and the script tag that includes the MD5 script.</p>
<p>The Gravatar&#8217;s <code>src</code> will change when you start typing in the input field. It uses only a few lines of JavaScript.</p>
<h2>Working JavaScript&#8217;s Magic</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> gravatar <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gravatar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> email <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
email.<span style="color: #660066;">onkeyup</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #339933;">=</span> MD5<span style="color: #009900;">&#40;</span>email.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    gravatar.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://www.gravatar.com/avatar/'</span><span style="color: #339933;">+</span>key<span style="color: #339933;">+</span><span style="color: #3366CC;">'?s=80&amp;d=wavatar'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>Lines 3 and 5</strong> fetch references to our image and input box to control them with the rest of our JavaScript.</p>
<p><strong>Line 7</strong> sets an <code>onkeyup</code> handler on the email field that is fired when a key is released.</p>
<p><strong>Line 8</strong> uses the webtoolkit MD5 algorithm and generates a key. We then pass this key into the Gravatar&#8217;s source and it shows our image. If it&#8217;s all worked out, this is where you should be seeing your own lovely face <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The quicker the user can interact with your application the better. This script allows you to generate Gravatar&#8217;s on the fly as the user types it, giving them results straight away.</p>
<p>For practice, try adding things like event timers to decrease the number of image changes at a time, or set the event to <code>onblur</code> so that it only ever fires when the focus is taken away from the field (good for comment forms).</p>
<p>And also, with their recent addition of Gravatar profiles that contain even more information, maybe &#8211; with a little more JavaScript &#8211; the email address will be all you ever need to type.</p>
<p>Happy coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/load-gravatars-on-the-fly/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Randomising Database Output</title>
		<link>http://stephenmcintyre.net/blog/randomising-database-output/</link>
		<comments>http://stephenmcintyre.net/blog/randomising-database-output/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 21:13:47 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=294</guid>
		<description><![CDATA[Collecting a random data set from your database can be useful for all kinds of data driven applications including analytics and &#8220;grey areas&#8221; in video game outcomes  (battles, gambling, etc.).
Here I will discuss the use and methods of randomisation in PHP with data from a MySQL database.
Collecting the Data
First we need to grab our [...]]]></description>
			<content:encoded><![CDATA[<p>Collecting a random data set from your database can be useful for all kinds of data driven applications including analytics and &#8220;grey areas&#8221; in video game outcomes  (battles, gambling, etc.).</p>
<p>Here I will discuss the use and methods of randomisation in PHP with data from a MySQL database.<span id="more-294"></span></p>
<h3>Collecting the Data</h3>
<p>First we need to grab our database rows.</p>
<p>For this we uses a simple <code>SELECT</code> statement, where in this example is taken from a &#8220;users&#8221; table with two fields &#8220;ID&#8221; and &#8220;name&#8221;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> MySQLi<span style="color: #009900;">&#40;</span>DB_HOST<span style="color: #339933;">,</span> DB_USER<span style="color: #339933;">,</span> DB_PASS<span style="color: #339933;">,</span> DB_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT `ID`, `name` FROM `users` ORDER BY `ID` ASC&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ID'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' = '</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will print out all of the data from our database into an ordered list, arranged by ID from 1 upwards.</p>
<h3>Randomise with PHP</h3>
<p>To then take this data and shuffle its values, we can then take the conveniently named <a href="http://php.net/shuffle">shuffle</a> method in PHP and apply it to our <code>$rows</code> array.</p>
<p>Add this code to the above script at line 13. It takes the data and again prints it out into an ordered list, this time with a randomised data set.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Using the MySQL Query</h3>
<p>Now, since here we are already using a MySQL query, we can utilise the <a href="http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand">RAND()</a> method by adding it to our <code>$query</code> string.</p>
<p>This should be used as an alternative to the PHP shuffle method, so as such should not include the above line with shuffle in it (unless you want to randomise it twice &#8211; but it won&#8217;t make your data any more unique).</p>
<p>Replace your query string with this, on line 3.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT `ID`, `name` FROM `users` ORDER BY RAND()&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Notice it&#8217;s only the last part (<code>ORDER BY...</code>) that changes.</p>
<p>This code &#8211; or at least the idea &#8211; can be applied to, and is useful for, all programming languages. The syntax will vary between them but fundamentally the results will be the same.</p>
<p>Making a Facebook app? Why not award the user with a random item when they log in once a day, or show a rearranged list of their friends if it&#8217;s otherwise too large to show on a page.</p>
<p>Own a blog? Apply this to your homepage and show some old posts. It will also get their page rankings higher if they are being re-indexed by search engines. It&#8217;s like fresh content!</p>
<p>It will also help your visitors find posts they never knew they were looking for. &#8216;Cos we&#8217;re all just browsing after all <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/randomising-database-output/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UserTweets &#8211; A Simple PHP Twitter Class</title>
		<link>http://stephenmcintyre.net/blog/usertweets-ph-twitter-class/</link>
		<comments>http://stephenmcintyre.net/blog/usertweets-ph-twitter-class/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 21:20:04 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=445</guid>
		<description><![CDATA[For those that are unfamiliar with the Twitter API or are looking to quickly fetch tweets from a user&#8217;s profile, this class will help.
In PHP, all you do is include the class in your document and construct it with a Twitter username (with some options for extra tweaking).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
&#60;?php
class UserTweets
&#123;
  var $username = '';
  [...]]]></description>
			<content:encoded><![CDATA[<p>For those that are unfamiliar with the Twitter API or are looking to quickly fetch tweets from a user&#8217;s profile, this class will help.</p>
<p>In PHP, all you do is include the class in your document and construct it with a Twitter username (with some options for extra tweaking).<span id="more-445"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UserTweets
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$page_content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$encoded_content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$user_info</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$result_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$connected</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$request_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'since_id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'max_id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'count'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'page'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$option_set</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$option_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'screen_name='</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request_options</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$req</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$option_set</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$option_string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$req</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'='</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$req</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://api.twitter.com/1/statuses/user_timeline.json?'</span><span style="color: #339933;">.</span><span style="color: #000088;">$option_string</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page_content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connected</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page_content</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$connection</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connected</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connected</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result_count</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result_count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_info</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>Usage</h3>
<p>To fetch a user&#8217;s tweets with default settings (simple method).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$tweets</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UserTweets<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_bigSteve'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Setting options with names and values as defined in the <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline">Twitter API Wiki</a>. This example returns 5 results posted on or before the update with ID of 12402600172.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'max_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">12402600172</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'count'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tweets</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UserTweets<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_bigSteve'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Displaying tweets in an ordered list.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result_count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;Tweets&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$tweet</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&#8230;And user info. This data is stripped out of the <code>results</code> array and added into its own named <code>user_info</code> to reduce bloat.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result_count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;h1&gt;User Info&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_info</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$info</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' =&gt; '</span><span style="color: #339933;">.</span><span style="color: #000088;">$info</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This script will work on server setups with either the <a href="http://php.net/manual/en/book.curl.php">cURL</a> library or <a href="http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen">allow_url_fopen</a> turned on, taking preference over the faster and more secure cURL. If none are in use, most of the object properties will be empty (like <code>results</code> and <code>page_content</code>) and it will handle itself properly without printing errors to the page.</p>
<p>Hope this class will make it a bit easier for developers to start using the great <a href="http://apiwiki.twitter.com/Twitter-API-Documentation">Twitter API</a> in their own mashups.</p>
<p>Comments are welcome as always.</p>
<p>Have fun <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/usertweets-ph-twitter-class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>5 Steps to Spring Cleaning your Files</title>
		<link>http://stephenmcintyre.net/blog/5-steps-spring-clean-files/</link>
		<comments>http://stephenmcintyre.net/blog/5-steps-spring-clean-files/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 19:55:11 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[Tweaks]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=417</guid>
		<description><![CDATA[Last week I found a post on Crunch Gear, with a video guide on &#8220;Spring cleaning your PC&#8220;. He came to the realisation that his PC hadn&#8217;t been cleaned in a while, and sets about it with a can of compressed air for ten minutes.
Might have been useful taking this approach myself.
As good as it [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I found a post on Crunch Gear, with a video guide on &#8220;<a href="http://www.crunchgear.com/2010/04/09/video-spring-cleaning-time-for-your-pc/" target="_blank">Spring cleaning your PC</a>&#8220;. He came to the realisation that his PC hadn&#8217;t been cleaned in a while, and sets about it with a can of compressed air for ten minutes.</p>
<p>Might have been useful <a href="http://twitter.com/_bigSteve/status/11891317718" target="_blank">taking this approach myself</a>.</p>
<p>As good as it is to clean out all that collective dust (and I highly recommend that you do), that&#8217;s not what this article is for.</p>
<p>After the jump, I&#8217;ll show you how to clean up the <strong>files</strong> in your PC, using some very useful programs and tweaks to get rid of the junk that slows it down.<span id="more-417"></span></p>
<h3>Step 1: Viruses</h3>
<p>First off, you should run whatever anti-virus program you use to get rid of viruses, spyware and rootkits.</p>
<p>Most PCs sold nowadays are bundled with one you can use, so chances are you&#8217;ll have one of these at hand.</p>
<p>If you don&#8217;t have one, there are really good <strong>free</strong> alternatives available including <a href="http://filehippo.com/download_avg_antivirus/" target="_blank">AVG</a>. FileHippo is great for finding free software like this.</p>
<p>For AVG:</p>
<ul>
<li>Download, install and open</li>
<li>Go to &#8220;Computer scanner&#8221; tab</li>
<li>Click &#8220;Scan whole computer&#8221;</li>
</ul>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/04/avg-scan.png"><img class="aligncenter size-medium wp-image-422" title="avg-scan" src="http://stephenmcintyre.net/wp-content/uploads/2010/04/avg-scan-300x103.png" alt="" width="300" height="103" /></a></p>
<h3>Step 2: Unused Files</h3>
<p>Another great tool that I&#8217;ve used for years is <a href="http://filehippo.com/download_ccleaner/" target="_blank">CCleaner</a> by Piriform.</p>
<p>There are a lot of files that can clog up your system pretty quickly like your Recycle Bin and pretty much everything that your web browsers use. CCleaner takes all of that and removes it.</p>
<p>I have everything ticked to be cleaned up apart from &#8220;Wipe Free Space&#8221;, which takes a while to run. Something you don&#8217;t need every time you use the program.</p>
<p>As for the rest, you should find out what they mean   before you tick them to be deleted. The default settings should still   free up loads of space if you&#8217;re unsure.</p>
<ul>
<li>Download, install and open</li>
<li>Go to &#8220;Cleaner&#8221; tab</li>
<li>Tick every box apart from &#8220;Wipe Free Space&#8221;</li>
<li>(Optional) Click &#8220;Analyze&#8221; button to see how much space you&#8217;ll be freeing up</li>
<li>Click &#8220;Run Cleaner&#8221; button</li>
</ul>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/04/ccleaner-clean.png"><img class="aligncenter size-medium wp-image-427" title="ccleaner-clean" src="http://stephenmcintyre.net/wp-content/uploads/2010/04/ccleaner-clean-300x93.png" alt="" width="300" height="93" /></a></p>
<h3>Step 3: Cleaning the Registry</h3>
<p>Programs (and Windows itself) aren&#8217;t too clever in cleaning up after themselves within the <a href="http://en.wikipedia.org/wiki/Windows_Registry" target="_blank">registry</a>. It holds a lot of crap left behind from software uninstalls and file associations.</p>
<p>I&#8217;d go into how much I despise it but that&#8217;s for another day. For now we&#8217;ll just clean up the mess.</p>
<ul>
<li>Open CCleaner (following on from last step)</li>
<li>Go to &#8220;Registry&#8221; tab</li>
<li>Click &#8220;Scan for Issues&#8221; button</li>
<li>Click &#8220;Fix Selected Issues&#8230;&#8221; button when it&#8217;s made up the list</li>
<li>Click &#8220;Fix All Selected Issues&#8221; button</li>
</ul>
<p>Any dialogue boxes that come up will just be checking that you know what you&#8217;re doing. Of course you do, you&#8217;re the soldier with <a href="http://en.wikipedia.org/wiki/User_Account_Control" target="_blank">UAC</a> switched off!</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/04/ccleaner-reg.png"><img class="aligncenter size-medium wp-image-429" title="ccleaner-reg" src="http://stephenmcintyre.net/wp-content/uploads/2010/04/ccleaner-reg-300x102.png" alt="" width="300" height="102" /></a></p>
<h3>Step 4: Checking for Large Files</h3>
<p>Another application I like to use for this sort of thing is <a href="http://www.win.tue.nl/cgi-bin/usr/sequoia/download3.cgi" target="_blank">SequoiaView</a>. The site isn&#8217;t too friendly but you&#8217;re looking to download &#8220;SequoiaView  version 1.3 XP&#8221; (works on Windows 7 and Vista).</p>
<p>Basically, this collects file size information from all of the files in your hard drive and displays them as squares inside the window &#8211; so I would give it a minute to load.</p>
<p>It groups them into folders and sets different colours for types, making it easier to visualise your file system on-screen.</p>
<ul>
<li>Download, install and open</li>
<li>Select &#8220;C:\&#8221; from drop down menu</li>
<li>Click &#8220;Colorscheme on/off&#8221; button</li>
<li>Go to &#8220;View&#8221; &gt; &#8220;Options&#8230;&#8221; &gt; &#8220;Selection&#8221; and select &#8220;File and entire path&#8221;</li>
<li>Click &#8220;OK&#8221;</li>
</ul>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/04/sequoia-files.png"><img class="aligncenter size-medium wp-image-432" title="sequoia-files" src="http://stephenmcintyre.net/wp-content/uploads/2010/04/sequoia-files-300x136.png" alt="" width="300" height="136" /></a></p>
<h3>Step 5: Defrag</h3>
<p>Something you&#8217;ll want to do every once in a while is a disk defragmentation (not as much as the rest of the list, it takes forever).</p>
<p>Windows has a built in program for this, but if you want a nice interface (or an interface at all) and a folder-specific option, <a href="http://www.filehippo.com/download_defraggler/" target="_blank">Defraggler</a> is the way to go. It&#8217;s another Piriform product.</p>
<p>I&#8217;ve heard there aren&#8217;t much speed differences if you&#8217;re defragmenting a full drive, and if this one isn&#8217;t going to be used as often then you might as well just use the default Windows application.</p>
<p>Windows Defrag:</p>
<ul>
<li>Go to &#8220;Start&#8221; &gt; &#8220;All Programs&#8221; &gt; &#8220;Accessories&#8221; &gt; &#8220;System Tools&#8221; &gt; &#8220;Disk Defragmenter&#8221;</li>
<li>Select drive &#8220;C:\&#8221; and click &#8220;Defragment disk&#8221;</li>
</ul>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/04/defrag.png"><img class="aligncenter size-medium wp-image-433" title="defrag" src="http://stephenmcintyre.net/wp-content/uploads/2010/04/defrag-300x69.png" alt="" width="300" height="69" /></a></p>
<p>Defraggler:</p>
<ul>
<li>Download, install and open</li>
<li>Go to &#8220;Defrag&#8221; &gt; &#8220;Defrag&#8221; from button at bottom of window</li>
<li>If asked to empty Recycle Bin before you start, click &#8220;OK&#8221;</li>
</ul>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/04/defraggler.png"><img class="aligncenter size-medium wp-image-434" title="defraggler" src="http://stephenmcintyre.net/wp-content/uploads/2010/04/defraggler-300x108.png" alt="" width="300" height="108" /></a></p>
<p>And there you have it, five simple steps to prepare your PC for another year of hard torture.</p>
<p>Enjoy your nice, clean, faster computer!  <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/5-steps-spring-clean-files/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Handling Game State Management in C++</title>
		<link>http://stephenmcintyre.net/blog/c-plus-plus-state-management/</link>
		<comments>http://stephenmcintyre.net/blog/c-plus-plus-state-management/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 11:54:15 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=405</guid>
		<description><![CDATA[One thing that is initially overlooked in game design is handling menus and levels within the user interface. However, we know that it&#8217;s always going to be, and is, a crucial part to any successful game, as it helps provide the user with an extra level of control and eases general flow. It&#8217;s just a [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that is initially overlooked in game design is handling menus and levels within the user interface. However, we know that it&#8217;s always going to be, and is, a crucial part to any successful game, as it helps provide the user with an extra level of control and eases general flow. It&#8217;s just a case then of finding the code to do it.</p>
<p>This tutorial will cover a simple technique for handling <a href="http://wikipedia.org/wiki/State_management">state management</a> with enums or &#8220;<a href="http://wikipedia.org/wiki/Enumerated_type">enumerated types</a>&#8220;. If you have used state management or even just enums in C# before, then this will all feel very familiar to you. There are however some very subtle differences between the markup of the two languages.<span id="more-405"></span></p>
<p>First off, I&#8217;ll assume that you already have a program that runs inside of a window, ready for holding graphics and text. You should also have a main game loop that you use for any updates.</p>
<h3>Setting the Enum Type</h3>
<p>We start by setting our enum. It&#8217;s set like a structure, in the way that we can create an object with this enum as its type. It doesn&#8217;t hold any values itself though &#8211; the key is that it can only be set to certain values that you give it. I&#8217;ll cover this a bit better below.</p>
<p>This should go before your game loop (so that it doesn&#8217;t keep trying to set itself and cause errors).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">enum</span> eGameState <span style="color: #009900;">&#123;</span>GSM_MENU<span style="color: #339933;">,</span> GSM_LEVEL<span style="color: #339933;">,</span> GSM_END<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Looks a lot like a structure, or even an array.</p>
<p>The first part should be okay. It&#8217;s just setting our new enum type and giving it the name of &#8220;eGameState&#8221; so we can use it later.</p>
<p>The stuff inside the braces are the values that you want it to hold. These technically don&#8217;t mean anything, as they&#8217;re just given IDs from 0 upwards, i.e. GSM_MENU = 0, GSM_LEVEL = 1&#8230;</p>
<p>Basically, each item in that list is turned into a constant (which is why I like to use capitals). It is similar to declaring them separately with <a href="http://msdn.microsoft.com/en-us/library/teas0593%28VS.80%29.aspx">#define</a> or <a href="http://msdn.microsoft.com/en-us/library/07x6b05d%28VS.80%29.aspx">const</a>.</p>
<p>The constants shouldn&#8217;t be defined before they go into the enum, otherwise you&#8217;ll get an error. Creating the type will do it for you.</p>
<h3>Declaring the Variable</h3>
<p>We then create an instance as we would for a class or struct:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">eGameState gameState <span style="color: #339933;">=</span> GSM_MENU<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can set this to be whatever option you like from the list that you put into your game state type, inside the braces. Remember, ONLY the options from the list can be used (the ones in capitals).</p>
<h3>The Switch-Case</h3>
<p>To check our <code>gameState</code> var, we set up a <a href="http://wikipedia.org/wiki/Switch_statement#C.2C_C.2B.2B.2C_Java.2C_php.2C_ActionScript">switch statement</a>. It means we can check one variable against lots of values, which is what we want. Plus it saves us dealing with loads of really annoying &#8220;else if&#8221; statements.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>gameState<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">case</span> GSM_MENU <span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Menu state</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">case</span> GSM_LEVEL <span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Level state</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">case</span> GSM_END <span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// End state</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This goes inside our main game loop.</p>
<p>We then write whatever we like into each part and treat it as a new state. For example, if you check for a button press to go from the menu into your level area, you just change the value of <code>gameState</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>buttonPressed<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  gameState <span style="color: #339933;">=</span> GSM_LEVEL<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>When the game loop updates again, the switch will then find that the <code>gameState</code> has changed and go to the new screen.</p>
<p>If you already have a game set up without state management, you can replace the code inside your main loop with this, and insert your original code into the &#8220;Level state&#8221; section.</p>
<p>That should cover it. Any questions, leave some comments below or contact me on Twitter <a href="http://twitter.com/_bigSteve">@_bigSteve</a>.</p>
<p>Have fun <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/c-plus-plus-state-management/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
