var generic = generic || {};
generic.flash = {
    abort: false,
    so : swfobject,
    defaults : {
        xist : {
            file: "/flash/expressInstall.swf",
            url: "http://www.adobe.com/go/getflashplayer",
            image: "http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif"
        },
        attr : { 
            playerversion: "9.0.28",
            width: "100%",
            height: "100%", 
            hspace: 0,
            vspace: 0,
            align: "top"
        },
        params : {
            wmode: "transparent", 
            quality: "high",
            menu: "true",
            swliveconnect: "true",
            allowscriptaccess: "always", 
            scale: "noScale",
            allowfullscreen: "true"
        } 
    },
    
    embed: function (attr, params, placeholderId) { 
        if (generic.flash.abort) return;
        
        if (!$(placeholderId)) { 
            console.log("generic.flash.embed: Element doesnt exist"); 
            return; 
        }    
                    
        //params = generic.mixin(generic.flash.defaults.params, params); // old ref
        params = Object.extend(generic.flash.defaults.params, params);
        //attributes = generic.mixin(generic.flash.defaults.attr, attr); // old ref
        var attributes = Object.extend(generic.flash.defaults.attr, attr);
        if (typeof params.flashvars != "string") { 
            params.flashvars = Object.toQueryString(params.flashvars); 
        }  
     
        // version check
        if (generic.flash.so.hasFlashPlayerVersion(attributes.playerversion)) {  
            generic.flash.so.addDomLoadEvent(function() { 
                generic.flash.so.createSWF(attributes,params,placeholderId);
            }); 
            return;
        }
         
        //content if flash doesn't embed
        var altid = attributes.altcontentid; 
        var defaultalt = $(placeholderId).select(".noflash"); 
        var altcontent = altid ? ($(altid) ? $(altid) : false) : (!!defaultalt.length ? defaultalt[0] : false);  
        if (altcontent) { // if hidden alternate content
            altcontent.style.visibility = "visible";
            altcontent.style.display = "block";
        }     
    },

    /**
     * @namespace favorites contains favorites-related methods that are called by Action Script.
     * @memberOf generic.flash
     */
     favorites: {
        /* Used by Action Script to add items to a collection. Response data
         * is returned via a callback function.
         * @param args {object}
         * @param args.movieName {String} the value of the embed/object tag's name attribute
         * @param args.callback {String} the name that the container (the browser) uses to access the callback Function
         * @param args.skuBaseId {String} the value of the SKU_BASE_ID field for the SKU that is to be added
         * @methodOf generic.flash.collections
         */
        add: function (args) {
             var options = Object.extend( {
                 movieName : "",
                 callback: "",
                 skuBaseId: ""
             }, args);

             if ( options.skuBaseId.length < 1 ) {
                 return null;
             }
             if ( !generic || !generic.account || !generic.account.collection ) {
                 return null;
             }
             var collectionsObj = generic.account.collection;           
             var callbackFn = function(options, responseObj) {
                 // console.log(responseObj.getMessages());
                 if ( options.movieName.length > 1 &&
                         document[options.movieName] &&
                         document[options.movieName][options.callback] &&
                         typeof document[options.movieName][options.callback] === "function" ) {
                     if (responseObj.getMessages() || responseObj.getError()) {
                         document[options.movieName][options.callback](responseObj.getMessages());                        
                     }
                 }
             }.curry(options);
             collectionsObj.update({
                 skuBaseId: options.skuBaseId,
                 action: "add",
                 onSuccess: callbackFn,
                 onFailure: callbackFn
             });
         }
     },

    /**
     * @namespace cart contains cart-related methods that are called by Action Script.
     * @memberOf generic.flash
     */
    cart: {
        /* Used by Action Script to add items to user's shopping cart. Response data
         * is returned via a callback function.
         * @param args {object}
         * @param args.movieName {String} the value of the embed/object tag's name attribute
         * @param args.callback {String} the name that the container (the browser) uses to access the callback Function
         * @param args.skus {Array} the value of the SKU_BASE_ID field for each SKU that is to be added
         * @param args.quantity: {Number} the quantity of items that will be added to the cart.             
         * @methodOf generic.flash.cart
         */
        add: function (args) {
            var options = Object.extend( {
                movieName : "",
                callback: "",
                skus: [],
                quantity: 1
            }, args);
            if ( options.skus.length < 1 ) {
                return null;
            }
            if ( !generic || !generic.checkout || !generic.checkout.cart ) {
                return null;
            }
            var cartObj = generic.checkout.cart;
            var callbackFn = function(options, responseObj) {
                if ( options.movieName.length > 1 &&
                        document[options.movieName] &&
                        document[options.movieName][options.callback] &&
                        typeof document[options.movieName][options.callback] === "function" ) {
                    if (responseObj.getData()) {
                        document[options.movieName][options.callback](responseObj.getData().ac_results);                        
                    } else if (responseObj.getError()) {
                        document[options.movieName][options.callback](responseObj.getMessages());
                    }
                }
            }.curry(options);
            cartObj.updateCart({
                params: {
                    skus: options.skus,            
                    INCREMENT: 1
                },
                onSuccess: callbackFn,
                onFailure: callbackFn
            });         
        }
    },
	/**
     * @namespace Api grabbed from EL France - handles 'products used' section via cue point for How To videos.
     * @memberOf generic.flash
     */
	Api : {
	   jsCall: function(method, args) {
		   console.log("generic.flash.Api.jsCall: ", method, " args:", args);

			// This method can still be used as a low-level api for javascript /flash
			// communication.  All methods called from flash are routed to this endpoint.
			// To use as an api, add your api methods to the object below to execute any
			// needed functionality and uncomment the dispatch code below.

			if (generic.flash.ApiMethods[method]) {
				var resp = generic.flash.ApiMethods[method](args);
				return resp;
			} else {
				console.log(method + ": No such method exists");
				return { success: false, results: "No such method exists" };
			}
		}
	},
	
	ApiMethods: {
		// You can restrict access to available methods by adding them here and
		// using the jsCall above as an api endpoint.

		cuePoint: function(arg) {
			var catProdSku = String(arg);
			if (catProdSku.indexOf("SKU") > -1) {
				//pop the sku off the end
				var sku=catProdSku.slice(catProdSku.indexOf("SKU"));
				catProdSku = catProdSku.slice(0,catProdSku.indexOf("SKU"));
			}
			if (catProdSku.indexOf("PROD") > -1) {
				//pop the prod off the end
				var prod=catProdSku.slice(catProdSku.indexOf("PROD"));
				catProdSku = catProdSku.slice(0,catProdSku.indexOf("PROD"));
			}
			//unlikely to have a sku provided here...in most cases we will use the default sku
			if (catProdSku.indexOf("CAT") > -1) {
				//pop the cat off the end
				var cat=catProdSku.slice(catProdSku.indexOf("CAT"));
			}
			
			console.log('cps:' + String(arg) + 'cat:' + cat + 'prod:' + prod + 'sku:' + sku);
			
			if (typeof sku != 'undefined') {
				console.log('sku:' + sku);
				var href = '/templates/products/flash_product.tmpl?category_id=' + cat + '&product_id=' + prod + '&skuId=' + sku;
			} else {
				var href = '/templates/products/flash_product.tmpl?category_id=' + cat + '&product_id=' + prod;
			}
			
			console.log('href:' + href);
			loadFlashProduct(href);
                        var resp = generic.flash.ApiResponse;
                        resp.success = true;
                        resp.results = catProdSku;
                        return resp;
		}
	},
        ApiResponse: {
                success: false,
                request: null,
                results: null
        }
}
