/*
Airtraffic.eu
*/

/*
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '; path=/';
        var domain = options.domain ? '; domain=' + (options.domain) : '; domain=.airtraffic.eu';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};










var airline_id = '';
var isCallback = false;
var ap_1 = 0;
var ap_2 = 0;
var mapLoadvar = 0;
var menuLoadvar = 0;
var tocookie = 0;
var standardicons = '<img src="img/icon_bl_hotel_na.gif" alt="" title="" width="23" height="23" /> <img src="img/icon_bl_car_na.gif" alt="" title="" width="23" height="23" /> <img src="img/icon_bl_plane_na.gif" alt="" title="" width="23" height="23" />';
$(function(){
	
	
	
	
	
	$.ajax({
		type: "GET",
		url: "routes/"+xmlname,
		dataType: "xml",
		success: function(xml) {
			
		// Lijst met alle luchthavens
		var aplijst = $(xml).find("d");
		var optionslijst = '<option value="-1"  selected="selected">-' + pulldowndefault + '-</option>'; 
		aplijst.each(function(){
		var apname = $(this).attr("n");
		var apidee = $(this).attr("a");
		var apiata = $(this).attr("i");
		optionslijst += '<option value="' + apidee + '" code="' + apiata + '">' + apname + ' (' + apiata + ')' + '</option>';
		});	
		$('select#airport_1').html(optionslijst); 
		menuLoad();
	}//einde xml
	});//einde xml
	
	
	$("select#airport_1").change(function(){
		if ($(this).val() == '-1') {
			clearForm();
			getMovie().resetForm();
			return;
		} 
		if (!isCallback) {
			getMovie().sendToActionScriptMenu1($(this).val());
		} else {
			isCallback = false;
			$("select#airport_2").val('-1');
			$("select#airport_2").attr('disabled', true);
			$("#search_1").attr('value', $("select#airport_1 option:selected").attr('code'));
			//alert('CHANGE menu 1 code ingesteld');
			$("#search_2").attr('value', "");
			$("#search_2").attr('disabled', true);
			//$("#swap_ap").hide("fast");
			$("#depoptions").html('<a href="http://' + langcode + '.airtraffic.eu/hotels.php?hotels_id=' + this.value + '"><img src="img/icon_bl_hotel.gif" alt="' + txtbookhotel + ' ' + this.options[this.selectedIndex].text + '" title="' + txtbookhotel + ' ' + this.options[this.selectedIndex].text + '" width="23" height="23" /></a> <a href="http://' + langcode + '.airtraffic.eu/cars.php?cars_id=' + this.value + '"><img src="img/icon_bl_car.gif" alt="' + txtrentacar + ' ' + this.options[this.selectedIndex].text + '" title="' + txtrentacar + ' ' + this.options[this.selectedIndex].text + '" width="23" height="23" /></a> <a href="http://' + langcode + '.airtraffic.eu/airports.php?airports_id=' + this.value + '"><img src="img/icon_bl_plane.gif" alt="' + txtinfoairport + ' ' + this.options[this.selectedIndex].text + '" title="' + txtinfoairport + ' ' + this.options[this.selectedIndex].text + '" width="23" height="23" /></a>');
			$.cookie("from", $("select#airport_1").val());
			
			if(!tocookie){
				$("#frabeltje").fadeOut('fast', function() {
					$("#frabeltje").html("<p>"+routemaphelp2+"</p>").fadeIn('fast');
					});
			}
			$.cookie("to", null);
		
			
			$.ajax({
		type: "GET",
		url: "routes/"+xmlname,
		dataType: "xml",
		success: function(xml) {
			
			// Zoek parent element waar id =..
			// en kijk of hij legs heeft
			var selectedairport = $("select#airport_1").val();
			var apinfo = $(xml).find("d[a='"+selectedairport+"']");
			//var apname = apinfo.attr("n");
			//var apiata = apinfo.attr("i");
			var options2lijst = '<option value="-1" selected="selected">-' + pulldowndefault + '-</option>'; 
			destinations = new Array(); 
			var legs = apinfo.children();
			var indexnr = 0;
			legs.each(function(){
			
			var apidee = $(this).attr("a");
			var vaderinfo = $(xml).find("d[a='"+apidee+"']");
			var apname = vaderinfo.attr("n");
			var apiata = vaderinfo.attr("i");
			destinations[indexnr] = [apname, apiata, apidee];
			indexnr++;	
			});	
			
			// Andersom werken
			// Zoek waar de bestemming (child) = 57.
			var legInfo = $(xml).find("s[a='"+selectedairport+"']");
			legInfo.each(function(){
			var vader = $(this).parent();
			var apname = vader.attr("n");
			var apiata = vader.attr("i");
			var apidee = vader.attr("a");
			destinations[indexnr] = [apname, apiata, apidee];
			indexnr++;	
			});
			
			destinations.sort();
			
			for (var index = 0; index < destinations.length; index++)
			{ 
			   options2lijst += '<option value="' + destinations[index][2] + '" code="' + destinations[index][1] + '">' + destinations[index][0] + ' (' + destinations[index][1] + ')' + '</option>';
			} 
							
			$('select#airport_2').html(options2lijst); 			
				
				if (ap_1 > 0) {
					$("select#airport_2").trigger("change");
					ap_1 = 0;
					ap_2 = 0;
				}
				$("select#airport_2").attr('disabled', false);
				$("#search_2").attr('disabled', false);
				setdessearch();
		
			}//einde xml
			});//einde xml
			
			
		}
	})
	
	
	
	
	
	
	
	
	$("select#airport_2").change(function(){
		if ($(this).val() == '-1') {
			//$("#swap_ap").hide('fast');
			$("#search_2").attr('value', '');
			if (!isCallback) {
				getMovie().sendToActionScriptMenu1($("select#airport_1").val());
			}
			isCallback = false;
			return;
		}
		//$("#swap_ap").show('fast');
		$("#search_2").attr('value', $("select#airport_2 option:selected").attr('code'));
		$.cookie("to", $("select#airport_2").val());
		$("#desoptions").html('<a href="http://' + langcode + '.airtraffic.eu/hotels.php?hotels_id=' + this.value + '"><img src="img/icon_bl_hotel.gif" alt="' + txtbookhotel + ' ' + this.options[this.selectedIndex].text + '" title="' + txtbookhotel + ' ' + this.options[this.selectedIndex].text + '" width="23" height="23" /></a> <a href="http://' + langcode + '.airtraffic.eu/cars.php?cars_id=' + this.value + '"><img src="img/icon_bl_car.gif" alt="' + txtrentacar + ' ' + this.options[this.selectedIndex].text + '" title="' + txtrentacar + ' ' + this.options[this.selectedIndex].text + '" width="23" height="23" /></a> <a href="http://' + langcode + '.airtraffic.eu/airports.php?airports_id=' + this.value + '"><img src="img/icon_bl_plane.gif" alt="' + txtinfoairport + ' ' + this.options[this.selectedIndex].text + '" title="' + txtinfoairport + ' ' + this.options[this.selectedIndex].text + '" width="23" height="23" /></a>');
		
		$("#frabeltje").fadeOut('fast', function() {
			$("#frabeltje").html("<p>&lt;&lt; "+routemaphelp3+"</p>").fadeIn('fast');
		});
		
		
		if (!isCallback) {
			getMovie().sendToActionScriptMenu2($("select#airport_1").val(), $(this).val());
		}
		isCallback = false;
	})
	$("#search_1").keyup(function(event) {
		if ($("#search_1").val().length == 3) {
			var el = $("select#airport_1 option[code='" + $("#search_1").val().toUpperCase() + "']");
			if (el.length > 0) {
				$("#search_1").blur();
				$("select#airport_1").val(el.val());
				$("select#airport_1").trigger("change");
			}
		}
	})
	$("#search_2").keyup(function(event) {
		if ($("#search_2").val().length == 3) {
			var el = $("select#airport_2 option[code='" + $("#search_2").val().toUpperCase() + "']");
			if (el.length > 0) {
				$("#search_2").blur();
				$("select#airport_2").val(el.val());
				$("select#airport_2").trigger("change");
			}
		}
	});
	//$('#swap_ap').click(function(){
	//	ap_1 = $("select#airport_1").val();
	//	ap_2 = $("select#airport_2").val();
	//	if (ap_1 > 0 && ap_2 > 0) {
	//		$("select#airport_1").val(ap_2);
	//		$("select#airport_1").trigger("change");
	//	}
	//});
});
function getMovie() {
	var movie = (navigator.appName.indexOf("Microsoft")!=-1 ? window : document)["map"];
	return movie;
}
function clearForm() {
	$("select#airport_1").val('-1');
	$("select#airport_2").val('-1');
	$("select#airport_2").attr('disabled', true);
	$("#search_1").attr('value', "");
	$("#search_2").attr('value', "");
	$("#search_2").attr('disabled', true);
	//$("#swap_ap").hide("fast");
	$.cookie("from", null);
	$.cookie("to", null);
	$("#depoptions").html(standardicons);
	$("#desoptions").html(standardicons);
	$("#frabeltje").fadeOut('fast', function() {
		$("#frabeltje").html("<p>"+routemaphelp1+"</p>").fadeIn('fast');
	});
}

//zijn map/xml en javamenu allebei geladen?
function init() {
		mapLoadvar = 1;
		if (menuLoadvar == 1){
		setdepsearch();
		}
	};

function menuLoad() {
		//alert('1. menu 1 geladen: '+$('select#airport_1').html());
		menuLoadvar = 1;
		if (mapLoadvar == 1){
		setdepsearch();
		}
	};
		
function setdepsearch() {
		if($.cookie("to")){
		tocookie = $.cookie("to");
		}
		if($.cookie("from")){
		$("select#airport_1").val($.cookie("from"));
		$("select#airport_1").trigger("change");
		//alert('2. kaart & menu geladen, menu1 wordt ingesteld, change aangeroepen');
			if(tocookie){
			$.cookie("to", tocookie);
			tocookie = 0;
			}
		}
	};	
		
function setdessearch() {
		if($.cookie("to")){
		$("select#airport_2").val($.cookie("to"));
		$("select#airport_2").trigger("change");
		}
	};	

//Komt altijd van de kaart terug, ook als je in dropdown selecteerd.
function menu1Selected(id1) {
	isCallback = true;
	$("select#airport_1").val(id1);
	$("select#airport_1").trigger("change");
}
function menu2Selected(id1, id2) {
	isCallback = true;
	$("select#airport_2").val(id2);
	$("select#airport_2").trigger("change");
}