var foInterval;
var buInterval;
var active = null;
var mapX = 0;
var mapY = 0;
var mapHeight = 772;
var mapOkay = true;
var overBubble = false;
var idBubble = 0;
var savedClassNames = [];
var jumpURL = 'http://summitenergygps.com/assess-sustainability-strategy.html';
var tiwyOn = false;
var tutorialOn = false;
var currentTutorialPg = 1;
var popupOkayToLuanch = true;
var pageOkayToChange = true;
var cookieFound = readCookie('SummitEnergy');
var firstTimeDisplayingMap = true;
var disableArrowsAndBubbles = false;
var mapImg = new Image();

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	}
	else var expires = '';
	document.cookie = name+'='+value+expires+'; path=/';
}

function readCookie(name) {
	var nameEQ = name+'=';
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) { createCookie('SummitEnergy', 'gpscookie', -1); }

function showPulldown() {

	if (firstTimeDisplayingMap) {
	
		firstTimeDisplayingMap = false;

		// IMPORT MAP DATA
		frames['map-iframe'].location.href = 'map.html';

		var pdInt = setInterval(function() {
			if ($('pulldown-map-outer').innerHTML.length > 100) { 

				clearInterval(pdInt);
//				$('pulldown-map-outer').innerHTML = frames['map-iframe'].document.getElementById('pulldown-map-outer').innerHTML;
				
				// ACTIVATE MAP POINTS
				var nodes = $('pulldown-map').childNodes;
				for (var i = 0; i < nodes.length; i++) {
					if (nodes[i].id != undefined) {
						$(nodes[i].id).onmouseover = function() { showBubble(this.id); };
						$(nodes[i].id).onmouseout = function() { possiblyHideBubble(); };
					}
				}

				// SETUP BUBBLE
				new Effect.Opacity('bubble', { from:0.0, to:0.0, duration:0.0 });
				$('bubble').onmouseover = function() { overBubble = true; clearInterval(buInterval); };
				$('bubble').onmouseout = possiblyHideBubble;
				$('pulldown-up').className = 'inactive';
				new Effect.Opacity('pulldown-up', { to:0.5, duration:0.25 });

				showMap();

			}
		}, 100);
		
		// LOAD MAP IMAGE
		mapImg.src = '_assets/img/map.jpg';
	} else {
		showMap();
	}
}

function showMap() {
	$('blackout').appear({ duration:0.5, to:0.7 });
	setTimeout(function() {
		Effect.SlideDown('pulldown-inner');
		var mapLoadInterval = setInterval(function() {
			if (!mapImg.complete) return;
			$('pulldown-map').style.background = 'url("_assets/img/map.jpg")';
			$('pulldown-lowsrc').fade({ duration:0.5 });
			if (!cookieFound) {
				tutorial();
				createCookie('SummitEnergy', 'gpscookie', 3650);
				cookieFound = true;
			}
			clearInterval(mapLoadInterval);
		}, 100);
	}, 250);
}

function hidePulldown(a) {
	Effect.SlideUp('pulldown-inner');
	setTimeout(function() {
		$('blackout').fade({ duration:0.5 });
	}, 250);
	setTimeout(function() {
		if (a) location.href = a;
	}, 750);
}

function tutorial() {
	if (popupOkayToLuanch && !tutorialOn) {
		popupOkayToLuanch = false;
		if (tiwyOn) {
			$('popup').fade({ duration:0.5 });
			setTimeout(function() { tiwyOn = false; popupOkayToLuanch = true; tutorial(); }, 600);
		} else {
			$('tiwy').fade({ duration:0.0 });
			$('tutorial-link-1').style.backgroundPosition = '0 0';
			$('tutorial-link-2').style.backgroundPosition = '0 0';
			$('tutorial-link-3').style.backgroundPosition = '0 0';
			$('tutorial-pg2').fade({ duration:0.0 });
			$('tutorial-pg3').fade({ duration:0.0 });
			$('tutorial-pg4').fade({ duration:0.0 });
			$('tutorial-pg1').appear({ duration:0.5 });
			$('tutorial').appear({ duration:0.0 });
			$('popup').appear({ duration:0.5 });
			currentTutorialPg = 1;
			tutorialOn = true;
			setTimeout(function() { popupOkayToLuanch = true; }, 600);
			disableArrowsAndBubbles = true;
		}
	}
}

