$(document).ready(function()
  {
    $('img[@src$=.png]').ifixpng();
    
    $('.input_search').val('zoeken');
    $('.input_search').click( function()
    {
      if ( this.value == 'zoeken' )
      {
        $(this).val('');
      }
    })

    $('.input_search').blur( function()
    {
      if ( this.value == '' )
      {
        $(this).val('zoeken');
      }
    })

    $('div#fotos').show();
    $('div#fotos').cycle(
    {
      timeout:       2000,  // milliseconds between slide transitions (0 to disable auto advance)
      speed:         2000,  // speed of the transition (any valid fx speed value)
      next:          null,  // id of element to use as click trigger for next slide
      prev:          null,  // id of element to use as click trigger for previous slide
      before:        null,  // transition callback (scope set to element to be shown)
      after:         null,  // transition callback (scope set to element that was shown)
      height:       'auto', // container height
      sync:          true,  // true if in/out transitions should occur simultaneously
      fit:           0,     // force slides to fit container
      pause:         0,     // true to enable "pause on hover"
      delay:         0,     // additional delay (in ms) for first transition (hint: can be negative)
      slideExpr:     null   // expression for selecting slides (if something other than all children is required)
    });

    $( function(){
      // default settings
      $.Tooltip.defaults = $.extend( $.Tooltip.defaults, {
        showBody: " - ",
        track: true
      });

      // initialize
      $('a.icon').Tooltip({
        showURL: false
      });
    });

    $('img#stemlink').click(function()
    {
      var v = $('.vote').fieldValue();
      $.post('/inc/pollvote.php', {
        vote:v
      }, function(feedback)

      {
        switch (feedback)
        {
          case 'ok':
            var resultaat = parseInt($('span#totalvotescounter').text())+1;
            $('span.totalvotes').text(resultaat);
            alert('Bedankt voor uw stem');
            $('div#stellingen').load('/inc/polloutcome.php');
            break;

          case 'no_vote':
            alert("U moet eerst een antwoord kiezen!\n\n ");
            break;

          case 'denied':
            alert("U hebt al gestemd op deze stelling\n\nPer stelling mag men 1 keer stemmen!\n\n ");
            $('div#stellingen').load('/inc/polloutcome.php');
            break;
        }
      });
    });

    $('a.show_reaction').click(function()
    {
      $('div.place_reaction').hide();
      $('div.blog_reactions').show();
    });

    $('a.place_reaction').livequery('click', function()
    {
      var id = $(this).attr('id');
      $('div.blog_reactions').hide();
      $('div.place_reaction').load('/inc/weblog_loginform.php', { 
        id:id
      });
      $('div.place_reaction').show();
    });

    $('a#register_link').livequery('click', function()
    {
      $('div.place_reaction').load('/inc/weblog_register.php');
    });

    $('input#nieuwsbrief_ja').livequery('click', function()
    {
      $('div.register_form_nieuwsbrief').slideDown('fast');
    });

    $('input#nieuwsbrief_nee').livequery('click', function()
    {
      $('div.register_form_nieuwsbrief').slideUp('fast');
      $('input#email').val('');
      $('input#adres').val('');
      $('input#postcode').val('');
      $('input#woonplaats').val('');
      $('input#telefoon').val('');
    });

    /**
     * WEBLOG REGISTER
     */
    $('img.sendbutton_register').livequery('click', function()
    {
      var name = $('input#name').fieldValue();
      var username = $('input#username').fieldValue();
      var password = $('input#password').fieldValue();
      var password2 = $('input#password2').fieldValue();
      var email = $('input#email').fieldValue();
      var adres = $('input#adres').fieldValue();
      var postcode = $('input#postcode').fieldValue();
      var woonplaats = $('input#woonplaats').fieldValue();
      var telefoon = $('input#telefoon').fieldValue();

      $.post('/inc/weblog_signup.php', {
        name:name,
        username:username,
        password:password,
        password2:password2,
        email:email,
        adres:adres,
        postcode:postcode,
        woonplaats:woonplaats,
        telefoon:telefoon
      }, function(feedback)
      {
        switch(feedback)
        {
          case 'name_error':
            alert('U heeft geen naam ingevuld!');
            $('input#name').focus();
            break;

          case 'username_error':
            alert('Een gebruikersnaam dient in elk geval 6 karakters groot te zijn!');
            $('input#username').focus();
            break;

          case 'user_exists':
            alert('Deze gebruikersnaam bestaat al!');
            $('input#username').focus();
            break;
                
          case 'password_error':
            alert('De ingevulde wachtwoorden komen niet overeen!');
            $('input#password').focus();
            break;

          case 'password_error_length':
            alert("Het ingevulde wachtwoord is ongeldig.\nEen wachtwoord heeft minstens 6 karakters!");
            $('input#password').focus();
            break;

          case 'success':
            alert("U bent succesvol aangemeld.\nU kunt nu meteen reageren.");
            $('div.place_reaction').hide();
            $('div.blog_reactions').hide();
            break;
        }
      });
    });

    /**
     * WEBLOG LOGIN
     */
    $('img#weblog_login_button').livequery('click', function()
    {
      var loginnaam = $('input#loginnaam').fieldValue();
      var password = $('input#password').fieldValue();
      $.post('/inc/weblog_login.php', {
        loginnaam:loginnaam,
        password:password
      }, function(feedback)

      {
        switch (feedback)
        {
          case 'error':
            $('div.place_reaction').load('/inc/weblog_loginform.php?error');
            break;

          case 'logedin':
            $('div.place_reaction').load('/inc/weblog_loginform.php');
            break;
        }
      });
    });

    /**
     * WEBLOG REACT
     */
    $('img#weblog_react_button').livequery('click', function()
    {
      var reactie = $('textarea#reaction').fieldValue();
      var security_code = $('input#security_code').fieldValue();
      var id = $('input#blogid').fieldValue();
      $.post('/inc/weblog_place_reaction.php', {
        reaction:reactie,
        security_code:security_code,
        blogid:id
      }, function(feedback)

      {
        if ( feedback == 'captcha_error' )
        {
          alert('De ingevulde code is onjuist');
        }
        else
        {
          alert('Uw reactie is geplaatst.');
          window.location.reload(true);
        }
      });
    });

    /**
     * MARQUEE
     */
    $('marquee').marquee('pointer').mouseover(function ()
    {
      $(this).trigger('stop');
    }).mouseout(function ()
    {
      $(this).trigger('start');
    });

    /*SAVE TEXTPAGES*/
    $('.sendbutton_edit').livequery('click', function ()
    {
      tinyMCE.triggerSave();

      /*TEKST*/
      var record_id1 = $('input#id').val();
      var id1 = $(this).attr('id');
      var pagetitle1 = $('input#pagetitle').val();
      var content1 = $('textarea#content').val();

      switch ( id1 )
      {
        case 'textedit' :
          $.post("/inc/save.php",
          {
            record_id: record_id1,
            id: id1,
            pagetitle: pagetitle1,
            content: content1
          },
         function(feedback)
         {
            alert(feedback);
            window.location.reload(true);
          });
          break;

        case 'polledit' :

          var polltitle1 = $('input#polltitle').val();
          var pollanswer0 = $('input#pollanswer0').val();
          var pollanswer1 = $('input#pollanswer1').val();
          var pollanswer2 = $('input#pollanswer2').val();
          var newpoll = $('input#newpoll').val();

          $.post("/inc/save.php",
          {
            record_id: record_id1,
            id: id1,
            polltitle: polltitle1,
            pollanswer0: pollanswer0,
            pollanswer1: pollanswer1,
            pollanswer2: pollanswer2,
            newpoll: newpoll
          },
         function(feedback)
         {
            alert(feedback);
            window.location.reload(true);
          });
          break;

        case 'faqedit' :
          
          var answer = tinyMCE.activeEditor.getContent();
//          var answer = ;
          var faqid = $(this).parents('tr').attr('id');
          faqid = faqid.substr('2', '2');
          var title = $('input#t_'+faqid).val();
          var cat = $('select#c_'+faqid).val();
//          alert(title);
//          return false;

          $.post("/inc/save.php",
          {
            id: id1,
            title: title,
            faqid: faqid,
            answer: answer,
            cat: cat
          },
         function(feedback)
         {
            alert(feedback);
            window.location.reload(true);
          });
          break;

        case 'faqedit_new' :
          
//          var answer2 = tinyMCE.activeEditor.getContent();

          var question = $('input#question').val();
          var cat1 = $('select#cat').val();

          $.post('/inc/save.php',
          {
            id: id1,
            question: question,
            cat: cat1,
            answer: answer
          },
          function(feedback)
          {
            alert(feedback);
            window.location.reload(true);
          })

          break;

        case 'videoedit_new' :

          var video = $('input#video').val();
          var bron = $('input#bron').val();
          var videotitle = $('input#title').val();

          $.post('/inc/save.php',
          {
            id: id1,
            video: video,
            bron: bron,
            title: videotitle,
            intro: content1
          },
          function(feedback)
          {
            alert(feedback);
            window.location.reload(true);
          })

          break;

        case 'planedit' :
          
          var descr = tinyMCE.activeEditor.getContent();
          var plantitle = $(this).siblings('div.titlecontainer').children('input.textinput').val();
          var planid = $(this).parents('div').attr('class');
          planid = planid.substr('9', '2');
          var day = $(this).siblings('div.titlecontainer').children('select.date_day').val();
          var month = $(this).siblings('div.titlecontainer').children('select.date_month').val();
          var year = $(this).siblings('div.titlecontainer').children('select.date_year').val();
          var unknown_date = $(this).siblings('div.titlecontainer').children('#unknown_date_'+planid).is(':checked');

          $.post('/inc/save.php',
          {
            id: id1,
            plantitle: plantitle,
            descr: descr,
            planid: planid,
            day: day,
            month: month,
            year: year,
            unknown_date: unknown_date
          },
          function(feedback)
          {
            alert(feedback);
            window.location.reload(true);
          })

          break;

        case 'planedit_new' :

          var plantitle_new = $('input#title_new').val();
          var descr_new = content1;
          var day_new = $('select#date_day_new').val();
          var month_new = $('select#date_month_new').val();
          var year_new = $('select#date_year_new').val();
          var unknown_date_new = $('input#unknown_date_new').is(':checked');

          $.post('/inc/save.php',
          {
            id: id1,
            plantitle: plantitle_new,
            descr: descr_new,
            day: day_new,
            month: month_new,
            year: year_new,
            unknown_date: unknown_date_new
          },
          function(feedback)
          {
            alert(feedback);
            window.location.reload(true);
          })
          break;

        case 'blogedit' :

          var blogtitle = $(this).siblings('div.titlecontainer').children('input.textinput').val();
          var text = tinyMCE.activeEditor.getContent();
          var blogid = $(this).parents('div').attr('class');
          blogid = blogid.substr('9', '2');

          $.post('/inc/save.php',
          {
            id: id1,
            blogtitle: blogtitle,
            text: text,
            blogid: blogid
          },
          function(feedback)
          {
            alert(feedback);
            window.location.reload(true);
          })

          break;

        case 'blogedit_new' :

          var blogtitle = $('input#title').val();
          var text = tinyMCE.activeEditor.getContent();

          $.post('/inc/save.php',
          {
            id: 'blogedit_new',
            blogtitle: blogtitle,
            text: text
          },
          function(feedback)
          {
            alert(feedback);
            window.location.reload(true);
          })

          break;

        case 'videoedit' :

          var videoid = $(this).parents('tr').attr('id');
          videoid = videoid.substr('2', '2');
          var videotitle1 = $('input#t_'+videoid).val();
          var videotag = $('input#v_'+videoid).val();
          var videobron = $('input#b_'+videoid).val();
          var videointro = tinyMCE.activeEditor.getContent();

          $.post('/inc/save.php',
          {
            id: id1,
            videoid: videoid,
            videotitle: videotitle1,
            videotag: videotag,
            videobron: videobron,
            videointro: videointro
          },
          function(feedback)
          {
            alert(feedback);
            window.location.reload(true);
          })

          break;
      }

    });

    $('img.new_poll').click(function()
    {
      $('div.newpoll').show();
      $('input.textinput_edit').val('');
      $('input#newpoll').val('yes');
    });

    $('img.del_poll').click(function()
    {
      if (confirm('Weet u zeker dat u deze poll wilt verwijderen?'))
      {
        $.post('/inc/del.php',
        {
          record_id: $('input#id').val(),
          id: 'polldelete'
        },
        function(feedback)
        {
          alert(feedback);
          window.location.reload(true);
        });
      }
    });

    $('a.questionhref').click(function()
    {
      var id = $(this).parents('tr').attr('id');
      id = id.substr('2', '2');

      $('div.a_'+id).slideToggle('fast');
    });

    $('a.delete_faq').click(function()
    {
      var id = $(this).parents('tr').attr('id');
      var id2 = id.substr('2', '2');

      if (confirm('Weet u zeker dat u deze F.A.Q. wilt verwijderen?'))
      {
        $.post('/inc/del.php',
        {
          record_id: id2,
          id: 'faqdelete'
        },
        function(feedback)
        {
          alert(feedback);
          window.location.reload(true);
        });
      }
    });

    $('a.delete_video').click(function()
    {
      var id = $(this).parents('tr').attr('id');
      var id2 = id.substr('2', '2');

      if (confirm('Weet u zeker dat u deze video wilt verwijderen?'))
      {
        $.post('/inc/del.php',
        {
          record_id: id2,
          id: 'videodelete'
        },
        function(feedback)
        {
          alert(feedback);
          window.location.reload(true);
        });
      }
    });

    // BLOG

    $('a.bloghref').click(function()
    {
      var id = $(this).parents('tr').attr('id');
      id = id.substr('2', '2');

      $('div.a_'+id).slideToggle('fast');
    });

    $('a.delete_blog').click(function()
    {
      var id = $(this).parents('tr').attr('id');
      var id2 = id.substr('2', '2');

      if (confirm('Weet u zeker dat u deze blog wilt verwijderen?'))
      {
        $.post('/inc/del.php',
        {
          record_id: id2,
          id: 'blogdelete'
        },
        function(feedback)
        {
          alert(feedback);
          window.location.reload(true);
        });
      }
    });

    $('a.delete_plan').click(function()
    {
      var id = $(this).attr('id');
      var id2 = id.substr('2', '2');

      if (confirm('Weet u zeker dat u deze planning wilt verwijderen?'))
      {
        $.post('/inc/del.php',
        {
          record_id: id2,
          id: 'plandelete'
        },
        function(feedback)
        {
          alert(feedback);
          window.location.reload(true);
        });
      }
    });

    $('a.edit_faq, a.edit_blog').click(function()
    {
      var wich = '';
      var id = $(this).siblings('div.textcontainer').attr('id');
      
      if( $(this).attr('class') == 'edit_faq' )
      {
        wich = 'faqedit';
      }
      else
      {
        wich = 'blogedit';
      }
      
      if (!tinyMCE.get(id))
      {
        tinyMCE.execCommand('mceAddControl', false, id);
        $(this).siblings('div.titlecontainer').show();
        $(this).siblings('div.textcontainer').siblings('a.edit_faq').html('Annuleren');
        $(this).siblings('div.textcontainer').siblings('a.edit_blog').html('Annuleren');
        $("." + id).prepend("<a class='save_faq sendbutton_edit' id='"+wich+"'>OPSLAAN</a>");
        //$("." + id).prepend("<a class='save_blog sendbutton_edit' id='blogedit'>OPSLAAN</a>");
      }
      else
      {
        tinyMCE.execCommand('mceRemoveControl', false, id);
        $(this).siblings('div.titlecontainer').hide();
        $(this).siblings('div.textcontainer').siblings('a.edit_faq').html('Bewerk');
        $(this).siblings('div.textcontainer').siblings('a.edit_blog').html('Bewerk');
        $('a.save_faq').remove();
        //$('a.save_blog').remove();
      }
    });

    $('a.edit_video').click(function()
    {
      var id = $(this).siblings('div.textcontainer').attr('id');
      if (!tinyMCE.get(id))
      {
        tinyMCE.execCommand('mceAddControl', false, id);
        $(this).siblings('div.titlecontainer').show();
        $(this).siblings('div.textcontainer').siblings('a.edit_video').html('Annuleren');
        $("." + id).prepend("<a class='save_video sendbutton_edit' id='videoedit'>OPSLAAN</a>");
      }
      else
      {
        tinyMCE.execCommand('mceRemoveControl', false, id);
        $(this).siblings('div.titlecontainer').hide();
        $(this).siblings('div.textcontainer').siblings('a.edit_video').html('Bewerk');
        $('a.save_video').remove();
      }
    });

    $('a#newfaq, img#faqedit_annuleer').click(function()
    {
      $('div#newfaq_container').slideToggle('fast');
    });

    $('a#newplan, img#planedit_annuleer').click(function()
    {
      $('div#newplan_container').slideToggle('fast');
    });

    $('a#newblog, img#blogedit_annuleer').click(function()
    {
      $('div#newblog_container').slideToggle('fast');
    });

    // EDIT PLANNING

    $('a.planhref').click(function()
    {
      var id = $(this).attr('id');
      id = id.substr('2', '2');

      $('div.a_'+id).slideToggle('fast');
    });

    $('a.edit_plan').click(function()
    {
      var id = $(this).siblings('div.textcontainer').attr('id');
      if (!tinyMCE.get(id))
      {
        tinyMCE.execCommand('mceAddControl', false, id);
        $(this).siblings('div.titlecontainer').show();
        $(this).siblings('div.textcontainer').siblings('a.edit_plan').html('Annuleren');
        $("." + id).prepend("<a class='save_plan sendbutton_edit' id='planedit'>OPSLAAN</a>");
      }
      else
      {
        tinyMCE.execCommand('mceRemoveControl', false, id);
        $(this).siblings('div.titlecontainer').hide();
        $(this).siblings('div.textcontainer').siblings('a.edit_plan').html('Bewerk');
        $('a.save_plan').remove();
      }
    });

    // EDIT NIEUWS

    $('a.delete_news').click(function()
    {
      var id = $(this).attr('id');

      if (confirm('Weet u zeker dat u dit nieuwsbericht wilt verwijderen?'))
      {
        $.post('/inc/del.php',
        {
          record_id: id,
          id: 'newsdelete'
        },
        function(feedback)
        {
          alert(feedback);
          window.location.reload(true);
        });
      }
    });

    $('a.addnews').click(function()
    {
      $('div#addnewscontainer').slideToggle('fast');
    });

    $('a.adddownload').click(function()
    {
      $('div#adddownloadcontainer').slideToggle('fast');
    });

    $('a.delete_download').click(function()
    {
      var id = $(this).attr('id');

      if ( confirm('Weet u zeker dat u deze download wilt verwijderen?') )
      {
        $.post('/inc/del.php',
        {
          record_id: id,
          id: 'downloaddelete'
        },
        function(feedback)
        {
          alert(feedback);
          window.location.reload(true);
        });
      }
    });

  });

