if(typeof NAP == 'undefined') {
    NAP = {};
}

(function() {
    var successCallback;
    var errorCallback;

    function getSiteContext() {
        var site = document.location.hostname;

        if(site.indexOf('mrporter.com')) {
            return 'mrp';
        } else if(site.indexOf('theoutnet.com')) {
            return 'out';
        } else {
            return 'nap';
        }
    }

    function dropShoppingBagCookie(cookieData) {
        var today = new Date();

        if(cookieData.value) {
            var cookieExpires = new Date(today.setTime(today.getTime()+(cookieData.maxAge * 1000))).toGMTString();
            var cookieString = cookieData.name + "=" + cookieData.value + "; expires=" + cookieExpires + "; path=" + cookieData.path;
            document.cookie = cookieString;
        }
    }

    function successCallbackDelegate(data) {
        if(data.cookie) dropShoppingBagCookie(data.cookie);
        if(typeof successCallback === 'function') successCallback(data);
    }

    function errorCallbackDelegate(errorData) {
        if(typeof errorCallback === 'function') errorCallback(errorData);
    }

    NAP.basketDelegate = {
        addSkuToBasket: function(sku, method, opts) {
            successCallback = opts.callback || null;
            errorCallback = opts.handleError || null;
			asynchronous = (opts.async !== null) ? opts.async : true;

            if(method === 'API') {
				$.ajax({
					url: '/'+channel+'/api/basket/addsku/'+sku+'.json',
					async: asynchronous,
					success: successCallbackDelegate,					
					dataType: 'json'
				});
            } else {
                // default to DWR method delegate
                BasketService.addSkuToBasket(sku, {
                    timeout: 5000,
                    callback: successCallbackDelegate,
                    errorHandler: errorCallbackDelegate
                });
            }
        },

        addSkusToBasket: function(skus, method, opts) {
            successCallback = opts.callback || null;
            errorCallback = opts.handleError || null;

            if(method === 'API') {
                // NAP API method delegate goes here
            } else {
                // default to DWR method delegate
                BasketService.addSkusToBasket(skus, {
                    timeout: 5000,
                    callback: successCallbackDelegate,
                    errorHandler: errorCallbackDelegate
                });
            }
        },

        //PL 05/04/11 : method only for MRP to add items to basket from reserved list
        addReservedProductToBasket: function(sku, qty, method, opts) {
            successCallback = opts.callback || null;
            errorCallback = opts.handleError || null;

            if(method === 'API') {
                // NAP API method delegate goes here
            } else {

                    var channel = NAP.getChannel();

                    if(sku.length >1){
                       $.post('/'+channel+'/customerreservations.mrp?selectedItemsOnlyJs',$("#special-orders-form").serialize(), function(data) {
                            successCallback(data);
                        }, "json");
                    } else{
                        $.getJSON('/'+channel+'/customerreservations.mrp?action=add_js&sku='+ sku, function(data) {
                            successCallback(data);
                        });
                    }

            }

        },

        /*
        //PL 05/04/11 : method only for MRP to add items to basket from reserved list
        addReservedProductToBasket: function(sku, qty, method, opts) {
            successCallback = opts.callback || null;
            errorCallback = opts.handleError || null;

            if(method === 'API') {
                // NAP API method delegate goes here
            } else {
                // default to DWR method delegate
                BasketService.addReservedProductToBasket(sku, qty, {
                    timeout: 5000,
                    callback: successCallbackDelegate,
                    errorHandler: errorCallbackDelegate
                });
            }
        },
        */
        removeSkuFromBasket: function(sku, method, opts) {
            // not yet implemented
        },

        removeSkusFromBasket: function(skus, method, opts) {
            // not yet implemented
        }
    };

    
})();


