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();
		Global.textfieldHintConfig("#TripStartSidebar","Do not include city/zip");
		Global.textfieldHintConfig("#TripEndSidebar","Do not include city/zip");
	},
	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("");}});
	},
	
	InitializeMisc:function(){
		Global.textfieldHintConfig("#EmailSubscriptionField","Email Address");
	},
	
	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="/schedule/" + $("#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();
	}, 
	
	tooltipsConfig:function(){
		$('.tooltip').qtip({
			position: {
				corner: {
					target: 'topMiddle',
					tooltip: 'bottomMiddle'
				}
			},// position
			style: { 
				width: 200,
				padding: 5,
				background: '#f2f2f2',
				color: '#666',
				textAlign: 'center',
				'font-size':12,
				border: {
					width: 7,
					radius: 10,
					color: '#f2f2f2'
				},
				tip: 'bottomMiddle',
				name: 'dark' // Inherit the rest of the attributes from the preset dark style
			} // style
		});// qtip
	},
	
	textfieldHintConfig:function(obj,hint){
		$(obj).val(hint);
		$(obj).css({ "font-size":"0.9em", "color":"gray","font-style":"oblique" });
		$(obj).focus(
			function () {
				if($(this).val() == hint) {
						$(this).val("");
						$(this).css({ "font-size":"0.9em","color":"black","font-style":"normal" });
				};
			}
		 );
		
		$(obj).blur(
			function () {
				if($(this).val() == '') {
						$(this).val(hint);
						$(this).css({ "font-size":"0.9em","color":"gray","font-style":"oblique" });
				};
			}
		 );
	}
	
};

$(Global.InitializeRiderAlerts);
$(Global.IntitializeTripPlanner);
$(Global.InitializeRouteSchedules);
$(Global.DeobfuscateEmailAddresses);
$(Global.InitializeMisc);
$(Global.tooltipsConfig);
