/////////////////////////   (c) Miso Antonic 2007   ///////////////////////////


  /////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////   Tools   //////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

function stankaa_getTime() { return Math.round((new Date()).getTime() / 1000); }

  /////////////////////////////////////////////////////////////////////////////
 /////////////////////////   Magic Animation 1.0   ///////////////////////////
/////////////////////////////////////////////////////////////////////////////

// Animation styles used in Magic /////////////////////////////////////////////

function stankaa_styleLinear(x) { return x; }
function stankaa_styleSin(x) { return Math.sin(x*Math.PI/2); }
function stankaa_stylePulse(x) { return (Math.sin(x*2*Math.PI) + 1) / 2; }
function stankaa_styleBounce(x) { return (x == 0) ? 0 : (((-Math.sin(x*25)/(x*25))+1)*(1-x))+x; }
function stankaa_styleBounceLess(x) { return (x == 0) ? 0 : (((-Math.sin(x*15)/(x*15))+1)*(1-x))+x; }

// Magic Animation Core ///////////////////////////////////////////////////////

stankaa_magic = new function ()
{
	this.effects = [];
	this.interval;
	this.frequency = 30;
	
	this.remove = function(effect)
	{
		var i = 0;
		while (i < this.effects.length) 
		  if (this.effects[i] == effect) 
		    this.effects.splice(i, 1);
		  else i++;
				
		if (this.effects.length == 0) 
		  window.clearInterval(this.interval);
	}
	
	this.tick = function()
	{
		for (var i=0; i<this.effects.length; i++)			
			this.effects[i].tick();
	}

	this.add = function(effect)
	{
		this.effects.push(effect);
		if (this.effects.length == 1)
			this.interval = window.setInterval(function() {stankaa_magic.tick();}, this.frequency);	
	}
}


function stankaa_effectObject(elementId, ticksToStart, speed, action, style, loop, coAction, endAction) 
{		
	this.elementId = elementId;
	this.ticksToStart = ticksToStart;
	this.speed = speed;
	this.now = 0;
	this.action = action;
	this.coAction = coAction;
	this.endAction = endAction;
	this.loop = (loop == undefined) ? false : loop;
	this.style = style;
	this.tick = function()
	{
		if (this.ticksToStart > 0)
		{
			this.ticksToStart--;
			return;
		}
		
		if (this.now > 100)
		{
			if (this.loop)
				this.now = 0;
			else
			{
				stankaa_magic.remove(this);				
				if (this.endAction != undefined) 
					this.endAction();
			}
		}
		else
		{
			if (document.getElementById(elementId) == null)
			{
				stankaa_magic.remove(this);
				return;
			}			
			this.action();
			
			if (this.coAction != undefined)
				this.coAction();
			
			this.now += this.speed;
			if (this.now > 100) this.now = 101;			
		}
	}
	
	stankaa_magic.add(this);
}

// Magic Effects //////////////////////////////////////////////////////////////


function stankaa_changeNumber(elementId, what, suffix, value1, value2, speed, ticksToStart, style, loop, coAction, endAction)
{
	action = function()
	{
		this.element.style[this.what] = Math.round(this.value1 + this.style(this.now/100)*(value2-value1)) + suffix;
	}
	effect = new stankaa_effectObject(elementId, ticksToStart, speed, action, style, loop, coAction, endAction);
	effect.element = document.getElementById(elementId);
	effect.what = what;
	effect.value1 = value1;
	effect.value2 = value2;
}

function stankaa_fade(elementId, opacity1, opacity2, speed, ticksToStart, style, loop, coAction, endAction)
{
	action = function()
	{		
		this.element.style.opacity = this.opacity1 + this.style(this.now/100) * (this.opacity2-this.opacity1);
		this.element.style.filter = "alpha(opacity=" + this.element.style.opacity*100 + ")";
	}
	effect = new stankaa_effectObject(elementId, ticksToStart, speed, action, style, loop, coAction, endAction);
	effect.element = document.getElementById(elementId);
	effect.opacity1 = opacity1;
	effect.opacity2 = opacity2;
}

