/* util.js - Utility Javascript for http://bitch-what.blogspot.com/
 * Copyright 2005-2006, (C) Paul William Tenny
 *
 * This code may not be used without permission, which can be asked for from
 * this address: http://pwtenny.googlepages.com/who.png
 */

/* Google Firefox ad for IE users only */
function google_firefox()
{
	if(navigator.userAgent.search("MSIE") == -1) return;
	document.write('                <table width="100%" style="border-bottom: 1px dotted #ccc;" id="mellow">');
	document.write('	                  <tr>');
	document.write('                    <td style="font-family: \'Times New Roman\'; font-size: .8em; padding: 6px;">Did you know that Internet Explorer 6 and IE7 are only capable of properly displaying <b style="color:#800000;">half</b> of today\'s web technology? Did you know that FireFox can display over 90% of those same web standards? What are you waiting for? <b style="color:#800000;">120,000,000</b> people can\'t be wrong. Oh, by the way, it\'s also safer too. -- Management</td>');
	document.write('                    <td valign="top" style="text-align: justify;">');
	document.write('                      <scr'+'ipt type="text/javascript"><!'+'--');
	document.write('                        google_ad_client = "pub-8290898144264971";');
	document.write('                        google_ad_width = 468;');
	document.write('                        google_ad_height = 60;');
	document.write('                        google_ad_format = "468x60_as_rimg";');
	document.write('                        google_cpa_choice = "CAAQ2YuYhAIaCL1G4OU2b5xnKOm293M";');
	document.write('                        google_ad_channel = "4754645961";');
	document.write('                      //--></sc'+'ript>');
	document.write('                      <scr'+'ipt type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">');
	document.write('                      </scr'+'ipt>');
	document.write('                    </td>');
	document.write('                  </tr>');
	document.write('                </table>');
}

/* Nobody truely knows what this does */
function getById(id,element)
{
	for(var divs = document.getElementsByTagName(element), a = 0; a < divs.length; a++)
		if(divs[a].getAttribute("id") == id) return (divs[a]);
}

/* Not used, but displays a random item from my cafepress store. */
function random_store_item()
{
	var divs = document.body.getElementsByTagName("div");
	for(var i = 0; i < divs.length; i++)
	{
		if(divs[i].getAttribute("blah") == "store")
		{
			var items = new Array();
			items[0] = "<a href=\"http://www.cafepress.com/pwtenny.54260073\"><img src=\"http://images.cafepress.com/product/54260073v2_150x150_B.jpg\" style=\"border: 0px;\"></a><br /><a href=\"http://www.cafepress.com/pwtenny\" style=\"text-decoration: underline; color: #999; font-weight: bold;\">The lonely store</a>"; // mug1
			items[1] = "<a href=\"http://www.cafepress.com/pwtenny.54560017\"><img src=\"http://images.cafepress.com/product/54560017v5_150x150_F.jpg\" style=\"border: 0px;\"></a><br /><a href=\"http://www.cafepress.com/pwtenny\" style=\"text-decoration: underline; color: #999; font-weight: bold;\">The lonely store</a>"; // tee1
			var a = Math.floor(Math.random() * 2);
			divs[i].innerHTML = items[a];
		}
	}
}

