var e3ShowMap = {
	mapView: 'closed', //list is closed or opened
	animating: false,
	switching: false,
	openTimer: null,
	closeTimer: null,
	
	smallMapInt:null,
	largeMapInt:null,
	
	sweepTime: 1000,
	sweepDelay: 5000,
	mapAndSweeperWidth: 0,
	mapType: "",
	blipTimeOuts: new Array(),
	lastDatasets: {},
	init: function(){
		$.fn.maphilight.defaults = {
			fill: true,
			fillColor: 'ffd704',
			fillOpacity: 0.2,
			stroke: true,
			strokeColor: 'ffd704',
			strokeOpacity: 1,
			strokeWidth: 1,
			fade: true,
			alwaysOn: false,
			neverOn: false,
			groupBy: false
		}
		$('img[usemap]').maphilight();
		//start small map if it's available
		if(!!$('#smallMap').length){
			e3ShowMap.doSweep("smallMap");
			e3ShowMap.smallMapInt = setInterval(function(){e3ShowMap.doSweep("smallMap")}, e3ShowMap.sweepDelay);
		}
	},
	
	openMap: function(){
//		console.log("supposed to open: " + e3ShowMap.mapView);
		if(e3ShowMap.mapView == 'closed'){
			e3ShowMap.mapType=$("#mapFloors .sections .selected a").html().replace(/&nbsp;&nbsp;/g,"");
			$('#smallMap .blips').empty();
			e3ShowMap.animating = true;
			if(mobileChecker.whichMobile == "iphone" || mobileChecker.whichMobile == "itouch" || mobileChecker.whichMobile == "ipad" || mobileChecker.whichMobile == "android" || mobileChecker.whichMobile == "webos"){
				$('#largeMap').toggleClass('closed opened');
				e3ShowMap.animating = false;
				e3ShowMap.mapView = 'opened';
			} else {
				$('#largeMap').switchClass('closed','opened',500,function(){e3ShowMap.animating = false; e3ShowMap.mapView = 'opened';});
			}
//			$('#largeMap').bind('mouseout',e3ShowMap.timeToClose);
//			$('#exploreShowMap').bind('mouseout',e3ShowMap.timeToClose).addClass('opened');
			$('#exploreShowMap').addClass('opened');
			$('#largeMap').bind('mouseover',e3ShowMap.resetCloseTimer);
			$('#exploreShowMap').bind('mouseover',e3ShowMap.resetCloseTimer);
			//stop the small map
			if(!!$('#smallMap')){
			clearInterval(e3ShowMap.smallMapInt);
			}
			e3ShowMap.doSweep("largeMap");
			e3ShowMap.largeMapInt = setInterval(function(){e3ShowMap.doSweep("largeMap")}, e3ShowMap.sweepDelay);
		}
	},
	closeMap: function(){
		if(e3ShowMap.mapView == 'opened'){
			if(!!$('#smallMap').length){e3ShowMap.mapType="Home"}
			e3ShowMap.animating = true;
			if(mobileChecker.whichMobile == "iphone" || mobileChecker.whichMobile == "itouch" || mobileChecker.whichMobile == "ipad" || mobileChecker.whichMobile == "android" || mobileChecker.whichMobile == "webos"){
				$('#largeMap').toggleClass('closed opened');
				e3ShowMap.animating = false;
				e3ShowMap.mapView = 'closed';
			} else {
				$('#largeMap').switchClass('opened','closed',500,function(){e3ShowMap.animating = false; e3ShowMap.mapView = 'closed';})
			}
//			$('#largeMap').unbind('mouseout',e3ShowMap.timeToClose);
//			$('#exploreShowMap').unbind('mouseout',e3ShowMap.timeToClose).removeClass('opened');
			$('#exploreShowMap').removeClass('opened');
			$('#largeMap').unbind('mouseover',e3ShowMap.resetCloseTimer);
			$('#exploreShowMap').unbind('mouseover',e3ShowMap.resetCloseTimer);
			//stop the large map
			clearInterval(e3ShowMap.largeMapInt);
			if(!!$('#smallMap')){
			e3ShowMap.doSweep("smallMap");
			e3ShowMap.smallMapInt = setInterval(function(){e3ShowMap.doSweep("smallMap")}, e3ShowMap.sweepDelay);
			}
		}
	},
	switchMap: function(btn){//only for large map view
		var yPos = 0;
		if(e3ShowMap.switching != true){
			e3ShowMap.switching = true;
			e3ShowMap.mapType = $(btn).html().replace(/&nbsp;&nbsp;/g,"");
//			var pathOfMap = '/images/map/' + e3ShowMap.mapType.toLowerCase().replace(/ /g,"-") + '-lrg.gif';
			var mapChosen = e3ShowMap.mapType.toLowerCase().replace(/ /g,"-");
			// console.log('this map is: ' + e3ShowMap.mapType);
			// fade out blips and current map
			$(btn).parent().siblings().removeClass('selected');
			$(btn).parent().addClass('selected');
			e3ShowMap.clearBlipTimeOuts();
			$('#largeMap .blips').empty();
			$('#largeMap .sweeper').stop().css('left','-' + $('#largeMap .sweeper').width() + 'px');
			
			
//			$("#mapInteraction .maps img." + mapChosen).siblings().stop().fadeOut('slow', function(){
//				$("#mapInteraction .maps img").attr({src:pathOfMap, alt:e3ShowMap.mapType});
				switch(e3ShowMap.mapType){
					case "Concourse Level":
						$("#mapFloors h5 span").html('<a href="/exhibitor/view/158/nvidia-corporation/">Nvidia</a>, <a href="/exhibitor/view/249/crytek-gmbh/">Crytek</a>, <a href="/exhibitor/view/178/riot-games/">Riot Games</a>, <a href="/exhibitor/view/89/atlus-index-digital-media-inc/">Atlus</a>, <a href="/exhibitor/view/381/into-the-pixel/"><em>Into the Pixel</em></a> &amp; more... ');
						yPos=-644;
						break;
					case "West Hall":
						$("#mapFloors h5 span").html('<a href="/exhibitor/view/182/sony-computer-entertainment-america/">Sony</a>, <a href="/exhibitor/view/155/nintendo-of-america-inc/">Nintendo</a>, <a href="/exhibitor/view/41/capcom-entertainment-inc/">Capcom</a>, <a href="/exhibitor/view/92/bethesda-softworks-llc/">Bethesda</a>, <a href="/exhibitor/view/194/trion-worlds-inc/">Trion Worlds</a> &amp; more...');
						yPos=-322;
						break;
					case "South Hall":
					default:
						$("#mapFloors h5 span").html('<a href="/exhibitor/view/276/microsoft/">Microsoft</a>, <a href="/exhibitor/view/1/electronic-arts/">Electronic Arts</a>, <a href="/exhibitor/view/82/activision/">Activision</a>, <a href="/exhibitor/view/197/ubisoft-entertainment/">Ubisoft</a>, <a href="/exhibitor/view/185/square-enix-inc/">Square Enix</a> &amp; more...');
						yPos=0;
						break;
				}
				$("#mapInteraction .maps .wrapper").stop().animate({top:yPos},500, function(){
					clearInterval(e3ShowMap.largeMapInt);
					e3ShowMap.doSweep("largeMap");
					e3ShowMap.largeMapInt = setInterval(function(){e3ShowMap.doSweep("largeMap")}, e3ShowMap.sweepDelay);
					e3ShowMap.switching = false;
				});
//			});
		}
	},
	timeToOpen: function(){
		e3ShowMap.openTimer = setTimeout("e3ShowMap.openMap();",500);
	},
	timeToClose: function(){
		e3ShowMap.closeTimer = setTimeout("e3ShowMap.closeMap();",500);
	},
	resetCloseTimer: function(){
		clearTimeout(e3ShowMap.closeTimer);
	},
	resetOpenTimer: function(){
		clearTimeout(e3ShowMap.openTimer);
	},
	doSweep: function(whichMap){
		e3ShowMap.clearBlipTimeOuts();
		if($('.tipsy').length > 0){$('.tipsy').stop().fadeOut(function() { $(this).remove(); });}
		e3ShowMap.mapAndSweeperWidth = $('#' + whichMap + ' .maps img').width() + ($('#' + whichMap + ' .sweeper').width() * 2);
		$('#' + whichMap + ' .sweeper').css('left','-' + $('#' + whichMap + ' .sweeper').width() + 'px');
		$('#' + whichMap + ' .sweeper').animate({left: e3ShowMap.mapAndSweeperWidth}, e3ShowMap.sweepTime, "linear", e3ShowMap.plotPoints(whichMap));
	},
	plotPoints: function(whichMap){
		var blipX = 0;
		var blipY = 0;
		var dataset = [];
		var topMapCount = 0;
		var smallToBigRatio = .6985;
		var sizeOfBlip = 0;
		var sizeScale = 0;
		var addPixels = 7;

		if(e3ShowMap.mapType.length == 0) return;
		//check if map should be focusing on Home or Large Map
		if(e3ShowMap.mapType == "Home") {
			// Do both a south and west dataset
			topMapCount = e3ShowMap.lastDatasets["South Hall"].length;
			dataset = e3ShowMap.lastDatasets["South Hall"].concat(e3ShowMap.lastDatasets["West Hall"]);
		} else {
			dataset = e3ShowMap.lastDatasets[e3ShowMap.mapType];
		}
//		console.log(dataset + " | " + e3ShowMap.mapType);

		//clear Previous Points
		$('#' + whichMap + ' .blips').empty();
		if(!!dataset && !!dataset.length && dataset.length > 0){
			$.each(dataset, function(index, exhibitor) {
				if(exhibitor && exhibitor.exhibitorid) {
					var exLookup = getExhibitor(exhibitor.exhibitorid);
					if(exLookup) {
						sizeScale = exhibitor.percent * 100;
						if(whichMap=="largeMap"){
							sizeOfBlip = sizeScale + addPixels;
							blipX = exLookup.big.x;
							blipY = exLookup.big.y;
						} else {
							sizeOfBlip = (sizeScale + addPixels) * smallToBigRatio;
							blipX = exLookup.big.x * smallToBigRatio + 9;  //set X offset
							blipY = exLookup.big.y * smallToBigRatio;
							if(index + 1 > topMapCount){  //set Y offset for map after top one
								blipY += 200;
							}
						}
						var style = "position:absolute;top:" + blipY;
						style += "px;left:" + blipX;
						style += "px;opacity:0;filter:alpha(opacity=0);margin-top:-" + sizeOfBlip/2 + "px;margin-left:-" + sizeOfBlip/2 + "px; z-index:" + (100 - sizeScale);
						var link = '<a href="'+exLookup.url+'" rel="tipsy" style="display:block;" title="' + exLookup.name + '" onmouseover="e3ShowMap.focusBlip(this);" onmouseout="e3ShowMap.blurBlip(this);"><img src="/images/map/ping.png" alt="" style="width:'+ sizeOfBlip +'px;height:'+ sizeOfBlip +'px;"></a>';
						$("<li/>", {"style": style, "class": "blip" + index}).append(link).appendTo($('#' + whichMap + ' .blips'));		
						setTimeout(function(){
							e3ShowMap.showBlip(whichMap, "blip" + index);
						},(e3ShowMap.sweepTime*blipX) / e3ShowMap.mapAndSweeperWidth);
					}
					else {
						//console.log("Can't find exhibitor in map...");
					}
				}
			});
		}
	},
	showBlip: function (whichMap, whichBlip){
		$('#' + whichMap + ' .' + whichBlip).animate({
			opacity: 1
		}, 1000, "linear");
		$('#' + whichMap + ' .' + whichBlip + ' a[rel=tipsy]').tipsy({
			html: true,
			title: 'original-title',
			gravity: 's',
			fade: true,
			delayIn: 500,
			delayOut: 500
		});
		
		e3ShowMap.blipTimeOuts[whichBlip] = setTimeout(function(){
			e3ShowMap.hideBlip(whichMap, whichBlip);
		},e3ShowMap.sweepDelay-2000);
	},
	hideBlip: function(whichMap, whichBlip){
		//start to fade the blips
		$('#' + whichMap + ' .' + whichBlip).animate({
			opacity: 0
		}, 1000, "linear");
	},
	clearBlipTimeOuts: function(){
		for(key in e3ShowMap.blipTimeOuts){
			clearTimeout(e3ShowMap.blipTimeOuts[key]);
		}
	},
	focusBlip: function(whichBlip){
		//console.log($(whichBlip).find('img').attr('alt'));
		$(whichBlip).find('img').attr('src','/images/map/ping-hovered.png');
	},
	blurBlip: function(whichBlip){
		//console.log('blurring');
		$(whichBlip).find('img').attr('src','/images/map/ping.png');
	}
}

$(document).ready(function(){
	setTimeout(function(){e3ShowMap.init();},500);
});
