/* 
	main.js is for global javascript
	requires jQuery
*/



/* OBJECTS =========== */
Point = function(_x,_y) {
	this.x = ( _x != null ? _x : 0 );
	this.y = ( _y != null ? _y : 0 );
}

/* VARIABLES ========== */
var mapInitialized = false;
var locationVisible = false;
var contactVisible = false;
var rolloverStateEnabled = false;
var clickCatcherEnabled = false;
var popoverOffset = new Point(11,7);
var usrAgt = navigator.userAgent;
var hasMapAppSupport = mapAppSupportCheck();
var sensorAvailable = sensorCheck();
var map; // global scope for map

stuffToDoWhilePageIsLoading();

/* FUNCTIONS ========== */
function stuffToDoWhilePageIsLoading()
{
	// load the google map API
	
	if (!hasMapAppSupport) {
		google.load("maps", "2", { "other_params":"sensor="+sensorAvailable.toString() });
	}
}

// Stuff to do once page DOM is set and images are loaded
$(document).ready(function() {
	
	descriptionArray = new Array();
	setRolloverDescriptionText();
	
	/* header button handlers */
	$('#btnLocation').click( function() {
		
		if (rolloverStateEnabled) menuRollOutHandler();
		
		if (contactVisible) hideContactLayer();
		
		if (locationVisible) { 
			hideLocationLayer()
		} else { 
			if (hasMapAppSupport) {
				document.location = "http://maps.google.com/maps?q=industrynext,+new+york,+ny&cid=10829003010629803992&t=default";
			} else {
				showLocationLayer()
			}
		}
		
	});
	
	$('#btnContact').click( function() {
		
		if (locationVisible) hideLocationLayer();

		if (contactVisible) { 
			hideContactLayer() 
		} else { 
			showContactLayer()
		}
		
	});
	
	/* close buttons for popover layers */
	$('#locationLayer > .closeButton').click( function() { hideLocationLayer() });
	$('#contactLayer > .closeButton').click( function() { hideContactLayer() });
	
	/* main navigation rollover handlers */
	$('#menu01').mouseenter( function() { menuRollInHandler(0) } ); 
	$('#menu02').mouseenter( function() { menuRollInHandler(1) } ); 
	$('#menu03').mouseenter( function() { menuRollInHandler(2) } ); 
	$('#menu04').mouseenter( function() { menuRollInHandler(3) } ); 
	$('#menu05').mouseenter( function() { menuRollInHandler(4) } ); 
	$('#menu06').mouseenter( function() { menuRollInHandler(5) } );
	$('#rolloverLayer').mouseleave( function() { menuRollOutHandler() } );
	$('#clickCatcher').click( function() { clickOutsideHandler() } );
	
});

function initMap()
{
	/* loads google map into location popover once the popover is triggered by the user */
	if (GBrowserIsCompatible()) {
	
		mapInitialized = true;
		mapInfoShown = false;
		
		var industryNextLocation = new google.maps.LatLng(40.741380, -73.99655);
		var industryNextPopoverContent = document.innerHTML = 
			'<div class="mapInfoWin"><h3>IndustryNext<\/h3><p>151 West 19th Street,<br \/>New York, NY 10011<br \/>(212) 542-8885<\/p><\/div>';
			
		var industryNextMapIcon = new google.maps.Icon(G_DEFAULT_ICON);
			industryNextMapIcon.image = "images/IndustryNext-map-marker.png";
			industryNextMapIcon.iconSize = new google.maps.Size("24", "36");
	
		map = new google.maps.Map2(document.getElementById("map"));
		map.checkResize();
		map.setCenter(industryNextLocation, 15);
		map.setUIToDefault();
		
		/* add industrynext info bubble */
		var industryNextMapMarker = new google.maps.Marker(industryNextLocation, { icon:industryNextMapIcon });
		map.addOverlay(industryNextMapMarker);
		
		var infoWin = map.openInfoWindow(industryNextLocation, industryNextPopoverContent);
		mapInfoShown = true;
	}
}

