$(document).ready(function() {
    /* VIEW FROM THE URL */
	var viewType = '';
	if (parent.location.hash != null || parent.location.hash != "") {
		if (parent.location.hash.indexOf("View") != -1) {
			viewType = parent.location.hash.substring(1, parent.location.hash.indexOf("View", 0));
		}
	}
	switchRegion(viewType);

	markTabAsSelected('exploreTravelMap');
});

// JavaScript Document
var regionInfoArr = [
	{region: 'northwest', desc: 'Northwest', compassImg: '20.4_compass_northwest.gif', miniMapImg: '19.2_small_map_northwest.jpg', mainMapImg: '20.1_nw_region_map.jpg', pins: '/ajax/travel/regionalStations.html?reg=NW', cities: '/ajax/travel/regionalStations.html?reg=NW'},
	{region: 'southwest', desc: 'Southwest', compassImg: '20.4_compass_southwest.gif', miniMapImg: '19.2_small_map_southwest.jpg', mainMapImg: '20.1_sw_region_map.jpg', pins: 'incRegionPinsSW.html', cities: 'incRegionCitiesSW.html'},
	{region: 'northeast', desc: 'Northeast', compassImg: '20.4_compass_northeast.gif', miniMapImg: '19.2_small_map_northeast.jpg', mainMapImg: '20.1_ne_region_map.jpg', pins: 'incRegionPinsNE.html', cities: 'incRegionCitiesNE.html'},
	{region: 'southeast', desc: 'Southeast', compassImg: '20.4_compass_southeast.gif', miniMapImg: '19.2_small_map_southeast.jpg', mainMapImg: '20.1_se_region_map.jpg', pins: 'incRegionPinsSE.html', cities: 'incRegionCitiesSE.html'},
	{region: 'midwest', desc: 'Midwest', compassImg: '20.4_compass_north.gif', miniMapImg: '19.2_small_map_north.jpg', mainMapImg: '20.1_n_region_map.jpg', pins: 'incRegionPinsMW.html', cities: 'incRegionCitiesMW.html'},
	{region: 'south', desc: 'South', compassImg: '20.4_compass_south.gif', miniMapImg: '19.2_small_map_south.jpg', mainMapImg: '20.1_s_region_map.jpg', pins: 'incRegionPinsS.html', cities: 'incRegionCitiesS.html'},
	{region: 'national', desc: 'National', compassImg: '20.4_compass_north.gif', miniMapImg: 'small_destination_map.jpg', mainMapImg: 'destination_map.jpg', pins: 'incRegionPinsS.html', cities: 'incRegionCitiesS.html'}
];

// function for changing the regions when clicking on mini maps or region links on the explore pages
function switchRegion(regionCode) {

	// switch the view if necessary
	if (regionCode == 'list' || regionCode == 'map') {
		switchView(regionCode);
		// put the selected view in the URL
		parent.location.hash = regionCode + 'View';
		// don't need to do anything else
		return;
	}

	// get the objects on the page for the compass and map
	//		var _miniMapCompass = $("#imgMiniMapCompass");
	var _miniMap = $("#imgMiniMap");
	// get the path to the images folder from one of the objects
	var imgPath = '/images/travel/'

	// look through the array for the region we need
	reg = getRegionInfo(regionInfoArr, regionCode);

	var cssObj = {
		backgroundImage: "url(" + imgPath + reg.mainMapImg + ")"
	}

	// switch the main map
	$("#divDestinationMap").css({ "background-image": "url(" + imgPath + reg.mainMapImg + ")" });

	// switch the compass and mini map
	$("#imgMiniMapCompass").attr({
		src: imgPath + reg.compassImg,
		alt: reg.desc + " compass"
	});
	$("#imgMiniMap").attr({
		src: imgPath + reg.miniMapImg,
		alt: reg.desc + " map"
	});

	// load the map pins
	$('#nationalPositions').css('display', 'none');
	$('#southPositions').css('display', 'none');
	$('#southwestPositions').css('display', 'none');
	$('#southeastPositions').css('display', 'none');
	$('#northeastPositions').css('display', 'none');
	$('#northwestPositions').css('display', 'none');
	$('#midwestPositions').css('display', 'none');
	$('#' + pinsDivName(regionCode)).css('display', 'block');

	// load the list of cities for the area below the map
	$('#nationalCities').css('display', 'none');
	$('#southCities').css('display', 'none');
	$('#southwestCities').css('display', 'none');
	$('#southeastCities').css('display', 'none');
	$('#northeastCities').css('display', 'none');
	$('#northwestCities').css('display', 'none');
	$('#midwestCities').css('display', 'none');
	$('#' + citiesDivName(regionCode)).css('display', 'block');

	hideMapCallout();
	if (regionCode != '') {
		// put the selected view in the URL
		parent.location.hash = regionCode + 'View';
	}
}

