// map locations
var mapLocations = new Array();
					// x, y, location name, location description (filename derrived from index)
mapLocations[1] = [348,229,"Fort Collins, CO","The journey begins in Ft. Collins. The following gallery of images features a sampling of some of the people we spoke to. "];
mapLocations[2] = [351,252,'Johnsons Corner, CO',"The plan...talk to everyone we see."];
mapLocations[3] = [375,272,'Kit Carson, CO',"Regular people get it."];
mapLocations[4] = [390,293,'Hugo, CO',"Thankfully there are lots of diners on the raod."];
mapLocations[5] = [414,328,'Oklahoma',"Heading east through okie country."];
mapLocations[6] = [475,334,'Enid, OK',"Staying ahead of the storm."];  
mapLocations[7] = [511,339,'Cleveland, OK',"The people seem to understand the problem."];
mapLocations[8] = [575,360,'Poplar Grove, AR',"Catfishin' and learning about America."];
mapLocations[9] = [598,370,'Stuttgart,AR',"You can't spend more than you take in forever."];
mapLocations[10] = [618,352,'Memphis, TN',"Walking in Memphis with my feet ten feet off of Beale."];
mapLocations[11] = [639,340,'Moscow and LaGrange TN',"Lessons from Viola on the cost of interest."];
mapLocations[12] = [665,325,'Dickson, TN',"Regular people get it."];
mapLocations[13] = [701,274,'Frankfort, KY',"The people seem to understand the problem."];
mapLocations[14] = [707,247,'Cincinatti, OH',"People who fight terrorists get it."];
mapLocations[15] = [735,223,'Gambier, OH','Interviewing people in front of a provebial "Bridge to Nowhere"'];
mapLocations[16] = [748,201,'Youngstown, OH',"On the radio with Louie Free and Steve Forbes."];
mapLocations[17] = [783,222,'Morgantown, WV to Frederick, MD',"Young people are critical to Fixing America."];
mapLocations[18] = [832,227,'Washington, DC, and Virginia Area',"Residents and visitors alike know the source of the biggest crisis facing our great nation."];
mapLocations[19] = [845,164,'Through Pennsylvania to New York',"Regular people seem to understand the problem."];
mapLocations[20] = [873,123,'Bennington, VT',"Regular people understand what needs to be done."];
mapLocations[21] = [913,124,'Massachusetts',"The people seem to understand the problem."];
mapLocations[22] = [911,143,'Rhode Island',"Regular people get it."];
mapLocations[23] = [873,177,'Through Connecticut to New York City',"The people seem to understand the problem."];
//mapLocations[24] = [348,189,'Location24',"Description24"];

// state
var currentSelectedLocation = 0; 
var currentRollLocation = 0; 

$(document).ready(function(e) 
{
    // position hitareas
	var map = $('#map-locations');
	for (var i = 1; i < mapLocations.length; i++)
	{
		$(map).append('<a href="#" id="hitarea-'+i+'" class="location-hitarea" style="position:absolute;left:' + (mapLocations[i][0] - 10) + 'px;top:' + (mapLocations[i][1] - 10) + 'px;" title="'+ mapLocations[i][2] +'"><img src="img/map-hitarea.png" alt=""></a>');
		$(map).append('<img class="hitarea-roll" id="roll-'+i+'" style="position:absolute;left:' + (mapLocations[i][0] - 15) + 'px;top:' + (mapLocations[i][1] - 15) + 'px;display:none;" alt="" src="img/map-hitarea-roll.png">');
		$(map).append('<img class="hitarea-roll" id="roll-'+i+'" style="position:absolute;left:' + (mapLocations[i][0] - 15) + 'px;top:' + (mapLocations[i][1] - 15) + 'px;display:none;" alt="" src="img/map-hitarea-roll.png">');
	}
	
	$('.location-hitarea').mouseenter(rollOnHandler);
	$('.hitarea-roll').mouseleave(rollOutHandler);
	$('.hitarea-roll').click(selectLocationHandler);
	
	// load default section data	
	$('#location-images').fadeOut(1);
	loadLocation();
});

function rollOnHandler()
{
	// get id
	currentRollLocation = parseInt($(this).attr('id').toString().split('-')[1]);
	// turn off all other hitareas
	disableRollover();
	// enable roll
	$('#roll-'+currentRollLocation).css('display','block');
	
	//$('#gallery-images article').append('<div id="location-caption" class="transparent"><p>'+mapLocations[currentRollLocation][2]+'</p></div>');
	$('#location-caption').html(mapLocations[currentRollLocation][2]);
	var xLoc = (mapLocations[currentRollLocation][0] + 10);
	if (xLoc >= 870)
	{
		xLoc = (930 - parseInt($('#location-caption').width())) + 'px';
		var yLoc = (mapLocations[currentRollLocation][1] - 55) + 'px';
	}
	else
	{
		xLoc += 'px';
		var yLoc = (mapLocations[currentRollLocation][1] - 30) + 'px';
	}
	$('#location-caption').css({'visibility':'visible','left':xLoc,'top':yLoc});
	
}

