/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

              _   _  ___  ____          ___  _____ 
             | | | ||_ _||  _ \        |_ _||_   _|
             | |_| | | | | |_) | _____  | |   | |
             |  _  | | | |  __/ |_____| | |   | |
             |_| |_||___||_|           |___|  |_|
 
   
        (c) Concept, vormgeving, ontwerp en uitvoering
         ~~ HIP-IT BV (Media Laboratory) Amsterdam ~~
           Tel. +31-(0)20-617 02 82, info@hip-it.nl
                        www.hip-it.nl

(c) 1999-2008 HIP-IT Media Produkties BV
(c) Redouan Salmoun

All rights reserved! Alle rechten voorbehouden. Niets uit deze uitgave
mag zonder voorafgaande schriftelijke toestemming van HIP-IT
verveelvoudigd en/of openbaar worden gemaakt!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
AttachEvent(document, 'keydown', checkEsc, false);var ajaxpr = newpost = tvid = false;
function initVideo()
{
  if (document.getElementById('comments'))
  {
    SWFAddress.addEventListener(SWFAddressEvent.CHANGE, haalBerichtenSWFAddress);
    AttachEvent(window, 'load', renderMedia, false);
  }
  if (document.getElementById('postform'))
  {
    haalBBcodeKnopjes('bericht');
  }
  //wbds_ads.load();
}

function checkEsc()
{
  if ((window.event.keyCode == 27) && (document.getElementById('dialog')))
  {
    sluitDivPop();
  }
}

function haalBerichten(vid, totals, pagenumber)
{
  writer('pagenavt', showLoader(1));
  writer('pagenavb', showLoader(1));
  haalData('GET', '/video/comments.php?nav=getcomments&vid='+vid+'&totals='+totals+'&pagenumber='+pagenumber, 'toonPost', null);
  tvid = vid;
  ttotals = totals;
  ajaxpr = true;
  SWFAddress.setValue('?p=' + pagenumber);
  ajaxpr = false;
}

function haalBerichtenSWFAddress(event)
{
  if (ajaxpr == false)
  {
    var pagenumber;

    if (pagenumber = event.parameters.p)
    {
      haalBerichten(tvid, ttotals, pagenumber);
    }
  }
}

function plaatsBericht(formulier)
{
  if (formulier.bericht.value.length < 15)
  {
    alert('Bericht is te kort');
    formulier.submitbutton.value = 'verstuur';
    formulier.submitbutton.disabled = false;
  }
  else if (formulier.bericht.value.length > 5000)
  { 
    alert('Bericht is te lang');
    formulier.submitbutton.value = 'verstuur';
    formulier.submitbutton.disabled = false;
  }
  else
  { 
    poststr = 'nav=newcomment&bericht='+escape(formulier.bericht.value)+'&vid='+formulier.vid.value+'&totals='+formulier.totals.value;
    haalData('POST', '/video/comments.php', 'toonPost', poststr);
    newpost = true;
  }
}

function toonPost(Aanvraag)
{
  if (Aanvraag.substr(5, 4) == 'MSGL' || Aanvraag.substr(5, 4) == 'MSGR')
  {
    alert(Aanvraag.substr(13));
    window.location.reload();
  }
  else
  {
    writer('comments', Aanvraag);
    if (document.getElementById('postform'))
    {
      cpostform.submitbutton.value = 'verstuur';
      cpostform.bericht.value = '';
      cpostform.submitbutton.disabled = false;
    }
    if (newpost == true)
    {
      ss('postform');
      newpost = false;
    }
    else
    {
      ss('comments');
    }
    if (document.getElementById('postform'))
    {
      haalBBcodeKnopjes('bericht');
    }
    focusPagina();
    renderMedia();
  }
}

function verwijderComment(commentid, vid, totals, pagenumber)
{
  blurPagina();
  if (confirm('Weet je zeker dat je dit bericht wilt verwijderen?'))
  {
    haalData('GET', '/video/comments.php?nav=delcomments&commentid=' + commentid + '&vid=' + vid + '&totals=' + totals + '&pagenumber=' + pagenumber, 'toonPost', null);
  }
  else
  {
    focusPagina();
  }
}

function rapporteerComment(vid, cid)
{
  var positie = haalPositie(document.getElementById('commentid-' + cid));
  positie[0] = document.documentElement.clientWidth/2 - positie[0]+120;
  positie[1] = positie[1] - 20;
  document.getElementById('meldshit').style.left = positie[0]+'px';
  document.getElementById('meldshit').style.top = positie[1]+'px'
  var rdata = document.rapportform;
  contract('rapportformloader');
  expand('rapportformbutton');
  rdata.rvid.value = vid;
  rdata.rcid.value = cid;
  rdata.rbericht.value = '';
  expand('meldshit');
  rdata.rbericht.focus();
}

function verstuurRapport()
{
  var rdata = document.rapportform;
  if (rdata.rvid.value > 0 && rdata.rcid.value > 0 && rdata.rbericht.value.length > 2)
  {
    writer('rapportformloader', showLoader(1));
    contract('rapportformbutton');
    expand('rapportformloader');
    haalData('GET', '/video/comments.php?nav=rapporteer&cid='+rdata.rcid.value+'&vid='+rdata.rvid.value+'&bericht='+rdata.rbericht.value, 'sluitRapport', null);
  }
  else
  {
    alert('Geen bericht opgegeven. Geef een korte omschrijving van je klacht');
  }
}

function sluitRapport(Aanvraag)
{
  contract('meldshit');
  var rdata = document.rapportform;
  rdata.rvid.value = '';
  rdata.rcid.value = '';
  rdata.rbericht.value = '';
  if (Aanvraag)
  {
    alert('Rapport succesvol verstuurd, dank je wel!');
  }
}

function playYoutube(divje, video, width)
{
  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="350">';
  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="350" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
  writer(divje, youtubeplayer);
}

function renderMedia(herhaal)
{
  var imgs = document.getElementById('comments').getElementsByTagName('img');
  div = 500;
  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('comments').getElementsByTagName('object');
  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, div+3);
    }
  }
}

function haalBBcodeKnopjes(textareatemp)
{
  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]');

  var taglength = myTags.length-1;
  var bbformbuttons = '';
  for (i = 0; i <= taglength; i++)
  {
    bbformbuttons = bbformbuttons + '<div class="floatl formbutton" id="' + myTags[i][0] + '" onMouseOver="this.className=\'floatl formbutton formbuttonover hand\'" onMouseOut="this.className=\'floatl formbutton hand\'" " onMouseDown="this.className=\'floatl formbutton formbuttondown hand\'" onmouseup="this.className=\'floatl formbutton formbuttonup hand\'" name="' + myTags[i][1] + '" onClick="WriteOption(\'' + myTags[i][0] + '\',\''+textareatemp+'\',\'' + myTags[i][3] + '\',\'' + myTags[i][4] + '\');"><img src="' + media['static'] + '/img/bbicon/' + myTags[i][1] + '.gif" alt="' + myTags[i][2] + '" width="22" height="22" /></div>';
  }
  bbformbuttons = bbformbuttons + '<div class="cleardiv"></div>';
  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();
  }
}