// Copyright (c) 2010 Human Engines Inc. All rights reserved.
//  
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//  
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//  
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//  
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


var fadeObj;
var fadingOut = false;
var overlayIsOn = false;
function setOpacity(value)
{
	fadeObj.style.opacity = value/10;
	fadeObj.style.filter = 'alpha(opacity=' + value*10 + ')';
	
	if (fadeObj.style.opacity == 0.0)
	{
		fadingOut = false;
		fadingOut = false;
		//overlayIsOn = false;
		fadeObj.style.display = "none";
	}
	else
	{
		fadeObj.style.display = "block";
	}
}	

function setImageMotion(name,x,y,value,bClip,bFadeIn)
{
	if (bFadeIn && fadingOut)
		return;
		
	var o = document.getElementsByName(name)[0];
	o.style.opacity = value/10;

	o.style.left = x + "px";
	o.style.top = y + "px";
	if (bClip)
		clipAgainstVortex(o);
	
	if (value == 0.0)
	{
		o.style.display = "none";
	}
	else
	{
		o.style.display = "block";
	}
}
function initIncomingImg(oImg,x,y,bClip)
{
	oImg.style.opacity = 1.0;
	oImg.style.left = x + "px";
	oImg.style.top = y + "px";
	if (bClip)
		clipAgainstVortex(oImg);
}

// re-order these to set the animation order
var ID_TIMELINE = 2;
var ID_SNACK = 3;
var ID_STACKING = 4;
var ID_LAUNCHER = 5;
var ID_ENVIRON = 6;
var ID_TECH = 7;
var ID_NETWORK = 1;
var ID_CUSTOM = 0;

var lastDIV = null;
function highlightDiv(id)
{
	if (lastDIV != null)
	{
		lastDIV.parentNode.style.backgroundColor = "transparent";
		lastDIV.parentNode.style.color = "#2b2b2b";
		lastDIV = null;
	}
	else if (id != "disable")
	{
		var oDiv = document.getElementById(id);
		lastDIV = oDiv;
		oDiv.parentNode.style.backgroundColor = "#2b2b2b";
		oDiv.parentNode.style.color = "white";
	}
}

function getFeatureDIV(id)
{
	if (id == ID_TIMELINE)
		return "timelineDIV";
	if (id == ID_SNACK)
		return "fbDIV";
	if (id == ID_STACKING)
		return "stackDIV";
	if (id == ID_LAUNCHER)
		return "appsDIV";
	if (id == ID_ENVIRON)
		return "sigalertDIV";
	if (id == ID_TECH)
		return "techDIV";
	if (id == ID_NETWORK)
		return "oem1DIV";
	if (id == ID_CUSTOM)
		return "oem2DIV";
}
function getFeatureID(name)
{
	if (name == "timelineDIV")
		return ID_TIMELINE;
	if (name == "fbDIV")
		return ID_SNACK;
	if (name == "stackDIV")
		return ID_STACKING;
	if (name == "appsDIV")
		return ID_LAUNCHER;
	if (name == "sigalertDIV")
		return ID_ENVIRON;
	if (name == "techDIV")
		return ID_TECH;
	if (name == "oem1DIV")
		return ID_NETWORK;
	if (name == "oem2DIV")
		return ID_CUSTOM;
}

