/**
 * ui.gm object serves as layer between gmap and ui=- //
**/
ui.gm = ui.gm || {}




ui.gm.pop_routemap = function(airline) {
console.group('ui.gm.pop_routemap('+airline+')');

    console.info('creating routes for '+airline);

    if (!airline) {
        console.warn('airline not set');
        console.groupEnd('ui.gm.pop_routemap');
        return;
    }

    gm.routemap.pop( airline );



console.groupEnd('ui.gm.pop_routemap('+airline+')');
}




ui.gm.routemap_hover = function ( options ) {
console.group('ui.gm.routemap_hover(options)');

    console.dir(options);

    var settings = {
        0 : null,
        1 : null
    };
    $.extend(settings, options || {});

    from = settings[0] || null;
    to   = settings[1] || null;

//  console.dir(from);
//  console.dir(to);

    if (to == null || to.length < 1) {
        console.info('No destinations');
        $.flash.info('');
    }

    if (from && to.length > 1)
    {
        $.flash.info(''+from.name+': '+to.length+' destinations');
        // $.flash.info(geo.cities[id1].name +' - '+ geo.cities[id2].name);
    }
    else if (from && to.length == 1)
    {
        // airports_from = geo.airports.in_city(id1);
        // airports_from = '('+airports_from.join(',')+')'
        $.flash.info(''+from.name+' - '+to[0].name);
    }
    else if (from.name)
    {
         $.flash.info('No destinations from '+from.name);
    } else {
         $.flash.info('');
    }


console.groupEnd('ui.gm.routemap_hover(options)');
}


ui.gm.routemap_click = function ( options ) {


}





ui.gm.set_pos = function(id) {
console.group('ui.gm.set_pos("'+id+'")');

        id = id.replace('#','');

        t = null;
        if (id.indexOf('city') >= 0) {
            t = 'cities';
            key = id.replace('city','');
        }
        if (id.indexOf('airport') >= 0) {
            t = 'airports';
            key = id.replace('airport','');
        }
        if (id.indexOf('airline') >= 0) {
            t = 'airlines';
            key = id.replace('airline','');
        }
        if (id.indexOf('subregion') >= 0) {
            t = 'subregions';
            key = id.replace('subregion','');
        }

        if (!t) {
            console.warn('unknown data type');
            console.groupEnd('ui.gm.set_pos("'+id+'")');
//            return;
        }

        var data = geo[t][key];
        if (data.length < 0) {
            console.warn('no data for '+t+ ': '+key);
            console.groupEnd('ui.gm.set_pos');
//            return;
        }


        if (data) {
            gm.set_pos(data.la, data.lo, data.zoom);
        } else {
            console.warn('no data for this object');
        }


console.groupEnd('ui.gm.set_pos("'+id+'")');
}




ui.gm.clear = function() {

    if (gm) {
        gm.clearMarkers();
    }

}




ui.gm.pop_cities = function(cities) {

    cities = cities || geo.top_cities || {};

    $.each(cities, function(i) {
        city = geo.cities[cities[i]];
        gm.city2airline.addMarker(city.la,city.lo, cities[i])
    });

}


ui.gm.city2airline_hover = function( id1, id2 ) {

    id1 = id1 || null;
    id2 = id2 || null;

    if (id2)
    {
        $.flash.info(geo.cities[id1].name +' - '+ geo.cities[id2].name);
    }
    else if (id1)
    {
        $.flash.info(geo.cities[id1].name +' - ?');
    }
    else
    {
         // $.flash.info('Choose a city or pair on map');
         $.flash.info('');
    }



}


ui.gm.city2airline_click = function( id1, id2 ) {

    id1 = id1 || null;
    id2 = id2 || null;

    if (id1 && id2) {
        ui.ajax('route2airline', { 'id1' : id1,  'id2' : id2 });
    }

    // ui.ajax('city2airline', marker.id);

}