function stankaa_move(elementId, top1, left1, top2, left2, speed, ticksToStart, style, loop, coAction, endAction)
{	
	stankaa_changeNumber(elementId, "top", "px", top1, top2, speed, ticksToStart, style, loop, coAction, endAction);
	stankaa_changeNumber(elementId, "left", "px", left1, left2, speed, ticksToStart, style, loop);
}

function stankaa_scale(elementId, width1, height1, width2, height2, speed, ticksToStart, style, loop, coAction, endAction)
{
	stankaa_changeNumber(elementId, "width", "px", width1, width2, speed, ticksToStart, style, loop, coAction, endAction);
	stankaa_changeNumber(elementId, "height", "px", height1, height2, speed, ticksToStart, style, loop);
}

function stankaa_morph(element1Id, element2Id, width1, height1, width2, height2, speed, ticksToStart, style, loop, coAction, endAction)
{
	stankaa_scale(element1Id, width1, height1, width2, height2, speed, ticksToStart, style, loop, coAction, endAction);
	stankaa_scale(element2Id, width1, height1, width2, height2, speed, ticksToStart, style, loop);
    stankaa_fade(element1Id, 1, 0, speed, ticksToStart, style, loop);
    stankaa_fade(element2Id, 0, 1, speed, ticksToStart, style, loop);
}

  /////////////////////////////////////////////////////////////////////////////
 //////////////////////////   Stankaa Magic Effects   ////////////////////////
/////////////////////////////////////////////////////////////////////////////

function stankaa_removeElementById(id)
{
	var element = document.getElementById(id);
	element.parent.removeChild(element);
}

///////////////////////////////////////////////////////////////////////////////

function stankaa_effectPulseButton() { stankaa_fade("stankaa_pulsebutton", 0, 1, 2, 0, stankaa_stylePulse, true); } // speed = 2
function stankaa_effectScrollText() { document.getElementById("stankaa_greetings").style.left = Math.floor(Math.random()*1250-1000) + "px"; } //speed = 0.1

function stankaa_effectTransform(welcome)
{
	var $ = function(id) { return document.getElementById(id); };
	var speed = 5;

	// Fade away the login form
	$("stankaa_name").blur();
	stankaa_fade("stankaa_loginform_elements", 1, 0, speed*2, 0, stankaa_styleSin);
	
	// Expand the gray area
	stankaa_changeNumber("stankaa_loginform_container", "top", "px", 150, 0, speed, 0, stankaa_styleBounce);	
	stankaa_changeNumber("stankaa_loginform_container", "height", "px", 130, 290, speed, 0, stankaa_styleBounce, false, null, 
		function() { // when area is ready, continue transformation
		
			$("stankaa_container").style.backgroundPosition = "-250px 0px";
	
			var conversation = $("stankaa_loginform_container");
			conversation.removeChild($("stankaa_loginform_elements"));
			conversation.innerHTML += welcome;
			conversation.setAttribute("id", "stankaa_conversation");
			conversation.style.position = "relative";
			conversation.style.textAlign = "left";
			
			var talkServer = $("stankaa_server");
			conversation.style.height = talkServer.offsetHeight + 20 + "px";
			
			stankaa_morph("stankaa_photo", "stankaa_server", 117, 117, talkServer.offsetWidth, talkServer.offsetHeight, speed/2, 0, stankaa_styleBounce); 
			
			
			stankaa_move("stankaa_photo", 10, 68, 10, 7, speed/2, 0, stankaa_styleBounce, false, null, 
				function () { $("stankaa_content").removeChild($("stankaa_photo")); });

			stankaa_changeNumber("stankaa_server", "marginLeft", "px", -59, -talkServer.offsetWidth/2-5, speed/2, 0, stankaa_styleBounce, false, null,  
				function () { 
					stankaa_resetIdleTimer();
					talkServer.removeAttribute("id");
					$("stankaa_sentence").removeAttribute(stankaa_disabled);			
					$("stankaa_sentence").focus();
				});
			
			$("stankaa_bottom").innerHTML = 
				'<form id="stankaa_sentenceform" method="post" action="">' +
						'<input type="text" id="stankaa_sentence" name="stankaa_sentence" size="30" value="" onKeyPress="return stankaa_submitNewSentence(this, event)" ' + stankaa_disabled + '="' + stankaa_disabled + '" />' +
						'<input type="hidden" id="stankaa_name" name="stankaa_name" value="' + stankaa_name + '" />' +
				'</form>'; 
		});
	
	// Removes greetings and bottom text
	$("stankaa_content").removeChild($("stankaa_greetings"));
	$("stankaa_bottom").innerHTML = "";
}

