﻿var gModalDialogMask = null;
var gModalDialogContainer = null;
var gModalDialogFrame = null;
var gReturnFunc;
var gModalDialogIsShown = false;
var gHideSelects = false;

var gTabIndexes = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A", "BUTTON", "TEXTAREA", "INPUT", "IFRAME");


/**
* Initializes ModalDialog code on load.	
*/
function initModalDialog() {
    // Add the HTML to the body
    var body = document.getElementsByTagName('body')[0];
    var ModalDialogmask = document.createElement('div');
    ModalDialogmask.id = 'modelDialogMask';
    var ModalDialogcont = document.createElement('div');
    ModalDialogcont.id = 'modelDialogContainer';
    ModalDialogcont.innerHTML = '' +
		'<div id="modelDialogInner">' +
			'<div id="modelDialogTitleBar">' +
				'<div id="modelDialogTitle"></div>' +
				'<div id="modelDialogControls">' +
					'<a onclick="hideModalDialog(false);">Close</a>' +
				'</div>' +
			'</div>' +
			'<div style="height:100%;" id="modelDialogContent"></div>' +
		'</div>';
    body.appendChild(ModalDialogmask);
    body.appendChild(ModalDialogcont);

    gModalDialogMask = document.getElementById("modelDialogMask");
    gModalDialogContainer = document.getElementById("modelDialogContainer");
    gModalDialogFrame = document.getElementById("modelDialogContent");

    // check to see if this is IE version 6 or lower. hide select boxes if so
    // maybe they'll fix this in version 7?
    var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
    if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
        gHideSelects = true;
    }
}

/**
* @argument width - int in pixels
* @argument height - int in pixels
* @argument url - url to display
* @argument returnFunc - function to call when returning true from the window.
*/
function showModalDialog(title, text, width, height, returnFunc) 
{
    //if (text) {
    //    text = text.replace(/\n/g, "<br />"); //vervang alle linebreaks door <br>
    //}
    text = text;
    gModalDialogIsShown = true;
    disableTabIndexes();
    gModalDialogMask.style.display = "block";
    gModalDialogContainer.style.display = "block";

    var titleBarHeight = parseInt(document.getElementById("modelDialogTitleBar").offsetHeight, 10);

    document.getElementById("modelDialogTitle").innerHTML = title;
  
    gModalDialogContainer.style.width = width + "px";
    if (height) 
    {
        gModalDialogContainer.style.height = (height + titleBarHeight) + "px";
    }
    //gModalDialogFrame.style.width = parseInt(document.getElementById("ModalDialogTitleBar").offsetWidth, 10) + "px";
    if (height != null) {
        gModalDialogFrame.style.height = height + "px";
    }
    if (text) 
    {
        gModalDialogFrame.innerHTML = text;
    }
     resizeModalDialogMask();
     centerModalDialog(width, height);
    //gReturnFunc = returnFunc;
    // for IE
    if (gHideSelects == true) {
        hideSelectBoxes();
    }
}

function showModalDialogWebService(title, paginaBlokID, identity, width, height, returnFunc) {

    return showModalDialogWebServicePerShop(title, paginaBlokID, identity, width, height, returnFunc, shopID);
}

function showModalDialogWebServicePerShop(title, paginaBlokID, identity, width, height, returnFunc, shopID) {

    var html = getPaginaBlokHtmlPerShop(paginaBlokID, identity, shopID);    

    showModalDialog(title, html, width, height, returnFunc);
}

/**
* @argument callReturnFunc - bool - determines if we call the return function specified
* @argument returnVal - anything - return value 
*/
function hideModalDialog() {
    gModalDialogIsShown = false;
    restoreTabIndexes();
    if (gModalDialogMask == null) {
        return;
    }
    gModalDialogMask.style.display = "none";
    gModalDialogContainer.style.display = "none";

    // display all select boxes
    if (gHideSelects == true) {
        displaySelectBoxes();
    }
}