/* I don't know how well this works, but it seems to do a good job. */
function stripHTML(oldString)
{
  return oldString.replace(/<&#91;^>&#93;*>/g, "");
}

/* For showing/hiding elements */
function expandcollapse(postid)
{
  whichpost = document.getElementById(postid); 
  if(whichpost.className == "postshown") whichpost.className="posthidden";
  else whichpost.className = "postshown";
}

/* For being deep */
function random_quote()
{
   var things = new Array();
   things[0] = "\"His hand disappears inside a drawer and comes back with a rusted shank of a key you could die from if you looked at it too long. I run my hand across the worn out desktop dug out with knife gouges and smeared with old coffee stains until the key is in my hand and I'm shoving it into my pocket, right against that gun. I've got my hand wrapped around it now and I'm staring at the geezer with enough intent to burn his face off, but he won't look at me. What have I gotta do to get him to look at me?\" -- <a href=\"http://bitch-what.blogspot.com/2006/04/whats-your-story.html\">What's your story?</a>";
   things[1] = "\"Two men walk into a bar. The first man orders a scotch and soda, the second man remembers something he'd forgotten and it doubles him over with pain. He falls to the floor, shaking. And then through the floor and into the earth. He looks back up at the first man but he doesn't call out to him. They're not that close.\" -- Wesley Wyndam-Pryce";
   things[2] = "A persons reach should exceed their grasp.";
   things[3] = "\"Boy, I love when I can do that. My favorite thing in the world is when I write a script and it'll be six or seven minutes long, and I'll cut an entire story from the episode. And I've already got a head start on next week.\" -- Aaron Sorkin";
   things[4] = "\"I don't break the real rules. I break the fake TV rules. The real rules are the rules of drama, the rules that Aristotle talks about. The fake TV rules are the rules that, not good TV execs, but dumb TV execs will tell you: \"You can't do this, you've got to do that. You need three of these and five of those.\" Those things are silly.\" -- Aaron Sorkin";
   things[5] = "\"I run my hand across the worn out desktop dug out with knife gouges and smeared with old coffee stains until the key is in my hand and I'm shoving it into my pocket, right against that gun.\" -- <a href=\"http://bitch-what.blogspot.com/2006/04/whats-your-story.html\">What's your story?</a>";
   things[6] = "\"The Windows API is so broad, so deep, and so functional that most ISVs would be crazy not to use it. And it is so deeply embedded in the source code of many Windows apps that there is a huge switching cost to using a different operating system [...] It is this switching cost that has given the customers the patience to stick with Windows through all our mistakes, our buggy drivers, our high TCO, our lack of a sexy vision at times, and many other difficulties [...] Customers constantly evaluate other desktop platforms, [but] it would be so much work to move over that they hope we just improve Windows rather than force them to move. [...] In short, without this exclusive franchise called the Windows API, we would have been dead a long time ago.\" -- Microsoft General Manager for C++ development, Aaron Contorer";
   var i = Math.floor(Math.random() * 7 + 1);
   document.write(things[i-1]);
}

function expand_latest_story()
{
	var postId;

	for(var e = document.getElementsByTagName('div'), a = 0; a < e.length; a++)
	{
		if(e[a].className == 'blogPost')
		{
			postId = e[a].getAttribute('blah');
	
			for(var f = document.getElementsByTagName('span'), b = 0; b < f.length; b++)
			{
				if(f[b].className == 'fullpost')
				{
					f[b].style.display = 'block';
					document.getElementById('hidden_statline'+postId).style.display = 'block';
					document.getElementById('link'+postId).style.display = 'none';
/*					
					for(var g = document.getElementsByTagName('span'), c = 0; c < g.length; c++)
					{
						if(g[c].className == 'hidden_statline') g[c].style.display = 'block';
					}
*/
					return;
				}
			}
			
			// Found first blog post but its probably already expanded
			return;
		}
	}
}

function check_short_items()
{
	for(var posts = document.getElementsByTagName("div"), a = 0; a < posts.length; a++)
	{
		if(posts[a].className == "post")
		{
			if(posts[a].innerHTML.search("fullpost") == -1)	document.getElementById(posts[a].id+"_s").style.display = "none";
			else
			{
				for(var spn = posts[a].getElementsByTagName("span"), b = 0; b < spn.length; b++)
				{
					if(spn[b].className == "fullpost")
					{
						var w = countwords(spn[b].innerHTML);
						document.getElementById(posts[a].id+"_s").innerHTML = "<span class=\"pifs\"></span>&nbsp;Click the title for <b>"+w+"</b> more words.";
					}
				}
			}
		}
	}
}

function countwords(text)
{
	var bodyText = stripHTML(text);
	var words = 0;
	var arrayA;
	arrayA = bodyText.split(/\s+/);
	var regex = /[a-zA-Z0-9]/;

	for(var i = 0; i < arrayA.length; i++)
		if(arrayA[i].match(regex)) words++;

	return words;	
}