﻿// Copyright(c) 2008-2009 IT One, Ltd, Slovakia
// Do not use, change or distribute.

function handleRefreshChart(){
	var combo = document.getElementById("chrtGrp");
	var groupID = combo.options[combo.selectedIndex].value;
	combo = document.getElementById("chrtCat");
	var catID = combo.options[combo.selectedIndex].value;
	combo = document.getElementById("chrtProp");
	var propID = combo.options[combo.selectedIndex].getAttribute("propID");
	combo = document.getElementById("chrtFunc");
	var func = combo.options[combo.selectedIndex].value;
	
	if (func == "COUNT")
		propID = -1;
	
	CleverGlobeService.GetCountryGroupAggregates(groupID, catID, propID, func, function(res){
		var chartRequest = "http://chart.apis.google.com/chart?";
		chartRequest += "cht=t&chtm=world&chs=440x220";
		chartRequest += "&chco=FFFFFF,00FF00,FF0000,FFFF00";
		chartRequest += "&chf=bg,s,EAF7FE";
		chartRequest += "&chld=";
		var i;
		var min = Number(res[0]);
		var max = Number(res[1].replace(',', '.'));
		for (i = 2; i < res.length; i+=2){
			chartRequest += res[i];
		}
		chartRequest += "&chd=t:";
		for (i = 3; i < res.length; i+=2){
			chartRequest += (Number(res[i].replace(',', '.')) / max * 100);
			if (i < res.length - 1)
				chartRequest += ",";
		}
		var img = document.getElementById("chartImg");
		img.setAttribute("src", chartRequest);
	});
}

function initCharts(){
	var mapCenter = map.getCenter();
	CleverGlobeService.GetCountryAndContinentFromPoint(mapCenter.lat(), mapCenter.lng(), 1, function(res){
	
		CleverGlobeService.AggrGetCountryGroupTypes(function(types){
			var i,j;
			var combo = document.getElementById("chrtGT");
				
			for (j = 0; j < types.length; j+=2){ 
				var newOption = document.createElement("option");
				newOption.text = types[j+1];
				newOption.value = types[j];
				combo.options.add(newOption);
			}
			
			var groupTypeID = combo.options[combo.selectedIndex].value;
			
			CleverGlobeService.AggrGetCountryGroups(groupTypeID, function (groups){
				var combo = document.getElementById("chrtGrp");
					
				for (j = 0; j < groups.length; j+=2){ 
					var newOption = document.createElement("option");
					newOption.text = groups[j+1];
					newOption.value = groups[j];
					combo.options.add(newOption);
				}
					
				if (res.length > 0){ //select continent
					var vi;
					for (vi = 0; vi < combo.options.length; vi++){
						if (combo.options[vi].value == res[2]){
							combo.selectedIndex = vi;
						}
					}
				}
				
				//get categories
				var i, j, c;
				var combo = document.getElementById("chrtCat");
				
				for (j = 0; j < layers.length; j++){
					if (layers[j].childs.length == 0){
						var newOption = document.createElement("option");
						newOption.text = layers[j].name;
						newOption.value = layers[j].id;
						combo.options.add(newOption);
					}
				}
				
				var catID = combo.options[combo.selectedIndex].value;
				//get properties for first layer (gas stations)
				CleverGlobeService.GetPropertiesForCategoryWithSelID(culture, 0, catID, null, function(props){
					var combo = document.getElementById("chrtProp");
					for (c = 1; c < props.length; c+=3){
						var newOption = document.createElement("option");
						newOption.text = props[c];
						newOption.setAttribute("propID", props[c+1]);
						newOption.value = props[c+2];
						combo.options.add(newOption);
					}
					
					if (combo.options.length > 6)
						combo.selectedIndex = 6;

					//get avaiable aggregation operations for property type
					if (combo.selectedIndex != -1){
						var type = combo.options[combo.selectedIndex].value;
						CleverGlobeService.GetAggregateOperations(Number(props[0]), type, function(opers){
							var combo = document.getElementById("chrtFunc");
							for (c = 1; c < opers.length; c+=2){
								var newOption = document.createElement("option");
								newOption.text = opers[c];
								newOption.value = opers[c];
								combo.options.add(newOption);
							}
							
							if (combo.options.length > 1) //select AVG
								combo.selectedIndex = 1;
								
							//finally, refresh the chart
							handleRefreshChart();
						});
					}
					else {
						var combo = document.getElementById("chrtFunc");	
						var newOption = document.createElement("option");
						newOption.text = "COUNT";
						newOption.value = "COUNT";
						combo.options.add(newOption);
					}
				});
						
			});
		});
	});
}

function handleChrtCatChanged(){
	var combo = document.getElementById("chrtCat");
	
	var catID = combo.options[combo.selectedIndex].value;
	//get properties for first layer
	CleverGlobeService.GetPropertiesForCategoryWithSelID(culture, 0, catID, null, function(props){
		var combo = document.getElementById("chrtProp");
		combo.options.length = 0;
		for (c = 1; c < props.length; c+=3){
			var newOption = document.createElement("option");
			newOption.text = props[c];
			newOption.setAttribute("propID", props[c+1]);
			newOption.value = props[c+2];
			combo.options.add(newOption);
		}
		
		//get avaiable aggregation operations for property type
		if (combo.selectedIndex != -1){
			var type = combo.options[combo.selectedIndex].value;
			CleverGlobeService.GetAggregateOperations(Number(props[0]), type, function(opers){
				var combo = document.getElementById("chrtFunc");
				combo.options.length = 0;
				for (c = 1; c < opers.length; c+=2){
					var newOption = document.createElement("option");
					newOption.text = opers[c];
					newOption.value = opers[c];
					combo.options.add(newOption);
				}
			});
		}
		else {
			var combo = document.getElementById("chrtFunc");
			combo.options.length = 0;	
			var newOption = document.createElement("option");
			newOption.text = "COUNT";
			newOption.value = "COUNT";
			combo.options.add(newOption);
		}
	});
}

function handleChrtPropChanged(){
	var combo = document.getElementById("chrtProp");
	
	//get avaiable aggregation operations for property type
	var type = combo.options[combo.selectedIndex].value;
	CleverGlobeService.GetAggregateOperations(0, type, function(opers){
		var combo = document.getElementById("chrtFunc");
		combo.options.length = 0;
		for (c = 1; c < opers.length; c++){
			var newOption = document.createElement("option");
			newOption.text = opers[c];
			newOption.value = opers[c];
			combo.options.add(newOption);
		}
	});
}

function handleChrtGroupTypeChanged(){
	var combo = document.getElementById("chrtGT");
	var groupTypeID = combo.options[combo.selectedIndex].value;
		
	CleverGlobeService.AggrGetCountryGroups(groupTypeID, function (groups){
		var combo = document.getElementById("chrtGrp");
		combo.options.length = 0;
		for (j = 0; j < groups.length; j+=2){ 
			var newOption = document.createElement("option");
			newOption.text = groups[j+1];
			newOption.value = groups[j];
			combo.options.add(newOption);
		}
	});
}