function resizeModalDialogMask()
{   
    var htmlheight = getScrollHeight();
    var windowheight = getViewportHeight();
    if (htmlheight < windowheight)
    {
        gModalDialogMask.style.height = windowheight + "px";
    }
    else
    {
        gModalDialogMask.style.height = htmlheight + "px";
    }
}

/**
* @argument width - int - width of ModalDialog 
* @argument height - int - height of ModalDialog
*/
function centerModalDialog(width, height) {
    if (gModalDialogIsShown == true) {

        var scrollTop = getScrollTop();
        var marginTop = 100;
        var top = scrollTop + marginTop;
        if (top < 0) { top = 0; }
    
        if (width == null || isNaN(width)) {
            width = gModalDialogContainer.offsetWidth;
        }

        var fullWidth = getViewportWidth();
        var scLeft = getScrollLeft();
        var left = scLeft + ((fullWidth - width) / 2);

        gModalDialogContainer.style.top = top + "px";
        gModalDialogContainer.style.left = left + "px";
    }
}

function getViewportHeight() {
    if (window.innerHeight != window.undefined) return window.innerHeight;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight;
    return window.undefined;
}

function getViewportWidth() {
    if (window.innerWidth != window.undefined) return window.innerWidth;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientWidth;
    if (document.body) return document.body.clientWidth;
    return window.undefined;
}

function getScrollLeft() {
    if (self.pageYOffset) {
        scLeft = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollLeft) {
        scLeft = document.documentElement.scrollLeft;
    } else if (document.body) {
        scLeft = document.body.scrollLeft;

    }
    return scLeft;
}

function getScrollTop() {
    if (self.pageYOffset) {
        scTop = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        scTop = document.documentElement.scrollTop;
    }
    else if (document.body) {
        scTop = document.body.scrollTop;
    }
    return scTop;
}

function getScrollHeight() {
    if (document.body) {
        scHeight = document.body.parentNode.scrollHeight;
    }
    else if (self.pageYOffset) {
        scHeight = self.scrollHeight
    }
    else if (document.documentElement && document.documentElement.scrollHeight) {
        scHeight = document.documentElement.scrollHeight;
    }

    return scHeight;
}


// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandler(e) {

    //key = e ? e.which : event.keyCode;
    //bovenstaande werkt niet in IE8
    
    var isIE = (window.event != null);
    var key = (isIE) ? event.keyCode : e.which;
    
    if (key == 27) {
        hideModalDialog()
        return false;
    }

}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
    if (document.all) {
        var i = 0;
        for (var j = 0; j < gTabbableTags.length; j++) {
            var tagElements = document.getElementsByTagName(gTabbableTags[j]);
            for (var k = 0; k < tagElements.length; k++) {
                gTabIndexes[i] = tagElements[k].tabIndex;
                tagElements[k].tabIndex = "-1";
                i++;
            }
        }
    }
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() {
    if (document.all) {
        var i = 0;
        for (var j = 0; j < gTabbableTags.length; j++) {
            var tagElements = document.getElementsByTagName(gTabbableTags[j]);
            for (var k = 0; k < tagElements.length; k++) {
                tagElements[k].tabIndex = gTabIndexes[i];
                tagElements[k].tabEnabled = true;
                i++;
            }
        }
    }
}

/**
* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
    for (var i = 0; i < document.forms.length; i++) {
        for (var e = 0; e < document.forms[i].length; e++) {
            if (document.forms[i].elements[e].tagName == "SELECT") {
                document.forms[i].elements[e].style.visibility = "hidden";
            }
        }
    }
}

/**
* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*/
function displaySelectBoxes() {
    for (var i = 0; i < document.forms.length; i++) {
        for (var e = 0; e < document.forms[i].length; e++) {
            if (document.forms[i].elements[e].tagName == "SELECT") {
                document.forms[i].elements[e].style.visibility = "visible";
            }
        }
    }
}

