var hoverColour = "#000";

$(function(){
	$("a.hover_all").show("fast", function() {
		var clase = $(this).attr('class');
		$(this).wrap("<div class=\""+clase+"\"> ");
		$(this).attr("class", "");
	});
		
	//display the hover div
	$(".hover_all").show("fast", function() {
		//append the background div
		$(this).append("<div></div>");
		
		//get link's size
		var wid = $(this).children("a").width();
		var hei = $(this).children("a").height();
		
		//set div's size
		$(this).width(wid);
		$(this).height(hei);
		$(this).children("div").width(wid);
		$(this).children("div").height(hei);
		
		//on link hover
		$(this).children("a").hover(function(){
			//store initial link colour
			if ($(this).attr("rel") == "") {
				$(this).attr("rel", $(this).css("color"));
			}
			//fade in the background
			$(this).parent().children("div")
				.stop()
				.css({"display": "none", "opacity": "1"})
				.fadeIn("fast");
			//fade the colour
			$(this)	.stop()
				.css({"color": $(this).attr("rel")})
				.animate({"color": hoverColour}, 350);
		},function(){
			//fade out the background
			$(this).parent().children("div")
				.stop()
				.fadeOut("slow");
			//fade the colour
			$(this)	.stop()
				.animate({"color": $(this).attr("rel")}, 0);
		});
	});
});

// DOCUMTENT READY
	$(document).ready(function() {
		
		if($('#googleMap_Canvas').length > 0 ) { initGoogleMap(); }
		
	});


/*
 *
 * Functies for GoogleMaps
 *
 */ 
// if(!google || !google.maps){
function js_on_demand(source, is_loaded, callback){
	if(!is_loaded){
		$.ajax({
			'type': "GET",
			'url': source,
			dataType: "script",
			success: callback
		});
	} else {
		callback();
	}
}
	
	
var map = null;
	function initGoogleMap() {
// 		var maps_src = 'http://maps.google.com/maps/api/js?sensor=false&language='+$('html').attr('lang');
		
		var latlng = new google.maps.LatLng(-36.512324, 174.660636);
	
		var mapTypes = new Array();
		mapTypes.push(google.maps.MapTypeId.ROADMAP); 
		mapTypes.push(google.maps.MapTypeId.HYBRID);
		mapTypes.push(google.maps.MapTypeId.SATELLITE); 
		
	    var myOptions = {
	      zoom: 14,
	      center: latlng,
	      mapTypeControlOptions: { mapTypeIds: mapTypes },
	      mapTypeId: google.maps.MapTypeId.ROADMAP,
	      streetViewControl: true
	    };
	    
		
		map = new google.maps.Map(document.getElementById("googleMap_Canvas"), myOptions);


		if($('#googleMap_settings #json').length > 0 ) {
			loadJson($('#googleMap_settings #json').val());
		} else { 
			loadKml($('#googleMap_settings #kml').val()+ '?' + $('#googleMap_settings').serialize());
		}
		
		$('#googleMap_Legend:not(.static) a.ico').click( function() { 
			kmlLayer.setMap(null);
			var li = $(this.parentNode);
			var inpt = li.find('input');
			
			if ($(this).hasClass('inactive')) { 					 					
				$(this).removeClass('inactive');
				li.append(li.data('input'));
			} else { 
				$(this).addClass('inactive');
				var inpt = inpt.remove();
				li.data('input', inpt);
			}
			url = $('#googleMap_settings #kml').val()+ '?' + $('#googleMap_settings').serialize();
// 			window.location = url;
			loadKml(url);
			return false;
		});
		$('#googleMap_Legend.static a.ico').click(function(){ return false; });
	}   
	
    

var kmlLayer = null; 
	function loadKml(url) {
		var latlng_hv = new google.maps.LatLng(-36.512324, 174.660636);
		kmlLayer = new google.maps.KmlLayer(url);
// 		if(confirm('test'))
// 		window.location = url;
		google.maps.event.addListener(kmlLayer, "metadata_changed" , function() {  
			bounds = kmlLayer.getDefaultViewport();
			map.setCenter(bounds.getCenter(), map.fitBounds(bounds));
			
		//	console.log(map.getZoom());
			if(map.getZoom() > 16){
				setTimeout(function(){ map.setZoom(16); });
			}
			if(map.getZoom() == 0){
				setTimeout(function(){
					map.setCenter(latlng_hv);
					map.setZoom(11);
				});
			}
		});

		kmlLayer.setMap(map);
	}





