var Routes_TimeOut;
var Global = {
	InitializeRiderAlerts: function () {
		$(".RiderAlerts_Popout_NavButtons").append('<a href="" class="AlertCloseButton">Close</a>');
		$("#RiderAlerts li div").hide().addClass("RiderAlertPopout").each( function () {
				$(this).children(":last").children(".AlertCloseButton").click(Global.CloseRiderAlert);
			});
		$("#RiderAlerts li > a")
			.click(function () {
				Global.CloseRiderAlerts();
				$(this).addClass("highlight").next().show();
				return false;
			});
	},
	IntitializeTripPlanner: function () {
		$("#TripEndSidebar").focus(Global.CloseTripPlannerFields);
		$("#TripEndSidebar").click(Global.CloseTripPlannerFields);
		$("#TripPlanner_Fields").hide();
		
		var TripPlanText = "Do not include city/zip";
		
		$("#TripStartSidebar").val(TripPlanText);
		$("#TripStartSidebar").css({ "font-size":"0.9em", "color":"gray", "width":"120px" });
		
		$("#TripEndSidebar").val(TripPlanText);		
		$("#TripEndSidebar").css({ "font-size":"0.9em", "color":"gray", "width":"120px" });
	
	
		$("#TripStartSidebar").focus(
			function () {
				if($(this).val() == TripPlanText) {
						$(this).val("");
						$(this).css({ "font-size":"0.9em", "color":"black", "width":"120px" });
				};
			}
		 );
		
		$("#TripStartSidebar").blur(
			function () {
				if($(this).val() == '') {
						$(this).val(TripPlanText);
						$(this).css({ "font-size":"0.9em", "color":"gray", "width":"120px" });
				};
			}
		 );
		
		$("#TripEndSidebar").focus(
			function () {
				if($(this).val() == TripPlanText) {
						$(this).val("");
						$(this).css({ "font-size":"0.9em", "color":"black", "width":"120px" });
				};
			}
		 );
		
		$("#TripEndSidebar").blur(
			function () {
				if($(this).val() == '') {
						$(this).val(TripPlanText);
						$(this).css({ "font-size":"0.9em", "color":"gray", "width":"120px" });
				};
			}
		 );
		
		
		
	},
	InitializeRouteSchedules:function() {
		$("#NJS_Routelst").hide();
		$("#RouteBox").show();
		$("#RSbtn").click(function(){
			clearTimeout(Routes_TimeOut);
			Routes_TimeOut = setTimeout('$("#Routeitems").hide()',5000);
			$("#Routeitems").show();
		});
		$("#RoutePickedBox").click(
			function () {
				clearTimeout(Routes_TimeOut);
				Routes_TimeOut = setTimeout('$("#Routeitems").hide()',5000);
				if($(this).val() == 'Route Number') {
						$(this).val("");
				};
				if($("#Routeitems").css("display")=='none'){
					$("#Routeitems").show();
				} else {
					$("#Routeitems").hide();
				}
			}
		 );
		$("#Routeitems").mouseover(
			function() {
				clearTimeout(Routes_TimeOut);
			}
		);								 
		$("#Routeitems").mouseout(
			function() {
				Routes_TimeOut = setTimeout('$("#Routeitems").hide()',3000);
			}
		);
		$("#RoutePickedBox").keyup(Global.FilterRouteList);
		$("#RoutePickedBox").keydown(function(){if($(this).val() == 'Route Number'){$(this).val("");}});
	},
	toProperCase: function (s)
	{
	 	return s.toLowerCase().replace(/^(.)|\s(.)/g, 
		function($1) { return $1.toUpperCase(); });
	},
	FilterRouteList: function() {
			clearTimeout(Routes_TimeOut);
			Routes_TimeOut = setTimeout('$("#Routeitems").hide()',5000);
			var filt = Global.toProperCase($("#RoutePickedBox").val());
			if(filt.length > 0) {	
				$(".routeSelect:hidden").show();
				$(".routeSelect:not(:contains('"+filt+"'))").hide();
				var ht = $(".routeSelect:visible").length;
				if(ht < 1){
					$("#Routeitems").hide();
				} else {
						if(ht <= 16){ht = ht * 16;}
						else{ht = 252;}
						$("#Routeitems").height(ht);
						$("#Routeitems").show();
				}
			}
	},	
	GetSchedule: function(){
		window.location.href="/BusService/Schedule.cfm?route=" + $("#RoutePickedBox strong").html();
	},
	CloseTripPlannerFields: function () {
		$("#TripPlanner_Fields").show("fast");
	},
	
	CloseRiderAlert: function () {
		$(this).parent().parent().hide().prev().removeClass("highlight");
		return false;
	},
	
	CloseRiderAlerts: function () {
		$("#RiderAlerts li div:visible p").children(":last").click();
	},
	
	DeobfuscateEmailAddresses: function () {
		$(".obfuscation").each(function () {
			var EmailComponents = this.innerHTML.split(' ');
			var EmailAddress = EmailComponents[0] + '@' + EmailComponents[2] + '.' + EmailComponents[4];
			var LinkTitle = (this.title == "") ? EmailAddress : this.title;
			this.innerHTML = '<a href="mailto:' + EmailAddress + '">' + LinkTitle + '<\/a>';
		});
	},
	
	HowDoI: function(){
		window.location.href=$("#HowDoIselect").val();
	}
	
};

$(Global.InitializeRiderAlerts);
$(Global.IntitializeTripPlanner);
$(Global.InitializeRouteSchedules);
$(Global.DeobfuscateEmailAddresses);