/*
@author    : Igor "SkAZi" Potapov <igor@potapoff.org>
@copyright : Plan-B Ltd.
@require   : MooTools 1.2
*/

var GMAP,
    MAPLIST,
    MAPCITY,
    CITY_TEMPLATE = "<p><a href='/" + LANGUAGE_CODE + "/map/list/' "+
                    "rel='lightbox[360 400]'>{title}</a> (",
    KOFFEINICON = new GIcon(G_DEFAULT_ICON);

if(LANGUAGE_CODE=='ru'){
    INFO_TEMPLATE = "{icon} <p>{address}</p><p><strong>{start}&mdash;{end} {comment}</strong></p>",
    INFO_TEMPLATE_NOTIME = "{icon} <p>{address}</p><p><strong>{comment}</strong></p>",
    LIST_TEMPLATE = "<p>{icon} <a>{address}</a></p>",
    INFO_TEMPLATE_ICONS = [
        '',
        '<img src="/media/img/new.png" class="png" width="45" height="13" alt="Новое" />',
        '<img src="/media/img/soon.png" class="png" width="45" height="13" alt="Скоро" />' 
    ]
} else {
    INFO_TEMPLATE = "{icon} <p>{address_en}</p><p><strong>{start}&mdash;{end} {comment_en}</strong></p>",
    INFO_TEMPLATE_NOTIME = "{icon} <p>{address_en}</p><p><strong>{comment_en}</strong></p>",
    LIST_TEMPLATE = "<p>{icon} <a>{address_en}</a></p>",
    INFO_TEMPLATE_ICONS = [
        '',
        '<img src="/media/img/new_en.png" class="png" width="35" height="13" alt="New" />',
        '<img src="/media/img/soon_en.png" class="png" width="43" height="13" alt="Soon" />' 
    ]
}
    
KOFFEINICON.iconSize = new GSize(25, 39);
KOFFEINICON.image = "/media/img/pointer.png";
KOFFEINICON.shadowSize = new GSize(44, 41);
KOFFEINICON.shadow = "/media/img/pointer_shade.png";

MARKEROPTIONS = {
    icon: KOFFEINICON
}

var cityChoice = function(id_city){
    requestForCities(id_city);
}

var generateMap = function(city){
    if (!GBrowserIsCompatible()) return;
    
    if(GMAP){
        GMAP.clearOverlays();
    } else {
        GMAP = new GMap2($("map_canvas"));
        GMAP.addControl(new GSmallZoomControl());
        GMAP.addControl(new GMapTypeControl());
    }
    
    GMAP.setCenter(new GLatLng(city.latitude, city.longitude), city.zoom);
    
    if(CITIESCOUNT > 1)
        MAPCITY.set('html', CITY_TEMPLATE.substitute(city) + city.items.length + ")</a>");
    else 
        MAPCITY.set('html', city.title);

    MAPCITY.getElements('a').mediabox();
    MAPLIST.set('html', '');
    city.items.each(function(baseitem){
        var item = baseitem.fields;
        item.icon = INFO_TEMPLATE_ICONS[baseitem.fields.state];
        item.start = item.start? item.start.replace(/:\d\d$/, ''): null;
        item.end = item.end? item.end.replace(/:\d\d$/, ''): null;
        
        var point = new GLatLng(item.latitude, item.longitude);
        var marker = new GMarker(point, MARKEROPTIONS);
        var list_item = new Element('div', {
            'class': 'news',
            'html': LIST_TEMPLATE.substitute(item)
        });
        
        list_item.inject(MAPLIST, 'bottom');
        
        list_item.addEvents({
            'mouseenter': function(){
                GEvent.trigger(marker, 'mouseover');
            },
            'mouseleave': function(){
                GEvent.trigger(marker, 'mouseout');
            },
            'click': function(){
                GEvent.trigger(marker, 'click');
            }
        });
        
        GEvent.addListener(marker, "mouseover", function(){
            list_item.addClass('hover');
        });
        
        GEvent.addListener(marker, "mouseout", function(){
            list_item.removeClass('hover');
        });
        
        GEvent.addListener(marker, "click", function(){
            var text = (!item.start || !item.end)? INFO_TEMPLATE_NOTIME.substitute(item): INFO_TEMPLATE.substitute(item);
            GMAP.openInfoWindowHtml(point, text, { maxWidth: 250 });
        });
        
        GMAP.addOverlay(marker);
    });

    $('map_list').getElements('.scroll-control').dispose();
    MAPLIST.setStyle('marginTop', 0);
    $('map_list').getElements('.scroll-content').each( makeScrollable );
    
}

var requestForCities = function(city_id){
    new Request.JSON({
        url: "/" + LANGUAGE_CODE + "/map/city/" + (city_id? city_id + "/": ""),
        onSuccess: generateMap
    }).get();
}


document.addEvent('domready', function() {
    MAPLIST = $('map_list_inner');
    MAPCITY = $('map_list_city');
});

document.addEvent('unload', function(){ GUnload() });
