
// adds explicit handlers for stupid browsers like IE6
// to be able to show popup menus
function addIEHandlers() {
  // traverse through all elements of the page
  var elements = document.getElementsByTagName("*");
  for(var i = 0; i < elements.length; i++) {
    var element = elements[i];
    // and if elements has class "submenu"...
    if(element.className && element.className.indexOf("submenu") != -1) {
      // attach handlers to its parent
      element.parentNode.onmouseout = function(element){return function(){element.style.display = "none";}}(element);
      element.parentNode.onmouseover = function(element){return function(){element.style.display = "block";}}(element);
    }
  }
}

// decides whether to show the login form or not
function handleLoginFormVisiblity() {
    // find the form
    var loginsubmenu = document.getElementById("logindropdown");
    if(loginsubmenu) {
        // initialize visibility with false;
        var visible = false;

        // check if cursor is above the form
        if((typeof loginsubmenu.mouseisover != "undefined") && loginsubmenu.mouseisover) {
            visible = true;
        } else {
            // find all inputs of the form and for each check if it is focussed
            var inputs = loginsubmenu.getElementsByTagName("input");
            for(var i = 0; i < inputs.length; i++) {
                if(inputs[i].hasFocus) {
                    visible = true;
                    break;
                }
            }
        }

        // set the visibility
        loginsubmenu.style.display = (visible ? "block" : "hidden");
    }
}

// adds event which make login dropdown form visible
// even if mouse is outside of it
function addLoginFormHandlers() {
    // try to find an element with ID "loginsubmenu"
    var loginsubmenu = document.getElementById("logindropdown");
    if(loginsubmenu) {
        // initialize the property of login sub-menu
        loginsubmenu.mouseisover = false;

        // add onouseover and onmouseout handlers to whole form
        loginsubmenu.onmouseover = function() {
            loginsubmenu.mouseisover = true;
            handleLoginFormVisiblity();
        }
        loginsubmenu.onmouseover = function() {
            loginsubmenu.mouseisover = false;
            handleLoginFormVisiblity();
        }

        // add onfocus and onblur handlers to individual inputs
        var inputs = loginsubmenu.getElementsByTagName("input");
        for(var i = 0; i < inputs.length; i++) {
            inputs[i].onfocus = handleLoginFormVisiblity;
            inputs[i].onblur = handleLoginFormVisiblity;
        }
    }
}

// now, run following as default onload function:
onload = function() {
    if(typeof haveIE6 != "undefined") {
        addIEHandlers();
    }
    addLoginFormHandlers();
    updateSlideshow(8, 100, 25, 3000);
    refreshForm();
}



// Sets opacity of element (opacity is in percents)
function setOpacity(element, opacity) {
    if(typeof element != 'undefined') {
        if(typeof element.imageobject != 'undefined') {
            if(typeof element.imageobject.style != 'undefined') {
                if(typeof element.imageobject.style.MozOpacity != 'undefined') {
                    element.imageobject.style.MozOpacity=opacity/100.0;
                }
            }
        }
        if(typeof element.filters != 'undefined') {
            if(typeof element.filters.alpha != 'undefined') {
                if(typeof element.filters.alpha.opacity != 'undefined') {
                    element.filters.alpha.opacity=opacity;
                }
            }
        }
        if(typeof element.style != 'undefined') {
            if(typeof element.style.filter != 'undefined') {
                element.style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity=' + opacity + ')';
            }
            if(typeof element.style.opacity != 'undefined') {
                element.style.opacity=opacity/100.0;
            }
        }
    }
}

// updates slideshow (opacity goes from 100-visible to 0-invisible)
function updateSlideshow(numImages, opacity, frameTiming, waitTime) {
    // find both image elements
    var backImage = document.getElementById('slideshow-back');
    var frontImage = document.getElementById('slideshow-front');
    if(backImage != null && frontImage != null) {
        // if new opacity is 0, swap the images and choose some new as back image
        if(opacity <= 0) {
            // determine old image index
            var oldIndex = parseInt(frontImage.src.replace('[^0-9]', ''));
            var newIndex = Math.min(Math.floor(Math.random() * numImages), numImages-1);

            // if indices are equal, use next free index
            if(newIndex == oldIndex) {
                newIndex = (newIndex + 1) % numImages;
            }

            // move back image to front and pick a new back image
            frontImage.src = backImage.src;
            backImage.src = 'media/slideshow-' + ("00" + newIndex).slice(-2) + '.jpg';

            // reset opacity
            setOpacity(frontImage, 100);

            // sleep a while before starting to fade
            window.setTimeout(function() {
                updateSlideshow(numImages, 100, frameTiming, waitTime)
                }, waitTime);
        } else {
            // clamp opacity into correct range
            opacity = Math.max(Math.min(100, opacity - 2), 0);

            // set new opacity
            setOpacity(frontImage, opacity);

            // wait
            window.setTimeout(function() {
                updateSlideshow(numImages, opacity, frameTiming, waitTime)
                }, frameTiming);
        }
    }
}


function copyValue(name) {
    // find destination
    destinations = document.getElementsByName('invoice-' + name);
    if(destinations.length > 0) {
        // make sure that destination is disabled
        if(destinations[0].disabled) {
            // find source
            sources = document.getElementsByName('delivery-' + name);
            if(sources.length > 0) {
                // copy value if found
                destinations[0].value = sources[0].value;
            }
        }
    }
}


function refreshForm() {
    // determine whether we should disable or enable fields
    var checkbox = document.getElementById('invoice-same');
    if(checkbox != null) {
        var copy = checkbox.checked;
        names = ['name', 'street', 'number', 'city', 'zip', 'country', 'phone'];
        for(var i = 0; i < names.length; i++) {
            inputs = document.getElementsByName('invoice-' + names[i]);
            if(inputs.length > 0) {
                inputs[0].disabled = copy;
                if(copy) {
                    copyValue(names[i])
                }
            }
        }
    }
}