function mapAppSupportCheck()
{
	chk = false;
	if ( 
		usrAgt.indexOf("iPad") > -1 || 
		usrAgt.indexOf("iPhone") > -1 ||
		usrAgt.indexOf("Android") > -1
		) 
	{
		chk = true;
	}
	return chk;
}

function sensorCheck()
{
	/* returns a boolean for a GPS sensor check based on a device matrix */
	/* probably should do a feature check instead of device check in the future */
	chk = false;
	if ( usrAgt.indexOf("iPad") > -1 || usrAgt.indexOf("iPhone") > -1 ) {
		chk = true;
	}
	
	return chk;
}

function setRolloverDescriptionText()
{
	descriptionArray[0] = "Architect and develop a platform to deliver video and applications to users' authorized devices over IP based networks";
	descriptionArray[1] = "Craft interfaces utilizing touch on iPads, iPhones, Flash enabled Android and Windows CE embedded devices";
	descriptionArray[2] = "Allow users to securely download their web video experience with them while traveling, at work, outside, at school, or anywhere";
	descriptionArray[3] = "Create sophisticated yet easy to use navigational elements that are the next generation in electronic programming guides";
	descriptionArray[4] = "Develop meaningful, revenue generating social, e-commerce, and advertising applications";
	descriptionArray[5] = "Optimize the TV experience understanding user interaction with programming and controls via remote";
}

function positionRollover(menuNum)
{
	/* menu ID's start at 1 instead of 0, correct for this */
	menuNum++;
	
	var relativePosition = $('#menu0' + menuNum).position();
	
	// fix offset bug on iPad
	if (usrAgt.indexOf("iPad") > -1) {
		relativePosition.left -= window.scrollX;
		relativePosition.top -= window.scrollY;
	}
	
	$('#rolloverLayer').css('left', relativePosition.left - popoverOffset.x + "px");
	$('#rolloverLayer').css('top', relativePosition.top - popoverOffset.y + "px");
}

function setRolloverContents(menuNum)
{	
	$('#rolloverTitle').removeClass();
	$('#rolloverTitle').addClass('titleImg0' + (menuNum + 1));
	$('#rolloverDescription').text( descriptionArray[menuNum] );
}

function showRollover()
{
	if (hasMapAppSupport) {
		$('#rolloverLayer').show();
	} else {
		$('#rolloverLayer').stop(true, true);
		$('#rolloverLayer').fadeIn(300);
	}
}

function hideRollover()
{
	if (hasMapAppSupport) {
		$('#rolloverLayer').hide();
	} else {
		$('#rolloverLayer').stop(true, true);
		$('#rolloverLayer').fadeOut(300);
	}
}

function menuRollInHandler(menuNum)
{
	if (!locationVisible && !contactVisible) {
		positionRollover(menuNum);
		setRolloverContents(menuNum);
		showRollover();
		rolloverStateEnabled = true;
	}
}

function showLocationLayer()
{
	if (!clickCatcherEnabled) showClickCatcher();
	
	$('#locationLayer').show();
	locationVisible = true;
	$('#btnLocation').removeClass().addClass('selected');
	if (mapInitialized == false) initMap();
}

function hideLocationLayer()
{
	if (clickCatcherEnabled) hideClickCatcher();

	$('#locationLayer').hide();
	locationVisible = false;
	$('#btnLocation').removeClass().addClass('normal');
}

function showContactLayer()
{
	if (!clickCatcherEnabled) showClickCatcher();
	
	$('#contactLayer').show();
	contactVisible = true;
	$('#btnContact').removeClass().addClass('selected');
}

function hideContactLayer()
{
	if (clickCatcherEnabled) hideClickCatcher();
	
	$('#contactLayer').hide();
	contactVisible = false;
	$('#btnContact').removeClass().addClass('normal');
}

function menuRollOutHandler()
{
	hideRollover();
	rolloverStateEnabled = false;
}

function showClickCatcher()
{
	$('#clickCatcher').show();
	clickCatcherEnabled = true;
}

function hideClickCatcher()
{
	$('#clickCatcher').hide()
	clickCatcherEnabled = false;
}

function clickOutsideHandler()
{
	if (locationVisible) hideLocationLayer();
	if (contactVisible) hideContactLayer();
}

