function fancyPar(a)
{
	var source = "/images/themes/old/letters/" + a.substring(0,1) + ".png";
	document.write("<img src='" + source + "' class='parfirstletter' /><span class='parrest'>" + a.substring(1))
}

function menuMouseOver(a, l)
{
	a.style.backgroundImage = "url('/images/themes/old/menuitem" + l + "bg.png')";
}

function menuMouseOut(a)
{
	a.style.backgroundImage = '';
}

function confirmdelete(what, where)
{
	var confirmation = confirm("Are you sure you want to delete this " + what + "?");
	if(confirmation)
	{
		var goloc = window.location.href;
		goloc = goloc.substring(0,goloc.indexOf("?"));
		goloc += where;
		window.location = goloc;
	}
}

function stripGet()
{
	var goloc = window.location.href;
		goloc = goloc.substring(0,goloc.indexOf("?"));
		window.location = goloc;
}

function myGetSelection()
{
	var txt = '';
    if(navigator.appName == 'Microsoft Internet Explorer')
    {
    	txt = document.selection.createRange();
		txt = txt.text;
    }
    else
    {
    	txt = window.getSelection();
    }
    return txt;
}

function addQuote()
{
    var txt = myGetSelection();
    $("#a").contents().find("#edit").prepend("<span class='quote'>\""+txt+"\"</span><br /><br />");
    $("#a").contents().find(".quote").css({"font-style":"italic"});
}

function manipulate_text(action) 
{
	var iframe = document.getElementById("a");
	var returnValue = iframe.contentWindow.inBetween(action);
    return returnValue;
}

function emailchange(a)
{
	if(a === true)
	{
		document.getElementById("nomail").innerHTML = "yes";
	}
	else{
		document.getElementById("nomail").innerHTML = "no";
	}
}

function isEmpty(a)
{
	if((a.value.length === 0) || (a.value === null))
	{
		return true;
	}
	else {
		return false;
	}
}

function displayError(a)
{
	document.getElementById('errormsg').innerHTML = a;
	document.getElementById('transBg').style.display = "block";
	document.getElementById('errorWrapper').style.display = "block";
}

function errorBeGone()
{
	document.getElementById("transBg").style.display = "none";
	document.getElementById("errorWrapper").style.display = "none";
}

function checkform(a)
{
	if(isEmpty(a.username))
	{
		displayError("Error #1:<br /> No username was Inserted");
	}
	else if(isEmpty(a.password))
	{
		displayError("Error #2:<br /> No password was Inserted");
	}
	else if(!(a.repassword.value == a.password.value))
	{
		displayError("Error #3:<br /> Passwords don't match");
	}
	else if(!(a.emailaddress.value.match(/.*@.*\.(nl|com|be|de|info)/)))
	{
		displayError("Error #4:<br /> You're e-mailaddress is invalid.<br />Note: only .nl, .com, .be, .de and .info are accepted.");
	}
	else 
	{
		a.submit();
	}
}

function showreply() {
    var hi = $("#replycontainer").height();
    $("#replycontainer").show(500);
    $(".bgdiv").animate({height: $(".bgdiv").height() + hi + "px"});
}

$(function() {
    $(".sidediv").hide(0);
    $(".replycontainer").hide(0);
    $.post("/resources/ajax/getParts.php",{}, function(parts){
        var i=0;
        while(eval("parts.title"+i) && (i<20))
        {
            $("#partselect").append("<option value='"+eval("parts.title"+i)+"'>"+eval("parts.title"+i)+"</option>");
            i++;
        }
    }, "json");

    $.post("/resources/ajax/getChapters.php", {"part":"all"}, function(chapters){
        var i=0;
        while(eval("chapters.title"+i))
        {
            $("#chapterselect").append("<option value='"+eval("chapters.title"+i)+"'>"+eval("chapters.title"+i)+"</option>");
            i++;
        }
    }, "json");

    $("#partselect").change( function(){
        $("#chapterselect option").remove();
        $("#chapterselect").append("<option value='all'>Alle</option>");
        var part = $("#partselect").val();
        $.post("/resources/ajax/getChapters.php", {"part":part}, function(chapters){
            var i=0;
            while(eval("chapters.title"+i))
            {
                $("#chapterselect").append("<option value='"+eval("chapters.title"+i)+"'>"+eval("chapters.title"+i)+"</option>");
                i++;
            }
        }, "json");
    });

    $("#chapterselect").change( function(){
        var chapter = $("#chapterselect").val().replace(/"/g, '&quot;');
        $.post("/resources/ajax/getParagraphs.php", {"chapter":chapter}, function(chapterId){
            window.location = "/reynaert?chapter="+chapterId;
        });
    });

    $(".footnotelink").live("click", function () {
                var linknr = $(this).text();
        $.post("/resources/ajax/getFootnote.php", {"linknr":linknr}, function(note){
            $(".sidediv").fadeIn(800);
            $(".sidediv *").remove();
            $(".sidediv").append(note.content);
            $(".sidediv").append($("<span class=close>Sluiten</span>").bind("click", function(){$('.sidediv').fadeOut(800)}));
            $(".sidediv").css({height: $('.sidediv').height()>$(window).height()?$(window).height():'auto'})
            $(".bgdiv, .inkbottle").animate({paddingRight: "320px"}, 800);
            $("body").animate({backgroundPosition: 0+"px 0px"}, 800);
        }, "json");
    });
    

    $(".content").append("<br style='clear:both;' />")
    $(".bgdiv").height($(".header").outerHeight()+$(".content").outerHeight()+20+"px");
});