
var validRoutes = ["1","2","3","4","5","7","7a","7b","7d","7n","8","10","10a","11","11b","13","14","14a","15","15a","15b","15e","15f","15n","16","16a","17","17a","18","19","19a","20b","25","25a","25n","25x","26","27","27b","27n","27x","29a","29n","31","31b","31n","32","32a","32x","33","33a","33b","33n","33x","37","37x","38","38b","39","39b","39n","39x","40","40a","40d","40n","41","41b","41c","41n","41x","42","42a","42n","43","44","44b","44n","45","45a","46a","46b","46c","46d","46e","46n","46x","47","48a","48n","49","49n","50","51","51b","51c","51d","51x","51n","53","53a","54a","54n","56","59","63","65","65b","66","66a","66b","66d","66n","66x","67","67a","67n","67x","68","69","69n","69x","70","70b","70n","70x","74","74a","75","76","76a","76b","77","77a","77n","77x","78","78a","79","83","84","84n","84x","84x*","88n","90","92","102","104","105","111","114","116","118","120","121","122","123","128","130","140","142","145","150","151","161","184","185","210","220","236","237","238","239","240","270","746","747","748","39c","4a","11a","13a","32b","38a","38c","39a","42b","49a","70a","79a","56a"];

$(document).ready(function() {
	
	
	// Turn off form submit since JS is obviously on.
	// Set up route search button
	$("#choose_route").bind("submit", function() {
		//disable form input
        $('form#choose_route input').attr('disabled', true);
        
        // Clear any other timers
        clearTimeout(timeoutID);    
		
		//load the route, no specified direction, from a form
		loadRoute($("input#bus_route").val(), false, true); 
		return false; 
	});
	
	//hide timetable and root footer when first loaded
	$('#timetable').hide();
	$("#footer.floated").removeClass();
	
	// load route in hash if there is one
	
	if(location.hash!="" && location.hash !="#choose_route")
	{
		// split route with _ to get number_direction
		route_details = location.hash.substring(1).split("_");
		route_number = route_details[0];
		route_direction = route_details[1];
		
		//load the route, with specified direction, not from a form
		loadRoute(route_number, route_direction, false);
		$('input#bus_route').val(route_number);
	} else {
		//show the timetale now since we won't be replacing it
		$('#timetable').show();
		$("#footer:not(.rooted)").addClass("floated");
	}
	
	if($('input#bus_route').val()=="")
	{
		// Set ghost text on route number search box
		$('input#bus_route').defaultvalue("Route number");
	} else
	{
		setUpTimer($('input#bus_route').val());
	}
	
});


function loadRoute(route_number, route_direction, from_form)
{

	route_number = route_number.toLowerCase();
	
	//undo branding changes
	$('#branding, body').removeClass();
								
	if(jQuery.inArray( route_number, validRoutes )!=-1)
	{
		
		$('form#choose_route').removeClass().addClass("loading");
				
		// Send the current millisecond as part of the URL, since IE tends
		// to cache in a most retarded fashion.
		
		s = new Date;
		
		$("#timetable").load(	"/times/" + route_number + "/" + s.getTime(), 
									function(){	
										$('#timetable').show();
										$("#footer").addClass("floated");
										if (from_form) {
											$('html,body').scrollTop($('#choose_route').offset().top).animate({scrollTop: $("#timetable").offset().top}, 1500, "easeOutSine");
											location.hash = route_number;
										}
										if (route_direction) {
											$('html,body').scrollTop($(location.hash).offset().top);
										}

										// Using words that IE can understand.
										document.title = 'Route No. ' + route_number + ' • Dublin Bus';
										
										//reenable form input
										$('form#choose_route input').removeAttr('disabled');														$('form#choose_route').removeClass();
										
										
										//change route link selects input field
										$("#route_head a").click(function(){
											$('#bus_route').select();
											$('html,body').animate({scrollTop: $('#choose_route').offset().top}, 1000, "easeOutSine");
											return false;
										});
										
										setUpTimer(route_number);
									}
							 );
							
	}
	else
	{
		// alert user that their route is invalid here.
		// remember to add whatever is necessary to undo this in the other part of this if statement!
		$('form#choose_route input').removeAttr('disabled')
		$('form#choose_route').removeClass("loading").addClass("alert")
		$('#bus_route').select();
		
		
		switch(route_number)
		{
			case "crash":
			case "999":
			case "911":
				window.location.href="/crash";
				break;
			case "404":
				$('body').addClass("matrix");
				break;
			case "666":
				$('#branding').addClass("devil");
				break;
			case "516":
				
				break;
			case "littledeer":
				
				break;
			case "supercrazyawesome":
				
				break;
			case "epicfail":
				window.location.href="http://www.episerver.com/en/Customers/Customers/Dublin-Bus/";
				break;
			case "fail":
				window.location.href="http://www.dublinbus.ie";
				break;
			case "iphone":
				window.location.href="http://www.supercrazyawesome.com/dublinbus/index.html";
				break;
			default:
				$('#branding').addClass("nobus");				
		}
	}
}

var timeoutID = "";

function setUpTimer(route_number)
{
	var nowTime = new Date(currentTimeMS);
	var nextTime = "";
	var diff;
	var delay = 86400000; // Refresh every hour anyway.
		
	// clear the previous timer
	clearTimeout(timeoutID);

	// Get the soonest delay till the next bus.
	jQuery.each($('.next .time abbr'), function(b,nexttime){
		nextTime = Date.parse(nexttime.title.substring(0,16));
		diff = new TimePeriod(nowTime, nextTime);
		diff_in_ms = ((diff.days * 86400) + 
					  (diff.hours * 3600) + 
					  (diff.minutes * 60) + 
					   diff.seconds) 	  * 1000 + 
					   diff.milliseconds;
		if(diff_in_ms < delay)
		{
			delay = diff_in_ms;
		}
	});
	// Turning this off for now
	//timeoutID = setTimeout(function() {loadRoute(route_number, false, false)}, delay);
	
}