﻿mapl = {
    url: window.location.href,
    server: window.location.href.match(/^.*?:\/\/.*?\//)[0],
    staticServer: {
        'file:///': '',
        'http://localhost:60598/': '',
        'http://localhost:3518/': 'http://testnp1a.digitalink.com/static_media/wp-admin/',
        'http://wppdev.digitalink.com/': 'http://testnp1a.digitalink.com/static_media/wp/',
        'default': 'http://media10.washingtonpost.com/wp/'
    },
    init: function() {
        if (mapl.staticServer[mapl.server]) {
            mapl.staticServer = mapl.staticServer[mapl.server];
        } else {
            mapl.staticServer = mapl.staticServer['default'];
        }

        this.addDomInit(this.baseDomInit.bind(this));
        this.domInit(document);

        this.addDomInit(this.appInit);
        this.appInit(document);
    },
    apps: {
        /* This is the geoCode button used on the django admins.
        ** It will look for and initialize all ids containing id_location.
        ** It will create two buttons Map It and GeoCode.  GeoCode will take
        ** the string in the location field query a url and return the lat lng
        ** into input id_latitude and id_longitude.  It will work with set of
        ** these inputs id_location* id_latitude* and id_longitude* *being
        ** common to the set  
        GeoCodeButton:{name:'Django Admin Geo Code and Map It Button',triggers:['input[id*=id_location]'],dependencies:null,url:'js/apps/GeoCodeButton.js'},
        */

        /* kind of overwraught dom configed google mapping layer */
        MappingLayer: { name: 'Dom Configed Google Mapping Layer', triggers: ['.imap'], dependencies: null, url: 'js/apps/MappingLayer.js' },
        Overlay: { name: 'Main Overlay Construction Class', triggers: [], dependencies: null, url: 'js/apps/Overlay.js' },
        AjaxOverlay: { name: 'Makes the content of any link open in a Modal Ajax Window', dependencies: ['Overlay'], triggers: ['.ajax-overlay'], url: 'js/apps/AjaxOverlay.js' },
        AnnotateInterface: { name: 'Annotations Overlay and Flash connector', dependencies: ['Overlay', 'FlashEmbed'], triggers: ['.annotate-section'], url: 'js/apps/AnnotateInterface.js' },
        FlashEmbed: { name: 'Quick Flash Embeder', triggers: ['.flash-embed'], dependencies: null, url: 'js/apps/FlashEmbed.js' },
        SideToggle: { name: 'Slides Elments Open or Closed and Hides Them', triggers: ['.slide-toggle'], dependencies: null, url: 'js/apps/SlideToggle.js' },
        Toast: { name: 'Toaster User Notification System', triggers: ['body'], dependencies: ['MootoolsMoreDragMove'], url: 'js/apps/Toast.js' },
        MootoolsMoreDragMove: { name: 'Mootools Drag and Move Library', triggers: [], dependencies: null, url: 'js/apps/MootoolsMoreDragMove.js' }
    },
    appInit: function(root) {
        $H(mapl.apps).each(function(app, appName) {
            app.triggers.each(function(scan) {
                if (root.getElement(scan) && !app.active) {
                    if (app.dependencies) {
                        app.dependencies.each(function(dependant) {
                            if (!mapl.apps[dependant].active) {
                                mapl.appLoad(mapl.apps[dependant]);
                            }
                        });
                    }
                    mapl.appLoad(app);
                }
            });
        });
    },
    appLoad: function(app) {
        app.active = true;
        if (app.url.match('://')) {
            new Asset.javascript(app.url);
        } else {
            new Asset.javascript(mapl.staticServer + app.url);
        }
    },
    baseDomInit: function(root) {
        /*
        root.getElements('input').each(function(s){
        s.defaultValue = s.get('value');
        });
        */
    },
    addDomInit: function(fn) {
        this.domInits.push(fn);
        return this.domInits.getLast();
    },
    domInit: function(root) {
        mapl.domInits.each(function(s) {
            s(root);
        });
    },
    domInits: [],
    classes: {}
}

window.addEvent('domready',mapl.init.bind(mapl));