var msie;
var urlRB;
var currentTab;
var siteroot = 'http://www.dispuutradix.nl/';
fotoboek_checked = false;
var shoutbox_loading;
var shouttime;
function preLoader() {
    images = new Array();
    var i = 0;
    /* tabs aan linkerkant */
    images[i++] = 'buttons/home_out.png';
    images[i++] = 'buttons/agenda_out.png';
    images[i++] = 'buttons/fotos_out.png';
    images[i++] = 'buttons/historie_out.png';
    images[i++] = 'buttons/snorum_out.png';
    images[i++] = 'buttons/home_hover.png';
    images[i++] = 'buttons/agenda_hover.png';
    images[i++] = 'buttons/fotos_hover.png';
    images[i++] = 'buttons/historie_hover.png';
    images[i++] = 'buttons/snorum_hover.png';
    images[i++] = 'tabs/tab1.png';
    images[i++] = 'tabs/tab2.png';
    images[i++] = 'tabs/tab3.png';
    images[i++] = 'tabs/tab4.png';
    images[i++] = 'tabs/tab5.png';
    
    /* Navigatie door items heen e.d. */
    images[i++] = 'buttons/back.png';
    images[i++] = 'buttons/backbutton_over.png';
    images[i++] = 'buttons/arrow_left.png';
    images[i++] = 'buttons/arrow_left_hover.png';
    images[i++] = 'buttons/arrow_right.png';
    images[i++] = 'buttons/arrow_right_hover.png';
    images[i++] = 'buttons/arrow_replacement.png';
    images[i++] = 'buttons/magn_glass.png';
    images[i++] = 'buttons/magn_glass_hover.png';
    images[i++] = 'buttons/pencil.png';
    images[i++] = 'buttons/pencil_hover.png';
    images[i++] = 'buttons/pencil_replacement.png';
    images[i++] = 'buttons/recycle.png';
    images[i++] = 'buttons/recycle_hover.png';
    images[i++] = 'buttons/recycle_replacement.png';
    images[i++] = 'buttons/recycle_hover.png';
    images[i++] = 'buttons/recycle_replacement.png';
    
    /* Backgrounds */
    images[i++] = 'radixhoog_bg.png';
    images[i++] = 'radixonly_guestbook_ro_bg.png';
    images[i++] = 'radixonly_ro_bg.png';
    images[i++] = 'albums_bg.png';
    images[i++] = 'foto_rb_bg.png';
    images[i++] = 'fotos_large_bg.png';
    images[i++] = 'historie_bg.png';
    images[i++] = 'overzicht_bg.png';
    images[i++] = 'smoelen_bg.png';
    images[i++] = 'activiteiten_home_bg.png';
    images[i++] = 'radixianen_bg.png';
    images[i++] = 'radixianen_nogb_bg.png';
    images[i++] = 'shoutbox_bg_large.png';
    images[i++] = 'shoutbox_bg_rt.png';
    
    objImage = new Array();
    for(i in images) {
        objImage[i] = new Image();
        objImage[i].src = siteroot + 'images/' + images[i];
    }
}


function checkFields(fieldIdOne, fieldIdTwo, msg) {
    value1 = document.getElementById(fieldIdOne).value;
    value2 = document.getElementById(fieldIdTwo).value;
    if (value1 != value2) {
        alert(msg);
        return false;
    }
    return true;
}

