﻿/* Sfeerbeelden */
var arrImgBig = new Array();
var arrImgThumb = new Array();
var arrTxt = new Array();
var imgCache = [];
var prvIndex = 0;
var timer = "";
var arrImgThumbID = new Array();

function init() {
    $(".sfeerbeeldspace > img").each(function(i, e) {
        arrImgBig.push(e);

        var cacheImg = new Image(50, 50);
        cacheImg.src = e.src;
        imgCache.push(cacheImg);

    });
    $("#thumbspace > img").each(function(i, e) {
        arrImgThumb.push(e);
        arrImgThumbID.push(e.id);

        var cacheImg = new Image(50, 50);
        cacheImg.src = e.src;
        imgCache.push(cacheImg);


    });
    $("#reftextspace > span").each(function(i, e) {
        arrTxt.push(e);
    });
    maakInitZichtbaar();
}

function maakInitZichtbaar() {
    //z-index
    $(arrImgBig[arrImgBig.length - 1]).css("z-index", "7");
    $(arrImgBig[arrImgBig.length - 1]).css("display", "block");

    //show image
    $(arrImgBig[arrImgBig.length - 1]).fadeIn(700);

    //Show the text
    $(arrTxt[arrImgBig.length - 1]).css("display", "block");

    //huidig zichtbaar element bijhouden
    prvIndex = arrImgBig.length - 1;
    if (arrImgBig.length > 1) { 
        timer = setTimeout(AutoChangeImage, 3000);
    }
}

function maakZichtbaar(obj, id) {
    if (id != prvIndex) {
        clearTimeout(timer);
        
        //z-index
        $(arrImgBig[id]).css("z-index", "7");
        $(arrImgBig[prvIndex]).css("z-index", "6");
        
        //show image
        $(arrImgBig[id]).fadeIn(700);
        $(arrImgBig[prvIndex]).fadeOut(700);

        //Show the text
        arrTxt[id].style.display = "block";
        arrTxt[prvIndex].style.display = "none";

        //switch thumb
        var ThumbPrnt = document.getElementById("thumbspace");
        ThumbPrnt.appendChild(obj, ThumbPrnt.lastChild);
        
        //huidig zichtbaar element bijhouden
        prvIndex = id;
        
        timer = setTimeout(AutoChangeImage, 3000);
    }
}
function AutoChangeImage() {
    var id = prvIndex + 1;
    if (id >= arrImgBig.length){
        id = 0;    
    }
    
    //z-index
    $(arrImgBig[id]).css("z-index", "7");
    $(arrImgBig[prvIndex]).css("z-index", "6");
    
    //show image
    $(arrImgBig[id]).fadeIn(700);
    $(arrImgBig[prvIndex]).fadeOut(700);

    //Show the text
    arrTxt[id].style.display = "block";
    arrTxt[prvIndex].style.display = "none";
    
    //switch thumb
    var ThumbPrnt = document.getElementById("thumbspace");

    var obj = document.getElementById(arrImgThumbID[id]);
    ThumbPrnt.appendChild(obj, ThumbPrnt.lastChild);
    
    
    
        
    //huidig zichtbaar element bijhouden
    prvIndex = id;
    timer = setTimeout(AutoChangeImage, 3000);
}
/*submenu*/
$(document).ready(function() {
    $("#navispace ul li ul").each(function(index, ctl) {
        var li = $(ctl).parent();
        $(ctl).width(1025 - li[0].offsetLeft);

        if (!li.hasClass('selected')) {
            $(li).hover(
                function() {
                    $("#navispace ul li.selected ul").css("display", "none");
                },
                function() {
                    $("#navispace ul li.selected ul").css("display", "block");
                }
            );
        }
    });
});


/* drag div */
function startdrag(t, e) {
    var targ;
    if (!e) e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) {  // defeat Safari bug
        targ = targ.parentNode;
    }
    var tname = targ.tagName;

    if ((tname.toLowerCase() != "input") && (tname.toLowerCase() != "select")) {
        if (e.preventDefault) e.preventDefault();
        e.cancelBubble = true;
        window.document.onmousemoveOld = window.document.onmousemove;
        window.document.onmouseupOld = window.document.onmouseup;
        window.document.onmousemove = dodrag;
        window.document.onmouseup = stopdrag;
        window.document.draged = t;
        t.dragX = e.clientX;
        t.dragY = e.clientY;
        return false;
    }
}

function dodrag(e) {
    if (!e) e = event;
    t = window.document.draged;
    t.style.left = (t.offsetLeft + e.clientX - t.dragX) + "px";
    t.style.top = (t.offsetTop + e.clientY - t.dragY) + "px";
    t.dragX = e.clientX;
    t.dragY = e.clientY;
    return false;
}

function stopdrag() {
    window.document.onmousemove = window.document.onmousemoveOld;
    window.document.onmouseup = window.document.onmouseupOld;

    if (localStorage) {

        var left = parseInt(document.getElementById("beheer").style.left, 10);

        if (left > 0) {
            localStorage.setItem("dragX", left);
        } else {
            localStorage.setItem("dragX", "10");
        }
        var top = parseInt(document.getElementById("beheer").style.top, 10);
        if (top > 0) {
            localStorage.setItem("dragY", top);
        } else {
            localStorage.setItem("dragY", "10");
        }
    }
}

function InitBeheer() {
    if (localStorage) {
        if (localStorage.getItem("dragX") != null) {
            var left = parseInt(localStorage.getItem("dragX"), 10);
            if (left > 0) {
                document.getElementById("beheer").style.left = parseInt(localStorage.getItem("dragX"), 10) + 'px';
            }
        }
        if (localStorage.getItem("dragY") != null) {
            var top = parseInt(localStorage.getItem("dragY"), 10);
            if (top > 0) {
                document.getElementById("beheer").style.top = parseInt(localStorage.getItem("dragY"), 10) + 'px';
            }
        }
    }
}
