$(document).ready(function() {
	if(GBrowserIsCompatible()) {
		// Creates a map and load the points from the database
		var map = new Map;
		map.buildMap(document.getElementById("map")); // Builds a plain map
		map.addControl(); // Adds default control to the map, which handles left click
		if (selected_oak_id>0) {
			map.load(true,null,true, selected_oak_id); // Load gemoetries from the database with control
		} else {
			map.load(true,null,true);
		}
	}

	// oaks/index - information tab:
	// This toggles to show/hide the add comment box
	$("a.addComment").click(function(){
	  $("div#addComment").slideToggle("slow");
	});    
	
	// This toggles to show/hide the login box
	$("a.login").click(function(){
	  $("div#login").slideToggle("slow");
	});    

	// oaks/index: initialize the tabs in the info-panel 
	$('#oaksInfo').tabs();
	//$('#oaksInfo').tabs('select',1); // NEEDS TO BE REMOVED!
	
	// oaks/index:
	// When submission tab is clicked, user is allowed to draw point/polygon
	$('a.sub').click(function() {
		map.setSubmissionMode(true);
		map.hideLabel();
	});
	
	// oaks/index:
	// When information tab is clicked, user is not allowed to draw point/polygon
	// And the existing geometry will be erased
	$('a#info').click(function(){
		map.setSubmissionMode(false);
		map.showLabel();
		map.clear();
	});
	
	// oaks/index - report tab
	// Extract the geometry (point/polygon) mode
	// Clear existing geometry when toggles between the two mode
	$('input:radio#pointMode, input:radio#polygonMode').click(function() {
		$('#LocateBy').slideUp('slow');
		map.setGeometryMode($(this).val());
		map.clear();
		$('input:text#address, input:button#locate, input:text#lat, input:text#lng').attr("disabled", true);
	});
	
	$('input:radio#addressMode').click(function() {
		$('#LocateBy').slideDown('slow');
		$('input:text#address, input:button#locate, input:text#lat, input:text#lng').removeAttr("disabled");
		map.setGeometryMode($(this).val());
		map.clear();
	});

	// oaks/index - report tab	
	$('input:button#locate').click(function() {
		if ($('input:text#address').val() != "") {
			map.geocode($('input:text#address').val());
		} else if ($('input:text#lat').val() != "" && $('input:text#lng').val()!="") {
			var latlng = Math.abs($('input:text#lat').val())+",-"+Math.abs($('input:text#lng').val());
			map.geocode(latlng);
		} else {
			alert("Please enter either an address or GPS coordinates");
		}
	});
	
	$('input').keypress(function (event){ return event.keyCode == 13 ? false : true; });
	
	$('form#OakAddForm div.checkbox').tooltip({
		delay:0,
		top: 10,
		left: 10,
		showURL: false,
		track: true,
		bodyHandler: function() {
			var pic = $(this).children().val();
			return $("<img/>").attr("src", webroot+"img/form/"+pic+".jpg")
							.attr("class","form-pic");
		}
	});

	$('#OakAddForm input:submit').click(function() {
		if (map.toString() == '') {
			if (map.getGeometryMode() == "point") {
				alert('There is not a point on the map.');
			} else {
				alert('The polygon must have at least three points.');
			}
			return false;
		} else {
			$('input:hidden#CommunityOakGeom').val(map.toString());
		}
	});
	
	$('input:checkbox').click(function() {
		if ($(this).attr('checked')) {
			map.showCategory($(this).val());
		} else {
			map.hideCategory($(this).val());		
		}
	});
	
	$('#help a').click(function() {
		jQuery.blockUI({ 
			message: $('#helpMsg'),
		    css: { 
		        padding:        0, 
		        margin:         0, 
		        width:          '50%', 
		        top:            '5%', 
		        left:           '25%', 
		        textAlign:      'center', 
		        cursor:         'default' 
	    	} 
	 	});
		
		$('input:button').click(function() {jQuery.unblockUI();});
		return false;
	});

	$('a#original').click(function() {
		jQuery.blockUI({ 
			message: '<img height="450px" src="'+$(this).attr('href')+'" />',
		    css: { 
		        padding:        '10px',
		        margin:         0,
		        width:          '70%', 
		        top:            '5%', 
		        left:           '15%', 
		        textAlign:      'center', 
		        cursor:         'default'					        
		    }
		});
		
		$('body').click(function() {
			jQuery.unblockUI();
		});
	
		return false;
	});

	
	// Submit form: Input box shows up only when others is chosen 
	$('select#CommunityOakOakSpecies').change(function() {
		if ($(this).val() == "other") {
			$('input#CommunityOakOakSpeciesOther').show();
		} else {
			$('input#CommunityOakOakSpeciesOther').hide();
		}
	});
	$('select#CommunityOakEnvSetting').change(function() {
		if ($(this).val() == "other") {
			$('input#CommunityOakEnvSettingOther').show();
		} else {
			$('input#CommunityOakEnvSettingOther').hide();
		}
	});	
	
	// To show validation error
	$("#OakAddForm").validate().showErrors();
	
	// Remove the flash message after it's displayed for 5 seconds
	if ($('div').is('#flashMessage')) {
		setTimeout("$('#flashMessage').slideUp()",5000);
	} else {
		//alert ("No flash message");
	}
	
	//scroll through the points
	$("a#scroll-old").click(function() {map.scroll(true);});
	$("a#scroll-new").click(function() {map.scroll(false);});
	

});
