/*
	File: stubs.js
	Auth: Anthony Decena
	Desc: This is a stub file for Dojo External Interface / FlashApi methods.  This defines the methods called
		 by the dojo JS/AS classes as no-ops.  This may need to be expanded upon depending on
		 the depth of the dojo integration into the flash movies being loaded.

		 I'm using an object map for all of the definition rather than defining dynamically for ease of use
		 and just in case there is a need for per instance manipulation.
*/

if (! ("console" in window) || !("firebug" in console)) {
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", 
				"groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	window.console = {};
	var EmptyFn = function() {};
	for (var i = 0; i <names.length; ++i) window.console[names[i]] = EmptyFn;
}


// Heres our no-op reference.  The arguments will contain the name of the
// calling method as well as the data passed in just for reference.
var __noop__ = function() {
	// This debug line is only for info purposes during development.
	console.log("noop: ", arguments);
   return 0;
};

var dojox = {
   flash: {
       comm: {
           _addExternalInterfaceCallback: __noop__,
			_encodeData: __noop__,
			_decodeData: __noop__,
			_execFlash: __noop__
       },
   	loaded: __noop__
	}
};

var 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);
                console.log('cps:' + catProdSku);
				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('cat:' + cat);
				console.log('prod:' + prod);
				if (typeof sku != 'undefined') {
				    console.log('sku:' + sku);
				    var href = '/templates/product/flash_product.tmpl?catId=' + cat + '&prodId=' + prod + '&skuId=' + sku;
				} else {
				    var href = '/templates/product/flash_product.tmpl?catId=' + cat + '&prodId=' + prod;
				}
				console.log('href:' + href);
				loadFlashProduct(href);
				var resp = generic.flash.ApiResponse;
				resp.success = true;
				resp.results = catProdSku;
				return resp;
			},
			
			alterCart: function(obj) {
			    if (typeof obj == 'string') {
			        var catProdSku = obj;
			    } else {
			        var catProdSku = obj[0].actions[0].path;
			    }
			    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"));
                }
                if (catProdSku.indexOf("CAT") > -1) {
                    //pop the cat off the end
                    var cat=catProdSku.slice(catProdSku.indexOf("CAT"));
                }
				console.log('cat: ' + cat);
				console.log('prod: ' + prod);
				console.log('sku: ' + sku);
				
				var href = '/templates/transaction/minibag_flash.tmpl?catId=' + cat + '&prodId=' + prod + '&skuId=' + sku;
				addToCartFlash(href);
				var resp = generic.flash.ApiResponse;
				resp.success = true;
				resp.results = catProdSku;
				return resp;
			},
			
			gotourl: function(arg) {
			    console.log('redirecting to: ' + arg);
			    location.href = arg;
				var resp = generic.flash.ApiResponse;
				resp.success = true;
				resp.results = arg;
				return resp;
			},

			echo: function(msg) {
				console.log(msg);
				var resp = generic.flash.ApiResponse;
				resp.success = true;
				resp.results = msg;
				return resp;
			}
		},
		ApiResponse: {
			success: false,
			request: null,
			results: null
		}
	}
};

var quick_view = {
    render: function(qvcat, qvprod) {
        // when developing this, calls from flash were 
        // being made with the product id argument
        // in the form "PRODXXXX" as opposed to XXXX
        if (typeof qvprod === "string") { 
            qvprod = parseInt(qvprod.replace("PROD", ""));
        }
        
        // does data exist in page already?
        var data_exists = $jq("#quickview_content_PROD" + qvprod).length;
        
        var triggerPopup = function(prodid) {
            var id = "quickview_PROD" + qvprod;
            var btn;
            
            if ($jq("#"+id)[0]) {
                btn = $jq("#"+id)[0];
            } else {
                btn= $jq('<div id="' + id + '" style="display:none" class="quickview_button"></div>')[0];
                $jq("#main_container").append(btn);
            }
            
            $jq("#" + id).largebox({
                name: 'quickview_ondemand',
                width: 765,
                top: 75,
                type: 'div',
                height: 485,
                stripAt: 'no_strip',
                callback: 'handle_tabs',
                dynamicAction: true
            });
            
            $jq("#" + id).click();
        }
        
        var largebox_div_id = "quickview_PROD" + qvprod + "_largebox_div";
        
        // if it's not already on the page, put it there
        if (!$jq("#" + largebox_div_id).length) {
            $jq("#main_container")
                .append($jq('<div id="' + largebox_div_id + '" style="display:none"></div>')
                .html("quickview_content_PROD" + qvprod)[0]);
        }
        
        if (data_exists) {
            triggerPopup(qvprod);
        } else {
            var cb = function(returnedMsg) {
                default_quickview_cb(returnedMsg);
                triggerPopup(qvprod);
            };
            
            make_request({
                category_id: qvcat,
                page_id: "all",
                callback: cb
            });
        }
    }
};

