var mapHolder;
var statsHolder;
var currentArea;
var showDefault;
var lastOver;
var areaColours = new Array("#9fd5b5","#1c3f95","#00aeef","#ffc423","#7170b3","#8cc63f","#c41230","#009590","#ed8037","#ec118f","#9fa615","#f1f268","#30b626","#5ea0de");
var textColours = new Array("#3b3b3b","white","white","#3b3b3b","white","white","white","white","white","white","white","#3b3b3b","white","white");
var tabIndex = 0;

$(document).ready(function(){
	initMap();
});

function initMap()
{
	$("#smartOne").hide();
	$("#smartTwo").hide();
	
	mapHolder = $("#mapHolder li");
	statsHolder = $("#statsHolder > *");
	
	$("#mapTabsHolder li").click(function(){selectTab(this);});
	//$("#tabAll").click(function(){ $("#smartOne").show(); });
	$("#tabSmart").click(function(){ $("#smartTwo").show(); });
	
	$("area").mouseover(function(){mouseOverMap(this);});
	$("area").mouseout(function(){mouseOutMap(this);});
	$("area").click(function(){setCurrentArea(this);});
	
	$(mapHolder).not("li:first-child").fadeTo(1,0);
	
	//Show only the first tab
	$("#mapTabsHolder li:first-child a").addClass("current");
	$("#statsHolder > *").hide();
	$("#statsHolder li:nth-child(1)").show();
	
	//Hide the child info elements until rolled over
	$("#statsHolder > * > * > * > * > *").hide();
	
	//Show London, the default area
	currentArea = 3;
	showStatsFromIndex(currentArea, true);
	showFromIndex(currentArea,true);
		
	updateElementCounts();	
}

function updateElementCounts()
{
	//Loop round the four product types
	for (var level1=0; level1<statsHolder.length; level1++)
	{			
		//Count every item (UK wide)
		// This was tripping up IE7 - lets just deduct 14 - the number of regions
		//var allRegionsCount = $(statsHolder[level1]).find("* > * > * > * > * > *").not("li:empty");
		var allRegionsCount = $(statsHolder[level1]).find("* > * > * > * > * > *");
		
		//Grab the product name
		var productNode = $("#statsHolder > *:nth-child("+(level1+1)+") > h2");
		var productName = $(productNode).html();
		
		//Add the count to the top level item
		var allClients = allRegionsCount.length-14;
		var allClientsPhrase = "<strong>" + allClients + "</strong>" + ((allClients == 1) ? " client " : " clients ") + "in "
			
		var allNode = $("#statsHolder > *:nth-child("+(level1+1)+") > ul > li > p").not("li:empty");
		$(allNode).prepend(allClientsPhrase);
		$(allNode).append(((allClients == 1) ? " uses " : " use ") + productName);
		$(allNode).addClass("allNode rc3");
		
		//Grab each indevidual region
		var regions = $(statsHolder[level1]).find("* > * > * > * > ul");
		for (var level2=0; level2<regions.length; level2++)
		{
			//Count the items per region
			//var clientCount = $(regions[level2]).find("li").not(".empty");
			//var clientCount = $(regions[level2]).find("li").not("li:contains('empty')");
			
			// This was tripping up IE7 - lets just deduct 1
			//var clientCount = $(regions[level2]).find("li").not("li:empty");
			var clientCount = $(regions[level2]).find("li");
			
			//Add the client count to the region title
			var regionClients = clientCount.length-1;
			var regionNode = $("#statsHolder > *:nth-child("+(level1+1)+") > * > li:nth-child(1) > ul > li:nth-child("+(level2+1)+") >p");
			if (regionClients > 0)
			{
				var regionClientsPhrase = "<strong>" + regionClients + "</strong>"  + ((regionClients == 1) ? " client " : " clients ") + "in "
				$(regionNode).prepend(regionClientsPhrase);
				$(regionNode).append(((regionClients == 1) ? " uses " : " use ") + productName);
			}		
			$(regionNode).addClass("regionNode rc3");
			$(regionNode).css("background-color", areaColours[level2]);
			$(regionNode).css("color", textColours[level2]);
		} 
	}
	
	addCornersToElements("#statsHolder p");
}

function selectTab(what)
{
	$("#mapTabsHolder a").removeClass("current");
	$(what).find("a").addClass("current");
	
	tabIndex = $("#mapTabsHolder li").index(what);
	$("#statsHolder > *").hide();
	$("#statsHolder > *:nth-child(" + (tabIndex+1) + ")").show();
	
	$("#smartOne").hide();
	$("#smartTwo").hide();
	
	showStatsFromIndex(currentArea, true);
}

function setCurrentArea(what)
{
	var areaIndex = what.id.split("_")[1];
	
	showStatsFromIndex(currentArea, false);
	showStatsFromIndex(areaIndex, true);
	
	currentArea = areaIndex;
}

function mouseOverMap(what)
{
	showFromObj(what,true);
	
	clearTimeout(showDefault);
}

function mouseOutMap(what)
{
	showFromObj(what,false);
	
	//Set a time out to trigger the current area when nothing is rolled over
	showDefault = setTimeout("showFromIndex(currentArea, true); lastOver=currentArea", 100);
}

function showFromObj(what, show)
{
	var areaIndex = what.id.split("_")[1];
		
	if (currentArea != areaIndex || lastOver != currentArea)
	{
		showFromIndex(currentArea, false);
		showFromIndex(areaIndex, show);
	
		if (show == true)
		{
			lastOver = areaIndex;
		}
	}
}

function showFromIndex(areaIndex, show)
{
	$(mapHolder[areaIndex]).show();
	if (show == true)
	{
		$(mapHolder[areaIndex]).fadeTo(1,1);
		//$("#statsHolder > * > * > * > * > li:nth-child("+areaIndex+")").show();
	}
	else
	{
		$(mapHolder[areaIndex]).fadeTo(1,0);
		//$("#statsHolder > * > * > * > * > li:nth-child("+areaIndex+")").hide();
	}
}

function showStatsFromIndex(areaIndex, show)
{
	if (show == true)
	{
		var theBlock = $("#statsHolder > * > * > * > * > li:nth-child("+areaIndex+")");
		//$(theBlock).slideDown(300);
		$(theBlock).show();
		if ($(theBlock).children("ul:contains('*')").length > 0 && tabIndex == 0)
		{
			$("#smartOne").show();
		}
		else
		{
			$("#smartOne").hide();	
		}
	}
	else
	{
		//$("#statsHolder > * > * > * > * > li:nth-child("+areaIndex+")").slideUp(600);
		$("#statsHolder > * > * > * > * > li:nth-child("+areaIndex+")").hide();
	}
}
