/**
 * main.js
 * 
 * Common javascript for all pages
 * 
 */

/**
 * @namespace TMI
 * 
 * The namespace container for the TMI classes and functions
 */
var TMI = {
    
    /**
     * Image Pre-loader Section
     */
    _icc: -1,
    
    imageCache: new Array(),
    
    preloadImg: function (imgSrc) {
        this._icc++;
        this.imageCache[this._icc] = new Image();
        this.imageCache[this._icc].src = imgSrc;
        return this.imageCache[this._icc];
    },
    
    showPrivacyPolicy: function () {
        if (!this.ppWin || this.ppWin.closed) {
            this.ppWin = open('privacy.html', 'ppWin', 'width=745,height=445,top=50,left=50');
        } else {
            this.ppWin.focus();
        }
        return false;
    },
    
    /**
     * Quiz Related Code...
     */
    story: null,
    
    setStory: function (story) {
        this.story = story;
        this.cookies.store('story', story);
    },
    
    getStory: function () {
        return this.story;
    },
    
    /**
     * Persistance...
     */
    cookies: {
        
        _whitelist: ['story'],
        
        store: function (name, val) {
            document.cookie = name + "=" + escape(val);
        },
        
        init: function () {
            var _cookies = document.cookie;
            if (!_cookies.blank()) {
                var _carray = _cookies.split(';');
                
                for (var i = 0; i < _carray.length; i++) {
                    var _cookie = _carray[i].split('=');
                    var _name = _cookie[0].strip();
                    var _val = unescape(_cookie[1]);
                    
                    for (var t = 0; t < this._whitelist.length; t++) {
                        if (_name == this._whitelist[t]) {
                            TMI[_name] = _val;
                            break;
                        }
                    }
                }
            }
        }
    }
    
}; // End TMI

TMI.cookies.init();
