var brand = $('.filterbrand-value').val();
var country_code = $('.filtercountry_code-value').val();
var language_code = $('.filterlanguage_code-value').val();
var site_url = $('.filtersite_url-value').val();

var onlineShop = $('.onlineShopEnabled-value').val();
var myDulux = $('.myDuluxEnabled-value').val();
var basket_items = new Array();

var duluxLoaded = false;
// var duluxLoading = false;
var basketLoaded = false;
// var basketLoading = false;	

(function($) {
    //Resize image on ready or resize
    $.fn.supersize = function() {
        //Invoke the resizenow() function on document ready
        $(document).ready(function() {
            $('#supersize').resizenow();
        });
        //Invoke the resizenow() function on browser resize
        $(window).bind("resize", function() {
            $('#supersize').resizenow();
        });
    };
    //Adjust image size
    $.fn.resizenow = function() {
        //Define starting width and height values for the original image
        var startwidth = 1440;
        var startheight = 956;
        //Define image ratio
        var ratio = startheight / startwidth;
        //Gather browser dimensions
        var browserwidth = $(window).width();
        var browserheight = $(window).height();
        //Resize image to proper ratio
        if ((browserheight / browserwidth) > ratio) {
            $(this).height(browserheight);
            $(this).width(browserheight / ratio);
            $(this).children().height(browserheight);
            $(this).children().width(browserheight / ratio);
        } else {
            $(this).width(browserwidth);
            $(this).height(browserwidth * ratio);
            $(this).children().width(browserwidth);
            $(this).children().height(browserwidth * ratio);
        }
        //Make sure the image stays center in the window
        $(this).children().css('left', (browserwidth - $(this).width()) / 2);
        $(this).children().css('top', (browserheight - $(this).height()) / 2);
    };
})(jQuery);

function showRecentlySavedItemsMyDulux() {

    var containerClasses = {
        rooms: '.box-recentlysaveditemsroomscontainer',
        abstracts: '.box-recentlysaveditemsabstractscontainer',
        colours: '.box-recentlysaveditemscolourscontainer',
        products: '.box-recentlysaveditemsproductscontainer'
    };

    var savedTemplates = {
        rooms: $('#roomRecentlySavedItemTemplate').children()[0],
        abstracts: $('#abstractRecentlySavedItemTemplate').children()[0],
        colours: $('#colourRecentlySavedItemTemplate').children()[0],
        products: $('#productRecentlySavedItemTemplate').children()[0]
    };
    $(document).attr('profile').addInitialisedCallBackFunctions(function() {
        fillMyDuluxContents(containerClasses, savedTemplates, true);
    });
   
}

function productSearchDisableControls() {

    $("#productSearchControl select.select").attr("disabled", true);
    $("#productSearchControl select.select").next("span").css("color", "#cccccc");
    $("#productSearchControl .inputradio input").attr("disabled", true);
    $("#productSearchControl .linkimage.findproduct-submit IMG").attr("src", "/SiteCollectionImages/buttons/view-selection-on.png");
    $("#productSearchControl .linkimage.findproduct-submit").click(function() {
        return false;
    });
}

function ShowMessage(title, text, delay, isCompleted) {
    $('#mydulux-message-title').html(title);
    $('#mydulux-message-text').html(text);

    if ($('#box-layer-message').is(":hidden")) {
        $('#box-layer-message').show(500);
    }

    var height = $('#mydulux-message').height() - 50;
    $('#box-layer-message').animate({ top: -height });

    if (isCompleted) {
        $('#box-layer-message').delay(delay).animate({ top: 50 }, null, null, function() { $(this).hide() });
    }
}

