


function extend(){
    for(var i=1; i<arguments.length; i++)
        for(var key in arguments[i])
            if(arguments[i].hasOwnProperty(key))
                arguments[0][key] = arguments[i][key];
    return arguments[0];
}

function getAllPromoImpressions(commands){
    /* deprecated */
    var imageCollection = $('.promo-image:not(.cloned .promo-image):visible');
    //var imageCollection = $('.promo-image:not(.cloned .promo-image):not(#menu2 .promo-image)'); // No menu images
    
    imageCollection.each(function(index,image){
        commands.push(getPromoData($(image)));
    });
}

function getBaksetProducts(){

    if (typeof universal_variable.basket != "undefined"){
        var product_item = universal_variable.basket.line_items;
        for (var i = 0; i < product_item.length; i++){
            var data = getProductData(product_item[i].product, null, "ec:addProduct", product_item[i].quantity);
            ga.apply(ga, data);
        }
    }

}

function getProductDetails(commands){

    if (typeof universal_variable.listing != undefined){
        var fullListName = getProductListName($());
        var product_item = universal_variable.listing.items;
        for (var i = 0; i < product_item.length; i++){
            var data = getProductData(product_item[i], null, "ec:addProduct");
            commands.push(data);    
        }

        commands.push(['ec:setAction', 'detail', {
            list: fullListName
        }]); 

        try {
            if (commands[1][1] == "detail") {
                delete(commands[1][2].list);
            }
        } catch (e) {
            //console.log(e);
        }
        
        
    }

}

function getAllProductImpressions(commands){
    if (typeof universal_variable.listing != "undefined"){
        var product_item = universal_variable.listing.items;
        for (var i = 0; i < product_item.length; i++){
            var isVisible = false;
            // is visible checks
            if (universal_variable.page.type == "category") {
                isVisible = $('.listing-items .associated-product:eq(' + i + ')').is(':visible');
            }

            if (isVisible) {
                var data = getProductData(product_item[i], "Category: " + universal_variable.listing.query);
                commands.push(data);    
            }
        }
    }

    getCarouselProductImpressions(commands, universal_variable.ctlrecommendation, "Complete the Look", "complete-the-look");
    getCarouselProductImpressions(commands, universal_variable.ymalrecommendation, "You may also Like", "also-like");
    getCarouselProductImpressions(commands, universal_variable.new_in_recommendation, "New In", "new-in");
}

function getCarouselProductImpressions(commands, product_list, list_name, modifier_class){
    if (typeof product_list != "undefined"){
        var product_item = product_list.items;

        for (var i = 0; i < product_item.length; i++){
            var isVisible = $('.associated-carousel--' + modifier_class + ' .associated-product:not(.associated-carousel--' + modifier_class + ' .cloned .associated-product)').is(':visible');

            if (isVisible) {
                var data = getProductData(product_item[i], list_name);
                commands.push(data);    
            }
        }
    }
}

function getProductData(product, list_name, ec_type, quantity){
    var data = {
        // ImpressionFieldObject stores product impression data
        'id': product.product_code,          // Product Code - Type: string
        'name': product.name,        // Product name - Type: string
        'category': product.category + (product.subcategory && product.subcategory != "" ? "/" + product.subcategory : ""),    // Name of the Product category.

        // Use / as a delimiter to specify up to 5 levels of hierarchy (e.g Men/Shirts/T-Shirts) - Type: string
        'brand': product.manufacturer,       // Name of Product brand - Type: string
        //'variant': '[Server Variable]',     // Variant of the product like: color, size etc - Type:string
        'price': '' + product.unit_price_in_site_currency_with_discounts,                //	No	The price of a product (e.g. 29.20).
        'position': product.position,    // Product position in a list. For example 2 - Type: numeric
    };

    if (list_name) {
        data.list = list_name;        // Name of the Product list like 'search results' - Type: string
    }

    try {
        if (data.category == "") {
            delete data['category'];
        }
    }
    catch (e) {
        //console.log(e);
    }
    

    if (quantity) {
        data.quantity = quantity;
    }

    return [ec_type ? ec_type : 'ec:addImpression', data];
}

 var data = {
    "version":	"1.1.1",
"GoogleUA": "UA-3094968-2","conversionID": "985877944","conversionLabel": "rK5vCIDIjgIQuJuN1gM","conversionFormat": "3",    "user": {"language":	"en-gb","Group":   "Control","Server":   "moss-web6","Session":   "did1cwvvzfrwnqpkegdmwkjf",},    "basket":	{
        "id":	"f0e5cb27-22f9-4b69-86a7-e6f347f492dd",
        "currency":	"GBP",
        "subtotal":	0,
        "subtotal_include_tax":	1,
        "voucher":	"",
        "voucher_discount":	0,
        "tax":	0,
        "total":	0,
        "line_items":	[

        ]
    },
    "enhanced_ecommerce": {
        "additional_commands": []
    }
}
universal_variable = extend({} ,data);
dataLayer.push(data);