var pulsers = new Array();
var bouncers = new Array();
var wordsIns = new Array();
var exclaimers = new Array();
var shakers = new Array();

function icing (){
	var pulserElms = getElementsByClassName("pulse",document.body);
	for(var i=0;i<pulserElms.length;i++)
		new Pulser(pulserElms[i]);
	var bouncerElms = getElementsByClassName("bounce",document.body);
	for(var i=0;i<bouncerElms.length;i++)
		new Bouncer(bouncerElms[i]);
	var wordInElms = getElementsByClassName("words_in",document.body);
	for(var i=0;i<wordInElms.length;i++)
		new WordsIn(wordInElms[i]);
	var exclaimerElms = getElementsByClassName("exclaim",document.body);
	for(var i=0;i<exclaimerElms.length;i++)
		new Exclaim(exclaimerElms[i]);
	var shakerElms = getElementsByClassName("shake",document.body);
	for(var i=0;i<shakerElms.length;i++)
		new Shake(shakerElms[i]);
};

function Pulser(elm)
{
	this.elm = elm;
	this.range = 10;
	if(this.elm.currentStyle)
		var color = this.elm.currentStyle.color;
	else
		var color = document.defaultView.getComputedStyle(this.elm,null).getPropertyValue('color');
	this.color = new Color(color);
	this.id = pulsers.length;
	this.pulse = function(curVal,dir)
	{
		if((curVal<30) || (curVal > 160))
			dir*=-1;
		curVal += dir;
		this.elm.style.color = 'rgb('+parseInt(this.color.r*curVal/100)+','+parseInt(this.color.g*curVal/100)+','+parseInt(this.color.b*curVal/100)+')';
		setTimeout('pulsers[' + this.id + '].pulse(' + curVal + ',' + dir + ')',20);
	}
	pulsers[this.id] = this;
	setTimeout('pulsers[' + this.id + '].pulse(100,-10)',10);
}

function Color(color)
{
	if(color.indexOf('#')==0)
	{
		if(color.length==4)
			color = '#' + color.charAt(1) + color.charAt(1) + color.charAt(2) + color.charAt(2) + color.charAt(3) + color.charAt(3)
		this.r = eval('0x' + color.substr(1,2));
		this.g = eval('0x' + color.substr(3,2));
		this.b = eval('0x' + color.substr(5,2));
	}
	else
	{
		var colArr = color.split(',');
		this.r = parseInt(colArr[0].match(/\d+/)[0]);
		this.g = parseInt(colArr[1]);
		this.b = parseInt(colArr[2].match(/\d+/)[0]);
	}
}

function Bouncer(elm)
{
	this.elm = elm;
	this.range = 7;
	this.id = bouncers.length;
	this.steps = 25;
	this.bounce = function(curVal,dir)
	{
		curVal += Math.PI/this.steps;
		if(curVal >= Math.PI)curVal = 0;
		this.elm.style.backgroundPosition = (Math.sin(curVal) * this.range) + 'px 0px';
		setTimeout('bouncers[' + this.id + '].bounce(' + curVal + ',' + dir + ')',25);
	}
	bouncers[this.id] = this;
	setTimeout('bouncers[' + this.id + '].bounce(0,-1)',300);
}

function WordsIn(elm)
{
	this.elm = elm;
	this.id = wordsIns.length;
	this.steps = 25;
	this.text = elm.innerHTML;
	this.words = this.text.split(" ");
	this.write = function(curVal)
	{
		var str = "";
		for(var i=0;i<curVal;i++)
		{
			if(str!="")str+=" ";
			str += this.words[i];
		}
		if(str=="")str = "&nbsp;";
		this.elm.innerHTML = str;
		if(++curVal <= this.words.length)
			setTimeout('wordsIns[' + this.id + '].write(' + curVal + ')',150 + Math.random()*150);
		else
			setTimeout('wordsIns[' + this.id + '].write(0)',2000);
	}
	wordsIns[this.id] = this;
	setTimeout('wordsIns[' + this.id + '].write(0)',1000);
}

function Exclaim(elm)
{
	this.elm = elm;
	this.maxLength=5;
	this.text = this.elm.innerHTML;
	if(this.text.indexOf("!") == this.text.length-1)
		this.text = this.text.substr(0,this.text.length-1);
	this.write = function(curVal)
	{
		if(curVal>=this.maxLength)
			curVal = 0;
		var str = this.text;
		for(var i=1;i<=curVal+1;i++)
			str += "!";
		this.elm.innerHTML = str;
		setTimeout('exclaimers[' + this.id + '].write(' + ++curVal + ')',200);
	}
	exclaimers[this.id] = this;
	setTimeout('exclaimers[' + this.id + '].write(0)',10);
}
document.write('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>');
document.write('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>');
checkJQuery();

var numDrops,lastBounce;
function checkJQuery()
{
	try
	{
		if(typeof $ != 'undefined')
		{
			//Start random bouncers
			setTimeout("RandomBounce('.random_bounce')",6000);
		}
		else
		{
			setTimeout('checkJQuery()',100);
		}
	}
	catch(ex)
	{
	}
}

function Shake(selector)
{
	$(selector).effect("shake",{
			times: 8,
			distance: 2
		},25);
	setTimeout("Shake('" + selector + "')",2500 + parseInt(Math.random() * 2000));
};

function RandomBounce(selector)
{
	var elms = $(selector);
	var elm = elms[parseInt(Math.random() * elms.length)];
	if((lastBounce!=elm) && elm)
		$(elm).effect("bounce",{times: 3,distance: 6},250);
	lastBounce = elm;
	setTimeout("RandomBounce('" + selector + "')",2000 + parseInt(Math.random() * 2000));
};

function link(link, bool)
{ 
	if(bool)
	{
		window.open(link, '_blank');
	}else{
        top.location=link;
    }
}

//Since IE doesn't support this as a method of document, we need our own
function getElementsByClassName(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
            node = document;
    if ( tag == null )
            tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
            if ( pattern.test(els[i].className) ) {
                    classElements[j] = els[i];
                    j++;
            }
    }
    return classElements;
};

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  }
};

//window.onload = icing;
addEvent(window,'load',icing);