//
// UnisonEffects.js - (C) 2010 Panic, Inc.
//
// Not for redistribution.

// UPDATES
// 8/12/2008 switched default loaded section from "sites-pane" to "new-pane" -- Tim
//

// Initialize important variables when page loads.

var isTouchDevice = false;
var isMacOS = true;
var browser = '';

function initializePageEffects() {
	
	if (navigator.appVersion.indexOf('Mac') < 0)
	{
		isMacOS = false;
	}
	
	if (navigator.appVersion.indexOf('iPhone') > -1 ||
		navigator.appVersion.indexOf('iPod') > -1 ||
		navigator.appVersion.indexOf('iPad') > -1)
	{
		isTouchDevice = true;
		document.body.className += ' touch';
		document.getElementById('downloadButton').onmouseover = download;
	}

}

//
// LARGE POPUP: Full-Screen Pop-up Functions
//

function download(elem)
{
	// Display Dialog
	showLargePopup(elem);
	
	// Begin Download (if appropriate)	
	if (isMacOS)
	{
		if (! isTouchDevice) {
			// Trigger download in 1.5 seconds (so pop-up graphics finish loading)
			refreshTimer = setTimeout("setLocation('"+elem.getAttribute("href")+"')", 1500);
			
			// Track download with Google Analytics
			_gaq.push(['_trackEvent', 'Downloads', 'Unison']);

		}
	}
}

function showLargePopup(elem)
{

    var popFullscreen = document.getElementById('fullscreen');
    var popLarge = document.getElementById('largepopup');
    
    // Show correct popup depending on the user's device
    if (isMacOS)
    {
    	// iPad, iPhone, or iPod touch
		if (isTouchDevice)
		{
		    document.getElementById('download-link').style.display = 'block';
		}
		else
		// A non-iPhone Mac
		{
		    document.getElementById('start-download').style.display = 'block';
		}
    }
    else
    {
    	// Not a Mac :(
	    document.getElementById('download-nonmac').style.display = 'block';
    }
 
    // Make fullscreen thing really full screen, and show it
    getSize();
    popFullscreen.style.height = myScrollHeight + 'px';
    popFullscreen.style.display = 'block';
	
    // Position pop-up
    popLarge.style.left = ((myWidth - popLarge.offsetWidth) / 2) + 'px';
    popLarge.style.top = (((myHeight - popLarge.offsetHeight) / 2) + myScroll) + 'px';
    popLarge.style.visibility = 'visible';
    
}

function setLocation(loc) {
	window.location = loc;
}

function hideLargePopup() {
    var popFullscreen = document.getElementById('fullscreen');
    var popLarge = document.getElementById('largepopup');
    
    popLarge.style.visibility = 'hidden';
    popFullscreen.style.display = 'none';
}

//
// DOWNLOAD SMALL POPUP: Download Hint Pop-up Functions
//
// An advantage of using a timer to do a hide is that we can ignore
// any spurious mouseOut events that have bubbled up, into <td>'s, etc.

var dpopTimer = "";

function showDownloadPopup(e) {
	var popDownload = document.getElementById('dpop');
	var btnDownload = document.getElementById('navbar');  // 11/2009 Changed this from "download" to "navbar" to make it work with the new image-free buttons. -Tim

	if (moveanim.timer != null) {
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	}

	// Determine where we should pop up in relation to the download button
	// 11/2009 - Changed this section to work with the new buttons. -Tim
	position = findElementPos(btnDownload);
	if (position[0] < 40)
		{
		popDownload.style.left = "15" +"px";
		popDownload.style.top = "-" + "83" +"px";
		}
	else {
		popDownload.style.left = "-" + "55" +"px";
		popDownload.style.top = "-" + "83" +"px";		
		}

	// If already trigger a rollover, cancel it because we're back in

	if (dpopTimer != "")
	{
		clearTimeout(dpopTimer);
		dpopTimer = "";
	} else {
		setOpacity(0, 'dpop');
		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('dpop', 0, 100, 13);
	}
}

function hideDownloadPopup() {
	// Start timer to hide the pop-up and the overlay
	dpopTimer = setTimeout("actuallyHide()", 500);
}

function actuallyHide() {
	var popDownload = document.getElementById('dpop');
	if (dpopTimer != "")
	{
		dpopTimer = "";
		moveStart(popDownload, parseInt(popDownload.style.left), parseInt(popDownload.style.left), parseInt(popDownload.style.top), parseInt(popDownload.style.top) - 10, 15);		
		fadeElementSetup('dpop', 100, 0, 13, 1);
	}
}

//
// MOVE: Animate the move of an element.
//
// Move is also synchronous. One at a time, please.
//

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);
		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++;
	}
}


//console.log("Initialized");
