var active_page = 1;

function switchPage(page_radio)
{
	var pagenr = page_radio.value;

	if (pagenr != active_page)
	{
		var tekst = tinyMCE.get('tekst').getContent();

		// indien nodig een veld aanmaken om de tekst van het veld naartoe te verplaatsen
		if ($('tekst_'+ active_page) == null)
			$(page_radio.form).insert('<input type="hidden" name="tekst_'+ active_page +'" id="tekst_'+ active_page +'" value="" />');

		if ($('tekst_'+ pagenr) == null)
			$(page_radio.form).insert('<input type="hidden" name="tekst_'+ pagenr +'" id="tekst_'+ pagenr +'" value="" />');

		// de huidige tekst naar het veld schrijven van de pagina waar het bij hoort
		$('tekst_'+ active_page).value = tekst;

		// de tekst van de geselecteerde pagina in tinyMCE plaatsen
		tinyMCE.get('tekst').setContent($F('tekst_'+ pagenr));

		// de geselecteerde pagina op actief zetten
		active_page = pagenr;
	}
}

function addPage(button, page_name)
{
	var pagenr = 1;

	// het eerstvolgende paginanummer opzoeken
	while ($('titel_'+ pagenr) != null)
		pagenr++;

	$('cms_news_pages_index').firstDescendant().insert(
		'<tr>'+
		'	<td><input type="radio" name="page" value="'+ pagenr +'" id="page'+ pagenr +'" onclick="switchPage(this);" /><label for="page'+ pagenr +'">'+ page_name +' '+ pagenr +'</label></td>'+
		'	<td><input type="text" name="titel_'+ pagenr +'" id="titel_'+ pagenr +'" value="" size="80"></td>'+
        '</tr>'
	);

	$(button.form).insert('<input type="hidden" name="tekst_'+ pagenr +'" id="tekst_'+ pagenr +'" value="" />');
}

function toggleMultipage(checkbox)
{
	if (checkbox.checked)
		$('cms_news_pages_index').show();
	else
		$('cms_news_pages_index').hide();
}


function RemoveFromRelated(id) {
    tr = document.getElementById('related_item_'+ id);
    while (tr.hasChildNodes()) {
        tr.removeChild(tr.firstChild);
    }
}

function AddToRelated(id, titel) {
    tr = window.opener.document.createElement('tr');
    tr.setAttribute('id', 'related_item_'+ id);

    // hidden input element met het id
    i = window.opener.document.createElement('input');
    i.setAttribute('type', 'hidden');
    i.setAttribute('name', 'related[]');
    i.setAttribute('value', id);

    // vakje voor de titel van het bericht
    td = window.opener.document.createElement('td');
    td.appendChild(i);
    td.appendChild(window.opener.document.createTextNode(titel));
    tr.appendChild(td);

    // vakje voor de 'verwijderen' link
    a = window.opener.document.createElement('a');
    a.setAttribute('href', 'javascript:void(RemoveFromRelated('+ id +'))');
    a.setAttribute('className', 'admin'); // voor IE
    a.setAttribute('class', 'admin'); // voor Moz
    a.appendChild(window.opener.document.createTextNode('verwijderen'));

    td = window.opener.document.createElement('td');
    td.appendChild(window.opener.document.createTextNode('['));
    td.appendChild(a);
    td.appendChild(window.opener.document.createTextNode(']'));
    td.setAttribute('align', 'right');
    tr.appendChild(td);

    tabletje = window.opener.document.getElementById('related_items');
    tabletje.appendChild(tr);
}

function showAlbums(url) {
    var width = 450;
    var height = 400;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url, 'popup_albums', attributes);
}

function showBanners(url) {
    var width = 750;
    var height = 600;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url, 'popup_banners', attributes);
}

function showPodcasting(url) {
    var width = 750;
    var height = 600;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url, 'popup_podcasting', attributes);
}

function showUploader(url) {
    var width = 450;
    var height = 400;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url, 'popup_albums_uploader', attributes);
}

function showPoll(url) {
    var width = 650;
    var height = 700;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url, 'popup_poll', attributes);
}

function showEvent(url) {
    var width = 650;
    var height = 700;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url, 'popup_event', attributes);
}

function showBlogroll(url) {
    var width = 650;
    var height = 700;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url, 'popup_blogroll', attributes);
}

function showReport(url) {
    var width = 650;
    var height = 700;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url, 'popup_report', attributes);
}

function showLayer(layer_id, at_mouse) {
    layer = getLayer(layer_id);

    if (layer.display=='none') {
        layer.display = 'block';
        layer.left = mouse_x +'5px';
        layer.top = mouse_y +'5px';
    } else {
        layer.display = 'none';
    }
}

function popup() {
    if (nav || dom) {
        kaderlinks.visibility   = 'visible';
        kaderrechts.visibility  = 'visible';
        kaderboven.visibility   = 'visible';
        kaderbeneden.visibility = 'visible';
    }
    else if (iex) {
        kaderlinks.display   = '';
        kaderrechts.display  = '';
        kaderboven.display   = '';
        kaderbeneden.display = '';
    }
}