function stankaa_effectShutdown()
{
	var $ = function(id) { return document.getElementById(id); };
	var speed = 5;
	
	stankaa_clearIdleTimer();
	
	stankaa_changeNumber("stankaa_sentenceform", "marginTop", "px", 0, -100, speed, 0, stankaa_styleSin, false, null, null
		/*function () {$("stankaa_bottom").removeChild($("stankaa_sentenceform"));}*/);
	
	stankaa_changeNumber("stankaa_bottom", "height", "px", 35, 65, speed*2, 10, stankaa_styleSin, false, null, 
		function () {
			$("stankaa_conversation").getElementsByTagName("div")[0].style.marginTop = "60px";
			$("stankaa_conversation").style.height = $("stankaa_conversation").offsetHeight + 60;
			$("stankaa_content").scrollTop += 60;
			
			$("stankaa_bottom").style.lineHeight = "18px";
			$("stankaa_bottom").innerHTML = "<div id='stankaa_goodbye'>Stankaa has ended the conversation. <ul class='stankaa_list'><li><a href='' onclick='setTimeout(stankaa_submitLoad, 3000);'><b>Talk</b></a> again</li><li><a href='http://www.stankaa.com'><b>Place</b></a> Stankaa on your webpage.</li></ul></div>";
			
		}); 
}

function stankaa_effectAddSentences(shutdown, ads)
{
	var $ = function(id) { return document.getElementById(id); };
	var speed = 5;

	var talkServer = $("stankaa_server");
	var talkClient = $("stankaa_client");
	
	var oldHeight = $("stankaa_conversation").offsetHeight;
	var newHeight = talkServer.offsetTop + talkServer.offsetHeight + (shutdown ? 65 : $("stankaa_bottom").offsetHeight);//    oldHeight + talkServer.offsetHeight + talkClient.offsetHeight + 19;
	
	stankaa_changeNumber("stankaa_conversation", "height", "px", oldHeight, newHeight, speed*2, 0 , stankaa_styleSin, false, 
		function () { with ($("stankaa_content")) { scrollTop = scrollHeight; }; },
		function () {
			stankaa_scale("stankaa_client", talkClient.offsetWidth, 0, talkClient.offsetWidth, talkClient.offsetHeight, speed, 0, stankaa_styleSin);
			stankaa_changeNumber("stankaa_client", "marginTop", "px", 100, 0, speed*3, 0, stankaa_styleSin);
			stankaa_fade("stankaa_client", 0, 1, speed, 0, stankaa_styleSin, false, null, 
				function () { $("stankaa_client").removeAttribute("id"); });
			
			stankaa_changeNumber("stankaa_server", "marginLeft", "px", 0, -talkServer.offsetWidth/2-5, speed, 15, stankaa_styleBounceLess);						
			stankaa_changeNumber("stankaa_server", "height", "px", 30, talkServer.offsetHeight, speed, 15, stankaa_styleBounceLess);
			stankaa_fade("stankaa_server", 0, 1, speed, 15, stankaa_styleBounceLess, false, null, 
				function () { 
					$("stankaa_server").removeAttribute("id");
					$("stankaa_sentence").removeAttribute(stankaa_disabled);
					$("stankaa_sentence").focus();					
					
					if (shutdown) 
						stankaa_effectShutdown();
					else
						stankaa_resetIdleTimer();
					
					if (ads != "")
					{
						$("stankaa_top").innerHTML = ads;
						stankaa_changeNumber("stankaa_top", "height", "px", 0, 65, speed*2, 10, stankaa_styleSin);
					}
					else
					{
						if ($("stankaa_top").style.height == "65px")
							stankaa_changeNumber("stankaa_top", "height", "px", 65, 0, speed*2, 10, stankaa_styleSin);
					}
					}); });
}


  //////////////////////////////////////////////////////////////////////////////
 //////////////////////////// Other interface effects /////////////////////////
