////////////////////////////////
// Functions for communicating TO Flash area map
////////////////////////////////

var flashFunctionsEnabled = false;
function enableFlashCallback(){
	flashFunctionsEnabled = true;
	ajax_areamap();
}

function birmFromFlash(){
	window.location.hash = "#birmingham=1";	
}
function constFromFlash(constId){	
	window.location.hash = "#constituency="+constId;
}
function constAndWardFromFlash(constId,wardId){
	window.location.hash = "#constituency="+constId+"&ward="+wardId;
}

function ajax_areamap() {

	if ( window.location.href.include("#") ) {

		var a_urlparts = window.location.href.split("#");
  		var ajax_url = a_urlparts[1];
	
		a_urlparts = a_urlparts[1].split("&");
		a_values = new Object;
	
		// Sorts all variables from hash location into associative array
		a_urlparts.each( function(currentPart){
			currentPart = currentPart.split("=");
			a_values[currentPart[0]] = currentPart[1];
		});
	
	} else {
		ajax_url = "";
	}

	$('areamap_menu').update('<img src=\"/data/birmingham/assets/images/common/loading-icon.gif\" />');
	$('areamap_item').update('<img src=\"/data/birmingham/assets/images/common/loading-icon.gif\" />');

	new Ajax.Updater('areamap_item', "/ajax-area-map-item?" + ajax_url, { 
		method: 'post'
	});

	new Ajax.Updater('areamap_menu', "/ajax-area-map?" + ajax_url, { 
		method: 'post'
	});
	

	function thisMovie(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
         }
     }

	// Communicate with Flash only if flashFunctionsEnabled = true
	if ( flashFunctionsEnabled && !a_values["updatedfromflash"]) {

		// Check first for a ward, ELSE for a constitency ELSE show Birmingham
		if ( a_values["ward"] ){

			thisMovie("flashareamap").showWardFromJS(a_values["ward"]);

		} else if ( a_values["constituency"] ) {

			thisMovie("flashareamap").showConstFromJS(a_values["constituency"]);

		} else {

			thisMovie("flashareamap").showBirmFromJS();

		}
		
	}
}

// Location checker timer
var currentHash = window.location.href;

function compareHref(){
	if ( window.location.href != currentHash ){
		currentHash = window.location.href;
		
		//Update all ajax content
		ajax_areamap();
	}
}

document.observe("dom:loaded", function() {

	ajax_areamap();

	compareTimer = setInterval(compareHref, 100);

}); 

	

