//Prototype sucks, who needs 150k .js file?
function gd(e) { return document.getElementById(e); }
function gs(e) { return document.getElementById(e).style; }
function gv(e) { return document.getElementById(e).value; }
function dw(i) { return document.write(i); }

function ClearValue(value,theid) { if (value != "") gd(theid).value = ""; }
function goUrl(what) { if(what[what.selectedIndex].value != ''){ document.location.href = what[what.selectedIndex].value; } }
function popit(id) { if (gs(id).display == "none"){ gs(id).display = "block"; } else { gs(id).display = "none"; } }

$(document).ready(function(){
	/*
	http://code.google.com/p/jqueryjs/downloads/list
	Source: http://www.kriesi.at/archives/create-a-multilevel-dropdown-menu-with-css-and-improve-it-via-jquery
	Source: http://snook.ca/archives/javascript/jquery-bg-image-animations/
	*/

	//No evil framing
	if (window.top !== window.self) window.top.location.href = window.self.location.href;

	//Toolbar
	$("#toolbar").mouseover(function(){
		$(this).stop().animate({"top": "0px"},700);
	}).mouseout(function(){
		$(this).stop().animate({"top": "-37px"},700);
	})

	//Ghetto fix to avoid IE7 and IE6 - http://groups.google.com/group/jquery-dev/browse_thread/thread/1f693c5f4e8ea650/f49280e7958bac7d
	if (document.addEventListener)
	{
		//Helper
		$("#helper ul").css("display","none");
		$("#helper li").hover(function(){
			$(this).find("ul:first:hidden").css({visibility: "visible", display: "none"}).fadeIn(300);
		},function(){
			$(this).find("ul:first").css("display","none");
		});

		//Navigation
		$("#nav ul div").css("display", "none");
		$("#nav li").hover(function(){
			$(this).find("div:first:hidden").css({visibility: "visible", display: "none"}).fadeIn(400);
		},function(){
			$(this).find("div:first").css("display","none");
		});

		//Code
		$("#content code").ToCode();
	}

	//Textboxes - @IE7, IE6
	if (document.attachEvent)
	{
		var el = "#content input[type='text'],input[type='password'],textarea";
		$(el).addClass("blur");
		$(el).blur(function(){
			$(this).removeClass().addClass("blur");
		}).focus(function(){
			$(this).removeClass().addClass("focus");
		});
	}	

	//Actions
	$(".actions ul").css("display","none");
	$(".actions li").hover(function(){
		$(this).find("ul:first:hidden").css({visibility: "visible", display: "none"}).fadeIn(300);
	},function(){
		$(this).find("ul:first").css("display","none");
	});	
	
	//External links Google Analytics tracking
	$("a[href^='http://']").click(function(){
		pageTracker._trackEvent("links", "external", $(this).attr("href"), 0)
	});	
});

(function ($) {
$.fn.ToCode = function() {
	return this.each(function(i){
		var line = $(this).html().replace(/\n/g, "<li>").trim();
		if (line.match("<li>") !== "<li>") tmp = "<li>" + line + "<\/li>";
		$(this).html("<ol class=\"code\">" + tmp + "<\/ol>");		
		//$(".code li:nth-child(odd)").addClass("alt");
		$(".code li:first-child, .code li:last-child").css("display","none");
	});
};
})(jQuery);

//http://www.somacon.com/p355.php
String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}

function ShowMore(selector,height)
{
	$(selector).each(function(i){
		var originalHeight = $(this).outerHeight() + 35;
		var uniqueID = "showmore-unique" + i;
		$(this).attr("id",uniqueID);

		if(originalHeight > height)
		{
			$(this).css({"height": height, "overflow": "hidden"});
			$(this).after('<a href="'+selector+'" onclick="return false;" rel="'+originalHeight+'" class="readmore">Read More</a>');
			$(".readmore").fadeTo(0, 0.5);

			$("#"+uniqueID + " + a.readmore").click(function(){
				$("#"+uniqueID).animate({height: $(this).attr("rel")+"px"}, 1500);
				$(this).fadeOut(300);
			}).mouseover(function(){
				$(this).fadeTo(200, 1.0);
			}).mouseout(function(){
				$(this).fadeTo(200, 0.5);
			});
		}
	});
}

var youTubePlayerLoggers = {}; //http://groups.google.com/group/youtube-api-gdata/browse_thread/thread/e8a8c85b801b9e25
var totalvideos = 0;
var videoid = new Array();
var videoplayed = new Array();

function onYouTubePlayerReady(id)
{
	var video = document.getElementById(id);
		
	//State constants
	var unstarted = -1;
	var ended 	  = 0;
	var playing   = 1;
	var paused    = 2;
	var buffering = 3;
	var cued      = 5; //Page loaded, video has not been played yet
	
	//Video array default values being set for first time
	videoid[totalvideos] = id;
	videoplayed[totalvideos] = false;
	
	totalvideos = totalvideos + 1;	
	
	youTubePlayerLoggers[id] = function(state)
	{		
		var el = document.getElementById(id);		
		var url = "http://www.youtube.com/watch?v=" + getYoutubeVideoID(el.getVideoUrl());		
		var arrayIndex = arraySearch(id);	
		var currentTime = Math.ceil(el.getCurrentTime());
		
		if (state == playing)
		{
			if (videoplayed[arrayIndex] == false)
			{
				pageTracker._trackEvent('videos', 'unique_play_per_page', url, 0);
			}

			pageTracker._trackEvent('videos', 'play', url, 0);
			
			//Flag video as having been played once
			videoplayed[arrayIndex] = true;
		}
		
		if (state == ended)
		{		
			pageTracker._trackEvent('videos', 'ended', url, currentTime);
		}		
		
		if (state == paused)
		{			
			pageTracker._trackEvent('videos', 'paused', url, currentTime);
		}
	};
	
	video.addEventListener("onStateChange", "youTubePlayerLoggers."+id);	
	video.addEventListener('onError', 'onPlayerError');
}

function onPlayerError(errorCode)
{
	pageTracker._trackEvent('videos', 'error', errorCode, 0);
}

function getYoutubeVideoID(url)
{
	var urlArr = url.split("v=");
	var urlArr2 = urlArr[1].split("&");			
	return urlArr2[0];	
}

function arraySearch(what)
{
	for (var i=0, len=totalvideos; i<len; ++i)
	{
		if(videoid[i].indexOf(what) >= 0) return i;
	}
}

// http://www.alistapart.com/articles/relafont - http://www.alistapart.com/articles/alternate/
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);