﻿/*
    Airport Map Classes 
*/
function AirportMap(popUp, me, service)
{
    this.me = me;
    this.asynchCallInProgress = false;
    
    this.popUp = document.getElementById(popUp); 
	//this.popUp.onclick = function(){document.getElementById(popUp).style.display="none";}
    
    document.body.onclick = function(){document.getElementById(popUp).style.display="none";}
    this.airports = {};
    
    this.service = service;
    
    this.htmlDiv = "<div class=\"top\"><h2>{airport}</h2><h3>Destinations</h3><ul class=\"quicklinks\">{destinations1}</ul><ul class=\"quicklinks\">{destinations2}</ul>{footer}</div>"
    this.htmlRow = "<li><a href=\"{url}\" >{title}</a></li>";
    
    this.htmlFooter = "<div class=\"clearit\"></div><h3>... plus flights via London to all destinations.</h3>"; 

    this.htmlNoFlightsDiv = "<div class=\"top\"><h2>{airport}</h2><h3>Flights via London to all destinations.</h3></div>"

}

AirportMap.prototype.showDestinations = function(id, name, isLondonAirport)
{
    // If destination table for this airport is cached, re-show it, otherwise 
    // get destinations.
    if (this.airports[id])
    {
        this.showPopUp(id);
    }
    else
    {
        // Get destinations. The callback method will show the pop-up when
        // ready.
        this.getDestinations(id, name, isLondonAirport);
    }    
}

AirportMap.prototype.showPopUp = function(id, name)
{
	
	var scrollPos = document.body.scrollTop;
	if (scrollPos == 0)
	{
		if (window.pageYOffset)
			scrollPos = window.pageYOffset;
		else
			scrollPos = (document.body.parentElement) ? document.documentElement.scrollTop : 0;
	}
	this.popUp.style.marginTop = scrollPos + 'px';
	
    this.popUp.innerHTML = this.airports[id];
    this.popUp.style.display = "inline";   

}

AirportMap.prototype.getMarkup = function(name, destinations, useFooter)
{

	if (destinations.length<1) 
	{
		return this.htmlNoFlightsDiv.replace("{airport}", name) 
	}
	
    var list1 = "";
	var list2 = "";
	var listsize = Math.round(destinations.length/2);
    for (var i=0; i<destinations.length; i++)
    {
		if ((i+1)>listsize)
		{
	        list2 += this.htmlRow.replace("{url}", destinations[i].url)
                            .replace("{title}", destinations[i].title);
		}
		else
		{
			list1 += this.htmlRow.replace("{url}", destinations[i].url)
							.replace("{title}", destinations[i].title);
		}
    }

    return this.htmlDiv.replace("{airport}", name)
                        .replace("{destinations1}", list1)
                        .replace("{destinations2}", list2)
                        .replace("{footer}", (useFooter? this.htmlFooter:""));
}

AirportMap.prototype.getDestinations = function(id, name, isLondonAirport)
{   
    // Prevent more than one simultaneous asynch call
    if (this.asynchCallInProgress) return;
    
    this.asynchCallInProgress = true;
    
    if (isLondonAirport == undefined) var isLondonAirport = false;

    var context = {};
    context.id = id;
    context.name = name;
    context.isLondonAirport = isLondonAirport;
    context.me = this;
    
    var callback = eval(this.me + ".getDestinationsComplete");
    
    var service = new TSD.CCH.MainService();
    service.ListAirportDestinations(id, callback, null, context, null);      

}

AirportMap.prototype.getDestinationsComplete = function(returnVal, context)
{
    try 
    {
        context.me.asynchCallInProgress = false;

        var result = eval('(' + returnVal + ')');
        if (result.status == -1) throw(result.errmsg);
        
        context.me.airports[context.id] = context.me.getMarkup(context.name, result.destinations, !context.isLondonAirport);
        context.me.showPopUp(context.id);
    
    }
    catch (ex)
    {
        alert(ex.message);
    }
}


