
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;
}

// Slideshow for product pages (only works if popup.js is linked on the page in question)

addOnLoad('ellosSlideshow.init()');

var ellosSlideshow = {
    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/)) {
                    ellosSlideshow.products.push(divs[i]);
                }
            }

            // Continue if we found any product boxes
            if (ellosSlideshow.products.length > 0) {
                // Add an onclick event to all product images
                for (var i = 0; i < ellosSlideshow.products.length; i++) {
                    var image;
                    if (image = ellosSlideshow.products[i].getElementsByTagName('img').item(0)) {
                        image.productNum = i;
                        image.onclick = ellosSlideshow.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;
    }

    posx = (posx + offsetx);
    posy = (posy + offsety);

    $('#' + id).css({ 'top': posy + 'px', '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 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>Ellos</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;
}

function ResetScrollPosition() { 
    
    var scrollX = document.getElementById('__SCROLLPOSITIONX');
    var scrollY = document.getElementById('__SCROLLPOSITIONY');
    
    if (scrollX && scrollY) {
        scrollX.value = 0;
        scrollY.value = 0;
    }
}
