/**
 * Collection of methods re. RealMedia ads
 * $Id: realmedia.js,v 1.6 2011/04/01 15:52:16 kmj Exp $
 */
/*jslint browser: true */
/*extern $, OAS_RICH */

var Realmedia = {

    /**
     * The config
     */
    config: null,
    
    /**
     * Sets the config
     * @param {Object} config
     */
    setConfig: function(config){
        Realmedia.config = config;
    },
    
    /**
     * Gets the config
     */
    getConfig: function(){
        return Realmedia.config;
    },
    
    /**
     * Initialises stuff
     */
    init: function(config){
        Realmedia.setConfig(config);
        Realmedia.OAS_SCRIPT();
    },
    
    /**
     * Renders a 'normal' (i.e. non-rich media) ad
     * @param {String} pos The ad position
     */
    OAS_NORMAL: function(pos){
        var settings = this.getConfig();
        var linkUrl = Realmedia.OAS_URL('click_nx.ads/', pos);
        var imgUrl = Realmedia.OAS_URL('adstream_nx.ads/', pos);
        document.write('<a href="' + linkUrl + '" target="_top">');
        document.write('<img src="' + imgUrl + '" border="0" alt="[Advertisement]"></a>');
    },
    
    /**
     * Creates a script element to include the OAS script from their server
     */
    OAS_SCRIPT: function(){
        var s = document.createElement('script');
        s.setAttribute('type', 'text/javascript');
        s.setAttribute('language', 'JavaScript1.1');
        s.setAttribute('src', Realmedia.OAS_URL('adstream_mjx.ads/'));
		// IE doesn't get the script in time unless you use document.write
        if ($.browser.msie) {
			document.write(s.outerHTML);
		}
		// Every other browser can be DOManipulated
		else {
			document.getElementsByTagName('head')[0].appendChild(s);
		}
    },
    
    /**
     * Builds a full OAS URL, for scripts, links or images
     *
     * @param {String} baseUri
     * @param {String} pos
     */
    OAS_URL: function(baseUri, pos){
        var settings = Realmedia.getConfig();
        var oasUrl = settings.url + baseUri + settings.sitepage + '/1' + settings.rns + '@' + settings.listpos;
        if (pos !== undefined) {
            oasUrl += '!' + pos;
        }
        oasUrl += settings.query;
        return oasUrl;
    },
    
    /**
     * Wrapper function around either OAS_NORMAL (local) or OAS_RICH
     * (in the included file from 247RM)
     *
     * @param {String} pos
     */
    OAS_AD: function(pos){
        if (typeof(OAS_RICH) !== 'undefined') {
            OAS_RICH(pos);
        }
        else {
            Realmedia.OAS_NORMAL(pos);
        }
    }
    
};

// Backwards-compatibility with old-style function call (for MPUs)
var OAS_AD = Realmedia.OAS_AD;

