var Demo = Class.create({
  initialize: function() {
  	this.hcParamArray = new Array();
	this.hcParamTypeArray = new Array();
	this.hcParamElementArray = new Array();
	this.hcElement = null;
	this.chartdata = null;
	this.ajaxobg = null;
	this.hcStaticValue = new Array();
	this.hcJsonUrl = "json_static.php";
	this.mapFileUrl = null;
	this.mapParent = null;
	this.map = null;
	this.mapId = null;
	this.mapWidth = null;
	this.mapHeight = null;
	this.map_xml_head = "<map bgcolor='000000' borderColor='000000' connectorColor='FFFFFF' connectorAlpha='90' fillAlpha='100'  showBevel='0'   legendBgColor='000000' legendBorderColor='000000' legendShadow='0' baseFontColor='FFFFFF' canvasBorderColor='000000' toolTipBgColor='000000' showShadow='0' fillColor='333333'  ";
	this.cur_map_xml_entity_blank = "<entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' /><entity id='' value='0' color='222222' />"
	this.mapEntityExpections = "";
	this.cur_map_xml_entity ="";
	this.mapShowLabels = 0;
	this.mapShowLegend = 0;
	this.primaryVariable = 'value';
	this.displayVariable = 'name';
	this.demoArray =null;
	this.demoArrayCurrent = 0;
	this.demoNameArray =null;
	this.numberSuffix = ' Years';
	this.mapHoverColor = '0000FF';
	this.PausePlayIconID = '';
	this.isPaused = false;

  },

  stopDemo : function ()
  {
	  if (!this.isPaused)
	  {
		  $(this.PausePlayIconID).title = 'Play';
		  /*stop demo  */
		  this.isPaused = true;
		  
		  
	  }
  },
  playDemo : function ()
  {

	  if (this.isPaused)
	  {
		  $(this.PausePlayIconID).title = 'Pause';
		  /*play demo  */
		  this.isPaused = false;
		  //this.nextDemo();
	  }
  },

  setPausePlayIconID : function (PausePlayIconID)
  {
	  this.PausePlayIconID = PausePlayIconID;
	  //$(this.PausePlayIconID).observe('onclick', 
		//							  this.PausePlayIconClick.bind(this)
			//						  );
  },
  
  PausePlayIconClick : function ()
  {
		if (this.isPaused)
		{
			this.playDemo();//alert('playDemo');
		}
		else
		{
			this.stopDemo();//alert('stopDemo');
		}  
		
  },
  
  mapSetLegend : function (legend)
  {
	  this.mapShowLegend = legend;
  },
  addDemoArray : function (demoArray)
  {
	this.demoArray  = demoArray;
	this.Demo = true;
	
  },
  setNumberSuffix : function (numberSuffix)
  {
	  this.numberSuffix = numberSuffix;
  },
  addDemoNameArray : function (demonamearray)
  {
	  this.demoNameArray = demonamearray;
  },
  showMapLabels : function ()
  {
	  this.mapShowLabels = 1; 
  },
  addMap : function (mapFileUrl, Parent, mapId, mapWidth, mapHeight, useNoData)
  {
	  this.useMap = true;
	  this.mapFileUrl = mapFileUrl;
	  this.mapParent = Parent;
	  this.mapId = mapId;
	  this.mapWidth = mapWidth;
	  this.mapHeight = mapHeight;
	  this.useNoData = useNoData;
	  $(this.mapParent).style.height = this.mapHeight;
	  $(this.mapParent).style.width = this.mapWidth;
	  
	   var map_xml_head = this.map_xml_head +" showlabels='"+this.mapShowLabels+"' legendPosition='"+this.mapLegendAlign+"' showLegend='"+this.mapShowLegend.toString()+"' numberSuffix='"+this.numberSuffix+"' hoverColor='"+this.mapHoverColor+"' ><data>";
	  
	this.map = new FusionMaps(this.mapFileUrl, this.mapId, this.mapWidth, this.mapHeight, "0", "1", "000000");
	this.map.setDataXML(map_xml_head+this.cur_map_xml_entity_blank+"</data><colorRange></colorRange></map>");	
	this.map.render(this.mapParent);
  },
  setmapFileUrl : function (mapFileUrl)
  {
	  this.mapFileUrl = mapFileUrl;
  },
  calculateMinMax : function ()
  {
	this.cur_map_xml_entity = "";
	this.cur_map_xml_footer = "";
	var map_xml_entity = "";
	for (w=0;w<this.chartdata.chart.countries.countryitem.length;w++)
	{
		var country_item = this.chartdata.chart.countries.countryitem[w];
			var tmpColor = '222222';
			if (!this.isFirst)
			{
				tmpColor = country_item.color;
			}
			this.cur_map_xml_entity +=   "<entity id='"+country_item.id.toString()+"' value='"+country_item.value.toString()+"' color='"+tmpColor+"'  />";	
		
	}
	for (var p = 0; p < this.chartdata.chart.legend.legenditem.length ; p++)
	{
		this.cur_map_xml_footer += "<color minValue='"+ this.chartdata.chart.legend.legenditem[p].minvar +"' maxValue='"+ this.chartdata.chart.legend.legenditem[p].maxvar +"' displayValue='"+this.chartdata.chart.legend.legenditem[p].minvar+" - "+ this.chartdata.chart.legend.legenditem[p].maxvar+"' color='"+this.chartdata.chart.legend.legenditem[p].color+"' />";	
	}
	this.cur_map_xml_footer = "</data><colorRange>"+this.cur_map_xml_footer+"</colorRange></map>";
  },
  
  renderMap : function ()
  {
	var map_xml_head = this.map_xml_head +" showlabels='"+this.mapShowLabels+"' legendPosition='"+this.mapLegendAlign+"' showLegend='"+this.mapShowLegend.toString()+"' numberSuffix='"+this.numberSuffix+"' hoverColor='"+this.mapHoverColor+"' ><data>";
	var xml = map_xml_head+this.cur_map_xml_entity+this.cur_map_xml_footer;
	
			 try
  {
	  this.map.setDataXML(xml);
	   $('hc_map_title').style.visibility = "visible";
  }
catch(err)
  {
	  $('hc_map_title').style.visibility = "hidden";
	  //alert(err);
  }
		 
	
		
  },
  setJsonUrl : function (Url){
	  this.hcJsonUrl = Url;
  },
  addParam : function (Param, ParamType, ParamElement){
	this.hcParamArray.push(Param);
	this.hcParamTypeArray.push(ParamType);
	this.hcParamElementArray.push(ParamElement);
  },
  updateData : function ()
  {
		var tmp_params = "";
		for (var i=0;i<this.hcParamArray.length;i++)
		{
			if (this.hcParamTypeArray[i] == 'combo')
			{
				tmp_params += this.hcParamArray[i]+"="+$(this.hcParamElementArray[i]).options[$(this.hcParamElementArray[i]).selectedIndex].value+"&";
				this.hcStaticValue[this.hcParamArray[i]] = $(this.hcParamElementArray[i]).options[$(this.hcParamElementArray[i]).selectedIndex].value;
			}
			if (this.hcParamTypeArray[i] == 'text')
			{
				tmp_params += this.hcParamArray[i]+"="+$(this.hcParamElementArray[i]).value+"&";
				this.hcStaticValue[this.hcParamArray[i]] = $(this.hcParamElementArray[i]).value;
			}
		}
		this.ajaxobg = new Ajax.Request(this.hcJsonUrl, {
			 method:'get',
			 parameters: tmp_params,
			 onSuccess: function(transport){
											  var json ;
												  try
												  {
													json=transport.responseText.evalJSON();
													 this.chartdata=json;
													 this.dataLoadCompleate();
												  }
													catch(err)
												  {
																										  
												  }
			}.bind(this),
			onException: function(transport,e){
					alert(e.message+ e.name);
			}
	   }); 
  },
  dataLoadCompleate : function (){
	this.calculateMinMax();
	$('hc_map_title').innerHTML =  this.demoNameArray[this.demoArrayCurrent];
	this.renderMap();
	//alert(this.isPaused);
	if ( !this.isPaused )
	{
		setTimeout(this.nextDemo.bind(this, null), 9000);
	}
	
  },
  nextDemo : function ()
  {
	if ( this.isPaused )
	{
		setTimeout(this.nextDemo.bind(this, null), 9000);
		return;
	}
	  
		  if (this.demoArrayCurrent != this.demoArray.length-1)
		  {
			this.demoArrayCurrent ++  ;
		  }
		  else
		  {
			  this.demoArrayCurrent = 0;
		  }
		  $('cause').value =  this.demoArray[this.demoArrayCurrent];
		  
		  this.updateData();

  }
}); 