/**
* X-browser event handler attachment and detachment
* @argument obj - the object to attach event to
* @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
* @argument fn - function to call
*/
function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
}

addEvent(window, "resize", centerModalDialog);
addEvent(document, "keypress", keyDownHandler);
addEvent(window, "load", initModalDialog);
addEvent(window, "scroll", centerModalDialog);


var tt = function () {
    var id = 'tt';
    var top = 3;
    var right = 0;
    var maxw = 144;
    var speed = 10;
    var timer = 20;
    var endalpha = 100;
    var alpha = 0;
    var tit, t, c, b, h;
    var ie = document.all ? true : false;
    return {
        show: function (shopID, paginaBlokID, identity, obj, w) {

            text = getPaginaBlokHtmlPerShop(paginaBlokID, identity, shopID);
            if (tit == null) {
                tit = document.createElement('div');
                tit.setAttribute('id', id);
                t = document.createElement('div');
                t.setAttribute('class', 'section-top');
                c = document.createElement('div');
                c.setAttribute('class', 'content');
                b = document.createElement('div');
                b.setAttribute('class', 'section-bottom');
                arrow = document.createElement('div');
                arrow.setAttribute('class', 'lefttooltip');
                tit.appendChild(arrow);
                tit.appendChild(t);
                tit.appendChild(c);
                tit.appendChild(b);
                document.body.appendChild(tit);
                tit.style.opacity = 0;
                tit.style.filter = 'alpha(opacity=0)';

            }

            tit.style.display = 'block';
            c.innerHTML = text;
            tit.style.width = w ? w + 'px' : maxw + 'px';
            if (!w && ie) {
                t.style.display = 'none';
                b.style.display = 'none';
                tit.style.width = tit.offsetWidth;
                t.style.display = 'block';
                b.style.display = 'block';
            }

            var el = obj;
            var obj2 = obj;
            var curtop = 0;
            var curleft = 0;
            if (document.getElementById || document.all) {
                do {
                    curleft += obj.offsetLeft - obj.scrollLeft;
                    curtop += obj.offsetTop - obj.scrollTop;
                    obj = obj.offsetParent;
                    obj2 = obj2.parentNode;
                    while (obj2 != obj) {
                        curleft -= obj2.scrollLeft;
                        curtop -= obj2.scrollTop;
                        obj2 = obj2.parentNode;
                    }
                } while (obj.offsetParent)
            } else if (document.layers) {
                curtop += obj.y;
                curleft += obj.x;
            }
            var trHeight = 0;
            var trWidth = 0;

            if (el.offsetHeight) { trHeight = el.offsetHeight; }
            else if (el.style.pixelHeight) { trHeight = el.style.pixelHeight; }
            if (el.offsetWidth) { trWidth = el.offsetWidth; }
            else if (el.style.pixelWidth) { trWidth = el.style.pixelWidth; }

            tit.style.top = (curtop + trHeight / 2 - 30) + 'px';
            tit.style.left = (curleft + (trWidth - right)) + 'px';
            if (tit.offsetWidth > maxw) { tit.style.width = maxw + 'px' }
            clearInterval(tit.timer);
            tit.timer = setInterval(function () { tt.fade(1) }, timer);
        },
        fade: function (d) {
            var a = alpha;
            if ((a != endalpha && d == 1) || (a != 0 && d == -1)) {
                var i = speed;
                if (endalpha - a < speed && d == 1) {
                    i = endalpha - a;
                } else if (alpha < speed && d == -1) {
                    i = a;
                }
                alpha = a + (i * d);
                tit.style.opacity = alpha * .01;
                tit.style.filter = 'alpha(opacity=' + alpha + ')';
            } else {
                clearInterval(tit.timer);
                if (d == -1) { tit.style.display = 'none' }
            }
        },
        hide: function () {
            if (typeof (tit) != 'undefined') {
                clearInterval(tit.timer);
                tit.timer = setInterval(function () { tt.fade(-1) }, timer);
            }
        }
    };
} ();