function kill(){
    if (nav || dom) {
        kaderlinks.visibility   = 'hidden';
        kaderrechts.visibility  = 'hidden';
        kaderboven.visibility   = 'hidden';
        kaderbeneden.visibility = 'hidden';
    }
    else if (iex) {
        kaderlinks.display   = 'none';
        kaderrechts.display  = 'none';
        kaderboven.display   = 'none';
        kaderbeneden.display = 'none';
    }
}

function moveKader(e) {
    if (nav || dom) {
        mouse_x = e.pageX;
        mouse_y = e.pageY;
    } else {
        mouse_x = event.clientX + document.documentElement.scrollLeft;
        mouse_y = event.clientY + document.documentElement.scrollTop;
    }

    x = mouse_x;
    y = mouse_y;

    //alert(x +' x '+ y);

    // plaats van kaderdelen bepalen t.o.v muiscursor
    klinks_x   = x - breedte/2 - kaderbreedte;
    klinks_y   = y - hoogte/2;

    // eventuele modificatie van de kader-coordinaten bepalen want de binnenranden van het kader mogen niet van de foto af
    if (klinks_x + kaderbreedte < leftmargin) { // te ver naar links
        klinks_x = leftmargin - kaderbreedte;
    }
    else if (klinks_x + kaderbreedte + breedte > leftmargin + plaatje_breedte) { // te ver naar rechts
        klinks_x = leftmargin + plaatje_breedte - breedte - kaderbreedte;
    }

    if (klinks_y < topmargin) { // te ver omhoog
        klinks_y = topmargin;
    }
    else if (klinks_y + hoogte > topmargin + plaatje_hoogte) { // te ver omlaag
        klinks_y = topmargin + plaatje_hoogte - hoogte;
    }

    krechts_x  = klinks_x + breedte + kaderbreedte;
    krechts_y  = klinks_y;
    kboven_x   = klinks_x;
    kboven_y   = klinks_y - kaderbreedte;
    kbeneden_x = klinks_x;
    kbeneden_y = klinks_y + hoogte;

    kaderlinks.left   = klinks_x +'px';
    kaderlinks.top    = klinks_y +'px';
    kaderrechts.left  = krechts_x +'px';
    kaderrechts.top   = krechts_y +'px';
    kaderboven.left   = kboven_x +'px';
    kaderboven.top    = kboven_y +'px';
    kaderbeneden.left = kbeneden_x +'px';
    kaderbeneden.top  = kbeneden_y +'px';
}

function showChannels(url) {
    var width = 450;
    var height = 300;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url,'popup_channels',attributes);
}

function showReview(url) {
    var width = 650;
    var height = 450;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url,'popup_reviews',attributes);
}

function showUsers(url) {
    var width = 500;
    var height = 300;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url,'popup_users',attributes);
}

function showTags(url) {
    var width = 640;
    var height = 600;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url,'popup_tags',attributes);
}

function showArticles(url) {
    var width = 650;
    var height = 700;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url,'popup_articles',attributes);
}

function showVideos(url) {
    var width = 650;
    var height = 660;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url,'popup_articles',attributes);
}

function searchRelated(url) {
    var width = 450;
    var height = 300;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url,'popup_related',attributes);
}

function editReactie(url) {
    var width = 650;
    var height = 450;
    var attributes = 'width='+ width +', height='+ height +', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open(url, 'popup_reactie', attributes);
}

function openPreview(content) {
    window.open('/cms_preview.php?content='+ escape(content), 'preview', 'width=400, height=300, dependent=yes, alwaysRaised=yes');
}

function showDocs(section)
{
	switch (section) {
		case 'nieuwsbericht':
			window.open('/docs_nieuwsbericht.php','docs_nieuws','width=400,height=300,scrollbars=yes,dependent=yes, alwaysRaised=yes');
			break;
	}
}

function showLinks() {
    var width = 450;
    var height = 300;
    var attributes = 'width='+ width +', height='+ height +//', top='+ (mouse_y-10) +', left='+ (mouse_x-width-5) +
                     ', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open('/cms_links_categorieen.php','popup_links',attributes);
}

function showLinksPage() {
    var width = 450;
    var height = 300;
    var attributes = 'width='+ width +', height='+ height +//', top='+ (mouse_y-10) +', left='+ (mouse_x-width-5) +
                     ', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open('/cms_linkspage_categorieen.php','popup_linkspage',attributes);
}

function showSyndication() {
    var width = 450;
    var height = 300;
    var attributes = 'width='+ width +', height='+ height +//', top='+ (mouse_y-10) +', left='+ (mouse_x-width-5) +
                     ', scrollbars=yes, dependent=yes, alwaysRaised=yes, status=yes';

    window.open('/cms_rss_feeds.php','popup_syndication',attributes);
}