function tutorialPg(pg) {
	if (pageOkayToChange && currentTutorialPg != pg) {
		pageOkayToChange = false;
		$('tutorial-link-1').style.backgroundPosition = '0 -112px';
		$('tutorial-link-2').style.backgroundPosition = '0 -112px';
		$('tutorial-link-3').style.backgroundPosition = '0 -112px';
		$('tutorial-link-'+(pg-1)).style.backgroundPosition = '0 -56px';
		$('tutorial-pg'+currentTutorialPg).fade({ duration:0.5 });
		setTimeout(function() {
			$('tutorial-pg'+pg).appear({ duration:0.5 });
			currentTutorialPg = pg;
			setTimeout(function() { pageOkayToChange = true; }, 600);
		}, 600);
	}
}

function takeItWithYou() {
	if (popupOkayToLuanch && !tiwyOn) {
		popupOkayToLuanch = false;
		if (tutorialOn) {
			$('popup').fade({ duration:0.5 });
			setTimeout(function() { tutorialOn = false; popupOkayToLuanch = true; takeItWithYou(); }, 600);
		} else {
			$('tutorial').fade({ duration:0.0 });
			$('tiwy').appear({ duration:0.0 });
			$('popup').appear({ duration:0.5 });
			tiwyOn = true;
			setTimeout(function() { popupOkayToLuanch = true; }, 600);
			disableArrowsAndBubbles = true;
		}
	}
}

function hidePopup() {
	tiwyOn = false;
	tutorialOn = false;
	$('popup').fade({ duration:0.5 });	
	disableArrowsAndBubbles = false;
}

function moveMap(dir) {
	if (mapOkay && !disableArrowsAndBubbles) {
		mapOkay = false;
		if (dir == 'up' && mapY != 0) mapY += 386;
		else if (dir == 'down' && mapY != -1*mapHeight) mapY -= 386;
		new Effect.Move('pulldown-map', { x:mapX, y:mapY, mode:'absolute', duration:0.5 });
		setTimeout(function() { mapOkay = true; }, 500);
		if (mapY == 0) {
			new Effect.Move('pulldown-minioutline', { x:15, y:5, mode:'absolute', duration:0.5 });
			$('pulldown-up').className = 'inactive';
			new Effect.Opacity('pulldown-up', { to:0.5, duration:0.25 });
		} else if (mapY == -386) {
			new Effect.Move('pulldown-minioutline', { x:15, y:19, mode:'absolute', duration:0.5 });
			$('pulldown-up').className = 'active';
			new Effect.Opacity('pulldown-up', { to:1.0, duration:0.25 });
			$('pulldown-down').className = 'active';
			new Effect.Opacity('pulldown-down', { to:1.0, duration:0.25 });
		} else {
			new Effect.Move('pulldown-minioutline', { x:15, y:32, mode:'absolute', duration:0.5 });
			$('pulldown-down').className = 'inactive';
			new Effect.Opacity('pulldown-down', { to:0.5, duration:0.25 });
		}
	}
}

