﻿if ( !window.Net )
    window.Net = {};

Net.KayakHotelSearch = function( params, responseParams, token, ref )
{
    this.ref = ref;
    this.params = params;
    this.responseParams = responseParams;
    new Net.KayakSearchKey( token, this );
}

Net.KayakHotelSearch.prototype.ref = null;
Net.KayakHotelSearch.prototype.params = null;
Net.KayakHotelSearch.prototype.responseParams = null;
Net.KayakHotelSearch.prototype.sid = null;
Net.KayakHotelSearch.prototype.searchid = null;
Net.KayakHotelSearch.prototype.hotels = [];

Net.KayakHotelSearch.prototype.onResult = function( doc, method )
{
    switch ( method )
    {
        case "key":
            this.onKayakSearchKeyReceived( doc );
            break;
        case "hotelSearch":
            this.onSearchIdReceived( doc );
            break;
        case "hotelResponse":
            this.onHotelResponse( doc );
    }
}

Net.KayakHotelSearch.prototype.onKayakSearchKeyReceived = function( doc )
{
    var downloader = this.ref.control.createObject("downloader");
	downloader.addEventListener("Completed", Silverlight.createDelegate(this, this.onHotelsLoaded));
	downloader.open("GET", "common/kayakHotels.xml");
	downloader.send();
}
Net.KayakHotelSearch.prototype.onHotelsLoaded = function( sender, eventArgs )
{
    var doc = (new Utils.XMLUtil()).getResponseDocument( sender.getResponseText("") );
    this.hotels = doc.getElementsByTagName("hotels")[0].childNodes;
    this.buildHotels();
}

/*
    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.KayakHotelSearch.prototype.onKayakSearchKeyReceived = function( doc )
{
    this.sid = doc.getElementsByTagName("sid")[0].childNodes[0].nodeValue;
    var url = "http://www.kayak.co.uk/s/apisearch?basicmode=true&apimode=1&action=dohotels&version=1&_sid_="+this.sid;
    for (key in this.params)
        url += "&" + key + "=" + this.params[key];
    new Net.Call( "GET", url, this, "hotelSearch" );
}

Net.KayakHotelSearch.prototype.onSearchIdReceived = function( doc )
{
    try
    {
        this.searchid = doc.getElementsByTagName("searchid")[0].childNodes[0].nodeValue;
        var url = "http://www.kayak.co.uk/s/basic/hotel?searchid=" + this.searchid;
        for (key in this.responseParams)
            url += "&" + key + "=" + this.responseParams[key];
        url += "&version=1&apimode=1&_sid_=" + this.sid;
        new Net.Call( "GET", url, this, "hotelResponse" );
    }
    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 register another Kayak dev account and amend the key in kayakKey.txt file.");
            else
                alert(err);
        }
        catch (e2) { alert( e.name + ": " + e.message ); }
    }
}

Net.KayakHotelSearch.prototype.onHotelResponse = function( doc )
{
    var morePending = "";
    
    try
    {
        morePending = doc.getElementsByTagName("morepending")[0].childNodes[0].nodeValue;
    } catch (e) {}
    this.hotels = doc.getElementsByTagName("hotels")[0].childNodes;
    if ( morePending == "true" )
        this.getMorePending();
    else
        this.buildHotels();
}

Net.KayakHotelSearch.prototype.getMorePending = function()
{
    var url = "http://www.kayak.co.uk/s/basic/hotel?searchid=" + this.searchid;
    for (key in this.responseParams)
        url += "&" + key + "=" + this.responseParams[key];
    url += "&version=1&apimode=1&_sid_=" + this.sid;
    new Net.Call( "GET", url, this, "hotelResponse" );
}
*/
Net.KayakHotelSearch.prototype.buildHotels = function()
{
    var _hotels = [];
    for ( var i=0; i<this.hotels.length; i++ )
        // additional condition for firefox:
        if ( this.hotels[i].nodeName == "hotel" )
            _hotels[ _hotels.length ] = new Entities.Hotel( this.hotels[i] );
    this.ref.onResult( _hotels, "hotels" );
}