function debug(data) {
    $('.box-debug').append('<p>'+data+'</p>');
    if(window.console) {
        console.info(data);
    }
}
function fillTemplate(template, values) {
    if (template) {
        return template.replace(/{([^{}]*)}/g,
            function(str, p1) {
                var replacement = values[p1];
                return (typeof replacement === 'string' || typeof replacement === 'number') ? replacement : str;
            }
        );
    }
}
function convert_xml_data(data) {
    var xml;
    if($.browser.msie && typeof data == 'string') {
        xml = new ActiveXObject('Microsoft.XMLDOM');
        xml.async = false;
        xml.loadXML(data);
    } else {
        xml = data;
    }
    return xml;
}
function body_background() {
//    window_width = $(window).width();
//    window_height = $(window).height();
//    left = (window_width-background_width)/2;
//    if(background_width < window_width) {
//        $('.body-background').css('width', '100%');
//        $('.body-background').css('height', 'auto');
//    } else {
//        $('.body-background').css('width', background_width);
//        $('.body-background').css('height', background_height);
//        $('.body-background').css('left', left);
//        $('.body-background').css('z-index', '-10');
    //    }
    $('#body-background-image').resizenow();
}
function display_overlay(url, width, height) {
    time_fade = 600;
    if(width == '') {
        width = 650;
    }
    if(height == '') {
        height = 516;
    }
    width_overlay = width + 3;
    height_overlay = height;
    $('.box-overlay').css('width', width_overlay);
    $('.box-overlay').css('height', height_overlay);
    //overlayCss: {width: width_overlay, height: height_overlay},
    $(".box-overlay").modal({
        zIndex: 5000,
        overlayClose: true,
        opacity: 70,
        onOpen: function (dialog) {
            dialog.overlay.fadeIn(time_fade, function () {
                dialog.container.fadeIn(time_fade, function () {
                    dialog.data.fadeIn(time_fade);
                        $('.display-overlay').html('<iframe src="'+url+'" width="'+width+'" height="'+height+'" frameborder="0"></iframe>');
                    });
                });
        },
        onClose: function (dialog) {
            dialog.data.fadeOut(time_fade, function () {
                dialog.container.fadeOut(time_fade, function () {
                    dialog.overlay.fadeOut(time_fade, function () {
                        $.modal.close(); // must call this!
                    });
                });
            });
        }
    });
}

function close_overlay() {
    $.modal.close(); // must call this!
}

function display_overlay_zoom(width, height) {
    time_fade = 600;
    if (width == '') {
        width = 650;
    }
    if (height == '') {
        height = 514;
    }
    width_overlay = width;
    height_overlay = height;
    $('.box-overlay').css('width', width_overlay);
    $('.box-overlay').css('height', height_overlay);
    //overlayCss: {width: width_overlay, height: height_overlay},
    $(".box-overlay").modal({
        zIndex: 5000,
        overlayClose: true,
        opacity: 70,
        onOpen: function(dialog) {
            dialog.overlay.fadeIn(time_fade, function() {
                dialog.container.fadeIn(time_fade, function() {
                    dialog.data.fadeIn(time_fade);
                });
            });
        },
        onClose: function(dialog) {
            dialog.data.fadeOut(time_fade, function() {
                dialog.container.fadeOut(time_fade, function() {
                    dialog.overlay.fadeOut(time_fade, function() {
                        $.modal.close(); // must call this!
                    });
                });
            });
        }
    });
}

