var labelAll = 'ALL';
var locationsRaw = '';

var selectedCity = -1;
var selectedMunicipality = -1;
var selectedArea = -1;

var locations = null;
var animationsStack = [];

function showCities(){
	$('#cboRELocCity').get(0).length = 0;
	$('#cboRELocCity').get(0).options[0] = new Option(window.labelAll, '-1');

	var index = 0;
	for(var i=0; i<locations.cities.length; i++){
		if (locations.cities[i].city_id != 1){
			index++;
			$('#cboRELocCity').get(0).options[index] = new Option(locations.cities[i].name, locations.cities[i].city_id);
		}
	}

	$("#cboRELocCity option[value='"+window.selectedCity+"']").attr('selected', 'selected');
}

function showMunicipalities(){
	var munIdBeforeLoading = $('#cboRELocMunicipality option:selected').val();
	
	
	$('#cboRELocMunicipality').get(0).length = 0;
	$('#cboRELocMunicipality').get(0).options[0] = new Option(window.labelAll, '-1');

	if (window.selectedCity != -1){
		var index = 0;
		for(var i=0; i<locations.municipalities.length; i++){
			if ((locations.municipalities[i].municipality_id != 1) &&
				(window.selectedCity == locations.municipalities[i].city_id)){
					index++;
					$('#cboRELocMunicipality').get(0).options[index] = new Option(locations.municipalities[i].name,
																				  locations.municipalities[i].municipality_id);
			}
		}

		if ((window.selectedMunicipality == -1) && ($('#cboRELocMunicipality').get(0).length == 2)){
			$("#cboRELocMunicipality").get(0).selectedIndex = 1;
		}else{
			$("#cboRELocMunicipality option[value='"+window.selectedMunicipality+"']").attr('selected', 'selected');
		}
	}
	
	var munIdAfterLoading = $('#cboRELocMunicipality option:selected').val();
	if (munIdBeforeLoading != munIdAfterLoading) window.animationsStack.push('cboRELocMunicipality');

}

function showAreas(){
	var areaIdBeforeLoading = $('#cboRELocArea option:selected').val();
	
	$('#cboRELocArea').get(0).length = 0;
	$('#cboRELocArea').get(0).options[0] = new Option(window.labelAll, '-1');

	if (window.selectedCity != -1){
		var index = 0;
		for(var i=0; i<locations.areas.length; i++){
			if ((locations.areas[i].area_id != 1) &&
				(window.selectedCity == locations.areas[i].city_id) &&
				((window.selectedMunicipality == -1) || (locations.areas[i].municipality_id == window.selectedMunicipality))
				){

					index++;
					$('#cboRELocArea').get(0).options[index] = new Option(locations.areas[i].name,
																		   locations.areas[i].area_id);
			}
		}
		$("#cboRELocArea option[value='"+window.selectedArea+"']").attr('selected', 'selected');
	}
	
	var areaIdAfterLoading = $('#cboRELocArea option:selected').val();
	
	if (areaIdBeforeLoading != areaIdAfterLoading) window.animationsStack.push('cboRELocArea');
}

function onCityChange(city_id){
	window.selectedCity = city_id;
	window.selectedMunicipality = -1;
	window.selectedArea = -1;

	showMunicipalities();
	showAreas();
		
	reSkin();
}

function onMunicipalityChange(municipality_id){
	window.selectedMunicipality = municipality_id;
	window.selectedArea = -1;
	showAreas();
	reSkin();
}

function onAreaChange(area_id){
	window.selectedArea = area_id;

	if (window.selectedMunicipality == -1){
		//Select municipality based on seleced area
		for(var i=0; i<window.locations.areas.length; i++){
			if (window.locations.areas[i].area_id == window.selectedArea){
				window.selectedMunicipality = window.locations.areas[i].municipality_id;
				var munIdBeforeLoading = $('#cboRELocMunicipality option:selected').val();
				$("#cboRELocMunicipality option[value='"+window.selectedMunicipality+"']").attr('selected', 'selected');
				var munIdAfterLoading = $('#cboRELocMunicipality option:selected').val();
				showAreas();
				if (munIdAfterLoading !== munIdBeforeLoading) window.animationsStack.push('cboRELocMunicipality');
				break;
			}
		}
	}

	reSkin();
}

function reSkin(){
	$("#cboRELocCity").select_unskin();
	$("#cboRELocMunicipality").select_unskin();
	$("#cboRELocArea").select_unskin();

	$("#cboRELocCity").select_skin();
	$("#cboRELocMunicipality").select_skin();
	$("#cboRELocArea").select_skin();
	
	while (window.animationsStack.length > 0){
		highlightComboBox(window.animationsStack.pop());
	}
}

function highlightComboBox(id){
	//This is the animation that higlights skinned combo box
	$('#' + id).parent().animate(
		{opacity: '0.1'}, 
		{
			duration: 0, 
			complete: function(){
				$('#' + id).parent().animate({opacity: '1'}, 400);	
			}
		}
	);
}
