// leaving or going_to
var mode = "leaving";
var leaving_cache = new Array();
var going_to_cache = new Array();
var search_string;
var thinkInterval;

// Jquery Init -------------------------------------------------------------------------
			
$(document).ready(function(){
	stopThinking();
	startThinking();
	// This should make sure the right marker
	// stay on the map. 
	leaving_cache[3] = country_code;
	going_to_cache[3] = country_code;
	
	// Over ride form funcitonality
	// This should be fixed later and made
	// more standards compliant
	$("#map_search").submit(function(){
		search_string = $("#search_string").attr("value");
		showAddress(search_string);
		return false;
	});
	
	// Funky Geekery Industries Date Picker
	$('.datepicker').gical2();	
	
	$("#tabs a").click(function(){
		$(".selected_tab").addClass("ready_tab");
		$(".selected_tab").removeClass("selected_tab");
		$(this).addClass("selected_tab");
	});
	
	$("#reset-button").click(reset);
	
	$.jqm.params.toTop = true;
	

// -------------------------------------------------------------------------------------
	
	function changeToGoingTo(){
		if(mode == "leaving" || mode == "hotel"){
			$("#leaving_form").css("display", "none");
			$("#going_to_form").css("display", "block");
			
			$("#mode").removeClass();
			$("#mode").addClass("going");
			
			
			$("#map_search").removeClass();
			$("#map_search").addClass("arriving_search");
			
			
			mode = "going_to";
			
			setCache("going_to");
			restoreCache("leaving");
			
			return false;
		}
	}

// -------------------------------------------------------------------------------------
	
	function changeToLeaving(){
		if(mode == "going_to" || mode == "hotel"){
			$("#leaving_form").css("display", "block");
			$("#going_to_form").css("display", "none");
			
			$("#mode").removeClass();
			$("#mode").addClass("leaving");
			
			$("#map_search").removeClass();
			$("#map_search").addClass("leaving_search");
			
			mode = "leaving";
			
			setCache("leaving");
			restoreCache("going_to");
			return false;
		}
	}
	
// -------------------------------------------------------------------------------------
	
	function changeToHotel(){
		if(mode == "going_to" || mode == "leaving"){
			$("#leaving_form").css("display", "none");
			$("#going_to_form").css("display", "none");
			
			$("#mode").removeClass();
			$("#mode").addClass("hotel");
			
			mode = "hotel";
			
			//setCache("leaving");
			//restoreCache("going_to");
			return false;
		}
	}
	
// -------------------------------------------------------------------------------------
	
	function setCache(type){
		if(type == "leaving"){
			leaving_cache[0] = map.getCenter();
			leaving_cache[1] = map.getZoom();
			leaving_cache[2] = $("#search_string").attr("value");
			leaving_cache[3] = country_code;
		}else{
			going_to_cache[0] = map.getCenter();
			going_to_cache[1] = map.getZoom();
			going_to_cache[2] = $("#search_string").attr("value");
			going_to_cache[3] = country_code;
		}
	}
	
// -------------------------------------------------------------------------------------
	
	function restoreCache(type){
		if(type == "leaving"){
			map.setCenter(leaving_cache[0], leaving_cache[1]);
			$("#search_string").attr("value", leaving_cache[2]);
			country_code = leaving_cache[3];
		}else{
			map.setCenter(going_to_cache[0],going_to_cache[1]);
			$("#search_string").attr("value", going_to_cache[2]);
			country_code = going_to_cache[3];
		}
	}
	
// -------------------------------------------------------------------------------------
	
	function findRoutes(){
		stopThinking();
		startThinking();
		//not so fast, let's check shit first
		if(markerSelections[a_airport].length == 0){
			modalAlert("NO SELECTIONS");
		}else if ($("#going_to_airports .airport").length == 0){
			modalAlert("NO DESTINATIONS");
		}else{
				
			// let's see some results dawg!	
			$("#results").html("");
			$("#results").append("<h1 id='result_feedback'><img class='loadicon'src='/images/loader1.gif' />Searching</h1>");
			var leaving_string = "";
			var going_to_string = "";
			
			for(var lv = 0; lv < markerSelections[a_code].length; lv++){
				if(markerSelections[a_mode][lv] == "leaving"){
					leaving_string += markerSelections[a_code][lv] + "-";
				}else if(markerSelections[a_mode][lv] == "going_to"){
					going_to_string += markerSelections[a_code][lv] + "-";
				}
			}
			
			var url = "/routes/find/"+ leaving_string +"/"+going_to_string;
			//alert(url);
			$.getJSON(url, displayRoutes);
			return false;

		}
		
		
	}
	
// -------------------------------------------------------------------------------------
	
	function displayRoutes(data){
		if(data){
			$(".loadicon").remove();
			$("#result_feedback").remove();
			// !! rename the shit here, make it more readable
			$.each(data, function(key, val){
				var iata_code = val[0].origin;
				makeTable(key, iata_code);
				var targetTable = "#" + iata_code + "_table tbody";
				$.each(val, function(i, val){
					//alert(val.airline_id);
					makeRow(iata_code, val.destination_name, val.carrier, val.airline_id, targetTable);
				})
			})
			// scroll down
			$('html, body').animate({scrollTop:400}, 'slow');
			
		}else{
			$(".loadicon").remove();
			$("#result_feedback").html("");
			modalAlert("NO RESULTS");
		}
		stopThinking();

	}
	
// -------------------------------------------------------------------------------------
	
	function makeTable(departure, code){
		var table_code = "<table class='result-table' id='"+code+"_table'><thead><tr><th class='leaving_airport' colspan='4'>Flying from "+departure+"</th></tr></thead><tbody><!-- ROWS GO HERE --></tbody></table>"
		$("#results").append(table_code);
	}
	
// -------------------------------------------------------------------------------------

	
	function makeRow(origin, destination, carrier, carrier_id, target){
		var row = "	<tr class='row'><td class='destination'>To "+destination+"</td><td class='With'>Flying with <a href='/refferal?carrier_id="+ carrier_id +" &carrier=" + carrier + "&origin=" + origin + "' target='_blank'>"+carrier+"</a></td><td class='travel_time'></td><td class='book_link'><a href='/refferal?carrier_id="+ carrier_id +" &carrier=" + carrier + "&origin=" + origin + "' target='_blank' class='smooth_button_white'><ins>Check Prices</ins></td></tr>";
		$(target).append(row);
	}

// -------------------------------------------------------------------------------------
	
	$("#going_tab").click(function(){ changeToGoingTo(); });
	$("#leaving_tab").click(function(){ changeToLeaving(); });
	$("#hotel_tab").click(function(){ changeToHotel(); });
	$("#go_button").click(function(){ findRoutes() });

// -------------------------------------------------------------------------------------

	function modalAlert(message){
			
		switch(message){
			case "NO SELECTIONS":
				$("#modal").jqm({ajax: "modal/no_airports.html"});	
				$("#modal").jqmShow();
				
			case "NO DESTINATIONS":
				$("#modal").jqm({ajax: "modal/no_destinations.html"});	
				$("#modal").jqmShow();
				
			case "NO RESULTS":
				$("#modal").jqm({ajax: "modal/no_results.html"});	
				$("#modal").jqmShow();
		}
		
		stopThinking();

	} 
	
	
}); // end of Ready

// -------------------------------------------------------------------------------------

Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {
    if (this[i] === obj) {
      return i;
    }
  }
  return false;
}

// -------------------------------------------------------------------------------------

function startThinking(){
	thinkInterval = setInterval("flashLED()", 100);
}

// -------------------------------------------------------------------------------------


function stopThinking(){
	clearInterval(thinkInterval);
	$("#thinking").css("background-position-x", "0px");
}

// -------------------------------------------------------------------------------------

function flashLED(){
	
	var light = $("#thinking");
	var posX = light.css("background-position-x");
	
	if(posX === "0px"){
		light.css("background-position-x", "-18px");
	}else{
		light.css("background-position-x", "0px");
	}

}

// -------------------------------------------------------------------------------------

function reset(){
	/*
for(i = 0; i < markerSelections[a_obj].length; i++){
		//alert(markerSelections[a_obj][i]);
		deselectMarker(markerSelections[a_obj][i]);	
	}
*/
	var length = markerSelections[a_obj].length;
	while(length > 0){
		deselectMarker( markerSelections[a_obj][length -1]);
		length--;
	}

}