var to = '';
var regTypes = Array();
var regCosts = Array();
var regAttending = Array();

$(document).ready(function(){	
	$('#btnSubmit').css({ opacity: '0' });
	 initGMaps();
	 initForms();
	 $('#txtDonation').bind('blur', updateTotal);
	 $('td.moreinfo sup').each(function(){
	  var tip = $(this).parent().attr('title');
	  $(this).parent().attr('title', '');
	  $(this).simpletip({
	    content: tip,
	    fixed: true
	  });
	 });
});

function initGMaps(){
	to = $('#hidLocation').val();
	map = new GMap2(document.getElementById('map_canvas'), { //Create a new maps object which will show up in the 'map_canvas' div
		size: new GSize(360, 230) //You really need an explanation?
	});
	geocoder = new GClientGeocoder(); //Create a new geocoder object. This takes a string address and converts it to lat/long
	geocoder.getLatLng(to, function(point){ //This is the function that does this the above
			map.setCenter(point, 15); //Center the map on the new point with a zoom of 13
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.addOverlay(new GMarker(point));
			gmapsReady = true; //Let the rest of the page know that the we are ready
			//$('#map_canvas').css({ display: 'block' });
		}
	);
	$(document).bind('unload', function(){
		GUnload();
	});
}

function getDirections(){
	from = $('#txtFrom').val();
	if (gmapsReady==true){
		$('#directionsPanel').html('');
		var directionsPanel = document.getElementById('directionsPanel');
		var directions = new GDirections(map, directionsPanel);
		directions.load('from: ' + from + ' to: ' + to, {travelMode:G_TRAVEL_MODE_DRIVING});
		$('#directionsPanel').fadeIn('medium');
	}
}

function initForms(){
	$('#extraDonation').hide();
	$('#chkDonation').bind('click', function(){
		if ($('#chkDonation').is(':checked')==false){
			$('#extraDonation').slideUp('slow', function(){
				$('#txtDonation').val('$0.00');
			});
		} else {
			$('#extraDonation').slideDown('slow');
		}
	});
}

function setRegType(regtype, thiscost, attending){
	var arrayIndex = in_array(regtype, regTypes);
	if (arrayIndex!=-1){
		regCosts[arrayIndex] = parseFloat(attending)*parseFloat(thiscost);
		regAttending[i] = attending;
	} else {
		regTypes[regTypes.length] = regtype;
		regCosts[regCosts.length] = parseFloat(attending)*parseFloat(thiscost);
		regAttending[regAttending.length] = attending;
	}
	
	updateTotal();
	buildFinal();
}

function in_array(needle, haystack){
	var index = -1;
	for (i=0; i<haystack.length; i++){
		if (needle==haystack[i]){
			index = i;
			break;
		}
	}
	
	return index;
}

function buildFinal(){
	var typeString = '';
	var costString = '';
	var attendingString = '';
	
	for (i=0; i<regTypes.length; i++){
		if (i+1==regTypes.length){
			typeString += regTypes[i];
			costString += regCosts[i];
			attendingString += regAttending[i];
		} else {
			typeString += regTypes[i] + ':';
			costString += regCosts[i] + ':';
			attendingString += regAttending[i] + ':';
		}
	}
	
	$('#hidRegType').val(typeString);
	$('#hidCost').val(costString);
	$('#hidAttending').val(attendingString);
}

function updateTotal(){
	var currentTotal = 0;
	for (i=0; i<regCosts.length; i++){
		currentTotal += parseFloat(regCosts[i]);
	}
	var donation = $('#txtDonation').val();
	donation = donation.replace('$', '');
	donation = donation.replace('.00', '');
	currentTotal += parseInt(donation);
	
	$('#total').html('<strong>$' + currentTotal + '.00</strong>');
}

