// JavaScript Document
var ExternalControlTimeoutID = false;
$(document).ready(function(){
						   	//$("#GoogleMap").animate({width:"751px",height:"75%"},"normal","linear");
						    $("#ExternalControls").corners("6px transparent");
							$("#LocationControls").corners("6px transparent");
							$("#MapLoading").corners("3px transparent");
							$("#GridReferenceControlButton").corners("3px transparent top");
							$("#LocationSearchControlButton").corners("3px transparent top");
							$("#LayersControlButton").corners("3px transparent top");
						    $("#FullScreenButton").corners("3px transparent top");
							
							$("#ExternalControls").css("visibility","visible");
							$("#LocationControls").css("visibility","visible");
							$("#ExternalControls").hide();
							$("#LocationControls").hide();
							
							$("#GridReferenceControlButton").click(function(){
							                        //Position the control
													AdjustControlPosition("#GridReferenceControlButton","#ExternalControls");
							
												  	//$("#ExternalControls").centerInClient();
												  	$("#ExternalControls").fadeIn();
												  });
							
							$("#LocationSearchControlButton").click(function(){
																	AdjustControlPosition("#LocationSearchControlButton","#LocationControls");
																	$("#LocationControls").fadeIn();
																  });
							
							$("#btnSearchGridReference").click(function(){
																   	var found = SearchLocation(document.getElementById('GridReference').value,true);
																	if(found == true) $("#ExternalControls").fadeOut();
																   });
							
							
							$("#btnLocationSearch").click(function(){
																   	var found = SearchLocation(document.getElementById('LocationName').value,false);
																	if(found == true) $("#LocationControls").fadeOut();
																   });
							//Add full screen button
							$("#FullScreenButton").toggle(function(){
																	$("#FullScreenButton").corners("3px transparent top");
																  	
																	//$("#GoogleMap").animate({width:"150%",height:"98%"},"normal","linear");
																	$("#MapWrapper").animate({width:"98%",height:"98%"},"normal","linear",function(){																													
																																				  map.checkResize();
																																				  });
																	
																	$("#FullScreenButton").text("Exit fullscreen");
																	
																	//LoadGoogleMap();
																  },function(){
																	  	$("#FullScreenButton").text("Fullscreen");
																		$("#FullScreenButton").corners("3px transparent top");
																		$("#MapWrapper").animate({width:"75%",height:"75%"},"normal");
																	  });
							//Google location name auto complete
							/*
							$("#LocationName").change(function(){
																 	if($("#LocationName").val().length <= 4) return;
																 	$.getJSON("http://maps.google.com/maps/api/geocode/json?address="+ $("#LocationName").val() +"&sensor=false",
																	  function(data, textStatus){
																		 console.log(data);
																		 for(i in data.Placemark){
																			 var placeName = data.Placemark[i].address;
																			 placeName = "<div style='cursor:pointer;height:20px;background:#ffffff;color:#000000'>"+ placeName +"</div>";
																			 $("#LocationNameAutoComplete").append(placeName);
																		 }
																		 //console.log(placeName);
																	  });
							
																 });*/
							// Enter button to search
							$("#GridReference").keypress(function(e){
																  	if(e.which == 13){
																		var found =	SearchLocation(document.getElementById('GridReference').value,true);
																		if(found == true) $("#ExternalControls").fadeOut();
																	}
																  });
							$("#LocationName").keypress(function(e){
																  	if(e.which == 13){
																		var found =	SearchLocation(document.getElementById('LocationName').value,false);
																		Debug("found:"+ found);
																		if(found == true) $("#LocationControls").fadeOut();
																	}
																  });
						   
						   	$("#EnableGridMap").click(function(){
															   
															   	$('#ExternalControls').fadeOut();
																RunGetGridRefFromClick();
															   
															   });
							$("#EnableGridDrawMap").click(function(){
																   
																   	$('#ExternalControls').fadeOut();
																	RunGetGridRefFromDrag();
																	
																   });
							
							/* Custom layers */
							$("#Layers").css("visibility","hidden");
							$("#LayersControlButton").toggle(function(){
															$("#Layers").css("visibility","visible");
														 	$("#Layers").slideDown();
														 },function(){
															 $("#Layers").slideUp();
															 });
							LoadCustomLayers();
							
						   });


function AdjustControlPosition(Button,Control){
	var ButtonPosition = $(Button).position();
	/*
	$(Control).css("top",ButtonPosition.top+19);
	$(Control).css("left",ButtonPosition.left);
	*/
	var top = ($(document).height()-$(Control).height())/2;
	var left = ($(document).width()-$(Control).width())/2;
	
	$(Control).css("top",top);
	$(Control).css("left",left);
	
}

function LoadCustomLayers(){
	//console.log(layers);
	for(i in layers){
		var temp = "<div class='EachLayer'><input type='checkbox' class='chbLayers' onChange='AddCustomOverlays();'>"+ layers[i].name.replace('.kmz','') +"</div>"
		$("#Layers").append(temp);
	}
}

function AddCustomOverlays(){
	//console.log($(".chbLayers"));
	//map.clearOverlays();
	$(".chbLayers").each(function(index){
								  	
								  	if($(this).attr('checked')){
										//console.log(index);
										//console.log(layers[index].obj);
										map.removeOverlay(layers[index].obj);
										map.addOverlay(layers[index].obj);
									}else{
										map.removeOverlay(layers[index].obj);
									}
								  
								  });
}











