/* 
WMS Lucrasoft Javascript Standaard functies
*/

function makeHttpRequest(url){ 
    if (window.XMLHttpRequest) { // Mozilla, Safari,... 
        http_request = new XMLHttpRequest(); 
        if (http_request.overrideMimeType) { 
            http_request.overrideMimeType('text/xml'); 
    } 
        } else if (window.ActiveXObject) { // IE 
            try { 
                http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
            } catch (e) { 
                try { 
                    http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
                } catch (e) {} 
            } 
        } 
    if (!http_request) { 
        alert('Helaas, jouw browser is te oud om deze pagina te kunnen laden.'); 
        return false; 
    } 
        http_request.open('GET', url, true); 
        http_request.send(null); 
}

function getField(fldName, strType) {
  var arrCheck = new Array;
  var blnFound = false;
  arrObjects = document.getElementsByTagName(strType.toUpperCase());

  for (i=0; i<arrObjects.length; i++) {
    if (arrObjects[i].id.split("_")[arrObjects[i].id.split("_").length - 1] == fldName) {
      blnFound = true;
      break;
    }
  }
	
  return arrObjects[i];
}

function PopUp(strPath) {
    window.open(strPath, 'WMSPOPUP', 'status=yes,resizable=yes,width=1024,height=764');
}

function MailTo(value) {
   if (value) {
		 var strTemp='';
		 for (i = value.length-1; i>=0; i--){
			 strTemp+=value.charAt(i);
		 }
		 strTemp = strTemp.split('|').join('@');
		 document.write('<a class="maillink" href=mailto:' + strTemp + '>' + strTemp + '</a>');
	 }
}

function tbFocus(tb) {
    if ((tb.value == 'Vul hier uw zoekopdracht in ...') || (tb.value == 'Vul hier uw e-mailadres in ...') || (tb.value == 'Uw e-mailadres')) {
        tb.value = '';
    }
}

function tbBlur(tb) {
    if (tb.value == '') {
        tb.value = 'Vul hier uw zoekopdracht in ...';
    }
}

function tbEmailBlur(tb) {
    if (tb.value == '') {
        tb.value = 'Vul hier uw e-mailadres in ...';
    }
}

function tbEmailSmlBlur(tb) {
    if (tb.value == '') {
        tb.value = 'Uw e-mailadres';
    }
}

var selectedMenu = '';

function selectMenu(link) {
    var menuTxt = link.innerHTML;
    var container = link.parentNode;

    if (container != null && selectedMenu != link.innerHTML) {
        selectedMenu = link.innerHTML;
        var pos = container.innerHTML.indexOf("<table cellp");

        if (pos == -1) {
            unselectMenu(link);
            var htmlStr = "";

            var offsetLeft = '';
            //offsetLeft = link.offsetLeft - link.parentNode.offsetLeft;
            offsetLeft = link.offsetLeft;

            //        if (link.parentElement.offsetLeft) {
            //            offsetLeft = link.parentElement.offsetLeft;
            //        } else {
            //            offsetLeft = link.offsetLeft;
            //        }

            htmlStr += "<table cellpadding='0' id='tblBorder' cellspacing='0' class='tblHover' style='width: " + link.clientWidth + "px; margin-left: " + offsetLeft + "px;'><tr>";
            htmlStr += "<td align='left' valign='top' width='5'><img src='./design/hoverLeft.jpg' alt='Menu hover' title='Menu hover image' border='0' /></td>";
            htmlStr += "<td align='left' valign='top'><img src='./design/hoverCenter.jpg' alt='Menu hover' title='Menu hover image' border='0' width='100%' height='10' /></td>";
            htmlStr += "<td align='left' valign='top' width='5'><img src='./design/hoverRight.jpg' alt='Menu hover' title='Menu hover image' border='0' /></td></tr></table>";
            container.innerHTML += htmlStr;
        }
    }
}


function unselectAll(link) {
    var container = link;
    if (document.getElementById('tblBorder')) {
        if (container) {
            container.removeChild(document.getElementById('tblBorder'));
            selectedMenu = '';
        }
    }
}

