<html>
    <head>
        <script src="/edgeResources/extensions/lib/jquery-1.7.1.min.js"></script>
        <script>
            "use strict";
            
            var $ = jQuery;
            
            window.AnimateAuthoring = {
                
                extensions: {},
                callbacks: {},
                commandCallbacks: {},
                
                registerExtension: function (name, ext) {
                    
                    var cb,
                        cmd;
                    
                    window.AnimateAuthoring.ext = ext;
                    window.AnimateAuthoring.extensionId = ext.id;
                    window.AnimateAuthoring.extensions[ext.id] = ext;
                    
                    if (ext.callbacks) {
                        for (cb in ext.callbacks) {
                            if (window.AnimateAuthoring.callbacks[cb] === undefined || window.AnimateAuthoring.callbacks[cb] === null) {
                                window.AnimateAuthoring.callbacks[cb] = [];
                            }
                            window.AnimateAuthoring.callbacks[cb].push(ext.callbacks[cb]);
                        }
                    }
                    
                    for (cmd in ext.menu) {
                        if (ext.menu.hasOwnProperty(cmd)) {
                            window.AnimateAuthoring.commandCallbacks[cmd] = ext.menu[cmd].callback;
                        }
                    }    
                    
                    window.setTimeout(function () {
                        var sServerExtensionDataPost = "/extensions/" + window.AnimateAuthoring.extensionId + "/extensionRegistered.js";
                        $.ajax({
                            type: "POST",
                            url: sServerExtensionDataPost,
                            dataType: "text",
                            data: ""
                        });
                    }, 100);
                },
                
                handleCommand: function (cmdId) {
                    
                    if (window.AnimateAuthoring.commandCallbacks[cmdId]) {
                        window.AnimateAuthoring.commandCallbacks[cmdId]();
                    }
                    
                },
                    
                getExtensionData: function () {
                    return JSON.stringify(window.AnimateAuthoring.ext);
                },
                
                issueCallback: function (sCB, prmA) {
                    var i,
                        aCB = window.AnimateAuthoring.callbacks[sCB];
                    
                    if (aCB) {
                        for (i = 0; i < aCB.length; i++) {
                            try {
                                aCB[i](prmA);
                            }
                            catch(except) {
                                console.log("error: " + except);
                            }
                        }
                    }
                },
                
                require: function (sURI) {
                    /*
                    //TBD: really use require.js
                    */
                    
                    var e = document.createElement("script");
                    e.src = sURI;
                    e.type = "text/javascript";
                    document.getElementsByTagName('head')[0].appendChild(e);
                    
                },
                
                //called by animate
                initialize: function () {
                    
                    //window.AnimateAuthoring._sendAwaitCommand();
                    
                    window.AnimateAuthoring.issueCallback("init");
                },
                
                //called by Animate
                selectionChanged: function (aSelection) {
                    window.AnimateAuthoring.issueCallback("selectionChanged", aSelection.dom);
                },
                
                //called by Aniamte
                published: function () {
                    window.AnimateAuthoring.issueCallback("published", null);
                },
                
                //called by Extensions
                popupWindow: function (sURI, sTitle, width, height) {
                    var sURL = "/extensions/" + window.AnimateAuthoring.extensionId + "/popupWindow.js?width=" + width + "&height=" + height + "&uri=" + escape(sURI) + "&title=" + escape(sTitle);
                    
                    $.ajax( {
                        type:"POST",
                        url:sURL,
                        dataType:"js",
                        data:"",
                        success:function ( js ) {
                            console.log("popup window success");
                        }
                    } );
                },
                
                _sendAwaitCommand: function () {
                    
                    if (!window.AnimateAuthoring.extensionId) {
                        console.log("ERROR: no extension id defined");
                    }
                    
                    var sServerAwaitURL = "/extensions/" + window.AnimateAuthoring.extensionId + "/awaitCommand.js";
                    $.ajax( {
                        type:"POST",
                        url:sServerAwaitURL,
                        dataType:"text",
                        data:"",
                        success:function ( js ) {
                            //execute the js we got back
                            if (js) {
                                try {
                                    eval( js );
                                }
                                catch (e) {
                                    console.log("exception while evaluating JS " + e);
                                }
                            }
                            //the command blocks, so we can requery immediately
                            setTimeout( function () {
                                    window.AnimateAuthoring._sendAwaitCommand();
                                }, 2000);
                        },
                        statusCode:{
                            404:function () {
                                console.log("status code 404");
                                setTimeout( "window.AnimateAuthoring._sendAwaitCommand()", 2500 );
                            }
                        },
                        error:function ( jqXHR, textStatus, errorThrown ) {
                            console.log("error " + errorThrown);
                            setTimeout( "window.AnimateAuthoring._sendAwaitCommand()", 2500 );
                        }
                    } );
                },
                
                _sendNotification:function (command, payload) {

                    var sURL = "/extensions/" + window.AnimateAuthoring.id + "/notification.js?msg=" + command;

                    $.ajax( {
                        type:"POST",
                        url:sURL,
                        dataType:"js",
                        data:payload,
                        success:function ( js ) {
                            console.log("send notifacation success");
                        }
                    } );
                }
                
                
            };
            
        </script>
        <script src="extension.js"></script>
    </head>
    <body>
        <script>
            window.AnimateAuthoring.initialize();
        </script>
    </body>
</html>