var citiesDivName = function(regionCode) {
	if (regionCode == '') {
		regionCode = 'national';
	}
	return regionCode + 'Cities';
}

var pinsDivName = function(regionCode) {
	if (regionCode == '') {
		regionCode = 'national';
	}
	return regionCode + 'Positions';
}

// function for getting the region information from the multidimensional array
function getRegionInfo(arr, regCode) {
	if (regCode == '') {
		regCode = 'national';
	}
	for (x = 0; x < arr.length; x++) {
		if (arr[x].region.toLowerCase() == regCode.toLowerCase()) {
			return arr[x];
		}
	}
}

function switchView(view) {
	if (view == 'list') {
		$('#exploreTravelMap').css('display', 'none');
		$('#exploreTravelList').css('display', 'block');
	}
	if (view == 'map') {
		$('#exploreTravelMap').css('display', 'block');
		$('#exploreTravelList').css('display', 'none');
	}

	hideMapCallout();
}

function hideMapCallout() {
	$("#divMapCallout").hide();
	$("#divMapCallout").appendTo("body");
	hideShowSelects("divMapCallout");
}



/* ============================================================================ */
/* hideShowSelects()                                                            */
/*   This function hides or shows selects if they are behind divId.  The        */
/*   function loops through all the 'select' tags on the page and checks to see */
/*   four values: tl - top left                                                 */
/*                tr - top right                                                */
/*                bl - bottom left (*default)                                   */
/*                br - bottom right                                             */
/*                                                                              */
/*   The divId parameter can be either a string containing a single div id or   */
/*   it can be an array of div ids. Selects are hidden/shown depending on their */
/*   proximity to all of the div ids that are passed in.
 /* ============================================================================ */
