/*  ©2006 Copyright HIP-IT www.hip-it.nl                    */
AttachEvent(document, 'keydown', checkEsc, false);
var recentCatBuild = false;

function playYoutube(divje, video, width, height)
{
  youtubeplayer = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '">';
  youtubeplayer = youtubeplayer + '<param name="src" value="http://www.youtube.com/v/'+video+'" />';
  youtubeplayer = youtubeplayer + '<param name="menu" value="false" />';
  youtubeplayer = youtubeplayer + '<param name="wmode" value="transparent" />';
  youtubeplayer = youtubeplayer + '<embed src="http://www.youtube.com/v/'+video+'" menu="false" wmode="transparent" width="' + width + '" height="' + height + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
  writer(divje, youtubeplayer);
}

function getPostform(postid)
{
  if (postid)
  {
    var formstring = 'wijzigpost&postid='+postid;
  }
  else
  {
    var formstring = 'nieuwepost';
  }
  haalData('GET', '/blog.php?nav='+formstring, 'toonPostForm', null);
}

function popCategorie()
{
	haalData('GET', '/blog.php?nav=doecategorie', 'divAlert', null);
}

function wijzigCategorie(newnaam, id)
{
  newnaam = newnaam.replace(/\#+/g, '@');
  haalData('GET', '/blog.php?nav=doecategorie&actie=wijzig&pop=true&catnaam='+newnaam+'&catid='+id, 'buildCat', null);
}

function verwijderCategorie(id)
{
  haalData('GET', '/blog.php?nav=doecategorie&actie=verwijder&pop=true&catid='+id, 'buildCat', null);
}

function nieuweCategorie(catnaam)
{
  if (!catnaam)
  {
    getBuildCategorie();
  }
  else
  {
    catnaam = catnaam.replace(/\#+/g, '@');
    haalData('GET', '/blog.php?nav=doecategorie&actie=nieuw&pop=true&catnaam='+catnaam, 'buildCat', null);
  }
}


function plaatsBericht(nieuwecomment)
{
  if (nieuwecomment.bericht.value.replace(/\s+/g, '').length <= 3)
  {
    alert('Je bericht is te kort');
    nieuwecomment.commentbutton.disabled=false;
    nieuwecomment.commentbutton.value='verstuur';
  }
  else
  {
    comment = nieuwecomment.bericht.value;
    if (comment.length > 3)
    {
      if (comment.length < 5000)
      {
        poststr = 'nav=newcomment&bericht='+comment+'&postid='+nieuwecomment.postid.value+'&blogid='+nieuwecomment.blogid.value
        haalData('POST', '/blog.php', 'toonPost', poststr);
      }
      else
      {
        alert('Je bericht is te lang');
        nieuwecomment.commentbutton.disabled=false;
        nieuwecomment.commentbutton.value='verstuur';
      }
    }
    else
    {
      alert('Je dient eerst een bericht op te geven');
      nieuwecomment.commentbutton.disabled=false;
      nieuwecomment.commentbutton.value='verstuur';
    }
  }
}

function haalBerichten(postid, blogid, totals, pagenumber)
{
  haalData('GET', '/blog.php?nav=getcomments&postid='+postid+'&blogid='+blogid+'&totals='+totals+'&pagenumber='+pagenumber, 'toonPost', null);
}

function verwijderComment(commentid, blogid, postid, totals, pagenumber)
{
  blurPagina();
  if(confirm('Weet je zeker dat je dit bericht wilt verwijderen?'))
  {
    haalData('GET', '/blog.php?nav=delcomments&commentid='+commentid+'&blogid='+blogid+'&postid='+postid+'&totals='+totals+'&pagenumber='+pagenumber, 'toonPost', null);
  }
  else
  {
    focusPagina();
  }
}

function verwijderPost(doverwijder)
{
  blurPagina();
  if(confirm('Weet je zeker dat je dit bericht wilt verwijderen?'))
  {
    window.location = doverwijder;
  }
  else
  {
    focusPagina();
  }
}

// toon postdata
function toonPost(post)
{
  writer('postcomments', post);
  haalBBcodeKnopjes('bericht', 'comment');
  focusPagina();
  setTimeout("renderMedia(true)", 1500);
  ss('newcommentform');
}

function renderMedia(herhaal)
{
  var imgs = document.getElementById('maincontent').getElementsByTagName('img');
  if (document.getElementById('postcontent'))
  {
    var div = document.getElementById('postcontent').style.width.substr(0, 3);
  }

  div = div-12;
  for (var i = 0; i < imgs.length; i++)
  {  
    if (imgs[i].width > div)
    {
      imgs[i].style.width = div+'px';
      imgs[i].style.cursor = 'pointer';
      imgs[i].style.border = 'dotted #FF0000 2px';
      imgs[i].style.margin = '0px 0px 5px 0px';
      imgs[i].title = 'Klik hier voor een groter plaatje';
      imgs[i].onclick = function(){openVenster(this.src, 'foto', 0);}
    }
  }
  
  var vidsobject = document.getElementById('maincontent').getElementsByTagName('object');
  orgvw = 425;
  orgvh = 355;
  if (div >= orgvw)
  {
    neww = orgvw;
    newh = orgvh;
  }
  else
  {
    newwp = Math.floor(100/orgvw*div);
    neww = div;
    newh = Math.floor(orgvh*(newwp/100));
  }
  for (var i = 0; i < vidsobject.length; i++)
  {
    if (!vidsobject[i].classid)
    {
      var filmpje = vidsobject[i].name.substr(4);
      var divje = vidsobject[i].name.substr(0, 4);
      playYoutube('youtube'+divje, filmpje, neww, newh);
    }
  }
  
  if (herhaal == true)
  {
    setTimeout("renderMedia(true)", 4000);
  }
}


// categorie
function getWijzigCategorie(cvalue, id)
{
  getBuildCategorie();
  writer('categorie_'+id, '<div style="padding: 4px 4px 0px 4px" class="alignr"><input type="text" style="width:100%" title="Wijzig naam" id="wijzigcatajax" value="'+cvalue+'" onmouseover="this.focus()" onkeydown="if(event.keyCode == 13)wijzigCategorie(this.value, \''+id+'\')"><br /><input type="button" class="button hand" value="wijzig" onclick="wijzigCategorie(document.getElementById(\'wijzigcatajax\').value, \''+id+'\')"/></div>');
}

function getVerwijderCategorie(id)
{
  getBuildCategorie();
  writer('bewerkcategorie_'+id, '<div class="alignr padding4 tnf" title="Verwijder categorie">Verwijderen? <a href="javascript:verwijderCategorie(\''+id+'\')">Ja</a> / <a href="javascript:getBuildCategorie()">Nee</a></div>');
}

function getNieuweCategorie()
{
  getBuildCategorie();
  writer('categorie_new', '<div style="padding: 5px 5px 0px 5px"><input type="text" style="width:100%" id="nieuwecatajax" title="Nieuwe categorie" onmouseover="this.focus()" onkeydown="if(event.keyCode == 13)nieuweCategorie(this.value)"></div>');
  document.getElementById('catopslaanbutton').value = 'voeg toe';
  document.getElementById('catopslaanbutton').onclick = function(){nieuweCategorie(document.getElementById('nieuwecatajax').value)};
}

function getBuildCategorie()
{
  if (recentCatBuild == false)
  {
    recentCatBuild = document.getElementById('categorie_pop_bits').innerHTML;
  }
  else
  {
    writer('categorie_pop_bits', recentCatBuild);
  }
}

function buildCat(Aanvraag)
{
  writer('categorie_pop_bits', Aanvraag);
  if (!document.getElementById('error'))
  {
    recentCatBuild = Aanvraag;
  }
}

function bewaarCat()
{
  if (recentCatBuild == false)
  {
    sluitDivPop();
  }
  else
  {
    location.reload();
  }
}

// nieuwe post
function checkNieuwePost()
{
  if (document.form_nieuwepost.ntitel.value.replace(/\s+/g, '').length < 2)
  {
    alert('Titel is te kort. Minimaal 2 karakters');
    return false;
  }
  
  if (document.form_nieuwepost.nbericht.value.replace(/\s+/g, '').length < 30)
  {
    alert('Bericht is te kort. Minimaal 30 karakters');
    return false;
  }
  
  var titel = document.form_nieuwepost.ntitel.value;
  var bericht = document.form_nieuwepost.nbericht.value;

  if ((titel.length > 0) && (bericht.length > 0))
  {
    if (titel.length < 2)
    {
      alert('Titel is te kort. Minimaal 2 karakters');
      return false;
    }
    
    if (titel.length > 100)
    {
      alert('Titel is te lang. Maximaal 100 karakters');
      return false;
    }
    
    if (bericht.length < 30)
    {
      alert('Bericht is te kort. Minimaal 30 karakters');
      return false;
    }
    
    if (bericht.length > 5000)
    {
      alert('Bericht is te lang. Maximaal 5000 karakters');
      return false;
    }
  }
  else
  {
    alert('U heeft niet alle verplichte velden meegestuurd');
    return false;    
  }
}

function checkEsc()
{
  if ((event.keyCode == '27') && (document.getElementById('dialog')))
  {
    if(document.getElementById('form_nieuwepost'))
    {
      if ((document.forms['form_nieuwepost'].nbericht.value.length > 0) || (document.forms['form_nieuwepost'].ntitel.value.length > 0) || (document.forms['form_nieuwepost'].plaatje.value.length > 0)) 
      {
        berichtvar = document.forms['form_nieuwepost'].nbericht.value;
        titelvar = document.forms['form_nieuwepost'].ntitel.value;
        if (confirm('Je bericht gaat verloren. Weet je zeker dat je dit venster wilt sluiten.'))
        {
          if (document.forms['form_nieuwepost'].plaatje.value.length > 0)
          {
            sluitDivPop()
          }
        }
        else
        {
          document.forms['form_nieuwepost'].nbericht.value = berichtvar;
          document.forms['form_nieuwepost'].ntitel.value = titelvar;
          document.forms['form_nieuwepost'].nbericht.blur();
          document.forms['form_nieuwepost'].ntitel.blur();
          return false;
        }
      }
    }
    sluitDivPop();
  }
}

function toonPostForm(Aanvraag)
{
  openDivPop(Aanvraag);
  ss('main');
  if (document.getElementById('divformbuttons'))
  {
    haalBBcodeKnopjes('bericht', 'post');
  }
}

function toonPostThumb(wdata)
{
  writer('thumbpreview', '<img src="' + media['static'] + '/blogs/temp/'+wdata+'" class="imgborder" alt="Verkleind plaatje" ondragstart="return false" onselectstart="return false" oncontextmenu="return false" />');
  document.forms['form_nieuwepost'].plaatje.value = wdata;
  document.forms['move_image'].wdata.value = wdata;
}

function haalBBcodeKnopjes(textareatemp, bbtype)
{
  var myTags = new Array();
  myTags[0] = new Array('idubb_strong','strong','VET','[B]','[/B]');
  myTags[1] = new Array('idubb_italic','italic','CURSIEF','[I]','[/I]');
  myTags[2] = new Array('idubb_underline','underline','ONDERSTREPEN','[U]','[/U]');
  myTags[3] = new Array('idubb_quote','quote','CITAAT','[QUOTE]','[/QUOTE]');
  myTags[4] = new Array('idubb_left','left','LINKS UITLIJNEN','[LEFT]','[/LEFT]');
  myTags[5] = new Array('idubb_center','center','CENTREREN','[CENTER]','[/CENTER]');
  myTags[6] = new Array('idubb_right','right','RECHTS UITLIJNEN','[RIGHT]','[/RIGHT]');
  myTags[7] = new Array('idubb_justify','justify','UITVULLEN','[JUSTIFY]','[/JUSTIFY]');
  myTags[8] = new Array('idubb_list','list','OPSOMMING','[LIST]','[/LIST]');
  myTags[9] = new Array('idubb_listnum','listnum','NUMMERING','[LIST=1]','[/LIST]');
  myTags[10] = new Array('idubb_url','url','LINK','[URL]','[/URL]');
  myTags[11] = new Array('idubb_img','img','PLAATJE','[IMG]','[/IMG]');
  myTags[12] = new Array('idubb_youtube','youtube','YOUTUBE VIDEO','[YOUTUBE]','[/YOUTUBE]');
  if (bbtype == 'post')
  {
    myTags[13] = new Array('idubb_more','more','LEES MEER','','\\n\\r[LEESMEER]\\n\\r');
  }
  
  var taglength = myTags.length-1;
  var bbformbuttons = '';
  for (i = 0; i <= taglength; i++)
  {
    bbformbuttons = bbformbuttons + '<input type="button" class="formbutton" id="' + myTags[i][0] + '" onMouseOver="this.style.backgroundColor=\'#FFE3B1\';this.style.border=\'solid 1px #828797\'" onMouseOut="this.style.backgroundColor=\'#FFFFFF\';this.style.border=\'solid 1px #FFFFFF\'" onMouseDown="this.style.backgroundColor=\'#FFB475\';" style="background:url(\'' + media['static'] + '/blogs/img/' + myTags[i][1] + '.gif\') no-repeat" name="' + myTags[i][1] + '" onClick="WriteOption(\'' + myTags[i][0] + '\',\''+textareatemp+'\',\'' + myTags[i][3] + '\',\'' + myTags[i][4] + '\');" title="' + myTags[i][2] + '" />';
  }
  writer('divformbuttons', bbformbuttons);
}

function WriteOption (myButId, myText, tagOpen, tagClose)
{
  myText = document.getElementById(myText);

  mySelection = '';

  if (window.getSelection || document.getSelection)
  {
    mySelection = myText.value.substring(myText.selectionStart, myText.selectionEnd);
  }
  else
  {
    mySelection = iestr = document.selection.createRange().text;
  }
  
  Content = '';
  
  myButSwitch = myButId.substring(6,myButId.length);

  // -- LIST
  if ( myButSwitch == 'list' || myButSwitch == 'listnum' ) {
    Content = tagOpen + "\n";

		do {
		  tmpval = prompt("Geef een punt op voor de opsomming:","");
		  if ( tmpval != undefined && tmpval != '') {
		    Content += "[*]" + tmpval + "\n";
		  }
		} while (tmpval != null && tmpval != "" && tmpval != undefined);

		Content = Content + tagClose + "\n";
  }
  // -- URL
  else if ( myButSwitch == 'url') {
    tagOpen = tagOpen.substring(0,tagOpen.length-1);
    Content  = tagOpen;
    tmphref  = prompt("Locatie:","http://");
    tmptext  = '';

    if ( mySelection == '' ) {
      tmptext  = prompt("Locatie Tekst:","");
    }
    else {
      tmptext = mySelection;
    }

    if ( tmphref == null || tmphref == "" || tmphref == undefined ) { Content = Content + '="#"'; }
    else                                                            { Content = Content + '="' + tmphref + '"'; }

    if ( tmptext == null || tmptext == "" || tmptext == undefined ) {
      if ( tmphref != undefined && tmphref != '' ) {
        Content = Content + ']' + tmphref;
      }
      else {
        Content = Content + ']';
      }
    }
    else {
     Content = Content + ']' + '' + tmptext;
    }

    Content = Content + tagClose;
  }
  // -- IMG
  else if ( myButSwitch == 'img') {
    //tagOpen = tagOpen.substring(0,tagOpen.length-1);
    Content  = "[IMG]";
    tmphref  = prompt("Bron afbeelding:","");

    if ( tmphref == null || tmphref == "" || tmphref == undefined ) {
      Content = Content;
    }
    else {
     Content = Content + tmphref;
    }

    Content = Content + tagClose;
  }
  
  else if ( myButSwitch == 'youtube') {
    //tagOpen = tagOpen.substring(0,tagOpen.length-1);
    Content  = "[YOUTUBE]";
    tmphref  = prompt("Link YouTube video:","");

    if ( tmphref == null || tmphref == "" || tmphref == undefined ) {
      Content = Content;
    }
    else {
     Content = Content + tmphref;
    }

    Content = Content + tagClose;
  }

	var startPos = myText.selectionStart;
	var endPos = myText.selectionEnd;
	var cursorPos = endPos;
	var scrollTop = myText.scrollTop;

  if ( window.getSelection || document.getSelection ) {
    // Almost Mozilla or Opera
    if ( myText.selectionStart != myText.selectionEnd ) {
      if ( Content != '' ) {
        myText.value =
          myText.value.substring(0,myText.selectionStart)
        + Content
        + myText.value.substring(myText.selectionEnd,myText.value.length);

        cursorPos += Content.length - mySelection.length;
      }
      else {
       myText.value =
        myText.value.substring(0,myText.selectionStart)
        + tagOpen
        + myText.value.substring(myText.selectionStart,myText.selectionEnd)
        + tagClose
        + myText.value.substring(myText.selectionEnd,myText.value.length);

        cursorPos += tagOpen.length + tagClose.length;

      }
    }
    else {
      if ( Content != '' ) {
        myText.value =
          myText.value.substring(0,myText.selectionStart)
        + Content
        + myText.value.substring(myText.selectionEnd,myText.value.length);

        cursorPos += Content.length;
      }
      else {
      myText.value =
        myText.value.substring(0,myText.selectionStart)
      + tagOpen
      + myText.value.substring(myText.selectionStart,myText.selectionEnd)
      + tagClose
      + myText.value.substring(myText.selectionEnd,myText.value.length);

      cursorPos += tagOpen.length;

      }
    }

		myText.focus();
		myText.selectionStart = cursorPos;
		myText.selectionEnd = cursorPos;
		myText.scrollTop = scrollTop;
  }
  else if (document.selection) {
    // Almost suckzors
    myText.focus();
    iestr = document.selection.createRange();

    if ( iestr.text.length > 0 ) {
      if ( Content != '' ) {
        iestr.text = Content;
      }
      else {
        iestr.text = tagOpen + iestr.text + tagClose;
      }
    }
    else {
      if ( Content != '' ) {
        iestr.text = Content;
      }
      else {
        iestr.text = tagOpen + tagClose;
      }
    }
    myText.focus();
  }
}