﻿if ( !window.Net )
    window.Net = {};

Net.KayakFlightSearch = function( params, responseParams, token, ref )
{
    this.ref = ref;
    this.params = params;
    this.responseParams = responseParams;
    new Net.KayakSearchKey( token, this );
}

Net.KayakFlightSearch.prototype.proxyUrl = null;
Net.KayakFlightSearch.prototype.ref = null;
Net.KayakFlightSearch.prototype.params = null;
Net.KayakFlightSearch.prototype.responseParams = null;
Net.KayakFlightSearch.prototype.sid = null;
Net.KayakFlightSearch.prototype.searchid = null;
Net.KayakFlightSearch.prototype.trips = [];

Net.KayakFlightSearch.prototype.onResult = function( doc, method )
{
    switch ( method )
    {
        case "key":
            this.onKayakSearchKeyReceived( doc );
            break;
        case "flightSearch":
            this.onSearchIdReceived( doc );
            break;
        case "flightResponse":
            this.onFlightResponse( doc );
    }
}

Net.KayakFlightSearch.prototype.onKayakSearchKeyReceived = function( doc )
{
    var downloader = this.ref.control.createObject("downloader");
	downloader.addEventListener("Completed", Silverlight.createDelegate(this, this.onFlightsLoaded));
	downloader.open("GET", "common/kayakFlights.xml");
	downloader.send();
}
Net.KayakFlightSearch.prototype.onFlightsLoaded = function( sender, eventArgs )
{
    var doc = (new Utils.XMLUtil()).getResponseDocument( sender.getResponseText("") );
    this.trips = doc.getElementsByTagName("trips")[0].childNodes;
    this.buildTrips();
}

/*
    This is regular Kayak search result handling.
    But because Kayak API throws errors while searching through ProxyImpl
    I had to switch to static data read :(.
    But it still connects to Kayak live API to get session id - not required but because works I'm leaving it.
Net.KayakFlightSearch.prototype.onKayakSearchKeyReceived = function( doc )
{
    var url = this.ref.restProxyUrl + "http://www.kayak.co.uk/s/apisearch?basicmode=true";
    for (key in this.params)
        url += "&" + key + "=" + this.params[key];
    this.sid = doc.getElementsByTagName("sid")[0].childNodes[0].nodeValue;
    url += "&_sid_=" + this.sid;
    new Net.Call( "GET", url, this, "flightSearch" );
}

Net.KayakFlightSearch.prototype.onSearchIdReceived = function( doc )
{
    try
    {
        this.searchid = doc.getElementsByTagName("searchid")[0].childNodes[0].nodeValue;
        var url = this.ref.restProxyUrl + "http://www.kayak.co.uk/s/basic/flight?searchid=" + this.searchid;
        for (key in this.responseParams)
            url += "&" + key + "=" + this.responseParams[key];
        url += "&apimode=1&_sid_=" + this.sid;
        new Net.Call( "GET", url, this, "flightResponse" );
    }
    catch (e)
    {
        try
        {
            var err = doc.getElementsByTagName("message")[0].childNodes[0].nodeValue;
            if ( err.indexOf("search quota exceeded") > -1 )
                alert("Kayak dev account quota exceeded. Plase wait one hour before executing next search.");
            else
                alert(err);
        }
        catch (e2) { alert( e.name + ": " + e.message ); }
    }
}

Net.KayakFlightSearch.prototype.onFlightResponse = function( doc )
{
    var morePending = "";
    
    try
    {
        morePending = doc.getElementsByTagName("morepending")[0].childNodes[0].nodeValue;
    } catch (e) {}
    this.trips = doc.getElementsByTagName("trips")[0].childNodes;
    if ( morePending == "true" )
        this.getMorePending();
    else
        this.buildTrips();
}

Net.KayakFlightSearch.prototype.getMorePending = function()
{
    var url = "http://www.kayak.co.uk/s/basic/flight?searchid=" + this.searchid;
    for (key in this.responseParams)
        url += "&" + key + "=" + this.responseParams[key];
    url += "&apimode=1&_sid_=" + this.sid;
    new Net.Call( "GET", url, this, "flightResponse" );
}
*/
Net.KayakFlightSearch.prototype.buildTrips = function()
{
    var _trips = [];
    for ( var i=0; i<this.trips.length; i++ )
        // additional condition for firefox:
        if ( this.trips[i].nodeName == "trip" )
            _trips[ _trips.length ] = new Entities.Trip( this.trips[i] );
    this.ref.onResult( _trips, "flights" );
}