function showBubble(id) {
	if (!overBubble && !disableArrowsAndBubbles) {
		clearInterval(buInterval);
		idBubble = id;
		
		var top = parseInt($(id).style.top.substring(0, $(id).style.top.length-2));
		var left = parseInt($(id).style.left.substring(0, $(id).style.left.length-2));
		var width = parseInt($(id).style.width.substring(0, $(id).style.width.length-2));
		var height = parseInt($(id).style.height.substring(0, $(id).style.height.length-2));
		
		if (top < 260-mapY) { // upside down
			$('bubble-content-inner').style.bottom = 'auto';
			$('bubble-content-inner').style.top = '0';
			$('bubble').style.top = (top+mapY+(height-12))+'px';
			$('bubble-bottom').style.height = '14px';
			$('bubble-bottom').className = 'bubble-bottom';
			$('bubble-middle').className = 'bubble-middle-down';
			$('bubble-middle-inner').className = 'bubble-middle-inner-down';
			$('bubble-top').style.height = '44px';
			if (left < 477-mapX) {
				$('bubble').style.left = (left+mapX+(width-32))+'px';
				$('bubble-top').className = 'bubble-top-left';
			} else {
				$('bubble').style.left = ((left+mapX)-219)+'px';  
				$('bubble-top').className = 'bubble-top-right';
			}
		} else { // right-side up
			$('bubble-content-inner').style.bottom = '0';
			$('bubble-content-inner').style.top = 'auto';
			$('bubble').style.top = (top+mapY+12)+'px';
			$('bubble-top').style.height = '14px';
			$('bubble-top').className = 'bubble-top';
			$('bubble-middle').className = 'bubble-middle-up';
			$('bubble-middle-inner').className = 'bubble-middle-inner-up';
			$('bubble-bottom').style.height = '44px';
			if (left < 477-mapX) {
				$('bubble').style.left = (left+mapX+(width-32))+'px';
				$('bubble-bottom').className = 'bubble-bottom-left';
			} else {
				$('bubble').style.left = ((left+mapX)-219)+'px';  
				$('bubble-bottom').className = 'bubble-bottom-right';
			}
		}
		
		$('bubble-middle-inner').innerHTML = $(id).innerHTML;
		$('bubble').style.display = 'block';
		new Effect.Opacity('bubble', { from:0.0, to:1.0, duration:0.25 });
	} else {
		clearInterval(buInterval);
		overBubble = false;
		if (id != idBubble) {
			new Effect.Opacity('bubble', { from:0.0, to:0.0, duration:0.0 });
			setTimeout(function() {
				showBubble(id);
			}, 50);
		}
	}
}

function possiblyHideBubble() {
	buInterval = setInterval(function() {
		$('bubble').style.display = 'none';
		new Effect.Opacity('bubble', { from:1.0, to:0.0, duration:0.0 });
		overBubble = false;
		clearInterval(buInterval);
	}, 100);
}

function showDrop(id) {
	clearInterval(foInterval);
	oldActive = active;
	active = id;
	for (var i = 1; i < 6; i++) $('nav-item'+i).className = 'inactive';	
	$('nav-item'+active).className = 'hover';
	Effect.BlindDown('dd'+active, { duration:0.25 });
	if (oldActive != active && oldActive != null) Effect.Fade('dd'+oldActive, { duration:0.1 });
}

function possiblyFade(id) {
	foInterval = setInterval(function() {
		for (var i = 1; i < 6; i++) $('nav-item'+i).className = savedClassNames[i];
		Effect.Fade('dd'+id, { duration:0.1 });
		clearInterval(foInterval);
		active = null;
	}, 50);
}

function killFade(id) { clearInterval(foInterval); }

function setup(index) {

	// Uncomment the next line if you ever want to intentionally delete the cookie when testing locally
	// createCookie('SummitEnergy', 'gpscookie', -1);

	// SETUP SLIDER
	if (index) {
		(function() {
			var zoom_slider = $('screen-slider'),
				box = $('screen-element');
			new Control.Slider(zoom_slider.down('.handle'), zoom_slider, {
			  range: $R(0, 14),
			  sliderValue: 0,
			  values: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
			  handleImage: 'screen-handle',
			  onChange: function(value) { 
				if (value < 3) jumpURL = 'http://summitenergygps.com/assess-sustainability-strategy.html';
				else if (value > 2 && value < 6)  jumpURL = 'http://summitenergygps.com/strategize-energy-sustainability-exchange.html';
				else if (value > 5 && value < 9)  jumpURL = 'http://summitenergygps.com/plan-sustainability-roadmap.html';
				else if (value > 8 && value < 12)  jumpURL = 'http://summitenergygps.com/optimize-portfolio-optimization.html';
				else if (value > 11)  jumpURL = 'http://summitenergygps.com/report-ghg-carbon-emission-reporting.html';
			  }		  
			});
		})();
		$('screen-btn').onclick = function() { window.location.href = ''+jumpURL; };
	}
	
	for (var i = 1; i < 6; i++) savedClassNames[i] = $('nav-item'+i).className;
}
