//google.load("maps", "3");
//google.load("jquery", "1.3.2");

$(function() {


	// Location

	if (google.loader.ClientLocation.address.city) {
		  //var cl = google.loader.ClientLocation;
		  //var location = cl.address.city, cl.address.region, cl.address.country].join(', ');
 
		  //alert(google.loader.ClientLocation.address.city);
	 } else {
		  //alert("Crap, I don't know. Good hiding!");
	 }



	// Ajax för importsidan
	
	$(".submit").bind("click", function(){
		var thisForm = $(this).closest("form");
		var data = $(this).closest("form").serialize();
		$.ajax({
			type: "POST",
			url: "/import/save/",
			data: data,
			success: function(data){
				//alert( data );
				thisForm.addClass('saved');
				$('.id', thisForm).val(data);
			}
		});
		return false;
	});
	
	$(".savevenue").bind("click", function(){
		var thisForm = $(this).closest("fieldset");
		//var data = $(this).closest("fieldset").serialize();
		var data = SubmitFieldset($(this).closest("fieldset"));
		$.ajax({
			type: "POST",
			url: "/import/saveVenue/",
			data: data,
			success: function(data){
				$('.venueselect', thisForm).after(data).remove();
				$('.venueselect select').change(function() {
					markPulldown(this);
				});
			}
		});
		return false;
	});
		

	$('.venueselect select').change(function() {
		markPulldown(this);
	});

	
	
	
	
	function markPulldown(o) {
		if ($(o).val() === '0') {
			$(o).closest("label").addClass('error').removeClass('ok');
		} else {
			$(o).closest("label").addClass('ok').removeClass('error');
		}
	}
	
	
	function SubmitFieldset(fieldsetName) {
		 //Add a new form and hide it.
		 $('body').append('<form id="form-to-submit" style="visibility:hidden;"></form>');
		  //Clone the fieldset into the new form.
		 $('#form-to-submit').html($(fieldsetName).clone());
		  //Serialize the data
		 var data = $('#form-to-submit').serialize();
		  //Remove the form
		 $('#form-to-submit').remove();
		 return data;
	}


});
