var BrowserHost = function() {
    
    var _messages = [];
    var _canSend = true;
    
    function encode(arr) 
    {
        var ret = '';
        for(i = 0; i < arr.length; ++i)
        {
            if (ret.length > 0)
                ret += '&';
            ret += escape('' + arr[i]);
        }
        return ret;
    }
    
    function callHost()
    {
        var loc = window.location.pathname + '';
        if (loc.length == 0 || loc.charAt(loc.length - 1) != '/')
            loc += '/';
        loc += "BrowserHostCall"
        window.location.pathname = loc;
    }
    
    
    return {
        
            makeInitData: function(_token, _deviceID, _prefs) {
                
                return {
                    token: _token,
                    deviceID: _deviceID,
                    prefs: _prefs
                }
            },
                
            makeBookID: function(_asin, _sample) {
                
                return {
                    asin: _asin,
                    sample: _sample
                }
            },
                
            makeBookStatus: function(_bookID, _status, _percentDownloaded) {
                
                return {
                    id: _bookID,
                    status: _status,
                    percentDownloaded: _percentDownloaded

                }
            },
        
                
            postMessage: function(cmd) {
            
                var args = Array.prototype.slice.call(arguments);
                _messages.push(args);
                if (_canSend)
                {
                    _canSend = false;
                    callHost();
                }
            },
            
            getMessage: function() {
            
                return _messages.length == 0 ? "" : encode(_messages.shift());
            },
        
            finishGetMessage: function() {
                
                _canSend = true;
            },
    
    };
}();



