
/*
// ------------------------------------------------------------
// Stratos PHP Framework
// Copyright (c) 2006 Sephira Software, LLC
// 
// This file is subject to the Stratos PHP Framework license
// which you should have received along with this file. The
// license is also accessible on the web at the following URI:
//   http://www.stratosframework.com/index.php/page/license
// If you did not receive a copy of the Stratos PHP Framework
// license or you are unable to obtain it through the web,
// please send an e-mail to stratos@sephirasoft.com so a copy
// can be sent to you.
// ------------------------------------------------------------
*/

var stratos = {
    SERVER_PROTOCOL: '',
    SERVER_HOST: '',
    SERVER_PORT: '',
    SCRIPT_PATH: '',
    SCRIPT_FILE: '',
    
    updateHTML: function(id, action, args, beginCallback, endCallback, view) 
    {
        if ( beginCallback ) stratos.callFunc(beginCallback);
        
        var request = {
            url: stratos.makeUrl('Stratos/Request/getOutput', {
                _action: action,
                _view: view ? view : ''
            }),
            load: function( type, data, transportImplementation, kwArgs )
            {
                var elem = dojo.byId(id);
                if ( elem )
                {
                    elem.innerHTML = data;
                }
                if ( endCallback ) stratos.callFunc(endCallback, [data]);
            },
            error: function( type, error, transportImplementation, kwArgs )
            {
            },
            timeout: function( type, empty, transportImplementation, kwArgs )
            {
            },
            method: (stratos.config.allowUrlArgsFrom == 'pathinfo' ? 'POST' : 'GET'),
            mimetype: 'text/plain'
        };
        
        if ( args && args.tagName && args.tagName.toLowerCase() == 'form' )
        {
            request.formNode = args;
        }
        else
        {
            request.content = args;
        }
        
        dojo.io.bind(request);
    },
    
    callback: function(action, args, beginCallback, endCallback)
    {
        if ( beginCallback ) stratos.callFunc(beginCallback);
        
        var request = {
            url: stratos.makeUrl('Stratos/Request/getJSON', {
                _action: action
            }),
            load: function( type, data, transportImplementation, kwArgs )
            {
                try
                {
                    eval('var res = (' + data + ');');
                }
                catch ( e )
                {
                    var res = {
                        result: [],
                        error: true,
                        flashes: []
                    };
                }
                
                if ( endCallback ) stratos.callFunc(endCallback, [res]);
            },
            error: function( type, error, transportImplementation, kwArgs )
            {
            },
            timeout: function( type, empty, transportImplementation, kwArgs )
            {
            },
            method: (stratos.config.allowUrlArgsFrom == 'pathinfo' ? 'POST' : 'GET'),
            mimetype: 'text/plain'
        };
        
        if ( args && args.tagName && args.tagName.toLowerCase() == 'form' )
        {
            request.formNode = args;
        }
        else
        {
            request.content = args;
        }
        
        dojo.io.bind(request);
    },
    
    makeUrl: function( target, args, anchor, override )
    {
        if ( !args ) args = {};
        if ( !anchor ) anchor = null;
        if ( !override ) override = {};
        
        // If the target is relative, make it absolute.
        if ( target.indexOf('://') < 0 )
        {
            var dest = (override.protocol ? override.protocol : stratos.SERVER_PROTOCOL)
                + (override.host ? override.host : stratos.SERVER_HOST)
                + (override.port ? ':' . override.port : stratos.SERVER_PORT)
                + (override.path ? override.path : stratos.SCRIPT_PATH);
            
            if ( target.charAt(0) == '?' || target.charAt(0) == '/' )
            {
                // target is a string of GET or PATH_INFO arguments
                if ( stratos.config.urlRewriteEnabled )
                {
                    dest += target.charAt(0) == '/'
                        ? target.substr(1)
                        : target;
                }
                else
                {
                    dest += stratos.SCRIPT_FILE + target;
                }
                
            }
            else if ( target.charAt(0) == '.'
                && (target.charAt(1) == '/' || target.charAt(1) == '\\') )
            {
                // target is a file
                dest += target.substr(2);
            }
            else
            {
                // target is an action
                if ( stratos.config.allowUrlArgsFrom == 'both'
                    || stratos.config.allowUrlArgsFrom == 'pathinfo' )
                {
                    dest += stratos.config.urlRewriteEnabled
                        ? target
                        : stratos.SCRIPT_FILE + '/' + target;
                    if ( dojo.isArray(args) || dojo.isObject(args) )
                    {
                        for ( k in args )
                        {
                            var v = args[k];
                            //if ( dojo.isBoolean(v) )
                            //{
                            //    v = v ? 1 : 0;
                            //}
                            
                            v = '' + v;
                            
                            dest += '/' + k + '/'
                                + v.replace(/\//g, '%252F')
                                    .replace(/\\/g, '%255C');
                        }
                    }
                }
                else
                {
                    dest += stratos.config.urlRewriteEnabled
                        ? '' : stratos.SCRIPT_FILE + '?action=' + target;
                    if ( dojo.isArray(args) || dojo.isObject(args) )
                    {
                        for ( k in args )
                        {
                            var v = args[k];
                            //if ( dojo.isBoolean(v) )
                            //{
                            //    v = v ? 1 : 0;
                            //}
                            
                            dest += '&' + k + '=' + v;
                        }
                    }
                }
            }
            
            if ( anchor ) dest += '#' + anchor;
            
            return dest;
        }
        else
        {
            return target;
        }
    },
    
    printFlashes: function(containerId, flashes)
    {
        var container = dojo.byId(containerId);
        if ( container )
        {
            var listHTML = '';
            
            if ( dojo.isArray(flashes) && flashes.length > 0 )
            {
                listHTML = '<ul class="stratos_flashes">';
                
                for ( i in flashes )
                {
                    listHTML = listHTML
                        + '<li class="stratos_flash_' + flashes[i][0] + '">'
                        + flashes[i][1] + '</li>';
                }
                
                listHTML = listHTML + '</ul>';
            }
            
            container.innerHTML = listHTML;
            
            if ( listHTML == '' )
            {
                container.style.display = 'none';
            }
            else
            {
                container.style.display = '';
            }
        }
    },
    
    callFunc: function( func, args )
    {
        if ( !dojo.isArray(args) && !dojo.isObject(args) )
        {
            args = args ? [args] : [];
        }
        
        var callStr = '';
        
        if ( dojo.isFunction(func) )
        {
            callStr += 'func(';
        }
        else if ( dojo.isString(func) )
        {
            callStr += func + '(';
        }
        else if ( dojo.isArray(func) && func.length == 2
            && dojo.isObject(func[0]) && dojo.isString(func[1]) )
        {
            callStr += 'func[0].' + func[1] + '(';
        }
        else
        {
            // TODO Throw an exception?
        }
        
        // Add arguments to callback
        var first = true;
        for ( i in args )
        {
            if ( !first )
            {
                callStr += ', ';
            }
            else
            {
                first = false;
            }
            
            if ( dojo.isNumber(i) )
            {
                callStr += 'args[' + i + ']';
            }
            else if ( dojo.isString(i) )
            {
                callStr += 'args["' + i.replace('"', '\\"') + '"]';
            }
            else
            {
                // TODO Throw an exception?
            }
        }
        
        callStr += ');';
        
        return eval(callStr);
    },
    
    init: function()
    {
        
    }
};

stratos.config = {
    allowUrlArgsFrom: 'both',
    urlRewriteEnabled: false,
    defaultAction: '',
    defaultView: '',
    masterView: 'overall-layout.php'
};

stratos.response = {
    result: [],
    error: false,
    flashes: []
};

stratos.ui = {};

stratos.ui.modalSpinner = {
    spinner: null,
    spinnerCount: 0,
    
    start: function()
    {
        if ( !stratos.ui.modalSpinner.spinner )
        {
            dojo.require('dojo.html.util');
            dojo.require('dojo.widget.Dialog');
            
            var nodes = dojo.html.createNodesFromText('<div id="stratosModalSpinner" dojoType="Dialog" bgColor="black" bgOpacity="0.6" toggle="fade" toggleDuration="250" style="display: none; height: 400px; width: 400px;">'
                + '<div id="stratosModalSpinnerContent" style="text-align: center;">'
                + '<img src="' + stratos.makeUrl('./stratos/views/images/big-spinner.gif') + '" style="margin-bottom: 10px;"/>'
                + '<div><a href="javascript:void(0);" onclick="stratos.ui.modalSpinner.close();" style="font-family: Arial; font-size: 12px; font-weight: normal; text-decoration: none; margin: 0; padding: 0; color: #999; border: 0; background: none;">[close]</a></div>'
                + '</div>'
                + '</div>');
            document.body.appendChild(nodes[0]);
            stratos.ui.modalSpinner.spinner = dojo.widget.createWidget(nodes[0]);
        }
        
        ++stratos.ui.modalSpinner.spinnerCount;
        
        if ( stratos.ui.modalSpinner.spinnerCount == 1 )
        {
            stratos.ui.modalSpinner.spinner.show();
        }
    },
    
    stop: function()
    {
        if ( stratos.ui.modalSpinner.spinnerCount > 0 )
        {
            --stratos.ui.modalSpinner.spinnerCount
            if ( stratos.ui.modalSpinner.spinnerCount == 0 )
            {
                stratos.ui.modalSpinner.spinner.hide();
            }
        }
    }
};

stratos.ui.floatingSpinner = {
    spinner: null,
    spinnerCount: 0,
    
    start: function()
    {
        if ( !stratos.ui.floatingSpinner.spinner )
        {
            dojo.require('dojo.html.util');
            
            var nodes = dojo.html.createNodesFromText('<div id="stratosFloatingSpinner" style="visibility: hidden; position: absolute; padding: 1px; text-align: center; width: 150px;">'
                + '<div style="font-family: Arial; font-size: 18px; font-weight: bold; padding: 2px 5px 2px 5px; color: #006600; border: solid green 1px; background-color: #bbffbb;">'
                + '<img src="' + stratos.makeUrl('./stratos/views/images/spinner1.gif') + '" style="vertical-align: middle; position: relative; top: -1px;"/> Working...</div>'
                + '</div>');
            stratos.ui.floatingSpinner.spinner = nodes[0];
            document.body.appendChild(stratos.ui.floatingSpinner.spinner);
            
            dojo.event.connect(window, "onscroll", stratos.ui.floatingSpinner.position);
            dojo.event.connect(window, "onresize", stratos.ui.floatingSpinner.position);
        }
        
        ++stratos.ui.floatingSpinner.spinnerCount;
        
        if ( stratos.ui.floatingSpinner.spinnerCount == 1 )
        {
            stratos.ui.floatingSpinner.spinner.style.visibility = 'visible';
            stratos.ui.floatingSpinner.position();
        }
    },
    
    stop: function()
    {
        if ( stratos.ui.floatingSpinner.spinnerCount > 0 )
        {
            --stratos.ui.floatingSpinner.spinnerCount
            if ( stratos.ui.floatingSpinner.spinnerCount == 0 )
            {
                stratos.ui.floatingSpinner.spinner.style.visibility = 'hidden';
            }
        }
    },
    
    position: function()
    {
        stratos.ui.floatingSpinner.spinner.style.top = dojo.html.getScrollTop() + 'px';
        stratos.ui.floatingSpinner.spinner.style.left = (dojo.html.getScrollLeft()+(dojo.html.getViewportWidth()-dojo.html.getMarginBoxWidth(stratos.ui.floatingSpinner.spinner)))+ 'px';
    }
};

dojo.addOnLoad(stratos, 'init');

var Stratos = stratos;