function loadTab(tab) {
    if (currentTab == 'home' || currentTab == 'historie') {
        tinyMCE.execCommand('mceRemoveControl', false, 'inhoud');
    }
    var newTab;
    var newLeft;
    var urlLeft;
    var urlRightTop;
    var newRT;
    var newRB = document.getElementById('right_bottom').className;
    var afterLeft = null;
    currentTab = tab;
    switch(tab) {
        case 'home':
            newTab = 'tab1';
            newLeft = 'radixhoog';
            urlLeft = 'loadText.php?name=home';
            newRT   = 'activhome';
            urlRightTop = 'activiteiten.php?n=5';
            urlRB = 'r_gegevens.php';
            newRB = (newRB == 'radixonlyhome' || newRB == 'members_gb')
                        ? 'radixonlyhome'
                        : 'radixonly_nogb';
            break;
        case 'agenda':
            newTab = 'tab2';
            newLeft = 'agenda';
            urlLeft = 'loadAgenda.php?f=list';
            newRT   = 'activhome';
            urlRightTop = 'detailActiviteit.php?d=next';
            urlRB = 'r_agenda.php';
            newRB = (newRB == 'radixonlyhome' || newRB == 'members_gb')
                        ? 'radixonlyhome'
                        : 'radixonly_nogb';
            break;
        case 'historie':
            newTab = 'tab3';
            newLeft = 'historie';
            urlLeft = 'loadText.php?name=historie';
            newRT   = 'smoelenboek';
            urlRightTop = 'smoelenboek.php';
            urlRB = 'r_radixianen.php';
            newRB = (newRB == 'radixonlyhome' || newRB == 'members_gb')
                        ? 'members_gb'
                        : 'members_nogb';
            break;
        case 'fotos':
            newTab = 'tab4';
            newLeft = 'albums';
            urlLeft = 'loadAlbums.php?l=lefttext';
            newRT   = 'foto_detail';
            urlRightTop = 'detailFoto.php?d=newest';
            urlRB = 'r_fotos.php';
            newRB = (newRB == 'radixonlyhome' || newRB == 'members_gb')
                        ? 'radixonlyhome'
                        : 'radixonly_nogb';
            break;
        case 'snorum':
            newTab = 'tab5';
            newLeft = 'shoutbox_large';
            urlLeft = 'loadShoutbox.php';
            newRT   = 'shoutbox_shout';
            urlRightTop = 'createShout.php';
            urlRB = 'snorum.php';
            newRB = (newRB == 'radixonlyhome' || newRB == 'members_gb')
                    ? 'radixonlyhome'
                    : 'radixonly_nogb';
            afterLeft = loadShoutBox;
            break;
    }
    document.getElementById('tabs').className = newTab;
    document.getElementById('large_left').className = newLeft;
    ajaxDivUrl(urlLeft, document.getElementById('lefttext'), true, afterLeft);
    document.getElementById('right_top').className = newRT;
    document.getElementById('right_bottom').className = newRB;
    ajaxDivUrl(urlRightTop, document.getElementById('rttext'), true, null);
    ajaxDivUrl(urlRB, document.getElementById('rbtext'), true, null);
}
function loadShoutBox() {

    if (currentTab == 'snorum') {
        if (!shoutbox_loading
            && (document.getElementById('shoutDate') == null
            || document.getElementById('shoutDate').innerHTML != shouttime)) {
            ajaxDivUrl('loadShoutbox.php', document.getElementById('lefttext'), false, loadShoutBox);
            shoutbox_loading = true;
        } else {
            shouttime = document.getElementById('shoutDate').innerHTML;
//            alert ("New shouttime! : " + shouttime);
            setTimeout(loadShoutBoxDate, 5000);
            shoutbox_loading = false;
        }
    }
}
function loadShoutBoxDate() {
    var element = document.getElementById('shoutDate');
    if (element != null)
        ajaxDivUrl('loadShoutboxDate.php', element, false,  loadShoutBox);
}

