// requires extern/ufo.js

__genericflash_done = { };
var GenericFlash = GenericBehavior.create('GenericFlash', {
    defaults: {
       majorversion:"8",
       build:"0",
       wmode:'opaque',
       scriptable: true,
       akamaize: true,
       xi: "false",
       ximovie:"/flash/ufo.swf",
       name:"flashmovie",
       swliveconnect: "true",
       allowscriptaccess:"always",
       bgcolor: "#ffffff"
    },
    
    framed: !(window.parent == window.self), // is this scrip running within a frame
    frame_tmpl: '/flash/iframe.tmpl',
    
    onDraw: function(event) {
        if (__genericflash_done[this.e.id]) { return; }
        __genericflash_done[this.e.id] = 1;
        
        if (GenericUtil.urlparam('noflash')) {
            this.e.style.visibility = 'visible';
            return;
        };
        
        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') {
            Object.applyDefaults(ufoargs.flashvars, {
                elcDom: GenericUtil.akamaiBase()+'/',
                moviePath: GenericUtil.urlpath(ufoargs.movie),
                siteDom: GenericUtil.siteBase(),
                cartBase: '/altercart/templates/transaction/viewbag.tmpl',
                section: GenericUtil.urlparam('section')
            });
            $H(ufoargs.flashvars).keys().each(function(key) {
                ufoargs.flashvars[key] = encodeURI(ufoargs.flashvars[key]);
            });
            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);
                this.e.style.visibility = 'visible';
            }
            else {
                if (ufoargs.akamaize) { 
                    ufoargs.movie = GenericUtil.akamaize(ufoargs.movie);
                }
                delete ufoargs.scriptable;
                delete ufoargs.akamaize;
                UFO.create(ufoargs, this.e.id);
            }
        }
        else {
            delete ufoargs.scriptable;
            delete ufoargs.akamaize;            
            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()) {
            var myCurrentModule = $H(_currentModule);
            for (var param in myCurrentModule) {
                if (!myCurrentModule[param]) delete myCurrentModule[param];
            }
            iframe_params._currentModule = myCurrentModule.toQueryString();
        }
        var action = GenericUtil.akamaize( this.frame_tmpl );
        var attrs = {
            src: '/flash/blank.html',
            width: ufoargs.width,
            height: ufoargs.height,
            name: '_iframe_'+ufoargs.id,
            scrolling: 'no',
            frameborder: 0
        };
        
        var form_id = '_form'+attrs.name;
        var params_html = '';
        $H(iframe_params).each(function(set) {
            params_html += '<input type="hidden"'
                + ' name="'+set.key+'"'
                + ' value="'+set.value+'" \>';
        });
        
        var attrs_html = '';
        $H(attrs).each(function(set) {
           attrs_html += set.key + '="' + set.value + '" ';
        });
        
        var iframe_html = '<iframe '+ attrs_html +' ></iframe>';
        
        var inner_html =
            '<form action="'+action+'" target="'+attrs.name+'" id="'+form_id+'" method="post" >'
            + params_html + iframe_html
            + '</form>';
            
        if (!GenericUtil.syndicated()) {
            document.domain = GenericUtil.rawHost();
        }
        this.e.innerHTML = inner_html;
        
        whenel(form_id, function(elem) { elem.submit(); } );
    }
    
    
});

// 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.indexOf('://') == -1) {
        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 = el('GenericFlash_download');
    dl.src = url;
    return;
};
GenericFlash.closewindow = function(){
    this.framed ? parent.window.close() : window.close();
};

// 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;
}

