/**
* @author giusbyte
*/
var futureArray = [ 'Signs point<br/>to yes.',
			'Yes.',
			'Most likely.',
			'Without<br/>a doubt.',
			'Yes -<br/>definitely.',
			'As I see it,<br/>yes.',
			'You may<br/>rely on<br/>it.',
			'Outlook<br/>good.',
			'It is certain.',
			'It is<br/>decidedly<br/>so.',
			'Reply hazy,<br/>try again.',
			'Better not<br/>tell you<br/>know.',
			'Ask again<br/>later.',
			'Concentrate<br/>and ask<br/>again.',
			'Cannot<br/>predict<br/>now.',
			'My sources<br/>say no.',
			'Very<br/>doubtful.',
			'My reply<br/>is no.',
			'Outlook not<br/>so good.',
			'Don\'t count<br/>on it'];
			
var MOBILE_INSTR = "Instructions<br/><br/>- Ask the Magic iEightBall your question.<br/>- Rotate your device 2 times.<br/>- The iEightBall will reveal the response!!<br/>";
var DEFAULT_INSTR = "Instructions<br/><br/>- Ask the Magic iEightBall your question.<br/>- Click on the iEightBall.<br/>- The iEightBall will reveal the response!!<br/>";
var SLIDE_EASE = 5;
var SLIDE_PANEL_Y = 40;
var SLIDE_PANEL_HEIGHT = 140;
var slidePanelY = SLIDE_PANEL_Y;
var isSliding = false;
var sliderSpeed;
var numShakes = 0;
var currentWidth = 0;
var isFutureRevealed = false;
var eightId = 'eight';
var futureId = 'future';
var futureTextId = 'futureText';

function isMobileApple() {
	return (navigator.platform.indexOf("iPhone") != -1 || navigator.platform.indexOf("iPod") != -1);
}
function checkBrowser() {
	document.getElementById(eightId).style.display = 'block';
	document.getElementById(eightId).style.visibility = 'visible';
	var instrObj = document.getElementById('instructions');
	if (isMobileApple()) {
		updateLayout();
		setInterval(updateLayout, 100);
		instrObj.innerHTML = MOBILE_INSTR;
	} else {
		instrObj.innerHTML = DEFAULT_INSTR;
	}
}
function updateLayout() {
    if (window.innerWidth != currentWidth) {
        currentWidth = window.innerWidth;
		numShakes++;
        setTimeout(function() { window.scrollTo(0, 1); }, 10);   
		if (numShakes > 2 && !isFutureRevealed) revealFuture();
    }
}
function fadeIn(objId, opacity, speed) {
	obj = document.getElementById(objId);
	if (opacity <= 100) {
		opacity += speed;
		setOpacity(obj, opacity);
		setTimeout("fadeIn('" + objId + "', " + opacity + ", " + speed + ")", 100);
	}
}
function fadeOut(objId, opacity, speed) {
	obj = document.getElementById(objId);
	if (opacity > 0) {
		opacity -= speed;
		setOpacity(obj, opacity);
		setTimeout("fadeOut('" + objId + "', " + opacity + ", " + speed + ")", 100);
	}
}
function setOpacity(obj, opacity) {
	obj.style.opacity = opacity/100;
}
function revealFuture() {
	var eightObj = document.getElementById(eightId); 
	var futureObj = document.getElementById(futureId); 
	var futureText = document.getElementById(futureTextId);
	
	if (isFutureRevealed) {
		futureObj.style.visibility = 'hidden';
		futureObj.style.display = 'none';
		eightObj.style.visibility = 'visible';
		eightObj.style.display = 'block';
		isFutureRevealed = false;
	} else {
		eightObj.style.visibility = 'hidden';
		eightObj.style.display = 'none';
		futureObj.style.visibility = 'visible';
		futureObj.style.display = 'block';
		setOpacity(futureObj, 0);
		futureText.innerHTML = futureArray[Math.floor(Math.random()*futureArray.length)];
		fadeIn(futureId, 0, 10);
		isFutureRevealed = true;
	}
	numShakes = 0;
}
function slidePanel() {
	if (isSliding) return;
	isSliding = true;
	if(slidePanelY == SLIDE_PANEL_Y) slideIn();
	else slideOut();
}

function slideIn() {
	var slidePanel = document.getElementById('slidePanel');
	if (SLIDE_PANEL_HEIGHT + slidePanelY > SLIDE_PANEL_Y) {
		sliderSpeed = (SLIDE_PANEL_HEIGHT + slidePanelY - SLIDE_PANEL_Y)/SLIDE_EASE;
		if (sliderSpeed < 0.1) sliderSpeed = 1;
		slidePanelY -= sliderSpeed;
		if (slidePanelY + SLIDE_PANEL_HEIGHT <= SLIDE_PANEL_Y) slidePanelY = SLIDE_PANEL_Y - SLIDE_PANEL_HEIGHT;
		slidePanel.style.top = slidePanelY + 'px';
		setTimeout("slideIn()", 30);
	} else {
		isSliding = false;
	}
}
function slideOut() {
	var slidePanel = document.getElementById('slidePanel');
	if (slidePanelY < SLIDE_PANEL_Y) {
		sliderSpeed = (SLIDE_PANEL_Y - slidePanelY)/SLIDE_EASE;
		if (sliderSpeed < 0.1) sliderSpeed = 1;
		slidePanelY += sliderSpeed;
		if (slidePanelY >= SLIDE_PANEL_Y) slidePanelY = SLIDE_PANEL_Y;
		slidePanel.style.top = slidePanelY + 'px';
		setTimeout("slideOut()", 30);
	} else {
		isSliding = false;
	}
}
setTimeout('slideIn()', 3000);
addEventListener("load", function() { setTimeout(checkBrowser, 0); }, false);
