document.observe("dom:loaded", function() {
   if($$('body.venues')[0]){
        venues_map(51.457269, -0.015965);
        venue_map_links();
        $('reset_map').observe('click', function(){
            venues_map(51.457269, -0.015965);
        })
    }
    if($('styles_editor')){
        styles_editor()
    }
    if($('sortable_pages')){
        index_marker()
    }
    if($$('.calendar')[0]){
        calendar();
    }

    // Fancy fonts
    Cufon.replace('h2');
    Cufon.replace('h3');
    Cufon.replace($$('#main_nav a'));
    if($$('body.calendar')[0]){
        Cufon.replace($$('th.monthName'));
        Cufon.replace($('previous_month'));
        Cufon.replace($('next_month'));
    }
    
    obfuscated_emails();
})

function obfuscated_emails(){
    $$('.obfuscated_email').each(function(e){
        address = e.innerHTML + '@' + e.href.gsub('http:', '').gsub('/', '');
        e.replace('<a href="mailto:'+address+'">'+address+'</a>');
    })
}

function calendar(){
    $$('.details').invoke('hide');
    $$('.event').each(function(e){
        e.observe('click', function(){
            //            alert(e.div);
            e.childElements()[1].toggle();
        });
    })
}

function index_marker(){
    $$('#sortable_pages li').invoke('observe', 'mouseup', function(){
        $('index_marker').remove();
        $$('#sortable_pages li')[0].insert('<strong id="index_marker">(index page)</strong>');
    })
}

function styles_editor(){
    $('styles_editor').hide();
    $('styles_editor').insert({
        before: '<p id="styles_editor_toggle">View css styles editor</p>'
    })
    $('styles_editor_toggle').observe('click', function(){
        $('styles_editor').toggle();
    })
}

// Create map of venues with all relevant params
var venues = {
    albany: {
        lat: '51.478216',
        lng: '-0.027401',
        // icon_img: 'albany.png',
        title: "The Albany",
        labelText: "The Albany"
    },
    brockley_jack: {
        lat: '51.457644',
        lng: '-0.039139',
        //icon_img: 'albany.png',
        title: "The Brockley Jack",
        labelText: "Brockley Jack Theatre"
    },
    age_exchange: {
        lat: '51.465104',
        lng: '0.009613',
        //icon_img: 'albany.png',
        title: "Age Exchange",
        labelText: "Age Exchange"
    },
    broadway_theatre: {
        lat: '51.445315',
        lng: '-0.022166',
        //icon_img: 'albany.png',
        title: "Broadway Theatre",
        labelText: "Broadway Theatre"
    },
    cafe_crema: {
        lat: '51.475757',
        lng: '-0.037315',
        //icon_img: 'albany.png',
        title: "Cafe Crema",
        labelText: "Cafe Crema"
    },
    the_hob: {
        lat: '51.440554',
        lng: '-0.053558',
        //icon_img: 'albany.png',
        title: "The Hob",
        labelText: "The Hob"
    }
}

var map
function venues_map(lat, lng) {
    // Basic map setup
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(lat, lng), 12);
    map.enableDoubleClickZoom();

    // Iterate through venues map, generating a LabeledMarker for each
    for (var i in venues) {
        lat = venues[i]['lat'];
        lng = venues[i]['lng'];
        icon_img = venues[i]['icon_img'];
        title = venues[i]['title'];
        labelText = venues[i]['labelText'];
        create_label(lat, lng, icon_img, title, labelText);
    }
}

function create_label(lat, lng, icon_img, title, labelText, popup_content_id){
    var latlng = new GLatLng(lat,lng);

    var icon = new GIcon();
    icon.image = '/images/maps/icons/' + icon_img;
    icon.iconSize = new GSize(32, 32);
    icon.iconAnchor = new GPoint(16, 16);
    icon.infoWindowAnchor = new GPoint(25, 7);

    opts = {
        "icon": icon,
        "clickable": true,
        "title": title,
        "labelText": labelText,
        "labelClass": 'label',
        "labelOffset": new GSize(-6, -10)
    };
    var marker = new LabeledMarker(latlng, opts);

    GEvent.addListener(marker, "click", function() {
        position_map(lat, lng);
    });

    map.addOverlay(marker);
}

// Sets up event handlers for links in venues list to move map to venue location
function venue_map_links() {
    $$('.venues li .show_on_map').each(function(e){
        e.observe('click', function(){
            // Extracts lng & lat data from venues map by using the html id of this link's immediate ancestor - the <li> tag
            position_map(venues[e.ancestors()[0].id]['lat'], venues[e.ancestors()[0].id]['lng']);
        })
    })
}

// Positions existing map to given lat & lng
function position_map(lat, lng) {
    map.setCenter(new GLatLng(lat, lng), 16);
}