function handleImageOverlayIncoming(id)
{
	var vContainer = document.getElementById("vortexcontainer");		
	if (id == ID_TIMELINE)
	{
		var oImg = document.getElementsByName("timeline")[0];
		oImg.style.display = "block";
		initIncomingImg(oImg,vContainer.offsetLeft,vContainer.offsetTop - oImg.offsetHeight,true);
		var incrementAmount = oImg.offsetHeight / 11;
		for (var j = 0; j < 11; j++)
		{
			var x = vContainer.offsetLeft;
			var y = vContainer.offsetTop - oImg.offsetHeight + (incrementAmount * (j+1));
			setTimeout('setImageMotion(\"timeline\",'+x+','+y+','+j+',true,true)', 25*j);
		}
		oImg.style.display = "none";
	}
	if (id == ID_SNACK)
	{
		var oImg = document.getElementsByName("fb")[0];
		oImg.style.display = "block";
		initIncomingImg(oImg,vContainer.offsetLeft - oImg.offsetWidth,vContainer.offsetTop,true);
		var incrementAmount = oImg.offsetWidth / 11;
		for (var j = 0; j < 11; j++)
		{
			var x = vContainer.offsetLeft - oImg.offsetWidth + (incrementAmount * (j+1));
			var y = vContainer.offsetTop;
			setTimeout('setImageMotion(\"fb\",'+x+','+y+','+j+',true,true)', 25*j);
		}
		oImg.style.display = "none";
	}
	if (id == ID_STACKING)
	{
		var oImg = document.getElementsByName("stack")[0];
		var x = vContainer.offsetLeft - 60;
		var y = vContainer.offsetTop + 40;
		initIncomingImg(oImg,x,y,false);
		oImg.style.display = "block";
	}
	if (id == ID_LAUNCHER)
	{
		var oImg1 = document.getElementsByName("apps")[0];
		var oImg2 = document.getElementsByName("contacts")[0];
		oImg1.style.display = "block";
		oImg2.style.display = "block";
		initIncomingImg(oImg1, vContainer.offsetLeft + vContainer.offsetWidth, vContainer.offsetTop, false);
		initIncomingImg(oImg2, vContainer.offsetLeft - oImg2.offsetWidth, vContainer.offsetTop, false);
		var increment1 = oImg1.offsetWidth / 11;
		var increment2 = oImg2.offsetWidth / 11;
		for (var j = 0; j < 11; j++)
		{
			var x1 = vContainer.offsetLeft + vContainer.offsetWidth - (increment1 * (j+1));
			var y1 = vContainer.offsetTop;
			var x2 = vContainer.offsetLeft - oImg2.offsetWidth + (increment2 * (j+1));
			var y2 = vContainer.offsetTop;
			setTimeout('setImageMotion(\"apps\",'+x1+','+y1+','+j+',true,true)', 25*j);
			setTimeout('setImageMotion(\"contacts\",'+x2+','+y2+','+j+',true,true)', 25*j);
		}
		oImg1.style.display = "none";
		oImg2.style.display = "none";
	}
	if (id == ID_ENVIRON)
	{
		var oImg = document.getElementsByName("sigalert")[0];
		oImg.style.display = "block";
		initIncomingImg(oImg,vContainer.offsetLeft + vContainer.offsetWidth,vContainer.offsetTop,true);
		var incrementAmount = oImg.offsetWidth / 11;
		for (var j = 0; j < 11; j++)
		{
			var x = vContainer.offsetLeft + vContainer.offsetWidth - (incrementAmount * (j+1));
			var y = vContainer.offsetTop;
			setTimeout('setImageMotion(\"sigalert\",'+x+','+y+','+j+',true,true)', 25*j);
		}
		oImg.style.display = "none";
	}
	if (id == ID_TECH)
	{
		overlayIsOn = true;
	}
	if (id == ID_NETWORK)
	{
		var oImg = document.getElementsByName("oem1")[0];
		var x = vContainer.offsetLeft + 20;
		var y = vContainer.offsetTop;
		initIncomingImg(oImg,x,y,false);
		oImg.style.display = "block";
	}
	if (id == ID_CUSTOM)
	{
		var oImg = document.getElementsByName("oem2")[0];
		var x = vContainer.offsetLeft + 15;
		var y = vContainer.offsetTop + 40;
		initIncomingImg(oImg,x,y,false);
		oImg.style.display = "block";
	}
	highlightDiv(getFeatureDIV(id));
}

function handleImageOverlayOutgoing(id)
{	
	var vContainer = document.getElementById("vortexcontainer");
	if (id == ID_TIMELINE)
	{
		var oImg = document.getElementsByName("timeline")[0];
		var incrementAmount = oImg.offsetHeight / 11;
		for (var j = 0; j < 11; j++)
		{
			var x = oImg.offsetLeft;
			var y = oImg.offsetTop + (incrementAmount * (j+1));
			setTimeout('setImageMotion(\"timeline\",'+x+','+y+','+(11-j-1)+',true,false)', 25*j);
		}
	}
	if (id == ID_SNACK)
	{
		var oImg = document.getElementsByName("fb")[0];
		var incrementAmount = oImg.offsetWidth / 11;
		for (var j = 0; j < 11; j++)
		{
			var x = oImg.offsetLeft - (incrementAmount * (j+1));
			var y = oImg.offsetTop;
			setTimeout('setImageMotion(\"fb\",'+x+','+y+','+(11-j-1)+',true,false)', 25*j);
		}
	}
	if (id == ID_STACKING)
	{
		var oImg = document.getElementsByName("stack")[0];
		oImg.style.display = "none";
	}
	if (id == ID_LAUNCHER)
	{
		var oImg1 = document.getElementsByName("apps")[0];
		var oImg2 = document.getElementsByName("contacts")[0];
		var increment1 = oImg1.offsetWidth / 11;
		var increment2 = oImg2.offsetWidth / 11;
		for (var j = 0; j < 11; j++)
		{
			var x1 = oImg1.offsetLeft + (increment1 * (j+1));
			var y1 = vContainer.offsetTop;
			var x2 = oImg2.offsetLeft - (increment2 * (j+1));
			var y2 = vContainer.offsetTop;
			setTimeout('setImageMotion(\"apps\",'+x1+','+y1+','+(11-j-1)+',true,false)', 25*j);
			setTimeout('setImageMotion(\"contacts\",'+x2+','+y2+','+(11-j-1)+',true,false)', 25*j);
		}
	}
	if (id == ID_ENVIRON)
	{
		var oImg = document.getElementsByName("sigalert")[0];
		var incrementAmount = oImg.offsetWidth / 11;
		for (var j = 0; j < 11; j++)
		{
			var x = oImg.offsetLeft + (incrementAmount * (j+1));
			var y = oImg.offsetTop;
			setTimeout('setImageMotion(\"sigalert\",'+x+','+y+','+(11-j-1)+',true,false)', 25*j);
		}
	}
	if (id == ID_TECH)
	{
		//overlayIsOn = false;
	}
	if (id == ID_NETWORK)
	{
		var oImg = document.getElementsByName("oem1")[0];
		oImg.style.display = "none";
	}
	if (id == ID_CUSTOM)
	{
		var oImg = document.getElementsByName("oem2")[0];
		oImg.style.display = "none";
	}
	highlightDiv("disable");
}