//////////////////////////////////////////////////////////////////////////////

function stankaa_resetIdleTimer()
{
	stankaa_clearIdleTimer();
	stankaa_idleTimer = setTimeout(stankaa_submitIdle, 20000);
}

function stankaa_clearIdleTimer()
{
	if (stankaa_idleTimer != undefined)
		clearTimeout(stankaa_idleTimer);
}

  //////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////   Ajax   ///////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function stankaa_send(message, callback)
{
	stankaa_callback = callback;
	stankaa_script = document.createElement("script");
	stankaa_script.src = "http://www.stankaa.com/system.php?" + message;
	document.getElementsByTagName("head")[0].appendChild(stankaa_script);
}

function stankaa_receive(data)
{
	document.getElementsByTagName("head")[0].removeChild(stankaa_script);
	stankaa_callback(data);
}

function stankaa_submitLoad()
{			
	stankaa_send("cache=" + Math.floor(Math.random()*10000),
		function (data) {
			stankaa_idleTimer = undefined;
			document.getElementById("stankaa").innerHTML = data;

			// Adds a CSS link to a client site
			if (document.createStyleSheet)
				document.createStyleSheet("http://www.stankaa.com/style/style.css");
			else
			{
				var linkElement = document.createElement('link');
				linkElement.rel = 'stylesheet';
				linkElement.href = 'data:text/css,' + escape("@import url('http://www.stankaa.com/style/style.css');");
				document.getElementsByTagName("head")[0].appendChild(linkElement);
			}		
			
			stankaa_effectPulseButton();
			stankaa_effectScrollText(); 
			});	
}

function stankaa_submitNewSentence(myfield, e)
{	
	stankaa_resetIdleTimer();
	var $ = function(id) { return document.getElementById(id); };
	var key = (window.event)?(window.event.keyCode):((e)?(e.which):(0));
	
	if (key == 13 && myfield.value != "")
	{
		stankaa_clearIdleTimer();
	    $("stankaa_sentence").setAttribute(stankaa_disabled, stankaa_disabled);
		
		stankaa_send("sentence=" + $("stankaa_sentence").value + "&name=" + $("stankaa_name").value + "&time=" + stankaa_getTime(),
			function (data) {
				var responseArray = data.split("|");
				$("stankaa_conversation").innerHTML += responseArray[0];
				$("stankaa_sentence").value = '';
				stankaa_effectAddSentences(responseArray[1] == "shutdown", responseArray[2]); });
	}
	return key != 13;
}

function stankaa_submitIdle()
{
	var $ = function(id) { return document.getElementById(id); };
	if ($("stankaa_sentence") == undefined) return;
	$("stankaa_sentence").setAttribute("readonly", "readonly");		
	stankaa_send("sentence=&name=" + $("stankaa_name").value + "&time=" + stankaa_getTime() + "&idle=true",
		function (data) {
			var responseArray = data.split("|");
			$("stankaa_conversation").innerHTML += responseArray[0];
			stankaa_effectAddSentences(responseArray[1] == "shutdown", responseArray[2]); });
}

function stankaa_submitLogin(myfield, e)
{	
	stankaa_name = document.getElementById("stankaa_name").value;
	var key = (window.event)?(window.event.keyCode):((e)?(e.which):(0));
	if (stankaa_name !="" && (key == 13 || myfield.name != "stankaa_name"))
	{	
		stankaa_name = stankaa_name.replace(/[^a-zA-Z0-9 _]+/g, '').substring(0, 15);
		stankaa_send("login=true&name=" + stankaa_name + "&time=" + stankaa_getTime(),
			function (data) { 
				stankaa_effectTransform(data); });
	}
	return key != 13;
}

/////////////////////////////   Load the login window   //////////////////////////////
stankaa_disabled = (navigator.appName == "Microsoft Internet Explorer" ? "disabled" : "readonly");
stankaa_callback = undefined;
stankaa_submitLoad();