var SpyFilms = (function(){
    var embed_swf,
        v_installed = swfobject.getFlashPlayerVersion(),
        v_split     = v_installed.major + '.' +
                      v_installed.minor + '.' +
                      v_installed.release,
        config      = {
            object_id:   'flash',
            v_required:  10,
            flashvars:   {
                main_swf: '',
                model:    ''
            },
            params:      {
                allowfullscreen: true,
                bgcolor: '#000000',
                wmode: 'normal',
                allowscriptaccess: 'always'
            },
            attrs:       {
                xiredirecturl: window.location.href
            }
        };

    return {
    /* PUBLIC methods
     ----------------------------------------------------------------------------*/
    loadFlash: function(options) {

        if(options.swf_preload.length) {
            embed_swf = options.swf_preload;
            config.flashvars.main_swf = options.swf_main;
            config.flashvars.model = options.model;
        }
        else {
            embed_swf = options.swf_main;
        }

        if (v_installed.major >= config.v_required) {
            swfobject.embedSWF(
                embed_swf,
                config.object_id,
                '100%', '100%',
                config.v_required + '.0.0',
                '/assets/swf/expressInstall.swf',
                config.flashvars,
                config.params,
                config.attrs
            );
        }
        else {
            _loadUpgradeMsg();
        }

        function _loadUpgradeMsg() {
            var screen_h = $('#' + config.object_id).height(),
                upgrade_h = $('#upgrade').height(),
                offset = Math.round((screen_h = upgrade_h) / 2);

            $('#upgrade span#ver').append(this.v_split);
            $('#upgrade span#minVersion').append(this.v_required);
            $('#upgrade').css('margin', offset + 'px auto 0 auto');
        }
    },

    addFeedLink: function(directorRef, directorTitle, format) {
        var href = '/feed/director/' + directorRef + '/' + format,
            title = directorTitle + ': Latest Work (' + format + ')';

        if ($('#directorfeed_' + format).length) {
            $('#directorfeed_' + format).attr({
                href:  href,
                title: title
            });
        }
        else {
            var link = document.createElement('link');
            link.id = 'directorfeed_' + format;
            link.type = 'application/atom+xml';
            link.rel = 'alternate';
            link.href = href;
            link.title = title;

            $('head link:last').after(link);
        }
    }
};
})();

/* COOKIES
 ----------------------------------------------------------------------*/
function getCookie(name) {
    var start = document.cookie.indexOf(name + '=');
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) return null;
    var end = document.cookie.indexOf(';', len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}

function setCookie(name, value, expires, path, domain, secure) {
    var today = new Date();
    today.setTime(today.getTime());
    expires = (expires) ? expires * 1000 * 60 * 60 * 24 : 0;
    var expires_date = new Date(today.getTime() + (expires));
    document.cookie = name + '=' + escape(value) +
        ((expires) ? ';expires=' + expires_date.toGMTString() : '') + //expires.toGMTString()
        ((path) ? ';path=' + path : '') +
        ((domain) ? ';domain=' + domain : '') +
        ((secure) ? ';secure' : '');
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + '=' +
            ((path) ? ';path=' + path : '') +
            ((domain) ? ';domain=' + domain : '') + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    }
}


