jQuery(document).ready(function($) {
    var grappos = {
        grapposApp: 'http://www.grappos.com/_app/widget/1.0/',
        browser_supported: null,
        grappos_div: null,
        client_config: null,
        config_param: ['uid', 'type', 'scope', 'prodAC', 'bkgdClr', 'txtClr', 
            'autoHover', 'resHover', 'recent', 'labels', 'spinner'],
        errMsgCss: {
            'font-family': 'Arial, Helvetica, sans-serif',
            'font-size': '11px',
        	'padding': '10px',
        	'color': '#000000',
        	'background': '#CCCCCC'
        },
        init: function() {
            if (!this.browserSupported()) {
                this.grapposDiv().css(this.errMsgCss)
                    .html('<div style="width: 300px">We support MSIE 7 and above for new development. Please contact support@grappos.com if you have any questions.</div>');
                return;
            }
            if (this.isRemote()) {
                this.initRemote();
                return;
            }
            this.initWidget();
        },
        initWidget: function() {
            this.grapposDiv()
                .html('<iframe src="' + this.grapposApp + 'widget.php?' + this.prepConfigQueryString() + '" id="grappos-iwidget" width="100%" height="100%" border="0" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>');
        },
        initRemote: function() {
            this.grapposDiv().css(this.errMsgCss)
                .html('<div style="width: 300px">Under Development.</div>');
        },
        prepConfigQueryString: function() {
            var qstrs = ['host=' + encodeURIComponent(window.location.hostname)];
            for(var i in this.config_param) {
                param = this.config_param[i];
                if (this.clientConfig(param)) {
                    qstrs.push(encodeURIComponent(param) + '=' + encodeURIComponent(this.clientConfig(param)));
                }
            }
            return qstrs.join('&');
        },
        browserSupported: function() {
            if (!this.browser_supported) this.browser_supported = !($.browser.msie && parseInt($.browser.version) < 7);
            return this.browser_supported;
        },
        grapposDiv: function() {
            if (!this.grappos_div) this.grappos_div = $('#grappos-widget');
            return this.grappos_div;
        },
        clientConfig: function(param) {
            if (!this.client_config) this.client_config = grapposConfig;
            if (param) return this.client_config[param];
            return this.client_config;
        },
        isWidget: function() {
            return (this.clientConfig('type') === 'widget');
        },
        isRemote: function() {
            return (this.clientConfig('type') === 'remote');
        }
    };
    grappos.init();
});
