
window.onload = init;

var onLoads = new Array();

function asyncEndRequest(sender, args) {
}

function init() {
    for (var i = 0; i < onLoads.length; i++) {
        eval(onLoads[i]);
    }
}

function addOnLoad(func) {
    onLoads[onLoads.length] = func;
}

$(function() { $('#alertLayer').jqm({ overlayClass: 'layerOverlay', popuplayer: '.layer' }); });

function alertLayer(header, content) {
    $('#alertLayerHeader').replaceWith(header);
    $('#alertLayerContent').replaceWith(content);
    $('#alertLayer').jqmShow();
}

// Slideshow for product pages (only works if popup.js is linked on the page in question)

addOnLoad('LaRedouteSlideshow.init()');

var LaRedouteSlideshow = {
    products: new Array(),

    init: function() {
        if (typeof (Popup) != 'undefined') {
            // Check for product boxes
            var divs = document.getElementsByTagName('div');
            for (var i = 0; i < divs.length; i++) {
                if (divs[i].className.match(/^product_box/)) {
                    LaRedouteSlideshow.products.push(divs[i]);
                }
            }

            // Continue if we found any product boxes
            if (LaRedouteSlideshow.products.length > 0) {
                // Add an onclick event to all product images
                for (var i = 0; i < LaRedouteSlideshow.products.length; i++) {
                    var image;
                    if (image = LaRedouteSlideshow.products[i].getElementsByTagName('img').item(0)) {
                        image.productNum = i;
                        image.onclick = LaRedouteSlideshow.show;
                    }
                }
            }
        }
    },

    show: function() {
        // Copy the contents of the corresponding product box into the popup
        if (Popup.readyToRun(true)) {
            nodes = this.parentNode.childNodes;
            content = Popup.popupContentObject;

            // First, remove anything already in the popup
            content.innerHTML = '';

            // Populate the popup
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].nodeName != '#text') {
                    content.appendChild(nodes[i].cloneNode(true))
                }
            }

            // Edit the path of the image, and remove its onclick property
            if (image = content.getElementsByTagName('img').item(0)) {
                image.removeAttribute('productNum');
                image.removeAttribute('onclick');
                image.src = image.src.replace('-medium.', '-large.')
            }

            // Show the popup
            Popup.show(true);
        }
    }
}

function toggleDisplay(elementId) {
    var element = document.getElementById(elementId);
    if (element != null) {
        if (element.style.display == 'none') {
            element.style.display = '';
        } else {
            element.style.display = 'none';
        }
    }
}

function setElementPositionByMouse(e, id, offsetx, offsety) {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
    }

    var element = $get(id);
    posx = (posx + offsetx);
    posy = (posy + offsety);

    element.style.top = posy + 'px';
    element.style.left = posx + 'px';
}

function validatelimit(obj, length) {
    if (this.id) obj = this;
    if (obj.value.length > length) {
        obj.value = obj.value.substring(length, 0);
        return false;
    }
    else {
        return true;
    }
}

function InitDynLiMenu() {
    if (document.all && document.getElementById) {
        navRoot = document.getElementById("ddMenu").childNodes[0].childNodes[0];
        for (i = 0; i < navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName == "LI") {
                node.onmouseover = function() {
                    this.className += " over";
                }
                node.onmouseout = function() {
                    this.className = this.className.replace(" over", "");
                }
            }
        }
    }
}

function InitDynLiMenu2() {
    if (document.all && document.getElementById) {
        nav = document.getElementById("myPageDropdownFixIE");
        if (nav.nodeName == "DIV") {
            nav.onmouseover = function() {
                this.className += " over";
            }
            nav.onmouseout = function() {
                this.className = this.className.replace(" over", "");

            }
        }
    }
}

// Run only if Internet Explorer <= 6
if (window.navigator.appName.toLowerCase().indexOf("microsoft internet explorer") != -1) {
    var appVersion = window.navigator.appVersion.toLowerCase();

    if (appVersion.indexOf("msie 5") != -1 || appVersion.indexOf("msie 6") != -1) {
        window.attachEvent("onload", InitDynLiMenu);
        window.attachEvent("onload", InitDynLiMenu2);
    }
}

function printContent(contentId, width, height) {
    var popup = window.open('', 'Print', 'width=' + width + ',height=' + height + ',top=200,left=250,toolbars=no,scrollbars=yes,status=no,resizable=no');
    popup.document.body.innerHTML = $('#' + contentId).html();
    popup.document.close(); popup.print(); popup.close();
}

function popupContent(url, width, height) {
    var popup = window.open('', 'popup', 'width=' + width + ',height=' + height + ',top=200,left=250,toolbars=no,scrollbars=yes,status=no,resizable=yes,location=no');
    var result;
    $.ajax({
        url: url,
        type: "GET",
        processData: false,
        dataType: "html",
        timeout: 10000,
        success: function(response) {
            result = response;
        },
        error: function() {
            alert('error!');
            return;
        },
        async: false
    });
    popup.document.write('<html><head><title>LaRedoute</title></head><body>' + result + '</body></html>');
}

function registerForNewsletter(email) {

    var result = false;
    var url = "/Services/Customer.svc/RegisterForNewsLetter/" + email + "/json";
    $.ajax({
        url: url,
        type: "GET",
        processData: false,
        timeout: 10000,
        success: function(response) {
            result = (response == 'true');
        },
        error: function(request, status, errorThrown) {
            result = false;
        },
        async: false
    });
    return result;
}
var scrollActivated = false;
function toggler(linkClass, containerClass, toggleEvent) {
    $(linkClass).bind(toggleEvent ? toggleEvent : 'click',
        function(event) {
            event.preventDefault();
            if (containerClass)
                $(containerClass).hide();
            $(linkClass).removeClass('selected')
            $(containerClass).removeClass('selected')
            $(this).addClass('selected')
            $('[content=' + $(this).text() + ']').show();
            if (!(scrollActivated)) {
                var targetOffset = $('.contentBox').offset().top;
                $('html,body').animate({ scrollTop: targetOffset }, 1000);
                scrollActivated = true;
            }


        });
}
