var turnRunning = new Object();
var turnPending = new Object();
var turnDirection = new Object();

var maxFrontBox = 36;

function cancleBubbling()
{
	if (!e) var e = window.onmouseover;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	if (!e) var e = window.onmouseout;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function handleFilter(data)
{
	i = 1;
	for (var entry in data)
	{
		// APPLY THE STYLES TO THE PAGE
		/*
		if( $("#stack"+i+"ContainerLi").attr("class") != data[entry].cssClass1 )
		{
			if(data[entry].cssClass1 == "projectBox")
			{
				// $("#stack"+i+"ContainerLi").switchClass("projectBoxHidden", data[entry].cssClass1, 250);
				$("#stack"+i+"ContainerLi").show( 250);
			}
			else
			{
				// $("#stack"+i+"ContainerLi").switchClass("projectBox", data[entry].cssClass1, 250);
				$("#stack"+i+"ContainerLi").hide( 250);
			}
		}
		*/
		tmpW = $("#stack"+i+"ContainerLi").attr("width");
		if(data[entry].cssClass1 == "projektBox")
		{
			// SHOW THE BOX
			if(tmpW=="0px" || typeof(window[tmpW]) == "undefined")
			{
				// $("#stack"+i+"ContainerLi").switchClass("projektBoxHidden", "projektBox", 750);
				$("#stack"+i+"ContainerLi").animate({width:'221px',marginRight:'32px'},500);
			}
		}
		else
		{
			// HIDE THE BOX
			// $("#stack"+i+"ContainerLi").switchClass("projektBox", "projektBoxHidden", 750);
			if(tmpW=="221px" || typeof(window[tmpW]) == "undefined")
			{
				$("#stack"+i+"ContainerLi").animate({width:'0px',marginRight:'0px'},500);
			}
		}
		i++;
	}
}

function registerStartTurn(obj)
{
	// START THE TURN ONLY, IF THE BOX DISPLAYS THE FRONT
	if(obj.className == "frontBoxGeneral frontBox0")
	{
		// if(document.getElementById(obj.id+"ContainerLi").className!="projectBoxHidden")
		// {
			// REGISTER THE TURN ONLY, IF IT'S NOT ALLREADY REGISTERED
			if(!turnPending[obj.id])
			{
				// SET THE DIRECTION TO SHOW THE BACK
				turnDirection[obj.id] = "showBack";
				// START THE TURN DELAYED
				// turnPending[obj.id] = window.setTimeout(function() { startTurn(obj); }, 250);
				turnPending[obj.id] = window.setTimeout(function() { startTurn(obj); }, 250);
			}
		// }
	}
}

function registerStopTurn(obj)
{
	// STOP A PENDING TURN
	turnPending[obj.id] = false;
	// IF THE BOX IS NOT ALLREADY DISPLAYING THE FRONT
	if(obj.className != "frontBoxGeneral frontBox0")
	{
		// CHANGE THE DIRECTION TO SHOW THE FRONT
		turnDirection[obj.id] = "showFront";
		// IF THE TURN IS NOT RUNNING ALLREADY, START IT 
		if(!turnRunning[obj.id])
		{
			turnRunning[obj.id] = window.setInterval(function() { runTurn(obj); }, 10);
		}
	}
}

function getTurnDirection(obj)
{
	if(obj.className == "frontBoxGeneral frontBox0")
	{
		turnDirection[obj.id] = "showBack";
	}
	if(obj.className == "frontBoxGeneral frontBox"+maxFrontBox+"")
	{
		turnDirection[obj.id] = "showFront";
	}
}



function startTurn(obj)
{
	// START THE TURN ONLY, IF IT'S A PENDING TURN
	// PENDING MIGHT BE DELETED BY registerStopTurn() ONMOUSEOUT
	if(turnPending[obj.id])
	{
		turnPending[obj.id] = false;
		turnRunning[obj.id] = window.setInterval(function() { runTurn(obj); }, 10);
	}
}

function runTurn(obj)
{
	opacityStart = 12;
	maxOpacity = 0.9;
	maxOpacityIE = maxOpacity * 100;
	opacityFactor = maxFrontBox - opacityStart;

	
	direction = turnDirection[obj.id];

	// alert(direction);

	if(direction=="showBack")
	{
		stopFrontBox = maxFrontBox;
		degreesTextFrontBox = -180;
	}
	if(direction=="showFront")
	{
		stopFrontBox = 0;
		degreesTextFrontBox = 0;
	}

	tempClassName = obj.className;
	if(obj.className == "frontBoxGeneral frontBox"+stopFrontBox+"")
	{
		obj.className = "frontBoxGeneral frontBox"+stopFrontBox+"";
		window.clearInterval(turnRunning[obj.id]);
		turnRunning[obj.id] = false;
		turnPending[obj.id] = false;
	}
	else
	{
		tempClassName = tempClassName.replace(/frontBoxGeneral/,"");
		tempClassName = tempClassName.replace(/frontBox/,"");
		tempClassName = Number(tempClassName);

		// document.getElementById("stat").innerHTML = tempClassName +"/"+direction;

		if(direction=="showBack")
		{
			tempClassName++;
			tempClassName++;
		}
		if(direction=="showFront")
		{
			tempClassName--;
			tempClassName--;
		}


			obj.className = "frontBoxGeneral frontBox"+tempClassName;

			if(tempClassName >= opacityStart)
			{
				// 0 == NICHT TRANSPARENT, 1 == VOLL TRANSPARENT / OP / FF / SF
				diff = maxFrontBox-tempClassName;
				
				newOpacity = 1 / opacityFactor * diff;
				if(newOpacity>maxOpacity)	newOpacity = maxOpacity;
				document.getElementById(obj.id+"text").style.opacity = newOpacity;

				// VON 100 AUF NULL VERRINGERN UM DURCHSICHTIG ZU WERDEN
				// AUF 100 ERHOEHEN, UM UNDURCHSICHTIG ZU SEIN / IE
				newOpacity = newOpacity * 100;
				if(newOpacity>maxOpacityIE)	newOpacity = maxOpacityIE;
				if(Number(tempClassName) == (maxFrontBox/2))
				{
					document.getElementById(obj.id+"text").style.webkitTransform = "rotateY("+degreesTextFrontBox+"deg)";
				}
				document.getElementById(obj.id+"text").style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity="+newOpacity+");";
			}
			else
			{
				document.getElementById(obj.id+"text").style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity="+maxOpacityIE+");";
				document.getElementById(obj.id+"text").style.opacity = maxOpacity;
			}
	}
}
