// JavaScript Document

$(document).ready(function() {
	//onfocus/onblur findGym text box
	$('input:text.zip').focus(function() { defaultFormValue(this,'ZIP CODE',true); });
	$('input:text.zip').blur(function() { defaultFormValue(this,'ZIP CODE'); });
	$('input:text.state').focus(function() { defaultFormValue(this,'STATE',true); });
	$('input:text.state').blur(function() { defaultFormValue(this,'STATE'); });
	$('input:text.city').focus(function() { defaultFormValue(this,'CITY',true); });
	$('input:text.city').blur(function() { defaultFormValue(this,'CITY'); });
	$('input:text.country').focus(function() { defaultFormValue(this,'COUNTRY',true); });
	$('input:text.country').blur(function() { defaultFormValue(this,'COUNTRY'); });
	
	//quickfinder text
	$('form#quickFinder input#quickZip').focus(function() { defaultFormValue(this,'Enter Zip',true); });
	$('form#quickFinder input#quickZip').blur(function() { defaultFormValue(this,'Enter Zip'); });
	
	//onhover schedule classes link
	$("div.classItem > div.name > a").hover(
											function() { viewClassInfo(this,true); },
											function() { viewClassInfo(this,false); }
											);
	
});


function defaultFormValue(el,txt,foc) { //reset/default form value
	element = $(el);
	if(foc==true && element.val()==txt) { element.val(''); }
	else if(!foc && element.val()=="") { element.val(txt); }
}

function viewClassInfo(el,display) { //schedules info popup box
	var scheduleItemId = $(el).attr("rel");
	var classElement = $("div.class" + scheduleItemId);
	var schedPopup = $("div.schedPopup");
	var popupElement = $("div#gymMiddle > div.schedPopup");
	popupElement.css("top",$(el).offset().top);
	popupElement.css("left",$(el).offset().left + 100);
	var popupContent = $("div#gymMiddle > div.schedPopup > div.schedPopup-middle");
	popupContent.html("");
	$.ajax({
			url: 'tpl/localGym/ajax.php',
			type: 'POST',
			data: {action: 'classInfo', scheduleItemId: scheduleItemId},
			dataType: "html",
			success: function(data) { popupContent.html(data) }
	});
	if(display) {
		popupElement.show();
	}
	else {
		popupContent.html("");
		popupElement.hide();
	}
}

function printSchedule() {
	window.print();
}

//GET LONG/LAT BY ADDRESS
function getLongLat(value,callback) {
	/*
	Returns the geocoder
	*/
	var geocoder = new google.maps.Geocoder();
	if(geocoder) {
		var geocoderRequest = {
			'address' : value
		};
		geocoder.geocode(geocoderRequest, callback);
	}
}
getLongLat.trialRetrieve = function(results,status) {
	if(status==google.maps.GeocoderStatus.OK) {
		var lng = results[0].geometry.location.lng();
		var lat = results[0].geometry.location.lat();
		$("div.search").hide();
		$("div.searching").show();
		$("select#gymId").load('tpl/trial/ajax/fetchLocations.php',{'lng' : lng, 'lat' : lat},function() {
			$("div.trialDisabled").show();
			$("div.searching").hide();
			$("div.search").show();
		});
	}
	else {
	  alert("We could not retrieve your location for the following reason: " + status);
	}
};
getLongLat.quickFinderRetrieve = function(results,status) {
	if(status==google.maps.GeocoderStatus.OK) {
		var lng = results[0].geometry.location.lng();
		var lat = results[0].geometry.location.lat();
		$('form#quickFinder input[name=lat]').val(lat);
		$('form#quickFinder input[name=lng]').val(lng);
		window.location.href="index.php?page=locations&lat="+lat+"&lng="+lng+"&method=zip";
	}
	else {
	  alert("We could not retrieve your location for the following reason: " + status);
	}
};