function hideShowSelects(divIds) {
	var divs = new Array();
	var divXs = new Array();
	var divYs = new Array();
	var divWs = new Array();
	var divHs = new Array();

	useLoop = true;
	if (typeof divIds == "string") {
		divs[divs.length] = divIds;
	} else if (isArray(divIds)) {
		divs = divIds;
	} else {
		divXs[0] = divIds.style.pixelLeft;
		divYs[0] = divIds.style.pixelTop;
		divWs[0] = divIds.offsetWidth;
		divHs[0] = divIds.offsetHeight;
		useLoop = false;
	}

	if (useLoop) {
		for (var i = 0; i < divs.length; i++) {
			// x, y, w, h of the layer covering the selects
            divXs[i] = document.getElementById(divs[i]).style.pixelLeft;
			divYs[i] = document.getElementById(divs[i]).style.pixelTop;
			divWs[i] = document.getElementById(divs[i]).offsetWidth;
			divHs[i] = document.getElementById(divs[i]).offsetHeight;
			//alert("divs[" + i + "]" + divs[i] + ":" + divXs[i] + ":" + divYs[i] + ":" + divWs[i] + ":" + divHs[i]);
		}
	}

	var selects = document.getElementsByTagName("select");
	var coords;

	if (selects.length > 0) {
		for (var i = 0; i < selects.length; i++) {

			var selCovered = false;

			for (var j = 0; (j < divs.length) && (! selCovered); j++) {
				// coords is the position of the select
				coords = getPageCoords(selects[i]);
				selX1 = coords.x;
				selX2 = coords.x + selects[i].offsetWidth;
				selY1 = coords.y;
				selY2 = coords.y + selects[i].offsetHeight;
				//alert("selects[" + i + "]" + selects[i].name + ":" + selX1 + ":" + selX2 + ":" + selY1 + ":" + selY2);

				var Xcovered = false;
				var Ycovered = false;

				if (Xcovered || (divXs[j] <= selX1 && selX1 <= (divXs[j] + divWs[j]))
						|| (divXs[j] <= selX2 && selX2 <= (divXs[j] + divWs[j]))
						|| (selX1 <= divXs[j] && (divXs[j] + divWs[j]) <= selX2)) {
					Xcovered = true;
				}
				if (Ycovered || (divYs[j] <= selY1 && selY1 <= (divYs[j] + divHs[j]))
						|| (divYs[j] <= selY2 && selY2 <= (divYs[j] + divHs[j]))
						|| (selY1 <= divYs[j] && (divYs[j] + divHs[j]) <= selY2)) {
					Ycovered = true;
				}

				selCovered = (Xcovered && Ycovered);
			} // end for(var j

			// if any part of the select was covered, hide it
			if (selCovered) {
				selects[i].style.visibility = "hidden";
			} else {
				// make this select visible again if it was previously hidden
				//if(selects[i].style.visibility == "hidden") {
				selects[i].style.visibility = "visible";
				//}
			} // end if(selCovered
		} // end for(var i
	} // end if(selects.length
} // end function


/* this function gets the x and y coordinates of any element */
function getPageCoords(elementId) {
	var coords = {x: 0, y: 0}
	var element;

	/* if elementId is an object id use, get the object by id, else use the object */
	if (typeof elementId == "string") {
		/* it could be an ID */
		if (document.all)
			element = document.all[elementId];
		else if (document.getElementById)
			element = document.getElementById(elementId);
	} else {
		/* it is an object */
		element = elementId;
	}

	while (element) {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
		element = element.offsetParent;
	}
	return coords;
}

// Major version of Flash required
var requiredMajorVersion = 6;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 0;

var loadFlashRouteMap = function() {
	var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if (hasRightVersion) {  // if we've detected an acceptable version embed the flash movie
	AC_FL_RunContent_OnExploreTravelPage(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0',
		'width', '550',
		'height', '300',
		'src', '/flash/travelguide_routemap',
		'quality', 'high',
		'pluginspage', 'http://www.adobe.com/go/getflashplayer',
		'align', 'middle',
		'play', 'true',
		'loop', 'true',
		'scale', 'showall',
		'wmode', 'transparent',
		'devicefont', 'false',
		'id', 'travelguide_routemap',
		'bgcolor', '#ffffff',
		'name', 'travelguide_routemap',
		'menu', 'false',
		'allowFullScreen', 'false',
		'allowScriptAccess','always',
		'movie', '/flash/travelguide_routemap',
		'salign', ''
	);
	} else {  // flash is too old or we can't detect the plugin
		var alternateContent = 'This content requires the Adobe Flash Player, version 6.0.'
			+ '<a href="http://www.adobe.com/go/getflashplayer/">Get Flash</a>';
		$('#routeMap').html(alternateContent);
	}
};

function showMapTab(name) {
	$('.tab-container').hide();
	if ($('#' + name).html() == "") {
		loadFlashRouteMap();
		$('#' + name).show();
	} else {
		$('#' + name).show();
	}
	markTabAsSelected(name);
}
function markTabAsSelected(name) {
	$('div.tabs ul.tabNavigation a').removeClass('selected').parent().removeClass('selected');
	$('#' + name + 'Tab').addClass('selected').parent().addClass('selected');
}