/*
	Copyright (c) Art. Lebedev Studio | http://www.artlebedev.ru/
	Andrew Shitov | ash@design.ru
	20.12.2005
*/

var animationInterval = 45; /*15*/
var animationSteps = 10; 
var updateStep = 7; 

var person = '';

var openNow = 1;
var tobeOpen = 0;
var isCached = false;
var question = new Array();
var answer = new Array();
var currentQ = null;
var currentA = null;
var currentAHeight = 0;
var animationTimer = null;
var animationStep = 0;
var collapcingStep = 0;
var collapcingValue = 0;

document.onkeydown = NavigateThrough;

function WPtoggle (n)
{
	if (animationTimer) return; 

	CacheParts();

	if (n <= 0) n = answer.length - 1;
	else if (n >= answer.length) n = 1;

	if (openNow)
	{
		ClosePart (openNow);
		OpenPart (n);
		Animate();
	}
	else
	{
		ForceOpen (n);
	}
}

function CacheParts()
{
	if (isCached) return;

	for (var c = 1;; c++)
	{
		var currentQ = document.getElementById ('wpq-' + c);
		var currentA = document.getElementById ('wpa-' + c);
		if (!currentQ || !currentA) break;

		question[c] = currentQ;
		answer[c] = currentA;
	}
	isCached = true;
}

function UpdateFace (n)
{
	var faceHolder = document.getElementById ('WorkFace');
	if (faceHolder) faceHolder.src = '/studio/wokrplaces/' + person + '/i/' + n + '.jpg';
}

function ClosePart (n)
{		
	currentQ = question[n];
	currentA = answer[n];
	currentAHeight = answer[n].offsetHeight;
}

function OpenPart (n)
{
	tobeOpen = n;
}

function Animate()
{
	animationTimer = setInterval (AnimationStep, animationInterval);
	animationStep = 1;
	collapcingValue = 0;
	collapcingStep = parseInt (currentAHeight / animationSteps);
}

function AnimationStep()
{	
	if (++animationStep >= animationSteps)
	{
		clearInterval (animationTimer);
		animationTimer = null;
		animationStep = 0;		
		AnimationPostprocess();
	}
	else
	{	
		collapcingValue -= collapcingStep;
		MoveBlocks();

		if (animationStep == updateStep) UpdateFace (tobeOpen);
	}	
}

function AnimationPostprocess()
{
	for (var c = 1; c != question.length; c++)
	{
		question[c].style.top = '0';
		answer[c].style.top = '0';
	}

	currentA.className = 'invisible';	
	currentA.style.color = '#000000';	
	
	if (tobeOpen != openNow) answer[tobeOpen].className = 'visible answer';
	else tobeOpen = 0;

	openNow = tobeOpen;
	tobeOpen = 0;
}

function MoveBlocks()
{
	currentA.style.color = StepColor();
	for (var c = openNow + 1; c != question.length; c++)
	{
		question[c].style.top = collapcingValue + 'px';
		answer[c].style.top = collapcingValue + 'px';		
	}	
}

function StepColor()
{	
	var colorChar = dec2hex (parseInt (16 * animationStep / animationSteps));
	var ret = colorChar + colorChar + colorChar;
	
	return '#' + ret;	
}

function dec2hex (dec)
{
	var hexChars = "0123456789ABCDEF";
	dec %= 16;
	return hexChars.charAt (dec);
}

function ForceOpen (n)
{
	answer[n].className = 'visible answer';
	openNow = n;
}

function NavigateThrough (event)
{
	if (!document.getElementById) return;

	if (window.event) event = window.event;

	if (event.ctrlKey)
	{
		switch (event.keyCode ? event.keyCode : event.which ? event.which : null)
		{
			case 0x25:				
				WPtoggle (openNow - 1);
				break;
			case 0x27:
				WPtoggle (openNow + 1);
				break;
		}
	}			
}

function OpenWindow(uri, width, height, name) {
  window.open(uri,name,'width='+width+',height='+height+',status=yes,menubar=no,resizable=yes,scrollbars=1')
  return false;
}

