var movieName = "AreaMap";

function thisMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

/* Checks if movie is completely loaded.  Returns true if yes, false if no. */
function movieIsLoaded (theMovie) {
	return (typeof(theMovie) != 'undefined') ? theMovie.PercentLoaded() == 100 : false;
}

/* This is an example
function selectMapAreadID(id) {
	var inputs = document.getElementsByName("MapAreaID");
	for(var i=0; i<inputs.length; i++) {
		var input = inputs[i];
		input.checked = (input.value == id);
	}
} */

/* Flash movie will call me when a new map is loaded so I can re-load the area checkbox list in html */
function loadAreaList(mapID) {
	var map = mlsMaps['m' + mapID];
	var areasElem = $('li#mapAreas');
	var selectAreas = $.cookie('AREAS');

	/* MAP TITLE */
	$('div#flashMap h2').html('Choose an area in ' + map.t);

	/* AREA LIST */
	var content = '<ol>';
	if (map.ms) { content += '<li><input type="checkbox" id="MapAreaToggle" name="MapAreaToggle" tabindex="1" onclick="checkFlashAll(this.checked);" /><label for="MapAreaToggle">Select All<\/label><\/li>'; }
	for (var i in map.ars) { content += '<li><input type="checkbox" id="MapAreaID_' + map['ars'][i]['id'] + '" name="SearchMapAreaID" value="' + map['ars'][i]['id'] + '" tabindex="1" onclick="areaBoxOnCheck(this);" \/><label for="MapAreaID_' + map['ars'][i]['id'] + '">' + map['ars'][i]['n'] +'<\/label><\/li>'; }
	areasElem.html(content);
	areasElem.get(0).scrollTop = 0;

	/* SELECT THE AREAS ON THE MAP */
	if(selectAreas) {
		var areaAry = selectAreas.split(',');
		for(var i = 0; i < areaAry.length; i++) { checkFlashArea(areaAry[i], true); }
	}

	/* BACK BUTTON */
	if (map.pidnt) {	
		var backElem = $('span#flashMapBack');
		if (backElem.size() == 0) {
			backSpan = document.createElement('span');
			backSpan.id = 'flashMapBack';
			backSpan.innerHTML = 'Back';
			$('div#flashMap').append(backSpan);
			backElem = $('span#flashMapBack');
		}
		backElem.click( function() { loadMap('/common/content/listing/maps/' + map.mls + '/', map.pidnt); } ).show();
	} else {
		$('span#flashMapBack').hide();
	}

	/* UPDATE MAP ID IN HIDDEN FIELD */
	$('input#hsfMapID').val(mapID);
}

/* AJAX VERSION */
function loadAreaList2(mapID) {
	var url = window.location.pathname + '?a=listing.loadMapAreas&MapID=' + mapID;
	$.ajax({type:'get', url:url, dataType:'script', async:true, complete:function(x, s) {
		$('li#mapAreas').html(response.content); prepareAreas();
		$('div#flashMap h2').html(response.maptitle);
		if (response.parentmap) {
			var backElem = $('span#flashMapBack');
			if (backElem.size() == 0) {
				backSpan = document.createElement('span');
				backSpan.id = 'flashMapBack';
				backSpan.innerHTML = 'Back';
				$('div#flashMap').append(backSpan);
				backElem = $('span#flashMapBack');
			}
			backElem.click(
				function() {
					loadMap('/common/content/listing/maps/' + response.mls + '/', response.parentmap);
				}
			).show();
		} else {
			$('span#flashMapBack').hide();
		}
		$('li#mapAreas input').each(
			function() { if (this.checked) { checkFlashArea(this); } }
		)
	}});
}

/* Flash movie will call me to check/uncheck an area in the html checkbox list */ 
function checkArea(areaID, isChecked) {
	$('input#MapAreaID_' + areaID).each(
		function() { this.checked = (isChecked.toLowerCase() == 'true'); }
	);
}

/* I will ask the Flash movie to load a map */
function loadMap(dir, map) {
	thisMovie(movieName).loadMap(dir, map);
}

/* I'm called whenever an area checkbox is clicked */
function areaBoxOnCheck(checkbox) {
	checkFlashArea(checkbox.id.split('_')[1], checkbox.checked);
}

/* I will ask the Flash movie to check/uncheck an area */
function checkFlashArea(areaID, isChecked) {
	thisMovie(movieName).checkArea(areaID, isChecked);
}

/* I will ask the Flash movie to check/uncheck all areas */
function checkFlashAll(isChecked) {
	thisMovie(movieName).toggleAllAreas(isChecked);
	/* IE IS NOT CHECKING ALL THE CHECKBOXES (ONLY THE LAST ONE) */
	$('li#mapAreas ol li input[@name="SearchMapAreaID"]').each(
		function() { this.checked = isChecked; }
	);
}

function checkAllInputs(isChecked) {
	$('li#mapAreas ol li input[@name="SearchMapAreaID"]')
}

function prepareAreas() {
	/* ONCLICK - SELECT MAP AREA */
	$('li#mapAreas ol li input').click( function(e) { checkFlashArea(this); } );
}