/**
 * SEARCH
 */
function submitSearch()
{
  var searchvalue = $('input#searchvalue').fieldValue();

  if ( searchvalue == 'zoeken' )
  {
    alert('Waar wilt u op zoeken?');
    $('a#searchbutton').focus();
    return false;
  }
  window.location = "/zoeken/"+escape(searchvalue)+'/';
}

/**
 * Overlay
 *
 */
function overlay()
{
  if($.browser.msie && $.browser.version <= 6.0)
  {
    var windowHeight = $(document).height();
    $('#overlay').height(windowHeight);
  }

  $('#overlay').css('opacity', 0.7).fadeIn('fast');
  $('#modal').fadeIn('slow');

  tinyMCE.init(
  {
    mode : "exact",
    content_css : "/css/tinymce.css",
    elements: "content",
    theme : "advanced",
    plugins : "style, youtube, paste2, media, advimage",
    media_strict: false,
    theme_advanced_buttons1 : "image,bold,italic,underline, strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,cut,copy,paste,paste2,|,search,replace,|,bullist,numlist,",
    theme_advanced_buttons2: "link,unlink,anchor,|,styleselect,|,youtube",
    theme_advanced_buttons3: "",
    theme_advanced_buttons4: "",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    cleanup_on_startup : true,
    file_browser_callback: "tinyBrowser",
    convert_urls : false,
    force_br_newlines : true,
    forced_root_block : ''
  });

  //Position it in the viewport
  var scrollPos = $(window).scrollTop() + 50;
  $('div#modal').css('top',scrollPos+'px');

  //Handle the clicking of close button or clicking the background
  $('div#modal a#close,div#overlay').bind('click', function()
  {
    $('div#modal').fadeOut('fast',function()
    {
      $('input#newpoll').val('no');
      $('div#overlay').fadeOut('fast');
    });
    return false;
  });
}
