$(function(){
	// update map on window resize
	$(window).bind("resize", resizeWindow);
	function resizeWindow( e ) {
		$("#map-wrapper").width($(window).width() - 264);
		$("#map-wrapper").height($(window).height() - 110);
	}
	resizeWindow();

	// Add Filter Handles
	$('#accordion').accordion({ autoHeight: false });
	$('#accordion').click(function( ) {
		//$('.logo').hide(1000);
	});
	$('.filter_group_title').click( function () {
		$(this).next().toggle();
	});
	$('#filtered_by_date').datepicker({dateFormat: "yy-mm-dd"});

	// Check all checkboxes
	$('#select_all').click(function(){
			$(this).parent().parent().find('input[type="checkbox"]').attr('checked', true);
		});
	$('#deselect_all').click(function(){
			$(this).parent().parent().find('input[type="checkbox"]').attr('checked', false);
		});

	
	// intialize the map
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(45.733, 21.2374), 13);
	map.setMapType(G_PHYSICAL_MAP);

	// intercept the filter form submission
	$('form#filters').submit(function(){
			var form = $(this);
			var url_filters = 'http://www.isutimis.ro/geoisu/json.php';
			var loading = $('<div />').html('Se incarca punctele... <br /> Sunt multe, din pacate.').dialog();
			$.getJSON(url_filters, form.serialize(), function(json){
					//alert(json.length);
					loading.dialog('destroy');
					

					// show the legend
					show_legend();
	
					// clean existing markers
					map.clearOverlays();	

					if( !json ){
						$('<div />').text('niciun punct nu satisface criteriul').dialog({modal:true});
						return false;
					}
					// initialize some min values
					var minLat, maxLat, minLong, maxLong, count=0;
					minLat = maxLat = parseFloat(json[0].latitudine);
					minLong = maxLong = parseFloat(json[0].longitudine);
					// add new markers
					$.each(json, function(index, point){
							count += 1;
							if( parseFloat(point.latitudine) > maxLat )
								maxLat = parseFloat(point.latitudine);
							if( parseFloat(point.latitudine) < minLat )
								minLat = parseFloat(point.latitudine);

							if( parseFloat(point.longitudine) > maxLong )
								maxLong = parseFloat(point.longitudine);
							if( parseFloat(point.longitudine) < minLong )
								minLong = parseFloat(point.longitudine);

							var marker = get_marker(point);

							GEvent.addListener(marker, "mouseover", function() {
									// 1000 - 2000 - Incendiu
									// 2000 - 3000 - ajutor medical de urgenta
									// 3000 - 4000 - descarcerare
									// 4000 - 5000 - asistenta persoanelor
									// 5000 - 6000 - alte sit. de urgenta (calamitati, etc)
									// 6000 - 7000 - prot. mediului
									// 7000 - 7010 - altele
									// 7050 - 7059 - arderi necontrolate
									// 7100 - 7109 - salvari de animale
									// 8100 - 8900 - deplasari fara interventie
									var grup = '';
									grup = parseInt(point.nr_grup);
									var local = '';
									local = point.localitate;									
									var text = '';
									var ziua = '';
									ziua = point.data;
									var tip = parseInt(point.tip);
									if( 1000 <= tip && tip <= 2000 ) 
										text = "Incendiu";
									else if( 2000 < tip && tip <= 3000 )
										text = "Ajutor medical de urgenta";
									else if( 3000 <= tip && tip < 4000 )
										text = "Descarcerare";
									else if( 4000 <= tip && tip < 5000 )
										text = "Asistenta persoanelor";
									else if( 5000 <= tip && tip < 6000 )
										text = "Alte sit. de urgenta (calamitati, etc)";
									else if( 6000 <= tip && tip < 7000 )
										text = "Protectia mediului";
									else if( 7000 <= tip && tip <= 7010 )
										text = "Altele";
									else if( 7050 <= tip && tip < 7060 )
										text = "Arderi necontrolate";
									else if( 7100 <= tip && tip <= 7109 )
										text = "Salvare de animale";
									else
										text = "Deplasare fara interventie";
									marker.openInfoWindowHtml(text + '<br />Forte SVSU: ' + point.nr_pc + '<br /> Cetateni: ' + point.alte_cet + '<br />  Localitatea: ' + local + '<br /> Data: ' + ziua);
								});
							GEvent.addListener(marker,'mouseout',function(){ marker.closeInfoWindow(); });
							map.addOverlay(marker);
						});
					var centerLat = (maxLat+minLat)/2; 
					var centerLong = (minLong+maxLong)/2;
					map.setCenter(new GLatLng(centerLat, centerLong), 9);
					document.getElementById("events").innerHTML = count;
				});

			return false;
		});

	// Make all checked
	$('#select_all').click();
	$('form').submit();
});


//
// Retrieves the type of image that needs to be displayed
// based on 'cod_comp' and 'felul'
function get_image( point ){
	// for quicker typing
	cod_comp = point.cod_comp;
	felul = point.felul;
	// default point settings
	var map_point = new GIcon();
	map_point.iconSize = new GSize(5, 5);
	map_point.iconAnchor = new GPoint(0, 0);
	map_point.infoWindowAnchor = new GPoint(9, 2);

	var icons = new Array();
	icons['blue'] = './css/images/albastru.png';
	icons['red'] = './css/images/rosu.png';
	icons['orange'] = './css/images/portocaliu.png';
	icons['orange_red'] = './css/images/portocaliu_rosu.jpg';
	icons['blue_red'] = './css/images/albastru_rosu.png';
	icons['green'] = './css/images/verde.png';

	if( cod_comp != 0 && felul ){
		map_point.image = icons['orange_red'];
		map_point.iconSize = new GSize(10, 5);
	} else if( cod_comp == 0 || felul ){
		map_point.image = icons['orange'];
	} else {
		map_point.image = icons['red'];
	}

	return map_point;
}



// 
// Generate a marker based on a point
function get_marker(point){
	var map_point = get_image(point);
	var latLng = new GLatLng(point.latitudine, point.longitudine);
	var marker = new GMarker(latLng, map_point);
	return marker;
}	


function show_legend(){
	$('#legend').removeClass('hidden').dialog({'position': ['right','top'], 'resizable': false});
}