function select_redirect(url) {
    window.location.href = url;
}
function slide_menu_auto() {
    $('.box-menu ul:first-child ul:visible').slideUp('slow');
    $('.box-menu ul:first-child li').removeClass('active');
    $('.box-menu ul:first-child li.selected').addClass('active');
    $('.box-menu ul:first-child li.selected ul').slideDown('slow');
    //manage_logo('calculate');
}
function manage_logo(action) {
    //debug(action);
    if(action == 'cookie-enabled') {
        //$('.logo-akzonobel').css('visibility', 'visible');
        $('.logo-akzonobel').fadeIn(500);
    }
    if(action == 'menuswitch-mouseover') {
        if(lock_menu == 0) {
            //$('.logo-akzonobel').css('visibility', 'visible');
        }
        $('.logo-akzonobel').fadeIn(500);
    }
    if(action == 'menusidebar-mouseleave') {
        if(lock_menu == 0) {
            $('.logo-akzonobel').hide();
        }
        if(lock_menu == 1) {
            $('.logo-akzonobel').fadeIn(500);
        }
    }
    if(action == 'menuswitch-click-up') {
        $('.logo-akzonobel').hide();
    }
    if(action == 'menuswitch-click-down') {
        $('.logo-akzonobel').fadeIn(500);
    }

    //debug('opacity: '+$('.logo-akzonobel').css('opacity')+' visibility: '+$('.logo-akzonobel').css('visibility'));

    menu_offset = $('.box-menu ul:first-child').offset();
    if (menu_offset) {
        /*menu_height = $('.box-menu ul:first-child').height();*/
        menu_height = $('.box-menu ul').height();
        var logo_offset = $('.logo-akzonobel').offset();
        var ref_top = menu_offset.top + menu_height;
        var logo_top = logo_offset.top;

        if (logo_top < ref_top) {
            $('.logo-akzonobel').css('visibility', 'hidden');
        } else {
            $('.logo-akzonobel').css('visibility', 'visible');
        }
    }
}
function resize_calculate() {
    var myWidth = 0, myHeight = 0;
    /* myHeight = f_clientHeight() + f_scrollTop() - 110; */
    myHeight = f_clientHeight() + f_scrollTop() - 140; 
    $('p.logo-akzonobel').css('bottom', 50);
    height_document = $(window).height();
    $('.box-page').css('height', height_document-10);
    //$('.box-menu').css('height', height_document - 150);
    $('.box-menu').css('height', myHeight);

    width_box_page = $('.box-page').width();
    width_box_menuswitch = $('.box-menuswitch').width();
    width_document = $(window).width();
    max_width = width_box_page+width_box_menuswitch;
    //debug(width_document);
    if(width_document <= max_width) {
        left = 125;
    } else {
        left = (width_document-width_box_page)/2;
        if(left < 125) {
            left = 125;
        }
    }
    $('.box-page').css('margin-left', left);
    if($('.box-contentfooter').css('position') == 'fixed') {
        $('.box-contentfooter').css('left', left);
    }
    //debug(left);
}
function f_clientWidth() {
    return f_filterResults(
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
    return f_filterResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
    return f_filterResults(
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
    return f_filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function create_profile(callback) {
    data = '<Profile>';
        data += '<Site>'+site_url+'</Site>';
    data += '</Profile>';
    $.ajax({
        url: calls['create-profile'],
        type: 'GET',
        data: data,
        async: false,
        error: function() {
        },
        success: function(data) {
            var xml = convert_xml_data(data);
            var profile_id = $(xml).find('ProfileId').text();
            $.cookie('profile_id', profile_id);
            if (callback != undefined) {
                callback();
            }
        }
    });
}

function get_profile_then_call(callback) {
    if(!$.cookie('profile_id')) {
        create_profile(callback);
    } else {
        callback();
    }
}

function get_items() {
    url = calls['get-items'];
    url = url.replace('[profile_id]', $.cookie('profile_id'));
    $.ajax({
        url: url,
        type: 'GET',
        error: function() {
        },
        success: function(data) {
            var xml = convert_xml_data(data);
        }
    });
}

function add_mybasket_colour(reference) {
    get_profile_then_call(function(){
        data = '<ColourItem>';
            data += '<Site>'+site_url+'</Site>';
            data += '<ProfileId>'+$.cookie('profile_id')+'</ProfileId>';
            data += '<ItemTypeId>3</ItemTypeId>';
            data += '<ColourId>'+reference+'</ColourId>';
        data += '</ColourItem>';

        url = calls['create-item'];
        url = url.replace('[profile_id]', $.cookie('profile_id'));
        $.ajax({
            url: url,
            type: 'POST',
            data: data,
            error: function() {
            },
            success: function(data) {
                var xml = convert_xml_data(data);
                var item_id = $(xml).find('ItemId').text();
                basket_items[item_id] = '';
            }
        });
    });    
}

function add_mybasket_sample(reference) {
    get_profile_then_call(function(){
        data = '<SampleItem>';
            data += '<Site>'+site_url+'</Site>';
            data += '<ProfileId>'+$.cookie('profile_id')+'</ProfileId>';
            data += '<ItemTypeId>3</ItemTypeId>';   //TODO : remplacer par l'id correspondant aux samples
            data += '<SampleId>'+reference+'</SampleId>';
        data += '</SampleItem>';

        url = calls['create-item'];
        url = url.replace('[profile_id]', $.cookie('profile_id'));
        $.ajax({
            url: url,
            type: 'POST',
            data: data,
            error: function() {
            },
            success: function(data) {
                var xml = convert_xml_data(data);
                var item_id = $(xml).find('ItemId').text();
                basket_items[item_id] = '';
            }
        });
    });
}

function add_mydulux_colour(reference) {
    //debug('add_mydulux_colour: '+reference);
}
function add_mydulux_room(reference) {
    //debug('add_mydulux_room: '+reference);
}



function addRoomToMyDulux() {
    var room = new Object();
    room.nirvanaImageLocalId = NirvanaRoomId;
    room.type = USER_ITEM_TYPES.Room;

    if (isNaN(NirvanaRoomId)) {
        return;
    }

    try {

        var sectionsString = eval("sectionscolorsbyid_" + NirvanaRoomId);

        var section = sectionsString.split(',');

        if (section != "") {
            room.sections = new Object();

            for (i = 0; i < section.length; i++) {
                var item = section[i].split(':');
                room.sections[i] = new Object();
                room.sections[i].recommendedProductId = null;
                room.sections[i].recommendedProductName = null;
                var color = {
                    colorId: parseInt(item[1].replace(/'/gi, ''))
                }
                room.sections[i].nirvanaSectionId = item[0];
                room.sections[i].color = color;
            } 
        }
        $(document).attr("profile").addErrorHandlerFunctions(myDuluxErrorHandler);
        $(document).attr('profile').addUserItems(room, popupChangedDulux);
    }
    catch (e) { }

}

function popupChangedDulux() {
    showRecentlySavedItemsMyDulux();
    myDulux_popup_hover();
    myDulux_popup_leave(true);
}

function addColorToMyDulux(colorId, rgb, name) {
    if ($(document).attr("profile")) {
        var colour = {
            colorId: colorId,
            rgb: rgb,
            name: name,
            type: "Fusion_Color"
        };

        $(document).attr("profile").addErrorHandlerFunctions(myDuluxErrorHandler);
        $(document).attr("profile").addUserItems([colour], popupChangedDulux);
    }
}


function add_mydulux_product(productId, productName, productThumbLocationUrl) {
    var product = new Object();
    product.productId = productId;
    product.name = productName;
    product.thumbLocationUrl = productThumbLocationUrl;
    product.type = USER_ITEM_TYPES.Product;
    $(document).attr("profile").addErrorHandlerFunctions(myDuluxErrorHandler);
    $(document).attr('profile').addUserItems(product, popupChangedDulux);
}

function myDuluxErrorHandler(jqXHR, textStatus, errorThrown) {
    ShowMessage(
            messagePopupErrorTitle == undefined || messagePopupErrorTitle == null ? 'MyDulux - An Error Occured' : messagePopupErrorTitle,
            messagePopupErrorText == undefined || messagePopupErrorText == null ? 'An error occured while trying to add the item to your collection.' : messagePopupErrorText,
            4000, true);
}

function myDulux_popup_hover() {
    if (!duluxLoaded) {
        duluxLoaded = true;

        //var pageData = $(data).find('.mydulux-popup').parent();
        //$('.box-layer-mydulux').html(pageData);
        //update_mydulux_data('footer'); // Function not implemented yet

        $('.box-layer-mydulux').show();
        height_layer = $('#mydulux-popup').height();
        $('.box-layer-mybasket').animate({ top: 0 }, null, null, function() {
            $('.box-layer-mybasket').remove();
            $('.box-footermy-basket').show();
            basketLoaded = false;
        });
        $('.box-footermy-dulux').hide();

        $('.box-layer-mydulux').animate({ top: -height_layer });
    }
    return false;
}

function myDulux_popup_leave(isCompleted) {
    //mouseleave
    var delay = 0;
    if (isCompleted) {
        delay = 2000;
    }
    $('.box-layer-mydulux').delay(delay).animate({ top: 0 }, null, null, function() {
        $('.box-layer-mydulux').hide();
        $('.box-footermy-dulux').show();
        duluxLoaded = false;
    });
    return false;
}


var set_surfaces = new Array();
var set_finishes = new Array();
var set_paintranges = new Array();
var set_features = new Array();

function get_products_settings() {
    var data = '/xml/ProductSearchSettings/' + brand + '/' + country_code + '/' + language_code;
    $.ajax({
        url: calls['get-products-searchsettings'] + data,
        type: 'GET',
        async: false,
        error: function() {
        },
        success: function(data) {
            var xml = convert_xml_data(data);

            u = 1;
            $(xml).find('ArrayOfSurfaceOption').find('SurfaceOption').each(function() {
                var label = $(this).text();
                //var id = $(this).attr('ID');
                var id = label;
                $('<option value="' + label + '" id="' + id + '">' + label + '</option>').appendTo('.findproduct-surface');
                //$('<option value="'+label+'" id="'+id+'">'+label+'</option>').appendTo('.whatpainting');
                set_surfaces[u] = id;
                u++;
            });

            u = 1;
            $(xml).find('ArrayOfSheenOption').find('SheenOption').each(function() {
                var label = $(this).text();
                //var id = $(this).attr('ID');
                var id = label;
                $('<option value="' + label + '" id="' + id + '">' + label + '</option>').appendTo('.findproduct-finish');
                $('<input id="xxx' + id + '" class="inputradio filters-type' + id + '" name="filters-type" type="radio" /><label for="xxx' + id + '">' + label + '</label>').appendTo('.filter-radio');
                set_finishes[u] = id;
                u++;
            });

            u = 1;
            $(xml).find('ArrayOfPaintRange').find('PaintRange').each(function() {
                var label = $(this).text();
                //var id = $(this).attr('ID');
                var id = label;
                $('<p><input id="paintrange' + u + '" value="' + id + '" class="inputcheckbox advancedsearch-paintrange' + u + '" name="advancedsearch-paintrange' + u + '" type="checkbox" /> <label for="paintrange' + u + '">' + label + '</label></p>').appendTo('.paintranges');
                set_paintranges[u] = id;
                u++;
            });

            u = 1;
            $(xml).find('ArrayOfFeature').find('Feature').each(function() {
                var label = $(this).text();
                //var id = $(this).attr('ID');
                var id = label;
                $('<p><input id="feature' + u + '" value="' + id + '" class="inputcheckbox advancedsearch-feature' + u + '" name="advancedsearch-feature' + u + '" type="checkbox" /> <label for="feature' + u + '">' + label + '</label></p>').appendTo('.features');
                set_features[u] = id;
                u++;
            });

            if (set_paintranges.length < 1 && set_features.length < 1) {
                $('.box-advancedsearch').hide();
            }
            else {
                if (set_features.length < 1) {
                    $('.features').hide();
                    $('#featuresAdvancedSearch').hide();
                }
                if (set_paintranges.length < 1) {
                    $('.paintranges').hide();
                    $('#paintrangesAdvancedSearch').hide();
                }
            }
        }
    });
};


function get_products_settings_json() {
    var data = '/json/ProductSearchSettings/' + encodeURIComponent(brand) + '/' + country_code + '/' + language_code;
    
    $.ajax({
        url: calls['get-products-searchsettings'] + data,
        type: 'GET',
        async: true,
        error: function() {
        },
        success: function(data) {

            for (i = 0; i < data.ArrayOfSurfaceOption.length; i++) {
                var label = data.ArrayOfSurfaceOption[i];
                var id = label;

                $('<option value="' + label + '" id="' + id + '">' + label + '</option>').appendTo('.findproduct-surface');

                set_surfaces[i + 1] = id;
            }

            for (i = 0; i < data.ArrayOfSheenOption.length; i++) {
                var label = data.ArrayOfSheenOption[i];
                var id = label;

                $('<option value="' + label + '" id="' + id + '">' + label + '</option>').appendTo('.findproduct-finish');
                $('<input id="xxx' + id + '" class="inputradio filters-type' + id + '" name="filters-type" type="radio" /><label for="xxx' + id + '">' + label + '</label>').appendTo('.filter-radio');

                set_finishes[i + 1] = id;
            }

            for (i = 0; i < data.ArrayOfPaintRange.length; i++) {
                var label = data.ArrayOfPaintRange[i];
                var id = label;

                $('<p><input id="paintrange' + (i + 1) + '" value="' + id + '" class="inputcheckbox advancedsearch-paintrange' + (i + 1) + '" name="advancedsearch-paintrange' + (i + 1) + '" type="checkbox" /> <label for="paintrange' + (i + 1) + '">' + label + '</label></p>').appendTo('.paintranges');

                set_paintranges[i + 1] = id;
            }

            for (i = 0; i < data.ArrayOfFeature.length; i++) {
                var label = data.ArrayOfFeature[i];
                var id = label;

                $('<p><input id="feature' + (i + 1) + '" value="' + id + '" class="inputcheckbox advancedsearch-feature' + (i + 1) + '" name="advancedsearch-feature' + (i + 1) + '" type="checkbox" /> <label for="feature' + (i + 1) + '">' + label + '</label></p>').appendTo('.features');

                set_features[i + 1] = id;
            }

            if (set_paintranges.length < 1 && set_features.length < 1) {
                $('.box-advancedsearch').hide();
            }
            else {
                if (set_features.length < 1) {
                    $('.features').hide();
                    $('#featuresAdvancedSearch').hide();
                }
                if (set_paintranges.length < 1) {
                    $('.paintranges').hide();
                    $('#paintrangesAdvancedSearch').hide();
                }
            }
        }
    });
}


function get_products_settings_json() {
    var data = '/json/ProductSearchSettings/' + encodeURIComponent(brand) + '/' + country_code + '/' + language_code;
    
    $.ajax({
        url: calls['get-products-searchsettings'] + data,
        type: 'GET',
        async: true,
        error: function() {
        },
        success: function(data) {

            for (i = 0; i < data.ArrayOfSurfaceOption.length; i++) {
                var label = data.ArrayOfSurfaceOption[i];
                var id = label;

                $('<option value="' + label + '" id="' + id + '">' + label + '</option>').appendTo('.findproduct-surface');

                set_surfaces[i + 1] = id;
            }

            for (i = 0; i < data.ArrayOfSheenOption.length; i++) {
                var label = data.ArrayOfSheenOption[i];
                var id = label;

                $('<option value="' + label + '" id="' + id + '">' + label + '</option>').appendTo('.findproduct-finish');
                $('<input id="xxx' + id + '" class="inputradio filters-type' + id + '" name="filters-type" type="radio" /><label for="xxx' + id + '">' + label + '</label>').appendTo('.filter-radio');

                set_finishes[i + 1] = id;
            }

            for (i = 0; i < data.ArrayOfPaintRange.length; i++) {
                var label = data.ArrayOfPaintRange[i];
                var id = label;

                $('<p><input id="paintrange' + (i + 1) + '" value="' + id + '" class="inputcheckbox advancedsearch-paintrange' + (i + 1) + '" name="advancedsearch-paintrange' + (i + 1) + '" type="checkbox" /> <label for="paintrange' + (i + 1) + '">' + label + '</label></p>').appendTo('.paintranges');

                set_paintranges[i + 1] = id;
            }

            for (i = 0; i < data.ArrayOfFeature.length; i++) {
                var label = data.ArrayOfFeature[i];
                var id = label;

                $('<p><input id="feature' + (i + 1) + '" value="' + id + '" class="inputcheckbox advancedsearch-feature' + (i + 1) + '" name="advancedsearch-feature' + (i + 1) + '" type="checkbox" /> <label for="feature' + (i + 1) + '">' + label + '</label></p>').appendTo('.features');

                set_features[i + 1] = id;
            }

            if (set_paintranges.length < 1 && set_features.length < 1) {
                $('.box-advancedsearch').hide();
            }
            else {
                if (set_features.length < 1) {
                    $('.features').hide();
                    $('#featuresAdvancedSearch').hide();
                }
                if (set_paintranges.length < 1) {
                    $('.paintranges').hide();
                    $('#paintrangesAdvancedSearch').hide();
                }
            }
        }
    });
}

function fillMyDuluxContents(containerClasses, savedTemplates, ifRecent) {
 // This function is used in the masterpage. In function.mydulux-saveditems.js this function will be overwritten.
}

function requiresDarkTextColour(hex) {
    hex = hex.replace('#', '');
    var average = (hex2r(hex) + hex2g(hex) + hex2b(hex)) / 3;
    return average > 128;
}

function hex2r(hex) { return parseInt((cutHex(hex)).substring(0, 2), 16); }
function hex2g(hex) { return parseInt((cutHex(hex)).substring(2, 4), 16); }
function hex2b(hex) { return parseInt((cutHex(hex)).substring(4, 6), 16); }
function cutHex(hex) { return hex.replace('#', ''); } 

function getNirvanaImageURL(nirvanaImageId, width, height, scheme) {
    function parseVisualiserScheme(scheme) {
        return scheme.replace(/=/g, '!').replace(/&/g, ';');
    }
    return calls['NirvanaImage']+"/pox/nirvanaimage/"+nirvanaImageId + "?Width=" + width + "&Height=" + height + "&SectionColorIds=" + parseVisualiserScheme(scheme);
}

