var wimgo = new Object();
wimgo.WidgetManager = {
    _list:[],
    _idx:0,
    _prefix:'wimgoWidget',
    registerWidget:function(widget)
    {
        for(var k in this._list){
            if(this._list[k] == widget){
                return k;
            }
        }
        var idx = this._prefix + (this._idx++);
        this._list[idx] = widget;
        window[idx]=widget;
        return idx;
    },
    unregisterWidget:function(widget)
    {
        for (var k in this._list) {
            if(this._list[k]==widget){
                this._list[k] =  null;
                window[k] = null;
                return;
            }
        }
    },
    getWidget: function(idx)
    {
        return this._list[idx];
    }
}
wimgo.Widget = function(init){
    this.wimgoCssUrl    = "http://wimgo.com/media/css/satellites/wimgo.php";
    this.wimgoJsUrl     = "http://wimgo.com/media/js/satellites/api.php";
    this.key            = 'WimgoApiKey';

/* Switch daily to the correct value for the template being used */
    switch(init.template)
    {
        case 1:
                init.daily  = 1;
                init.ecal   = "large";
                break;
        case 2:
                init.daily  = 0;
                init.ecal   = "large";
                break;
        case 3:
                init.daily  = 0;
                init.ecal   = "off";
                break;
        case 4:
                init.daily = 0;
                init.ecal   = "large";
                break;
        case 5:
                init.daily = 1;
                init.ecal   = "small";
                break;
        case 6:
                init.daily  = 1;
                init.ecal   = "large";
                break;
        case 7:
                init.daily = 1;
                init.ecal   = "large";
        case 10:
                break;
    }

    if(typeof(init.key) != 'undefined')  key  = init.key;

    if(typeof(init.wimgoCssUrl) != 'undefined') this.wimgoCssUrl = init.wimgoCssUrl;
    if(typeof(init.wimgoJsUrl) != 'undefined')  this.wimgoJsUrl  = init.wimgoJsUrl;  

    this.wimgoCssUrl    +=  "?key="+this.key;
    this.wimgoJsUrl     +=  "?key="+this.key;
    if(typeof(init.movies) != 'undefined') {
        this.wimgoCssUrl += '&movies='+escape(init.movies)
        this.wimgoJsUrl += '&movies='+escape(init.movies)
    }

    this.idx = wimgo.WidgetManager.registerWidget(this);
    this.init = init || {};
    this.widget = {};
    this.initialized = false;
    this.loading=false;
    this.initialize();
};
wimgo.Widget.prototype.initialize = function()
{
    if(this.initialized) return;
    if (typeof(wimgoWidget) != 'function') {
        if (!this.loading) {
            this.load();
        }
        var d = new wimgo.Delegate(this, this.initialize);
        setTimeout(d, 1000);
    } else {
        this.initialized = true;
        var href= this.wimgoCssUrl

        if(typeof(this.init.template) != 'undefined')          href += '&template='          + escape(this.init.template)
        if(typeof(this.init.ecal) != 'undefined')              href += '&ecal='              + escape(this.init.ecal)
        if(typeof(this.init.id) != 'undefined')                href += '&id='                + escape(this.init.id)
        if(typeof(this.init.fontFamily) != 'undefined')        href += '&fontFamily='        + escape(this.init.fontFamily)
        if(typeof(this.init.linkColor) != 'undefined')         href += '&linkColor='         + escape(this.init.linkColor)
        if(typeof(this.init.fgColor) != 'undefined')           href += '&fgColor='           + escape(this.init.fgColor)
        if(typeof(this.init.bgColor) != 'undefined')           href += '&bgColor='           + escape(this.init.bgColor)
        if(typeof(this.init.headerBgColor) != 'undefined')     href += '&headerBgColor='     + escape(this.init.headerBgColor)
        if(typeof(this.init.headerFgColor) != 'undefined')     href += '&headerFgColor='     + escape(this.init.headerFgColor)
        if(typeof(this.init.ecalBorderColor) != 'undefined')   href += '&ecalBorderColor='   + escape(this.init.ecalBorderColor)
        if(typeof(this.init.eventBgColor1)!='undefined')       href += '&eventBgColor1='     + escape(this.init.eventBgColor1)
        if(typeof(this.init.eventBgColor2)!='undefined')       href += '&eventBgColor2='     + escape(this.init.eventBgColor2)
        if(typeof(this.init.calDayBgSelected) !='undefined')   href += '&calDayBgSelected='  + escape(this.init.calDayBgSelected)
        if(typeof(this.init.calDayFgSelected) !='undefined')   href += '&calDayFgSelected='  + escape(this.init.calDayFgSelected)
        if(typeof(this.init.widgetBorderBgColor)!='undefined') href += '&widgetBorderBgColor='+escape(this.init.widgetBorderBgColor)
        if(typeof(this.init.widgetBorderFgColor)!='undefined') href += '&widgetBorderFgColor='+escape(this.init.widgetBorderFgColor)
        if(typeof(this.init.movies) != 'undefined')            href += '&movies='             +escape(this.init.movies)

        var _css=document.createElement("link");
        _css.setAttribute("rel", "stylesheet");
        _css.setAttribute("type", "text/css");
        _css.setAttribute("href", href);
        document.getElementsByTagName("head")[0].appendChild(_css)

        if(typeof(this.init.cssOverride)!='undefined') {
            var _cssOverride=document.createElement("link");
            _cssOverride.setAttribute("rel", "stylesheet");
            _cssOverride.setAttribute("type", "text/css");
            _cssOverride.setAttribute("href", this.init.cssOverride);
            document.getElementsByTagName("head")[0].appendChild(_cssOverride)
        }

        this.widget = new wimgoWidget(this.init.id, this.init, "window."+ this.idx+".widget");
        return this.widget;
    }
};
wimgo.Widget.prototype.load = function()
{
    if(this.loading) return;
    this.loading = true;
    
    var js = this.wimgoJsUrl;
    
    if(typeof(this.init.ecal) != 'undefined')       js += '&ecal='      + escape(this.init.ecal)
    if(typeof(this.init.daily) != 'undefined')      js += '&daily='     + escape(this.init.daily)
    if(typeof(this.init.style) != 'undefined')      js += '&style='     + escape(this.init.style)
    if(typeof(this.init.template) != 'undefined')   js += '&template='  + escape(this.init.template)
    
    var _jsApi=document.createElement("script");
    _jsApi.setAttribute("type", "text/javascript");
    _jsApi.setAttribute("src", js);
    document.getElementsByTagName("head")[0].appendChild(_jsApi);
};
wimgo.Delegate = function(self, func)
{
     if(typeof(func) == "string"){
         func = self[func];
     }
     //this vars will init to undefined if func has not already been delegated.
     //but we can delegate delegates so this is necessary.
     var im_func = func.im_func;
     var im_preargs = func.im_preargs;
     var im_self = func.im_self;
     if(typeof(func) == "function" && typeof(func.apply) == "undefined"){
     }
     if(typeof(im_func) != 'function'){
         im_func = func;
     }
     if(typeof(self) != 'undefined'){
         im_self = self;
     }
     if(typeof(im_preargs) == 'undefined'){
         im_preargs = [];
     } else {
         im_preargs = im_preargs.slice();
     }
     var newFunc = function(){
         var args = [];
         for(var i=0; i<arguments.length; i++){
             args.push(arguments[i]);
         }
         var me = arguments.callee;
         if (me.im_preargs.length > 0) {
             args = me.im_preargs.concat(args);
         }
         var self = me.im_self;
         if (!self) {
             self = this;
         }
 
         return me.im_func.apply(self, args);
     }
     newFunc.im_self = im_self;
     newFunc.im_func = im_func;
     newFunc.im_preargs = im_preargs;
     return newFunc;
}

