/**
 *******************************************
 * Author: Greg Brown [greg@12dnetworks.com]
 * 
 * Description:
 *  Manages the creation of flash objects
 *  on the page that communicate with a 
 *  webservice to get their data.
 *
 *  This code primarily creates the flash
 *  objects on the page and ensures they 
 *  have the correct variables upon init.
 * 
 *******************************************
 */

/**
 * The _onFocus global namespace
 * @constructor
 */
var _onFocus = window._onFocus || {};

_onFocus.MediaManager = Class.create();
_onFocus.MediaManager.prototype = {

    initialize:function(showId, imagePath, mediaPath, scriptPath)
    {
        this.showId = showId;
        this.imagePath = imagePath;
        this.mediaPath = mediaPath;
        this.scriptPath = scriptPath;
    },
    
    /*
     *  Get a query string variable from the page
     */
    getQueryVariable:function(varName) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if (pair[0] == varName) {
                return pair[1];
            }
        } 
        return "";
    },

    /*
     *  Renders a flash file with a call of topX
     */
    getTopX:function(element, swf, width, height, count) {
        var flashVars = "&callType=topx&count=" + count;
        this.renderFlash(element, swf, flashVars, width, height);
    },
    
    /*
     *  Renders a flash file with a call of recentX
     */
    getRecentX:function(element, swf, width, height, count) {
        var flashVars = "&callType=recentx&count=" + count;
        this.renderFlash(element, swf, flashVars, width, height);
    },
    
    /*
     *  Renders a flash file with a call of search
     */
    getSearch:function(element, swf, width, height, keywords, count) {
        var flashVars = "&callType=search&keywords=" + escape(keywords) + "&count=" + count;
        this.renderFlash(element, swf, flashVars, width, height);
    },
    
    /*
     *  Gets a media item by its' id.
     */
    getMedia:function(mediaId) {
        var srcParams = "mediaID=" + escape(mediaId) + "&returnFormat=js";
        this.renderScript(srcParams);
    },
    
    /*
     *  Gets a media item by pulling a var from the querystring
     */
    getMediaFromUrl:function(varName) {
        var srcParams = "mediaID=" + escape(this.getQueryVariable(varName)) + "&returnFormat=js";
        this.renderScript(srcParams);
    },
    
    /*
     *  Writes a script tag to the window
     */
    renderScript:function(srcParams) {
        document.write('<script type="text/javascript" src="' + this.scriptPath + 'view.cfm?' + srcParams +'"></script>');
    },

    /*
     *  Creates a flash object on the screen into a div
     */
    renderFlash:function(element, swf, flashVars, width, height) {
        // keep browser from caching the script tag by adding the time in the querystring
        var noCache =  "&noCache=" + (new Date()).getTime();
        var flashVars = flashVars + "&showID=" + this.showId + "&imagePath=" + escape(this.imagePath) + "&mediaPath=" + escape(this.mediaPath) + "&scriptPath=" + escape(this.scriptPath);

        var FO = {
            movie: swf + "?&noCache=" + noCache,
            width: width,
            height: height,
            flashvars: flashVars,
            majorversion: "6",
            build:"0"
        };
        UFO.create(FO, element);
    }
};