var movie;

//{{{ CookieManager
var CookieManager = {
    cookies: new Array(),
    cached: '',
    accepts: undefined,

    set: function(name, value, expires_days) {
        if (!this.accepts && this.accepts != undefined) {
            if (!this.check()) {
                return false;
            }
        }

        var expires = new Date(new Date().getTime() + (expires_days * 24 * 60 * 60 * 1000));
        var cookie_str = name + "=" + escape(value);
        if (expires_days) {
            cookie_str += ";expires=" + expires.toGMTString();
        }
        cookie_str += ";path=/;";

        document.cookie = cookie_str;
        return true;
    },

    get: function(name) {
        this.cache();
        var value = null;
        if (this.cookies[name] != undefined) {
            value = this.cookies[name];
        }
        return value;
    },

    remove: function(name) {
        this.set(name, '', -1);
    },

    check: function() {
        if (this.accepts != undefined) {
            return this.accepts;
        }

        if (document.cookie == 'undefined') {
            this.accepts = false;
            return false;
        }

        this.set('test_cookie_name', 'test_cookie_value');
        if (this.get('test_cookie_name') != 'test_cookie_value') {
            this.accepts = false;
            return false;
        }
        this.remove('test_cookie_name');
        
        this.accepts = true;
        return true;
    },

    cache: function() {
        // Already cached
        if (this.cached == document.cookie) {
            return;
        }

        this.cookies = new Array();
        this.cached = document.cookie;
        var pairs = this.cached.split("; ");
        var name = '', value = '';
        
        for (var i = 0; i < pairs.length; i++) {
            var parts = pairs[i].split('=');
            if (parts.length > 1) {
                value = unescape(parts[1]);
            }

            name = unescape(parts[0]);
            if (name) {
                this.cookies[name] = value;
            }
        }
    }
};
//}}}

//{{{ wheel(event)
function wheel(event) {
    var delta = 0;
    if (!event) event = window.event;
    if (event.wheelDelta) {
        delta = event.wheelDelta/120;
        if (window.opera) delta = -delta;
    } else if (event.detail) {
        delta = -event.detail/3;
    }

    if( /AppleWebKit/.test(navigator.userAgent) ) {
        delta /= 3;	
    }

    if (delta) {
        movie.jsWheel(delta);
    }

} //}}}

//{{{ movie_loaded()
function movie_loaded() {
    if (window.addEventListener) {
        window.addEventListener('DOMMouseScroll', wheel, false);
    }
    window.onmousewheel = document.onmousewheel = wheel;
} //}}}

//{{{ open_popup(url)
function open_popup(url) {
    var name = arguments[1] || 'popup';
    var width = arguments[2] || 500;
    var height = arguments[3] || 500;
    var params = arguments[4] || {};

    var _width = screen.width;
    var _height = screen.height;

    var left = (_width - width) / 2;
    var top = (_height - height) / 2 - 20;

    var default_params = {
        scrollbars: 'yes',
        directories: 'no',
        location: 'no',
        menubar: 'no',
        status: 'no',
        toolbar: 'no',
        resizable: 'yes'
    };

    if (!params) params = {};
    for (var key in params)
    {
        default_params[key] = params[key];
    }

    var options = '';
    for (var key in default_params)
    {
        options += ',' + key + '=' + default_params[key];
    }

    var popup = window.open(url, name, 'width='+width+',height='+height+',top='+top+',left='+left+','+options);
    if (popup.focus)
    {
        setTimeout(function() { popup.focus(); }, 100);
    }
}
//}}}

window.onload = function() {
    movie = window['distinct_main'] || document['distinct_main'];
    movie.focus();
}