function toSubmit() {
    document.getElementById('loginform').action = 'do_login.php?url=' + encodeURIComponent(urlRB);
    document.getElementById('loginform').submit();
    //  return true;
}
function dblBlend(idOne, idTwo, newImg) {
    var divOne = document.getElementById(idOne);
    var divTwo = document.getElementById(idTwo);
    var crossFadeDuration = 1;
    var preLoad = new Image();
    preLoad.src = newImg;
    msie = false;// (window.navigator.userAgent.indexOf ( "MSIE " ) > 0) ? true : false;
    if (msie) {
        if (document.all){
            // object.style.filter="blendTrans(duration=5)";
            divOne.style.filter="blendTrans(duration=0.1)";
            divOne.filters.blendTrans.Apply();
            divOne.style.backgroundImage = "url('" + preLoad.src + "')";
        }
        if (document.all){
            divOne.filters.blendTrans.Play();
        }
    } else {
        filterOne = divOne.style.opacity * 100;
        if (filterOne != 100 && filterOne != "100") {
            divOne.style.backgroundImage = "url('" + preLoad.src + "')";
            opacity(idOne, 100, 100, null);
        } else {
            divTwo.style.backgroundImage = "url('" + preLoad.src + "')";
            opacity(idOne, 0, 100, null);
        }
    }
}
function opacity(id, opacEnd, msec, nextFunc) {
    var opacStart = document.getElementById(id).style.opacity * 100;
    var speed;
    var timer = 0;
    if (opacStart < opacEnd) {
        speed = Math.round(msec / (opacEnd - opacStart));
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ", '" + id + "');", (timer++ * speed));
        }
        if (nextFunc != null) 
            setTimeout(nextFunc, timer++ * speed);
    } else {
        speed = Math.round(msec / (opacStart - opacEnd));
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ", '" + id + "');", (timer++ * speed));
        }  
        if (nextFunc != null)
            setTimeout(nextFunc, timer++ * speed);
    }
}
function changeOpac(opacity, id) {
    var div = document.getElementById(id);
    div.style.opacity = (opacity / 100);
    //div.innerHTML = opacity;
}

var tabsBar;
function homeLoad() {
    preLoader();
    tabsBar = document.getElementById('tabs');
    ajaxImgUrl('fotorandom.php', document.getElementById('fotodivfront'), false, requestNextPhoto);
    loadTab('home');
}

function ajaxUrl(url, nextFunction) {                                                   
    var httpObj = false;
    httpObj = window.XMLHttpRequest ? new window.XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("MSXML2.XMLHTTP"): null);
    httpObj.onreadystatechange = function() {
        if (httpObj.readyState == 4) {
            if (nextFunction != null)
                nextFunction();
            return;
        }
    };
    httpObj.open('GET', siteroot + 'functions/' + url, true);          
    httpObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');                                
    httpObj.send(null); 
}

function ajaxDivUrl(url, element, loadImg, nextFunction) {                                                   
    if (loadImg == true)
        element.innerHTML = '<div align="center"><img src="images/loader.gif"></div>';

    var httpObj = false;
    httpObj = window.XMLHttpRequest ? new window.XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("MSXML2.XMLHTTP"): null);
    httpObj.onreadystatechange = function() {
        if (httpObj.readyState == 4) {
            element.innerHTML = httpObj.responseText;
            if (nextFunction != null)
                nextFunction();
            return;
        }
    };
    httpObj.open('GET', siteroot + 'functions/' + url, true);          
    httpObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');                                
    httpObj.send(null); 
}

    function ajaxImgUrl(url, element, loadImg, nextFunction) {                                                   
        if (loadImg == true)
            element.style.backgroundImage = 'url(images/loading.gif)';

        var httpObj = false;
        httpObj = window.XMLHttpRequest ? new window.XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("MSXML2.XMLHTTP"): null);
        httpObj.onreadystatechange = function() {
            if (httpObj.readyState == 4) {
                element.style.backgroundImage = 'url(\'' + httpObj.responseText + '\')';
                        if (nextFunction != null)
                        nextFunction();
                        return;
                        }
                        };
                        httpObj.open('GET', siteroot + 'functions/' + url, true);          
                        httpObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');                                
                        httpObj.send(null); 
                        }

                        // Set slideShowSpeed (milliseconds)
                        var slideShowSpeed = 6000
                        var crossFadeDuration = 6
                        var t;
                        function runSlideShow(){
                        if (msie) {
                        if (document.all){
                        document.getElementById('fotodivfront').style.filter="blendTrans(duration=crossFadeDuration)"
                        document.getElementById('fotodivfront').filters.blendTrans.Apply()      
                        }
                        ajaxImgUrl('fotorandom.php', document.getElementById('fotodivfront'), false, doIECrossfade);
                        } else {
                            var opacity = document.getElementById('fotodivfront').style.opacity * 100; 
                            if (opacity != 100 && opacity != "100") {
                                ajaxImgUrl('fotorandom.php', document.getElementById('fotodivfront'), false, showFrontPhotoDiv);
                            } else {
                                ajaxImgUrl('fotorandom.php', document.getElementById('fotodivback'), false, showBackPhotoDiv);
                            }
                        }
                        }