var bSlideShow = false;
var slideShowID = -1;
var tSlideShowTimeout;
function slideShowFeatures()
{	
	if (slideShowID != -1)
		handleImageOverlayOutgoing(slideShowID);
	
	slideShowID++
	if (slideShowID > 7)
		slideShowID = 0;

	handleImageOverlayIncoming(slideShowID);
	tSlideShowTimeout = setTimeout('slideShowFeatures()', 2000);
}

function endSlideShow()
{
	if (bSlideShow)
	{
		handleImageOverlayOutgoing(slideShowID);
		bSlideShow = false;
		slideShowID = -1;
		clearTimeout(tSlideShowTimeout);
	}
}

function startSlideShow()
{
	if (!bSlideShow)
	{
		slideShowID = currSelectedFeature;
		bIncoming = true;
		bSlideShow = true;
		slideShowFeatures();
	}
}

var currSelectedFeature = -1;
var tGoIntoSlideShow = -1;
function clickOnFeature(obj)
{
	endSlideShow();
	if (tGoIntoSlideShow != -1)
		clearTimeout(tGoIntoSlideShow);
		
	if (currSelectedFeature != -1)
		handleImageOverlayOutgoing(currSelectedFeature);
		
	currSelectedFeature = getFeatureID(obj.children[0].id);
	handleImageOverlayIncoming(currSelectedFeature);
	
	tGoIntoSlideShow = setTimeout('triggerSlideShow()', slideShowTriggerTime);
}

function triggerSlideShow()
{
	if (!bSlideShow)
	{
		if (currSelectedFeature != -1)
			handleImageOverlayOutgoing(currSelectedFeature);

		startSlideShow();
		currSelectedFeature = -1;
	}
}

var weekday=new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var order
function insertExposition()
{
	var phoneBorder = document.getElementsByName("phoneborder")[0];
	var vc = document.getElementById("vortexcontainer");
	phoneBorder.style.left = vc.offsetLeft - 22 + "px";
	phoneBorder.style.top = vc.offsetTop - 56 + "px";
	phoneBorder.style.display = "block";
	phoneBorder.style.position = "absolute";
	
	for (var i = 0; i < 3; i++)
	{
		var thisDay = new Date();
		thisDay.setDate(thisDay.getDate() + i);
		var thisHdr = document.getElementById("cal" + i);
		thisHdr.innerText = weekday[thisDay.getDay()] + " " + (thisDay.getMonth()+1) + "/" + thisDay.getDate();
	}
	tGoIntoSlideShow = setTimeout('triggerSlideShow()', slideShowTriggerTime);
}

var L = 0;
var T = 1;
var R = 2;
var B = 3;

function computeClipRect(o1,o2)
{
	var bIntersect = false;
	if ((o2[L] < o1[R]) && (o2[R] > o1[L]) && (o2[T] < o1[B]) && (o2[B] > o1[T]))
	{
		bIntersect = true;
	}
	if (!bIntersect)
	{
		return new Array(0,0,0,0);
	}
	else
	{
		var intersectR = new Array(Math.max(o1[L],o2[L]),Math.max(o1[T],o2[T]),Math.min(o1[R],o2[R]),Math.min(o1[B],o2[B]));
		return new Array(intersectR[L] - o1[L], intersectR[T] - o1[T], intersectR[R] - o1[L], intersectR[B] - o1[T]);
	}
}
function clipAgainstVortex(e2)
{
	var o = document.getElementById("vortexcontainer");
	var clipAgainst = new Array(o.offsetLeft, o.offsetTop, o.offsetLeft + o.offsetWidth, o.offsetTop + o.offsetHeight - 10);
	var clipRect = new Array(e2.offsetLeft, e2.offsetTop, e2.offsetWidth + e2.offsetLeft, e2.offsetHeight + e2.offsetTop);
	var clipCoords = computeClipRect(clipRect, clipAgainst);
	e2.style.clip = "rect(" + clipCoords[T] + "px," + clipCoords[R] + "px," + clipCoords[B] + "px," + clipCoords[L] + "px)";
	//alert(clipCoords);
	if ((clipCoords[0] == 0) && (clipCoords[1] == 0) && (clipCoords[2] == 0) && (clipCoords[3] == 0))
		return false;
	
	return true;
}
