
var WinkController = {
    _cache: {},
    _current: null,
    _delay: 500,
    _find: function(movie) {
        var wink = WinkController._cache[movie] || WinkController._current;
        if (!wink) {
            wink = WinkController._cache[movie] = window.document[movie];
        }
        WinkController._current = wink;
        return wink;
    },
    /*
    ** Not sure if this stuff is needed....
    onLoad: function(movie) {
        // called by the flash shell
        var wink = WinkController._find(movie);
        var loaders = wink.getAttribute("load_funcs") || [];
        for (var i = 0; i < loaders.length; i++) {
            var func = loaders[i];
            func();
        }
    },
    attachOnLoad: function(movie, func) {
        var wink = WinkController._find(movie);
        var loaders = wink.getAttribute("load_funcs");
        if(!loaders) {
            loaders = new Array();
            wink.setAttribute("load_funcs", loaders);
        }
    },
    */
    setVariable: function(movie, key, value) {
        var wink = this._find(movie);
        wink.SetVariable('/:' + key, value);
    },
    setImghost: function(movie, imghost) {
        WinkController.setVariable(movie, 'imghost', imghost);
    },
    attachEnd: function(movie, func) {
        var wink = WinkController._find(movie);
        wink["end_func"] = func;
    },
    attachPreview: function(movie, func) {
        var wink = WinkController._find(movie);
        wink["preview_func"] = func;
    },
    setText: function(movie, text) {
        var wink = WinkController._find(movie);
        wink["wtext"] = text;
    },
    getText: function(movie) {
        var wink = WinkController._find(movie);
        return wink.GetVariable("/:winkText");
    },
    getMaxTextLength: function(movie) {
        var wink = WinkController._find(movie);
        var value = wink.GetVariable("/:winkTextLength");
        if (value) {
            value = parseInt(value);
        }
        return value;
    },
    setPhoto: function(movie, photourl, width, height) {
        var wink = WinkController._find(movie);
        wink["wphoto"] = photourl;
        if (width) {
            wink["wphotow"] = width;
        }
        if (height) {
            wink["wphotoh"] = height;
        }
    },
    setMatte: function(movie, matte) {
        var wink = WinkController._find(movie);
        wink["wmatte"] = matte;
    },
    play: function(movie, product, width, height) {
        var wink = WinkController._find(movie);
        if (product) {
            //alert("setting product")
            WinkController.setVariable(movie, "winkProdnum", product);
        }
        if (width) {
            WinkController.setVariable(movie, "winkWidth", width);
        }
        if (height) {
            WinkController.setVariable(movie, "winkHeight", height);
        }
        if (wink["wtext"]) {
            WinkController.setVariable(movie, "winkText", wink["wtext"]);
        }
        if (wink["wphoto"]) {
            WinkController.setVariable(movie, "winkPhoto", wink["wphoto"]);
        }
        if (wink["wphotow"]) {
            WinkController.setVariable(movie, "winkPhotoWidth", wink["wphotow"]);
        }
        if (wink["wphotoh"]) {
            WinkController.setVariable(movie, "winkPhotoHeight", wink["wphotoh"]);
        }
        if (wink["wmatte"]) {
            WinkController.setVariable(movie, "winkMatte", wink["wmatte"]);
        }
        wink.GotoFrame(2);
        wink.Play();
    },
    preview: function(movie, product, width, height, endfunc) {
        var wink = WinkController._find(movie);
        if (endfunc) {
            WinkController.attachEnd(movie, endfunc);
        }
        if (wink["preview_func"]) {
            wink["preview_func"](wink);
        }
        if (WinkController._delay) {
            setTimeout(function() {
                WinkController.play(movie, product, width, height);
            }, WinkController._delay);
        } else {
            WinkController.play(movie, product, width, height);
        }
    },
    stop: function(movie) {
        var wink = WinkController._find(movie);
        wink.GotoFrame(3);
        wink.Play();
    },
    end: function(movie) {
        var wink = WinkController._find(movie);
        if (wink["end_func"]) {
            wink["end_func"](wink);
        }
    }
};



if (typeof(agi) == "undefined") { agi = {}; }
if (typeof(agi.product) == "undefined") { agi.product = {}; }
if (typeof(agi.product.WinkManager) == "undefined") {

agi.product.WinkManager = {
    _cache: {},
    _current: null,
    _pre_play_func: null,
    _post_play_func: null,
    _find: function(movie, fallback_to_current) {
        if (typeof(fallback_to_current) == "undefined") {
            fallback_to_current = true;
        }
        var wink = WinkManager._cache[movie];
        if ((!wink) && fallback_to_current) {
            wink = WinkManager._current;
        }
        if (!wink) {
            wink = WinkManager._cache[movie] = window.document[movie];
        }
        WinkManager._current = wink;
        return wink;
    },
    register_wink: function(movie, fo) {
		// i'm forcing the scaling to default
		fo.params.scale = 'default';
        WinkManager._cache[movie] = WinkManager._current = fo;
        var funcname = fo.getAttribute("id") + "_DoFSCommand";
        window[funcname] = function(command, args) {
            if (command == "quit") {
                var el = document.getElementById(fo.getAttribute("id"));
                if (el && navigator.userAgent.toLowerCase().indexOf("msie") > 0) {
                    el = el.parentNode;
                    if (el) { el.innerHTML = '<div style="display: none;"> </div>'; }
                }
                if (WinkManager._post_play_func) {
                    WinkManager._post_play_func();
                }
            }
        };
        if (navigator.userAgent.toLowerCase().indexOf("msie") >= 0) {
            var script = '<script type="text/javascript" ';
            script += 'event="FSCommand(command,args)" ';
            script += 'for="' + fo.getAttribute("id") + '">\n';
            script += funcname + '(command,args);\n';
            script += '</script>';
            document.write(script);
        }
    },
    register_functions: function(pre, post) {
        WinkManager._pre_play_func = pre;
        WinkManager._post_play_func = post;
    },
    preview: function(movie) {
        if (WinkManager._pre_play_func) {
            WinkManager._pre_play_func();
        }
        var fo = WinkManager._find(movie);
        fo.write(fo.getAttribute("id") + "_div");
    },
    _set_flash_variable: function(movie, name, value) {
        var wink = WinkManager._find(movie, false);
        if (wink) {
            wink.addVariable(name, value);
        }
    },
    set_text: function(movie, text) {
        WinkManager._set_flash_variable(movie, "PERSONALIZATION", text);
    }
};
}

var WinkManager = agi.product.WinkManager;

