// treat this as a static object

var WidgetFactory = new Class({
    assetsLoaded: [],
    assetsPending: new Object(), // assoc array

    getInstance: function(schmidgetType, dbID, desk, pendingDiv) {
        var scriptID = schmidgetType + 'JS';
        var cssID = schmidgetType + 'CSS';

        // var headContents = document.head.innerHTML.toString();        // grabs the contents of the document <head>

        // if  { // evaluates wether or not the supporting CSS file for this schmidget is loaded
        // new Asset.css('apps/' + schmidgetType + '/' + schmidgetType + '.css', {'id': cssID});    // if it's not there, we load it now
        // }

        // need the schmidget JS before we instantiate

        // var isLoaded = (headContents.indexOf(cssID) > 0) && (headContents.indexOf(scriptID) > 0);

        var isLoaded = false;

        // console.log('schmidget (' + schmidgetType + ') is loaded? ' + isLoaded);

        var factory = this;    // reference to self

        if (this.assetsLoaded.contains(schmidgetType)) { // evaluates wether or not the supporting JS file for this schmidget is loaded
            this.createInstance(schmidgetType, dbID, desk, pendingDiv);
        }
        else {
            // load CSS and JS
            // console.log('Loading assets for ' + schmidgetType);

            // comp is a global variable
            // removed random strings.  allow caching since we're sending Etags now.
            var jsPath = '/apps/' + schmidgetType + '/' + schmidgetType + comp + '.js?r=' + Math.random(0,999999);
            var cssPath = '/apps/' + schmidgetType + '/' + schmidgetType + comp + '.css';

            if (!this.assetsPending[schmidgetType]){
                this.assetsPending[schmidgetType] = new Array();
            }

            var pending = this.assetsPending[schmidgetType];

            pending[pending.length] = {'schmidgetType': schmidgetType, 'dbID': dbID, 'desk': desk, 'pendingDiv': pendingDiv};

            if (pending.length == 1){
                new Asset.css(cssPath, {
                    'id': cssID
                });

                var self = this;
                new Asset.javascript(jsPath, {
                    'id': scriptID,
                    'onload': function(){
                        self.createPending(schmidgetType);
                    }
                });
            }
        }
    },

    createPending: function(schmidgetType){
        var pending = this.assetsPending[schmidgetType];
        for(var i in pending){
            var params = pending[i];

            // add to loaded list
            this.assetsLoaded.include(params.schmidgetType);
            this.createInstance(params.schmidgetType, params.dbID, params.desk, params.pendingDiv);
        }
        delete this.assetsPending[schmidgetType];
    },

    createInstance: function(schmidgetType, dbID, desk, pendingDiv) {
        var schmidget;
	
        switch(schmidgetType) {     // switch based on type, instantiating the correct class
	    
        case 'personal': schmidget = new Personal(schmidgetType, dbID, desk);
            break;
        case 'title': schmidget = new Title(schmidgetType, dbID, desk);
            break;
        case 'text': schmidget = new Text(schmidgetType, dbID, desk);
            break;
        case 'photo': schmidget = new Photo(schmidgetType, dbID, desk);
            break;
            
        case 'music': schmidget = new Music(schmidgetType, dbID, desk);
	    	break;
	    
        case 'video': schmidget = new Video(schmidgetType, dbID, desk);
            break;
        case 'embed': schmidget = new Embed(schmidgetType, dbID, desk);
            break;
            
        case 'comments': schmidget = new Comments(schmidgetType, dbID, desk);
            break;
	    
        case 'search': schmidget = new Search(schmidgetType, dbID, desk);
            break;
	    
        case 'images': schmidget = new Images(schmidgetType, dbID, desk);
            break;
	    
        case 'weather': schmidget = new Weather(schmidgetType, dbID, desk);
            break;
	    
        case 'games': schmidget = new Games(schmidgetType, dbID, desk);
            break;
        case 'asteroids': schmidget = new Asteroids(schmidgetType, dbID, desk);
            break;
        case 'frogger': schmidget = new Frogger(schmidgetType, dbID, desk);
            break;
	    
        case 'movies': schmidget = new Movies(schmidgetType, dbID, desk);
            break;
        case 'previews': schmidget = new Previews(schmidgetType, dbID, desk);
            break;
        case 'showtimes': schmidget = new Showtimes(schmidgetType, dbID, desk);
            break;
	    
        case 'youtube': schmidget = new Youtube(schmidgetType, dbID, desk);
            break;
	    
        case 'radio': schmidget = new Radio(schmidgetType, dbID, desk);
            break;
        case 'lastfm': schmidget = new LastFM(schmidgetType, dbID, desk);
            break;
	    
        case 'maps': schmidget = new Maps(schmidgetType, dbID, desk);
            break;
            
        case 'stocks': schmidget = new Stocks(schmidgetType, dbID, desk);
            break;
	    
        case 'amazon': schmidget = new Amazon(schmidgetType, dbID, desk);
            break;
	    
        case 'rss': schmidget = new RSS(schmidgetType, dbID, desk);
            break;
        case 'feed': schmidget = new Feed(schmidgetType, dbID, desk);
            break;
	    
        case 'stickyNotes': schmidget = new StickyNote(schmidgetType, dbID, desk);
            break;
            
        case 'clock': schmidget = new Clock(schmidgetType, dbID, desk);
            break;
	    
        case 'calendar': schmidget = new Calendar(schmidgetType, dbID, desk);
            break;
	    
        case 'todoList': schmidget = new TodoList(schmidgetType, dbID, desk);
            break;
	    
        case 'bookmarks': schmidget = new Bookmarks(schmidgetType, dbID, desk);
            break;
	    
        case 'facebook': schmidget = new Facebook(schmidgetType, dbID, desk);
            break;
	    
	case 'myspace': schmidget = new MySpace(schmidgetType, dbID, desk);
            break;
	    
	case 'twitter': schmidget = new Twitter(schmidgetType, dbID, desk);
            break;
            
        case 'friendfeed': schmidget = new FriendFeed(schmidgetType, dbID, desk);
	    	break;
	    
	case 'contacts': schmidget = new Contacts(schmidgetType, dbID, desk);
            break;
	    
	case 'email': schmidget = new Email(schmidgetType, dbID, desk);
            break;
	case 'gmail': schmidget = new Gmail(schmidgetType, dbID, desk);
            break;

	case 'yahooMail':
            schmidget = new YahooMail(schmidgetType, dbID, desk);
	    break;

	case 'hotmail': schmidget = new Hotmail(schmidgetType, dbID, desk);
	    break;

	case 'aolMail': schmidget = new AolMail(schmidgetType, dbID, desk);
	    break;

	case 'mobileMe': schmidget = new MobileMe(schmidgetType, dbID, desk);
	    break;

	case 'genericMail': schmidget = new GenericMail(schmidgetType, dbID, desk);
	    break;
	case 'slideshow':
	    schmidget = new Slideshow(schmidgetType, dbID, desk);
	    break;

	case 'launcher': schmidget = new Launcher(schmidgetType, dbID, desk);
            break;
        default: return;
        }
        desk.finishAddWidget(schmidget);
        if (pendingDiv) pendingDiv.destroy();
    }
});