function unselectMenu(link) {
    var container = link.parentNode;

    if (document.getElementById('tblBorder')) {
        if (container) {
            container.removeChild(document.getElementById('tblBorder'));
            //selectedMenu = '';
        }
    }
}

var selectedIndex = 1;
var isfadeout = 0;

function showNextItem(direction, img, rotator) {
    var oldIndex = selectedIndex;
    var step = 1;

    if (direction == 1) { // direction 1 = right
        selectedIndex = selectedIndex+1;
        if (selectedIndex > 5) selectedIndex = 1;
    }
    else {
        selectedIndex = selectedIndex - 1;
        step = -1;
        if (selectedIndex < 1) selectedIndex = 5;
    }

    var tempimg = document.getElementById('tempImg');
    tempimg.onload = function () {
        if (isfadeout == 0) {
            var realImage = document.getElementById('imgRotatorBG2');

            realImage.onload = function () {
                $('#imgRotatorBG2').fadeIn(350, function () { // Animation complete. 
                    timerSecLeft = 5;
                });
                $('#imgRotatorBG').fadeOut(350, function () { // Animation complete. 
                    timerSecLeft = 5;
                });
                isfadeout = 1;
                moveMenu(step, img);
            }

            realImage.src = document.getElementById('tempImg').src;
        } else {
            var realImage = document.getElementById('imgRotatorBG');

            realImage.onload = function () {
                $('#imgRotatorBG').fadeIn(350, function () { // Animation complete. 
                    timerSecLeft = 5;
                });
                $('#imgRotatorBG2').fadeOut(350, function () { // Animation complete. 
                    timerSecLeft = 5;
                });
                isfadeout = 0;
                moveMenu(step, img);
            }

            realImage.src = document.getElementById('tempImg').src;
        }

        for (i = 0; i < 6; i++) {
            if ((selectedIndex - 1) != i) {
                $('#rotItem' + i).fadeOut(350, function () { });
            }
        }

        $('#rotItem' + (selectedIndex - 1)).fadeIn(350, function () { // Animation complete. 
        });

        if (rotator != null) {
            // De checkTimer aanroepen pas als de foto geladen is 
            timerSecLeft = 5;
            setTimeout("checkTimer('" + img + "', '" + rotator + "');", 1000);
        }
    }

    tempimg.src = './rotatorImage/' + selectedIndex + '.jpg';
    //tempimg.src = './rotatorImage/' + selectedIndex + '.jpg?t=' + new Date().getTime();
}

var isChanging = 0;

function setRotatorIndex(index, img) {
    if (isChanging == 0) {
        isChanging = 1;

        var oldIndex = selectedIndex;
        var newIndex = index;

        if (oldIndex > newIndex) { moveMenu((newIndex - (oldIndex - 1)), img); } else { moveMenu((newIndex - oldIndex) + 1, img); }

        // BGimage
        var tempimg = document.getElementById('tempImg');
        tempimg.onload = function () {
            if (isfadeout == 0) {
                var realImage = document.getElementById('imgRotatorBG2');

                realImage.onload = function () {
                    $('#imgRotatorBG2').fadeIn(350, function () { // Animation complete. 
                        // var oRotator = $find(rotator).set_currentItemIndex(index);
                        selectedIndex = (currentMenuPosition + 1);
                        isChanging = 0;
                    });
                    $('#imgRotatorBG').fadeOut(350, function () { // Animation complete. 
                        isChanging = 0;
                    });

                    isfadeout = 1;
                }

                realImage.src = document.getElementById('tempImg').src;
            } else {
                var realImage = document.getElementById('imgRotatorBG');

                realImage.onload = function () {
                    $('#imgRotatorBG').fadeIn(350, function () { // Animation complete. 
                        // $find(rotator).set_currentItemIndex(index);
                        selectedIndex = (currentMenuPosition + 1);
                        isChanging = 0;
                    });
                    $('#imgRotatorBG2').fadeOut(350, function () { // Animation complete. 
                        isChanging = 0;
                    });

                    isfadeout = 0;
                }

                realImage.src = document.getElementById('tempImg').src;
            }

            $('#rotItem' + (oldIndex - 1)).fadeOut(350, function () { // Animation complete. 
                isChanging = 0;
            });

            $('#rotItem' + (newIndex)).fadeIn(350, function () { // Animation complete. 
                isChanging = 0;
            });
        }


        tempimg.src = './rotatorImage/' + (currentMenuPosition + 1) + '.jpg';
        //tempimg.src = './rotatorImage/' + (currentMenuPosition + 1) + '.jpg?t=' + new Date().getTime();
    }
}

