// The following borrowed from Panic  - (C) 2007 Panic, Inc.

//
// FADE Functions
//

function fadeOut(elem) { if (elem.id) fadeElementSetup(elem.id, 100, 0, 10); }
function fadeIn(elem) { if (elem.id) fadeElementSetup(elem.id, 0, 100, 10);	}

// Initialize the fade function

var fadeActive = new Array();
var fadeQueue  = new Array();
var fadeTimer  = new Array();
var fadeClose  = new Array();

function fadeElementSetup(theID, fdStart, fdEnd, fdSteps, fdClose) {
	if (fadeActive[theID] == true) {
		fadeQueue[theID] = new Array(theID, fdStart, fdEnd, fdSteps);
	} else {
		fadeSteps = fdSteps;
		fadeCurrent = 0;
		fadeAmount = (fdStart-fdEnd)/fadeSteps;
		fadeTimer[theID] = setInterval("fadeElement('"+theID+"', '"+fadeCurrent+"', '"+fadeAmount+"', '"+fadeSteps+"')", 15);
		fadeActive[theID] = true;
		if (fdClose == 1) {
			fadeClose[theID] = true;
		} else {
			fadeClose[theID] = false;
		}
	}
}

// Do the fade. This function will call itself, modifying the parameters, so many instances can run concurrently.

function fadeElement(theID, fadeCurrent, fadeAmount, fadeSteps) {
	if (fadeCurrent == fadeSteps) {
	    clearInterval(fadeTimer[theID]);
	    fadeActive[theID] = false;
	    if (fadeClose[theID] == true) {
	    	document.getElementById(theID).style.visibility = 'hidden';
	    }
	    if (fadeQueue[theID] && fadeQueue[theID] != false) {
	    	fadeElementSetup(fadeQueue[theID][0], fadeQueue[theID][1], fadeQueue[theID][2], fadeQueue[theID][3]);
	    	fadeQueue[theID] = false;
	    }  
	} else {
		fadeCurrent++;
		if (fadeAmount < 0) { setOpacity(Math.abs(fadeCurrent*fadeAmount), theID); }
		else { setOpacity(100-(fadeCurrent*fadeAmount), theID); }
		clearInterval(fadeTimer[theID]);
		fadeTimer[theID] = setInterval("fadeElement('"+theID+"', '"+fadeCurrent+"', '"+fadeAmount+"', '"+fadeSteps+"')", 15);
	}
}

//
// UTILITY FUNCTIONS
//

function setOpacity(opacity, theID) {
	var object = document.getElementById(theID).style;
	// If it's 100, set it to 99 for Firefox.
	if (navigator.userAgent.indexOf('Firefox') != -1) {
		if (opacity == 100) opacity = 99.9999;		// This is majorly retarded
	}
	object.filter = 'alpha(opacity='+opacity+')';	// IE Win
	object.opacity = (opacity/100);					// Safari 1.2, Firefox

}

// Math functions for animation calucations - From http://www.robertpenner.com/easing/
//
// t = time, b = begin, c = change, d = duration
// time = current frame, begin is fixed, change is basically finish - begin, duration is fixed (frames),

function linear(t, b, c, d) { return c*t/d+b; }
function sineInOut(t, b, c, d) { return -c/2*(Math.cos(Math.PI*t/d)-1)+b; }
function cubicIn(t, b, c, d) { return c*(t /= d)*t*t+b; }
function cubicOut(t, b, c, d) { return c*((t = t/d-1)*t*t+1)+b; }
function cubicInOut(t, b, c, d) { if ((t /= d/2) < 1) return c/2*t*t*t+b; return c/2*((t -= 2)*t*t+2)+b; }
function bounceOut(t, b, c, d) {
	if ((t /= d) < (1/2.75)) { return c*(7.5625*t*t)+b; }
	else if (t < (2/2.75)) { return c*(7.5625*(t -= (1.5/2.75))*t+.75)+b; }
	else if (t < (2.5/2.75)) { return c*(7.5625*(t -= (2.25/2.75))*t+.9375)+b; }
	else { return c*(7.5625*(t -= (2.625/2.75))*t+.984375)+b; }
}

