// requires extern/ufo.js

__genericflash_done = { };
var GenericFlash = GenericBehavior.create('GenericFlash', {
    defaults: {
        majorversion:"8",
        build:"0",
        wmode:'opaque',
        scriptable: true,
        akamaize: true,
        xi: "true",
        ximovie:"/flash/ufo.swf",
        name:"flashmovie",
        id:"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/session/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();
        }
        /*
        if (!ufoargs.id) {
            var objid = this.e.id;
            objid = objid.replace(/[-]/,'_');
            ufoargs.id = objid+'_flashobj';
            ufoargs.id = 'flashmovie';
        }
        */
                
        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);
            iframe_params._currentModule = myCurrentModule.toQueryString();
        }
        var action = GenericUtil.akamaize( this.frame_tmpl );
        var attrs = {
            src: GenericUtil.akamaize( '/flash/blank.html' ),
            width: ufoargs.width,
            height: ufoargs.height,
            name: '_iframe_'+ufoargs.id,
            scrolling: 'no',
            frameborder: 0,
            ALLOWTRANSPARENCY: 'true'
        };
        if (GenericUtil.syndicated()) {
            attrs.src = wsmlMakeResourceUrl(attrs.src);
            action = wsmlMakeResourceUrl(action);
        }
        
        try {
            document.domain = GenericUtil.rawHost();
        } catch(e) { }
        
        var queryparams = $H(iframe_params).toQueryString();
        var ismsie = ( navigator.userAgent.indexOf('MSIE') != -1 );
        if (ismsie && (queryparams.length > 2000)) {
            // have to use a form submit because of ie
            // this might break coremetrics,
            // but should only be needed in syndication          
            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>';
                
            this.e.innerHTML = inner_html;
            
            whenel(form_id, function(elem) { 
                elem.submit(); 
            });
        
        }
        else {
            // can use a get url string
            
            attrs.src = action+'?'+queryparams;
            
            var attrs_html = '';
            $H(attrs).each(function(set) {
               attrs_html += set.key + '="' + set.value + '" ';
            });
            
            var iframe_html = '<iframe '+ attrs_html +' ></iframe>';
            
            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.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 = function(url, args) {
    GenericPage.popup(url, args);
}
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;
    parent.cmCreatePageviewTag(pageID, searchString, categoryID, searchResults);
    return;
};
GenericFlash.cmCreateManualLinkClickTag = function(href,name,pageID) {
    if (GenericUtil.syndicated()) return;
    parent.cmCreateManualLinkClickTag(href,name,pageID);
    return;
}; 
GenericFlash.cmCreateProductviewTag = function(productID, productName, categoryID) {
    if (GenericUtil.syndicated()) return; 
    parent.cmCreateProductviewTag(productID, productName, categoryID);
    return;
}
GenericFlash.cmCreateConversionEventTag = function (eventID, actionType, categoryID, points) {
    if (GenericUtil.syndicated()) return; 
    parent.cmCreateConversionEventTag(eventID, actionType, categoryID, points);
    return;
}