var currentMenuPosition = 0;
var maxMenuItems = 4; // array items for index so maxMenuItems = items-1

function moveMenu(move, img) {
    setStyleNormal(currentMenuPosition);
    timerSecLeft = 5; // reset timer

    if ((currentMenuPosition + move) < 0) {
        document.getElementById(img).style.marginLeft = (maxMenuItems * document.getElementById(img).width) + 'px';
        currentMenuPosition = maxMenuItems;
        setStyleSelected(currentMenuPosition);
    } else if ((currentMenuPosition + move) > maxMenuItems) {
        currentMenuPosition = (parseInt(currentMenuPosition) + parseInt(move)) - 5;
        setStyleSelected(currentMenuPosition);
        document.getElementById(img).style.marginLeft = 0 + 'px';
    } else if (((currentMenuPosition + move) <= maxMenuItems) && (currentMenuPosition + move) >= 0) {
        currentMenuPosition = parseInt(currentMenuPosition) + parseInt(move);
        setStyleSelected(currentMenuPosition);
        document.getElementById(img).style.marginLeft = ((currentMenuPosition = currentMenuPosition) * document.getElementById(img).width) + 'px';
    }
}

function preloadImages() {
    // counter
    var i = 0;
    // create object
    imageObj = new Image();
    // set image list
    images = new Array();
    images[0] = "./rotatorImage/1.jpg";
    images[1] = "./rotatorImage/2.jpg";
    images[2] = "./rotatorImage/3.jpg";
    images[3] = "./rotatorImage/4.jpg";
    images[4] = "./rotatorImage/5.jpg";
    // start preloading
    for (i = 0; i <= 4; i++) {
        imageObj.src = images[i];
        imageObj.onLoad = imagesLoaded(imageObj);
    }
}

function imagesLoaded(img) {
    //alert(img.src);
}

var lastClick = null;
function ToggleShowNextItem(direction, img, rotator) {

    if (lastClick == null || lastClick < substractSecNow(1)) {
        showNextItem(direction, img, rotator);
        lastClick = new Date();
    }
    
}

function substractSecNow(s) {
    var d = new Date();
    return new Date(d.valueOf()-s*1000);
}



function setStyleNormal(currentMenuPosition) {
    if (document.getElementById('menuItem' + parseInt(currentMenuPosition))) {
        document.getElementById('menuItem' + parseInt(currentMenuPosition)).setAttribute("class", "aHomeBannerMenu");
    }
}

function setStyleSelected(currentMenuPosition) {
    if(document.getElementById('menuItem' + parseInt(currentMenuPosition))){
        document.getElementById('menuItem' + parseInt(currentMenuPosition)).setAttribute("class", "aHomeBannerMenuSelected");
    }
}

var timerSecLeft = 7;
function startrotTimer(img, rotator) {
    setTimeout("checkTimer('" + img + "', '" + rotator  + "');", 1000);
}

function checkTimer(img, rotator) {
    timerSecLeft = (timerSecLeft - 1);

    if (timerSecLeft == 0) {
        showNextItem(1, img, rotator); // 1=right
        //timerSecLeft = 5;
    } else {
        setTimeout("checkTimer('" + img + "', '" + rotator + "');", 1000);
    }
}

function doHome() {
    if (document.getElementById('pnBannerBreadcrum')) document.getElementById('pnBannerBreadcrum').style.display = 'none';
}


function changeImg(inImg, inSrc) {
    inImg.src = inSrc;
}

function checkClick(hf, f) {
    var hfb = document.getElementById(hf);


    if (hfb.value.indexOf(f) == -1) {
        // add
        hfb.value += f + ',';
    } else {
        // remove
        hfb.value = hfb.value.replace(f + ',', '');
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

var isclicked = '';
function isButtonClicked() {
    if (isclicked == '') {
        this.disabled = 'true';
        return true;
    } else {
        return false;
    }
}