// Find the Y position of an element on a page. Return Y and X as an array

function findElementPos(elemFind) {
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while (elemFind = elemFind.offsetParent);
	return Array(elemX, elemY);
}

//
// POPUP: Pop-up Functions
//

var dpopTimer = '';

function showPopup(e, ct) {
 	var popDownload = document.getElementById('mpop');
	var btnDownload = document.getElementById('map');
	
	var yScroll = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	yScroll = (yScroll) ? yScroll : 0;
	activeCountry = e;
	activeMap(activeCountry);
	popupOK = false;
	document.getElementById('map-scroll').style.left = '0';
	if (moveanim.timer != null) {
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	}
	position = findElementPos(btnDownload);
	popDownload.style.top = (yScroll-50)+'px';
	popDownload.style.left = '5px';
	if (dpopTimer != '') {
		clearTimeout(dpopTimer);
		dpopTimer = '';
	} else {
		//setOpacity(0, 'mpop');
		popDownload.style.visibility = 'visible';
		moveStart(popDownload, parseInt(popDownload.style.left), parseInt(popDownload.style.left), parseInt(popDownload.style.top) + 10, parseInt(popDownload.style.top), 15);
		//fadeElementSetup('mpop', 0, 100, 13);
	}
	contactsUpdate(e);
//	document.getElementById('map-scroll').innerHTML = frames['map-'+activeCountry].document.getElementById(ct).innerHTML;
	globalCT = ct;
	for (var i = 0; i < countries.length; i++) {
		if (fullNames[i].toLowerCase().substring(0, 4) == activeCountry.substring(0, 4)) fullIndex = i;
	}
	document.getElementById('map-country').innerHTML = fullNames[fullIndex].toUpperCase();
}

function hidePopup() {
	popupOK = true;
	document.getElementById('map-scroll').style.overflow = 'hidden';
	dpopTimer = setTimeout(actuallyHide, 500);
}

function actuallyHide() {
	var popDownload = document.getElementById('mpop');
	if (dpopTimer != '') {
		dpopTimer = '';
		moveStart(popDownload, parseInt(popDownload.style.left), parseInt(popDownload.style.left), parseInt(popDownload.style.top), parseInt(popDownload.style.top)-10, 15);
		//fadeElementSetup('mpop', 100, 0, 13, 1);
		document.getElementById('mpop').style.visibility = 'hidden';
	}
}

//
// MOVE: Animate the move of an element
//

var moveanim = {time:0, beginX:0, changeX:0.0, beginY:0, changeY:0, duration:0.0, element:null, timer:null};

function moveStart(elem, startX, endX, startY, endY, duration) {
	if (moveanim.timer != null) { clearInterval(moveanim.timer); moveanim.timer = null; }
	moveanim.time = 0;
	moveanim.beginX = startX;
	moveanim.changeX = endX-startX;
	moveanim.beginY = startY;
	moveanim.changeY = endY-startY;
	moveanim.duration = duration;
	moveanim.element = elem;
	moveanim.timer = setInterval(moveAnimDo, 15);
}

function moveAnimDo() {
	if (moveanim.time > moveanim.duration) {
		clearInterval(moveanim.timer);
		if (!popupOK) {
			document.getElementById('map-scroll').style.left = '0';
			document.getElementById('map-scroll').style.overflow = 'auto';
		} else {
			document.getElementById('map-scroll').style.left = '-1000px';
		}
		moveanim.timer = null;
	} else {
		moveX = cubicOut(moveanim.time, moveanim.beginX, moveanim.changeX, moveanim.duration);
		moveY = cubicOut(moveanim.time, moveanim.beginY, moveanim.changeY, moveanim.duration);
		moveanim.element.style.left = moveX+'px';
		moveanim.element.style.top = moveY+'px';
		moveanim.time++;
	}
}