function showFrontPhotoDiv() {
    document.getElementById('fotodivfront').style.zIndex = 3;
    document.getElementById('fotodivback').style.zIndex = 2;
    opacity('fotodivfront', 100, 500, "opacity('fotodivback', 0, 0)");
    requestNextPhoto();
}
function showBackPhotoDiv() {
    document.getElementById('fotodivfront').style.zIndex = 2;
    document.getElementById('fotodivback').style.zIndex = 3;
    opacity('fotodivback', 100, 500, "opacity('fotodivfront', 0, 0)");
    requestNextPhoto();
}
    function doIECrossfade() {
        if (document.all) 
            document.getElementById('fotodivfront').filters.blendTrans.Play()
                requestNextPhoto();
    }
function requestNextPhoto() {
    t = setTimeout('runSlideShow()', slideShowSpeed)
}
function checkDatum(yearelt, monthelt, dayelt) {
    var year    = document.getElementById( yearelt ).options[document.getElementById( yearelt ).selectedIndex].value;
    var month   = document.getElementById( monthelt ).options[document.getElementById( monthelt ).selectedIndex].value;
    var day     = document.getElementById( dayelt ).options[document.getElementById( dayelt ).selectedIndex].value;
    if (day > 30 && (month == '4' || month == '6' || month == '9' || month == '11')) {
        document.getElementById( dayelt ).selectedIndex--;
    } else if (month == '2') {
        while (day > daysInFebruary(year)) {
            document.getElementById( dayelt ).selectedIndex--;
            day = document.getElementById( dayelt ).options[document.getElementById( dayelt ).selectedIndex].value;
        }
    }
}

function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function loadCurrentTab() {
    loadTab(currentTab);
}
function verwijderAgendaItem(id) {
    if (confirm("Weet je zeker dat je dit item wilt verwijderen?")) {
        ajaxUrl("verwijderAgendaItem.php?i=" + id, loadCurrentTab);
    }
}
function verwijderAlbum(id) {
    if (confirm("Weet je zeker dat je dit album wilt verwijderen?")) {
        ajaxUrl("verwijderAlbum.php?i=" + id, loadCurrentTab);
    }
}
function turnTinyOn() {
    tinyMCE.execCommand('mceAddControl', false, 'inhoud');
}
function toggleEditor(id) {
    if (!tinyMCE.get(id))
        tinyMCE.execCommand('mceAddControl', false, id);
    else
        tinyMCE.execCommand('mceRemoveControl', false, id);
}
function geefFotoboek(element) {
    if (!fotoboek_checked) {
        ajaxDivUrl('geefJaarboekLink.php', element, false, null);
        fotoboek_checked = true;
    }
}

/**
* Insert text at position
*/
function insert_text(text, spaces, elementName)
{
	var textarea = document.getElementById(elementName);

	if (spaces) 
	{
		text = ' ' + text + ' ';
	}
	
	if (!isNaN(textarea.selectionStart))
	{
		var sel_start = textarea.selectionStart;
		var sel_end = textarea.selectionEnd;

		mozWrap(textarea, text, '')
		textarea.selectionStart = sel_start + text.length;
		textarea.selectionEnd = sel_end + text.length;
	}
	else if (textarea.createTextRange && textarea.caretPos)
	{
		if (baseHeight != textarea.caretPos.boundingHeight) 
		{
			textarea.focus();
			storeCaret(textarea);
		}

		var caret_pos = textarea.caretPos;
		caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
	}
	else
	{
		textarea.value = textarea.value + text;
	}
	textarea.focus();
}
/**
* From http://www.massless.org/mozedit/
*/
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	var scrollTop = txtarea.scrollTop;

	if (selEnd == 1 || selEnd == 2) 
	{
		selEnd = selLength;
	}

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);

	txtarea.value = s1 + open + s2 + close + s3;
	txtarea.selectionStart = selEnd + open.length + close.length;
	txtarea.selectionEnd = txtarea.selectionStart;
	txtarea.focus();
	txtarea.scrollTop = scrollTop;

	return;
}

/**
* Insert at Caret position. Code from
* http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
*/
function storeCaret(textEl)
{
	if (textEl.createTextRange)
	{
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}

