// requires extern/ufo.js

var GenericFlash = GenericBehavior.create('GenericFlash', {
    defaults: {
       majorversion:"7",
       build:"0",
       wmode:'opaque',
       scriptable: true,
       akamaize: true,
       xi: "true",
       ximovie:"/flash/ufo.swf",
       name:"flashmovie",
       swliveconnect: "true",
       allowscriptaccess:"always"
    },
        
    framed: !(window.parent == window.self), // is this scrip running within a frame
    frame_tmpl: '/flash/iframe.tmpl',
    
    onDraw: function(event) {
        var ufoargs = new Object();
        Object.applyDefaults(ufoargs, this.args);
        Object.applyDefaults(ufoargs, this.defaults);
        if (ufoargs.flashvars == null) ufoargs.flashvars = new Object();
        if (typeof ufoargs.flashvars == 'object') {
            if (ufoargs.flashvars.elcDom == null) 
                ufoargs.flashvars.elcDom = GenericUtil.akamaiBase()+'/';
            if (ufoargs.flashvars.moviePath == null)
                ufoargs.flashvars.moviePath = GenericUtil.urlpath(ufoargs.movie);
            ufoargs.flashvars = $H(ufoargs.flashvars).toQueryString();
        }
        
        UFO.getFlashVersion();
        if (UFO.hasFlashVersion(ufoargs.majorversion, ufoargs.build)) {
            if (ufoargs.scriptable && ufoargs.akamaize && !this.framed) {
                this._iframe(ufoargs);
            }
            else {
                UFO.create(ufoargs, this.e.id);
            }
        }
        else {
            UFO.create(ufoargs, this.e.id); 
        }
        return true;
    },
    
    _iframe: function(ufoargs) {
        var iframe_params = {
            width: ufoargs.width,
            height: ufoargs.height,
            movie: ufoargs.movie,
            flashvars: ufoargs.flashvars,
            wmode: ufoargs.wmode,
            bgcolor: ufoargs.bgcolor,
            ngextredir: 1
        };
        if (GenericUtil.syndicated()) {
            iframe_params.wsmlTmpl = wsmlMakeWebServiceHref('/[[replaceme]]');
        }
        var attrs = {
            src: GenericUtil.akamaize( this.frame_tmpl +'?'+ $H(iframe_params).toQueryString() ),
            width: ufoargs.width,
            height: ufoargs.height,
            scrolling: 'no',
            frameborder: 0
        };

        var attrs_html = '';
        $H(attrs).each(function(set) {
           attrs_html += set.key + '="' + set.value + '" ';
        });
        
        var iframe_html = '<iframe '+ attrs_html +' ></iframe>';
        
        if (!GenericUtil.syndicated()) {
            document.domain = GenericUtil.rawHost();
        }
        this.e.innerHTML = iframe_html;
    }
    
    
});

// Due to some browser bug, functions are not callable from flash unless defined using an assignment
GenericFlash.framed = !(window.parent == window.self);
GenericFlash.gotourl = function(url) {
    if (typeof window.wsmlMakeWebServiceHref == 'function') { 
        url = wsmlMakeWebServiceHref(url); }
    if (!url.match(/^(.+\/\/:)/)) {
        var thehost = parent.location.host || GenericUtil.wwwHost();
        url = '//' + thehost + url;
    }
    this.framed ?
        parent.location.href = url:
        location.href = url;
    return;
};
GenericFlash.gotoanchor = function(anchor) {
    this.framed ?
        parent.location.hash = anchor:
        location.hash = anchor;
    return;
};
GenericFlash.popup = GenericPage.popup;
GenericFlash.downloadurl =  function(url) {
    var url = GenericUtil.akamaize(url);
    var dl = parent.el('GenericFlash_download');
    dl.src = url;
    return;
};
if (!GenericFlash.framed) { document.write("<iframe src='#' id='GenericFlash_download' width='0' height='0' frameborder='0' style='position: absolute; top: 0px; left: 0px;' ></iframe>"); }

// Make coremetrics tags available via GenericFlash
GenericFlash.cmCreatePageviewTag = function(pageID, searchString, categoryID, searchResults) {
    if (GenericUtil.syndicated()) return;
    window.parent.cmCreatePageviewTag(pageID, searchString, categoryID, searchResults);
    return;
};
GenericFlash.cmCreateManualLinkClickTag = function(href,name,pageID) {
    if (GenericUtil.syndicated()) return;
    window.parent.cmCreateManualLinkClickTag(href,name,pageID);
    return;
}; 
GenericFlash.cmCreateProductviewTag = function(productID, productName, categoryID) {
    if (GenericUtil.syndicated()) return;
    window.parent.cmCreateProductviewTag(productID, productName, categoryID);
    return;
}