function rollOutHandler()
{
	// disable roll
	if (currentSelectedLocation != currentRollLocation)
	{
		$('#roll-'+currentRollLocation).css('display','none');
		$('#location-caption').css('visibility','hidden');
	}
	if (currentSelectedLocation > 0)
	{
		$('#roll-'+currentSelectedLocation).css('display','block');
		$('#location-caption').html(mapLocations[currentSelectedLocation][2]);
		var xLoc = (mapLocations[currentSelectedLocation][0] + 10);
		if (xLoc >= 870)
		{
			xLoc = (930 - parseInt($('#location-caption').width())) + 'px';
			var yLoc = (mapLocations[currentSelectedLocation][1] - 55) + 'px';
		}
		else
		{
			xLoc += 'px';
			var yLoc = (mapLocations[currentSelectedLocation][1] - 30) + 'px';
		}
		$('#location-caption').css({'visibility':'visible','left':xLoc,'top':yLoc});
	}
}

function disableRollover()
{
	if (currentSelectedLocation > 0)
	{
		$('#roll-'+currentSelectedLocation).css('display','none');
	}
	$('#roll-'+currentRollLocation).css('display','none');
}

function selectLocationHandler()
{
	currentSelectedLocation = lastRollLocation = parseInt($(this).attr('id').toString().split('-')[1]);
	
	// update title and description
	$('#location-name').html(mapLocations[currentSelectedLocation][2]);
	$('#location-description').html(mapLocations[currentSelectedLocation][3]);
	
	// load data
	loadLocation();
}

function loadLocation()
{
	var checkInt = /\d+/; 
	if ((currentSelectedLocation.toString().match(checkInt))&&(currentSelectedLocation < mapLocations.length))
	{
		$('#location-images').fadeOut(250);
		setTimeout(ajaxLoad, 350);
	}
}

function ajaxLoad()
{
	$.ajax({
	  url: "gallery/" + currentSelectedLocation + ".html",
	  success: function(data){
		$('#location-images').html(data);
		var newHeight = parseInt(458 - parseInt($('#location-description').height()));
		$('#location-images').css('height',newHeight + 'px');
		$('#location-images').fadeIn(1000);
	  }
	})
}

function validateForm()
{
	var f = document.getElementById('commentForm');
	var valid = true; 
	if (f.name.value == '') { flagInput(f.name); valid = false; }
	if (f.email.value.indexOf('@') == -1) { flagInput(f.email); valid = false; }
	if (f.subject.value == '') { flagInput(f.subject); valid = false; }
	if (f.message.value == '') { flagInput(f.message); valid = false; }
	
	if (valid) { f.submit(); }
}

function flagInput(e)
{
	$(e).clearQueue();
	$('.required-element').clearQueue();
	$(e).css({backgroundColor:'darkred'}).delay(300).animate({backgroundColor:'darkgrey'},500);
	$('.required-element').css({color:'#990000'}).delay(300).animate({color:'#666666'},500);
}


(function(d){d.each(["backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","color","outlineColor"],function(f,e){d.fx.step[e]=function(g){if(!g.colorInit){g.start=c(g.elem,e);g.end=b(g.end);g.colorInit=true}g.elem.style[e]="rgb("+[Math.max(Math.min(parseInt((g.pos*(g.end[0]-g.start[0]))+g.start[0]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[1]-g.start[1]))+g.start[1]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[2]-g.start[2]))+g.start[2]),255),0)].join(",")+")"}});function b(f){var e;if(f&&f.constructor==Array&&f.length==3){return f}if(e=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(f)){return[parseInt(e[1]),parseInt(e[2]),parseInt(e[3])]}if(e=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(f)){return[parseFloat(e[1])*2.55,parseFloat(e[2])*2.55,parseFloat(e[3])*2.55]}if(e=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(f)){return[parseInt(e[1],16),parseInt(e[2],16),parseInt(e[3],16)]}if(e=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(f)){return[parseInt(e[1]+e[1],16),parseInt(e[2]+e[2],16),parseInt(e[3]+e[3],16)]}if(e=/rgba\(0, 0, 0, 0\)/.exec(f)){return a.transparent}return a[d.trim(f).toLowerCase()]}function c(g,e){var f;do{f=d.curCSS(g,e);if(f!=""&&f!="transparent"||d.nodeName(g,"body")){break}e="backgroundColor"}while(g=g.parentNode);return b(f)}var a={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[153,153,153],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[200,60,60],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0],transparent:[255,255,255]}})(jQuery);
