﻿//a(x)Map x=2 
//x.title = "Chinese Heat Map"
//x.description = "Displays map of China with color intensities that match given values on provincial scale.."
//x.version = 0.0.1 beta
//x.update = 2008.11.20
//x.author = Jingking
//x.email = axmap_x=2@jingking.net
//

	function extractData(data) {
		var id="";
		var val = new Array();
		for (var col = 0; col < data.getNumberOfColumns(); col++) 
		{
			if ( col < data.getNumberOfColumns()-1 )
				{
					val[col] = "";
					}
			for (var row = 0; row < data.getNumberOfRows(); row++) 
			{
				var formattedValue = data.getFormattedValue(row, col);
				formattedValue = formattedValue.replace(/\s/g,"");
				if ( row < data.getNumberOfRows()-1)
				{
					if (col == 0 )
					 {
						id = id + formattedValue + ",";
						}  
					else 
					 {
						val[col-1] = val[col-1] + formattedValue + ",";
						}
				}
				else
					{
					 if (col == 0 )
						{
						id = id + formattedValue;
							}  
					else 
						{
						val[col-1] = val[col-1] + formattedValue;
							}
				}
			}
		}
		 post_id = id;
		 post_val = val;
	}
	
	function initializeFlash() {
		var ie = navigator.appName.indexOf("Microsoft") != -1;
		Flash_chinesemap = (ie) ? window['chinesemap'] : document['chinesemap'];
		if (Flash_chinesemap)
		{
			return true;
		}
		else 
			return false;
	}

	function drawMap(data){
		var updateFlash;
		var timeLapse = 0;
		extractData(data);
		updateFlash = setInterval(function(){
			if (initializeFlash()) {
				try{
					Flash_chinesemap.jsInput(post_id, post_val, post_head, color_s, color_e);
					clearInterval(updateFlash);
				}catch(err){return true;}
			}
			else{
				if (timeLapse > 8){
					clearInterval(updateFlash);
					showinfo("Error! can not load Flash");
				}
				else timeLapse++;
			}
		}
		, 1000);
	}

	function updateGadgetHeight(){
		var updateHeight;
		var timeLapse = 0;
		updateHeight = setInterval(function(){
			if (gadgets) {
				try{
					gadgets.window.adjustHeight();
					clearInterval(updateHeight);
				}catch(err){return true;}
			}
			else{
				if (timeLapse > 7){
					clearInterval(updateHeight);
					showinfo("Error! can not resize height");
				}
				else timeLapse++;
			}
		}
		, 1000);
	}

	function showinfo(txt){		
		document.getElementById("showinfo").style.display="block";
		document.getElementById("showinfo").innerHTML=txt;
	}
