var search_text = 'Zeuke';

function IsNumeric(sText) {
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;
  
  for (i = 0; i < sText.length && IsNumber == true; i++) { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) {
      IsNumber = false;
    }
  }
  return IsNumber;   
}

function inputFocus(object) {
  if (object.val() == search_text) {
    object.val('');
    
  }
  
  inputClear(object)
  
}

function inputBlur(object) {
  if (object.val() == '') {
    object.val(search_text);
    
  }
  
  inputClear(object);
  
}

function inputClear(object) {
  if (object.val() != '' && object.val() != search_text) {
    object.css({width: '273px'});
    $('#clear-search').show();
    $('#search').css({backgroundPosition: 'bottom left'});
    
  } else {
    object.css({width: '292px'});
    $('#clear-search').hide();
    $('#search').css({backgroundPosition: 'top left'});
  
  }
  
}

function deletePhoto(id) {
  var answer = confirm('Album verwijdere?');
  if (answer) {
    $('#album-' + id).remove();
    $.get('ajax/functions.php?function=remove_photo&id=' + id);
    if ($('.item-change').length == 0) {
      $('.add').first().html('<p>Nog gen albums beschikbaar</p>');
      Cufon.refresh();
    }
  
  }
  
  return false;
}

function deleteNews(id) {
  var answer = confirm('Niejs item verwijdere?');
  if (answer) {
    $.get('ajax/functions.php?function=remove_news&id=' + id);
    return true;
  
  }
  
  return false;
}

function deleteEvent(id) {
  var answer = confirm('Agenda item verwijdere?');
  if (answer) {
    $('#agenda-content-' + id).remove();
    $('.divider').last().hide();
    $.get('ajax/functions.php?function=remove_event&id=' + id);
    
    if ($('#agenda-items').children().length == 0) {
      $('#agenda-items').html('<div id="agenda-no-items">Nog niks in de agenda</div>');
      Cufon.refresh();
    }
  
  }
  
  return false;
}

function resizeAgenda(target) {
  if  (target.parent().height() == 57) {
    height = 30 + target.height();
    target.parent().animate({ height: (height) + 'px' }, 300 );
  } else {
    target.parent().animate({ height: '57px' }, 300 );
  }
  
  return false;

}

function initPhotos() {
  $('.news-title').css({fontSize: '10px', color: '#afafaf', fontStyle: 'italic'});
  $('.news-day, .news-month, .news-year').css({color: '#afafaf', fontStyle: 'italic'});
  $('.news-day, .news-month, .news-year').focus(function() {
    $(this).css({color: '#000', fontStyle: 'normal'});
  });
  $('.news-title').focus(function() {
    if ($(this).css('color') != 'rgb(0, 0, 0)' && $(this).val() == 'Titel') {
      $(this).val('');
    }
    $(this).css({fontSize: '14px', color: '#000', fontStyle: 'normal'});
  });
  
  $('#photo_list, #photo_list_add').click(function () {
    if ($('.content').css('marginLeft') == ('-1163px')) {
      $('.content').animate({marginLeft: ('-506px')}, 500);
    }
  });
  
  $('.photo-item').click(function() { clickPhotos(-506, this); return false });
  $('.photo-portrait-item').click(function() { clickPhotos(-506, this); return false });
  $('.photo-item-side').click(function(){ clickPhotos(-1163, this); return false });
  $('.photo-portrait-item-side').click(function() { clickPhotos(-1163, this); return false });
  
  $('.photo-item-side').hover(function() { hoverPhotos(-506, 'bottom', this, true) }, function() { hoverPhotos(-506, 'top', this, true) });
  $('.photo-portrait-item-side').hover(function() { hoverPhotos(-506, 'bottom', this, true) }, function() { hoverPhotos(-506, 'top', this, true) });
  $('.photo-item').hover(function() { hoverPhotos(-1163, 'bottom', this, false) }, function() { hoverPhotos(-1163, 'top', this, false) });
  $('.photo-portrait-item').hover(function() { hoverPhotos(-1163, 'bottom', this, false) }, function() { hoverPhotos(-1163, 'top', this, false) });
}

function hoverPhotos(margin, pos, object, side) {
  var right = '';
  if (side) right = '-side';
  if ($('.content').css('marginLeft') == (margin + 'px')) {
    $('.photo-item'+right).css({backgroundPosition: (pos)});
    $('.photo-portrait-item'+right).css({backgroundPosition: (pos)});
  } else {
    object.style.backgroundPosition = pos;
  }
}

function clickPhotos(margin, object) {
  if ($('.content').css('marginLeft') == (margin + 'px')) {
    showPhoto(object.href, 0, 0, '');
  } else {
    $('.content').animate({marginLeft: (margin + 'px')}, 500);
  }
}

function validateMail(email) {
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  if(reg.test(email) == false) {
    return false;
  } else {
    return true;
  }
   
}

function validateURL(url) {
  var reg = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
  if(reg.test(url) == false) {
    return false;
  } else {
    return true;
  }
   
}

function checkGuestbook(form){ 
  if (form.name.value == "") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, e-mail as ein berich in te völle!" );
    form.name.focus();
    return false;
  } else if (form.mail.value == "" || !validateMail(form.mail.value)) {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, e-mail as ein berich in te völle!" );
    form.mail.focus();
    return false;
  } else if (form.message.value == "") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, e-mail as ein berich in te völle!" );
    form.message.focus();
    return false;
  } else if (form.control.value != "11") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, e-mail as ein berich in te völle!" );
    form.control.focus();
    return false;
  }
  
  return true;
}

function gotoGuestbookPage(begin) {
  var id = begin;
  $('#height .text').hide();
  
  while ($('#height').height() < 720 && $('#guest-'+id).length != 0) {
    $('#guest-'+id).show();
    if ($('#height').height() > 720) {
      $('#guest-'+id).hide();
    }
    id = id + 1;
  
  }
  
  var total_height = 0;
  
  for (var j = begin; j <= id; j++) {
    $('#guest-' + j).css({marginLeft: ((45 - (total_height/30)) + 'px'), width: ((580 + (total_height/30)) + 'px')});
    $('#guest-' + j + ' .reply').css({width: ((582 + (total_height/30)) + 'px')});
    total_height += $('#guest-' + j).height();
  }
  
}

function initGuestbook(offset) {
  $('.text').hide();
  
  if (offset == false) {
    offset = 0;
  }
  var begin = 0;
  var end = 0;
  var id = 0;
  var total_height = 0;
  
  for (var i = 0; i <= offset; i++) {
    begin = end;
    total_height = 0;
    
    while (total_height < 720 && $('#guest-'+id).length != 0) {
      total_height = total_height + $('#guest-'+id).height();
      
      id = id + 1;
    
    }
    
    if (total_height > 720) {
      total_height = total_height - $('#guest-'+id).height();
      id = id-1;
    }
    end = id;
  }
  
  total_height = 0;
  
  if (end+1 >= $('.text').length) {
    $('.guestbook-back').hide();
  }
  
  $('.text').hide();
  
  for (var j = begin; j < end; j++) {
    $('#guest-' + j).show();
    $('#guest-' + j).css({marginLeft: ((45 - (total_height/30)) + 'px'), width: ((580 + (total_height/30)) + 'px')});
    $('#guest-' + j + ' .reply').css({width: ((582 + (total_height/30)) + 'px')});
    total_height += $('#guest-' + j).height();
  }
  
  $('.guestbook').click(function() {
    if ($('.content').css('marginLeft') != ('-486px')) {
      $('.content').animate({marginLeft: ('-486px')}, 500);
    }
  });
  
  $('.new_entry').click(function() {
    if ($('.content').css('marginLeft') != ('-1050px')) {
      $('.content').animate({marginLeft: ('-1050px')}, 500);
    }
  });
}

function checkContact(form){ 
  if (form.name.value == "") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, e-mail as ein berich in te völle!" );
    form.name.focus();
    return false;
  } else if (form.mail.value == "" || !validateMail(form.mail.value)) {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, e-mail as ein berich in te völle!" );
    form.mail.focus();
    return false;
  } else if (form.message.value == "") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, e-mail as ein berich in te völle!" );
    form.message.focus();
    return false;
  } else if (form.control.value != "11") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, e-mail as ein berich in te völle!" );
    form.control.focus();
    return false;
  }
  
  return true;
}

function checkSignup(form){ 
  if (form.name.value == "") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, laeftied, e-mail en tillefoon nummer in te völle!" );
    form.name.focus();
    return false;
  } else if (form.lastname.value == "") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, laeftied, e-mail en tillefoon nummer in te völle!" );
    form.lastname.focus();
    return false;
  } else if (form.age.value == "") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, laeftied, e-mail en tillefoon nummer in te völle!" );
    form.age.focus();
    return false;
  } else if (form.mail.value == "" || !validateMail(form.mail.value)) {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, laeftied, e-mail en tillefoon nummer in te völle!" );
    form.mail.focus();
    return false;
  } else if (form.phone.value == "") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, laeftied, e-mail en tillefoon nummer in te völle!" );
    form.phone.focus();
    return false;
  } else if (form.control.value != "11") {
    alert( "Weej vraoge ôch vrintelik zoëwaal naam, laeftied, e-mail en tillefoon nummer in te völle!" );
    form.control.focus();
    return false;
  }
  
  return true;
}

function checkNewNews(form) {
  if (form.title.value == "" || form.title.value == 'Titel') {
    form.title.focus();
    return false;
  
  }
  
  if (form.hours.value == "" || !IsNumeric(form.hours.value) || (form.hours.value > 23)) {
    form.hours.focus();
    return false;
  
  }
  
  if (form.minutes.value == "" || !IsNumeric(form.minutes.value) || (form.minutes.value > 59)) {
    form.minutes.focus();
    return false;
  
  }
  
  var position = 1;
  while ($('#item-' + position).length > 0) {
    var type = $('#item-' + position + ' input[name="' + position + '-type"]').val();
    
    if (type == 'text') {
      if ($('#item-' + position + ' .new-textarea').val() == '') {
        $('#item-' + position + ' .new-textarea').focus();
        return false;
      }
    
    } else if (type == 'youtube') {
      var val = $('#item-' + position + ' .youtube-title').val();
      if (val == '' || val == 'Titel') {
        $('#item-' + position + ' .youtube-title').focus();
        return false;
      }
      val = $('#item-' + position + ' .youtube-content').val();
      if (val == '' || val == '<Insluiten> code') {
        $('#item-' + position + ' .youtube-content').focus();
        return false;
      }
      val = $('#item-' + position + ' .news-file').val();
      if (val == '' || val.lastIndexOf(".jpg") == -1) {
        $('#item-' + position + ' .news-file').focus();
        return false;
      }
    
    } else if (type == 'image') {
      var val = $('#item-' + position + ' .image-title').val();
      if (val == '' || val == 'Titel') {
        $('#item-' + position + ' .image-title').focus();
        return false;
      }
      val = $('#item-' + position + ' input[name="' + position + '-image"]').val();
      if (val == '' || val.lastIndexOf(".jpg") == -1) {
        $('#item-' + position + ' input[name="' + position + '-image"]').focus();
        return false;
      }
      val = $('#item-' + position + ' input[name="' + position + '-image-thumb"]').val();
      if (val == '' || val.lastIndexOf(".jpg") == -1) {
        $('#item-' + position + ' input[name="' + position + '-image-thumb"]').focus();
        return false;
      }
    
    }
    
    position++;
  
  }
  
}

function checkLinks(form) {  
  var position = 1;
  while ($('#item-' + position).length > 0) {
    var title = $('#item-' + position + ' input[name="' + position + '-title"]').val();
    var url = $('#item-' + position + ' input[name="' + position + '-url"]').val();
    
    if (title == '' || title == 'Titel') {
      $('#item-' + position + ' input[name="' + position + '-title"]').focus();
      return false;
    }
    if (url == '' || url == 'Titel') {
      $('#item-' + position + ' input[name="' + position + '-url"]').focus();
      return false;
    }
    
    position++;
  
  }
  
}

function checkNewAgenda(form) {
  if (form.title.value == "" || form.title.value == 'Titel') {
    form.title.focus();
    return false;
  
  }
  
  if (form.subtitle.value == "" || form.subtitle.value == 'Subtitel') {
    form.subtitle.focus();
    return false;
  
  }
  
  if (form.hours.value == "" || !IsNumeric(form.hours.value) || (form.hours.value > 23)) {
    form.hours.focus();
    return false;
  
  }
  
  if (form.minutes.value == "" || !IsNumeric(form.minutes.value) || (form.minutes.value > 59)) {
    form.minutes.focus();
    return false;
  
  }
  
  if (form.text.value == "" || form.text.value == 'Beschrieving') {
    form.text.focus();
    return false;
  
  }
  
  if (form.location.value == "" || form.location.value == 'Lokatie') {
    form.location.focus();
    return false;
  
  }
  
}

function resetActions(item) {
  if (item != '') {
    item = item + ' ';
  }
  
  $(item + '.button-up').click(function() {
    homeButtonUp(this);
  });
  
  $(item + '.button-down').click(function() {
    homeButtonDown(this);
  
  });
  
  $(item + '.button-delete').click(function () {
    homeButtonDelete(this);
  });
  
  $(item + '.button-edit').click(function () {
    homeButtonEdit(this);
  });
  
  $('.button-up').show();
  $('.button-down').show();
  $('#item-1 .button-up').hide();
  $('.button-down').last().hide();
  
  $('.news-title, .news-time, .youtube-title, .youtube-content, .image-title').focus(function() {
    if ($(this).css('color') != 'rgb(0, 0, 0)' && ($(this).val() == 'Titel' || $(this).val() == '<Insluiten> code')) {
      $(this).val('');
    }
    $(this).css({fontSize: '14px', color: '#000', fontStyle: 'normal'});
  });
  
  Cufon.refresh();

}

function addNewsField(type) {
  var inner = $('#new-' + type).html();
  var position = $('#new-news-content').children().size();
  inner = inner.replace(/name="/g,  'name="' + position + '-');
  inner = '<div id="item-' + position + '" class="new-' + type + '">' + inner + '</div>';
  $('.button-down').last().show();
  $('#new-news-content').append(inner);
  $('.button-down').last().hide();
  
  $('.new-' + type).hover(function () {
    $('#' + $(this).attr('id') + ' .controls').fadeIn('fast');
  },
  function () {
    $('#' + $(this).attr('id') + ' .controls').fadeOut('fast');
    
  });
  
  resetActions('#item-' + position);
}

function homeButtonEdit(dit) {
  var parent = $(dit).parent().parent();
  var item_id = parent.attr('id').substr(5)*1;
  var type = $('#item-' + item_id + ' input[name="' + item_id + '-block-type"]').val();
  var delete_id = $('#item-' + item_id + ' input[name="' + item_id + '-block-id"]').val();
  var inner = $('#new-' + type).html();
  var answer = true;
  
  if (type == 'text') {
    var text = $('#item-' + item_id + ' .news-text-p').html().replace(/<br>\n<br>/gi, '\n');
    text = text.replace(/<br>/gi, '');
  } else if (type == 'youtube') {
    answer = confirm("Beej ut wijzigen van ein youtube filmpke mot ein nieje veurbeeld aafbeelding geupload wuure. Wilt geej toch door gaon?");
    var movie = $('#item-' + item_id + ' input[name="' + item_id + '-youtube"]').val();
    var width = $('#item-' + item_id + ' input[name="' + item_id + '-youtube-width"]').val();
    var height = $('#item-' + item_id + ' input[name="' + item_id + '-youtube-height"]').val();
    var title = $('#item-' + item_id + ' input[name="' + item_id + '-youtube-title"]').val();
  } else if (type == 'image') {
    answer = confirm("Beej ut wijzigen van ein foto mot ein nieje foto en ein nieje veurbeeld aafbeelding geupload wuure. Wilt geej toch door gaon?");
    var title = $('#item-' + item_id + ' input[name="' + item_id + '-image-title"]').val();
  }
  
  if (answer) {
    inner = inner.replace(/name="/g,  'name="' + item_id + '-');
    $('#item-' + item_id).attr('class', 'new-' + type);
    $('#item-' + item_id).html(inner);
    $('.button-down').last().hide();
    
    if (type == 'text') {
      $('#item-' + item_id + ' textarea').val(text);
      inputHeight($('#item-' + item_id + ' textarea'));
    } else if (type == 'youtube') {
      $('#item-' + item_id + ' input[name="' + item_id + '-youtube-title"]').val(title);
      $('#item-' + item_id + ' input[name="' + item_id + '-youtube"]').val('<object width="' + width + '" height="' + height + '"><param name="movie" value="http://www.youtube.com/v/' + movie + '&amp;hl=nl_NL&amp;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + content + '&amp;hl=nl_NL&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + width + '" height="' + height + '"></embed></object>');
    } else if (type == 'image') {
      $('#item-' + item_id + ' input[name="' + item_id + '-image-title"]').val(title);
    }
    
    $('#delete_block').val($('#delete_block').val() + delete_id + ',');
    
    $('.new-' + type).hover(function () {
      $('#' + $(this).attr('id') + ' .controls').fadeIn('fast');
    },
    function () {
      $('#' + $(this).attr('id') + ' .controls').fadeOut('fast');
      
    });
    
    resetActions('#item-' + item_id);
  }
}

function homeButtonDelete(dit) {
  var parent = $(dit).parent().parent();
  var item_id = parent.attr('id').substr(5)*1;
  
  if (parent.attr('class') == 'news-item') {
    var delete_id = $('#item-' + item_id + ' input[name="' + item_id + '-block-id"]').val();
    $('#delete_block').val($('#delete_block').val() + delete_id + ',');
  
  }
  
  $('#item-' + item_id).remove();
  item_id++;
  while ($('#item-' + item_id).length > 0) {
    $('#item-' + item_id + ' input, #item-' + item_id + ' textarea').each(function (i) {
      $(this).attr('name', (item_id - 1) + $(this).attr('name').substr((item_id + '').length));
    });
    $('#item-' + item_id).attr('id', 'item-' + (item_id - 1));

    item_id++;
  }
  
  $('.button-up').show();
  $('.button-down').show();
  $('#item-1 .button-up').hide();
  $('.button-down').last().hide();

}

function homeButtonUp(dit) {
  var parent = $(dit).parent().parent();
  var item_id = parent.attr('id').substr(5)*1;
  
  var type = $('#item-' + item_id + ' input[name="' + item_id + '-type"]').val();
  var clas = $('#item-' + item_id).attr('class');
  
  var type_next = $('#item-' + (item_id - 1) + ' input[name="' + (item_id - 1) + '-type"]').val();
  var clas_next = $('#item-' + (item_id - 1)).attr('class');
  
  var title = null;
  var title_next = null;
  var youtube = null;
  var youtube_next = null;
  
  $('#item-' + item_id + ' input, #item-' + item_id + ' textarea').each(function (i) {
    var subs = $(this).attr('name').substr(((item_id) + '').length);
    if ((type == 'image' && subs == '-image-title') || (type == 'text' && subs == '-text') || (type == 'youtube' && subs == '-youtube-title')) {
      title = $(this).val();
    } else if (type == 'youtube' && subs == '-youtube') {
      youtube = $(this).val();
    }
    $(this).attr('name', (item_id - 1) + subs);
  });
  
  $('#item-' + (item_id - 1) + ' input, #item-' + (item_id - 1) + ' textarea').each(function (i) {
    var subs = $(this).attr('name').substr(((item_id - 1) + '').length);
    if ((type_next == 'image' && subs == '-image-title') || (type_next == 'text' && subs == '-text') || (type_next == 'youtube' && subs == '-youtube-title')) {
      title_next = $(this).val();
    } else if (type_next == 'youtube' && subs == '-youtube') {
      youtube_next = $(this).val();
    }
    $(this).attr('name', item_id + subs);
  });
  
  var inner = $('#item-' + item_id).html();
  var inner_next = $('#item-' + (item_id - 1)).html();
  
  $('#item-' + item_id).html(inner_next);
  $('#item-' + (item_id - 1)).html(inner);
  
  $('#item-' + item_id).attr('class', clas_next);
  $('#item-' + (item_id - 1)).attr('class', clas);
  
  $('#item-' + (item_id - 1) + ' input, #item-' + (item_id - 1) + ' textarea').each(function (i) {
    var subs = $(this).attr('name').substr(((item_id - 1) + '').length);
    if ((type == 'image' && subs == '-image-title') || (type == 'text' && subs == '-text') || (type == 'youtube' && subs == '-youtube-title')) {
      $(this).val(title);
    } else if (type == 'youtube' && subs == '-youtube') {
      $(this).val(youtube);
    }
  });
  
  $('#item-' + (item_id) + ' input, #item-' + (item_id) + ' textarea').each(function (i) {
    var subs = $(this).attr('name').substr(((item_id) + '').length);
    if ((type_next == 'image' && subs == '-image-title') || (type_next == 'text' && subs == '-text') || (type_next == 'youtube' && subs == '-youtube-title')) {
      $(this).val(title_next);
    } else if (type_next == 'youtube' && subs == '-youtube') {
      $(this).val(youtube_next);
    }
  });
  
  resetActions('#item-' + item_id);
  resetActions('#item-' + (item_id - 1));
}

function homeButtonDown(dit) {
  var parent = $(dit).parent().parent();
  var item_id = parent.attr('id').substr(5)*1;
  
  var type = $('#item-' + item_id + ' input[name="' + item_id + '-type"]').val();
  var clas = $('#item-' + item_id).attr('class');
  
  var type_next = $('#item-' + (item_id + 1) + ' input[name="' + (item_id + 1) + '-type"]').val();
  var clas_next = $('#item-' + (item_id + 1)).attr('class');
  
  var title = null;
  var title_next = null;
  var youtube = null;
  var youtube_next = null;
  
  $('#item-' + item_id + ' input, #item-' + item_id + ' textarea').each(function (i) {
    var subs = $(this).attr('name').substr(((item_id) + '').length);
    if ((type == 'image' && subs == '-image-title') || (type == 'text' && subs == '-text') || (type == 'youtube' && subs == '-youtube-title')) {
      title = $(this).val();
    } else if (type == 'youtube' && subs == '-youtube') {
      youtube = $(this).val();
    }
    $(this).attr('name', (item_id + 1) + subs);
  });
  
  $('#item-' + (item_id + 1) + ' input, #item-' + (item_id + 1) + ' textarea').each(function (i) {
    var subs = $(this).attr('name').substr(((item_id + 1) + '').length);
    if ((type_next == 'image' && subs == '-image-title') || (type_next == 'text' && subs == '-text') || (type_next == 'youtube' && subs == '-youtube-title')) {
      title_next = $(this).val();
    } else if (type_next == 'youtube' && subs == '-youtube') {
      youtube_next = $(this).val();
    }
    $(this).attr('name', item_id + subs);
  });
  
  var inner = $('#item-' + item_id).html();
  var inner_next = $('#item-' + (item_id + 1)).html();
  
  $('#item-' + item_id).html(inner_next);
  $('#item-' + (item_id + 1)).html(inner);
  
  $('#item-' + item_id).attr('class', clas_next);
  $('#item-' + (item_id + 1)).attr('class', clas);
  
  $('#item-' + (item_id + 1) + ' input, #item-' + (item_id + 1) + ' textarea').each(function (i) {
    var subs = $(this).attr('name').substr(((item_id + 1) + '').length);
    if ((type == 'image' && subs == '-image-title') || (type == 'text' && subs == '-text') || (type == 'youtube' && subs == '-youtube-title')) {
      $(this).val(title);
    } else if (type == 'youtube' && subs == '-youtube') {
      $(this).val(youtube);
    }
  });
  
  $('#item-' + (item_id) + ' input, #item-' + (item_id) + ' textarea').each(function (i) {
    var subs = $(this).attr('name').substr(((item_id) + '').length);
    if ((type_next == 'image' && subs == '-image-title') || (type_next == 'text' && subs == '-text') || (type_next == 'youtube' && subs == '-youtube-title')) {
      $(this).val(title_next);
    } else if (type_next == 'youtube' && subs == '-youtube') {
      $(this).val(youtube_next);
    }
  });
  
  resetActions('#item-' + item_id);
  resetActions('#item-' + (item_id + 1));
}

function linksButtonMove(dit, direction) {
  var parent = $(dit).parent().parent();
  var item_id = parent.attr('id').substr(5)*1;
  
  var id = $('#item-' + item_id + ' input[name="' + item_id + '-id"]').val();
  var title = $('#item-' + item_id + ' input[name="' + item_id + '-title"]').val();
  var url = $('#item-' + item_id + ' input[name="' + item_id + '-url"]').val();
  
  var id_next = $('#item-' + (item_id + direction) + ' input[name="' + (item_id + direction) + '-id"]').val();
  var title_next = $('#item-' + (item_id + direction) + ' input[name="' + (item_id + direction) + '-title"]').val();
  var url_next = $('#item-' + (item_id + direction) + ' input[name="' + (item_id + direction) + '-url"]').val();
  
  if ($('#item-' + item_id + ' a').is(":visible") != $('#item-' + (item_id + direction) + ' a').is(":visible")) {
    if ($('#item-' + item_id + ' a').is(":visible")) {
      $('#item-' + (item_id + direction) + ' a').html($('#item-' + item_id + ' .input[name="title"]').val());
      $('#item-' + (item_id + direction) + ' a').attr('href', $('#item-' + item_id + ' .input[name="url"]').val());
      $('#item-' + (item_id + direction) + ' a').show();
      $('#item-' + (item_id + direction) + ' .input').hide();
      $('#item-' + item_id + ' a').hide();
      $('#item-' + item_id + ' .input').show();
    } else {
      $('#item-' + item_id + ' a').html($('#item-' + item_id + ' .input[name="title"]').val());
      $('#item-' + item_id + ' a').attr('href', $('#item-' + item_id + ' .input[name="url"]').val());
      $('#item-' + item_id + ' a').show();
      $('#item-' + item_id + ' .input').hide();
      $('#item-' + (item_id + direction) + ' a').hide();
      $('#item-' + (item_id + direction) + ' .input').show();
    }
  
  }
  
  $('#item-' + (item_id + direction) + ' input[name="' + (item_id + direction) + '-id"]').val(id);
  $('#item-' + (item_id + direction) + ' input[name="' + (item_id + direction) + '-title"]').val(title);
  $('#item-' + (item_id + direction) + ' input[name="' + (item_id + direction) + '-url"]').val(url);
  $('#item-' + (item_id + direction) + ' a').html(title);
  $('#item-' + (item_id + direction) + ' a').attr('href', url);
  
  $('#item-' + item_id + ' input[name="' + item_id + '-id"]').val(id_next);
  $('#item-' + item_id + ' input[name="' + item_id + '-title"]').val(title_next);
  $('#item-' + item_id + ' input[name="' + item_id + '-url"]').val(url_next);
  $('#item-' + item_id + ' a').html(title_next);
  $('#item-' + item_id + ' a').attr('href', url_next);
  
}

function aboutButtonMove(dit, direction) {
  var parent = $(dit).parent().parent();
  var item_id = parent.attr('id').substr(5)*1;
  
  var id = $('#item-' + item_id + ' input[name="' + item_id + '-id"]').val();
  var title = $('#item-' + item_id + ' input[name="' + item_id + '-title"]').val();
  var text = $('#item-' + item_id + ' textarea').val();
  
  var id_next = $('#item-' + (item_id + direction) + ' input[name="' + (item_id + direction) + '-id"]').val();
  var title_next = $('#item-' + (item_id + direction) + ' input[name="' + (item_id + direction) + '-title"]').val();
  var text_next = $('#item-' + (item_id + direction) + ' textarea').val();
  
  if ($('#item-' + item_id + ' h1').is(":visible") != $('#item-' + (item_id + direction) + ' h1').is(":visible")) {
    if ($('#item-' + item_id + ' h1').is(":visible")) {
      $('#item-' + (item_id + direction) + ' h1').html($('#item-' + item_id + ' .input[name="title"]').val());
      $('#item-' + (item_id + direction) + ' .textbox').html($('#item-' + item_id + ' .input[name="text"]').val());
      $('#item-' + (item_id + direction) + ' h1').show();
      $('#item-' + (item_id + direction) + ' .textbox').show();
      $('#item-' + (item_id + direction) + ' .input').hide();
      $('#item-' + item_id + ' h1').hide();
      $('#item-' + item_id + ' .textbox').hide();
      $('#item-' + item_id + ' .input').show();
    } else {
      $('#item-' + item_id + ' h1').html($('#item-' + item_id + ' .input[name="title"]').val());
      $('#item-' + item_id + ' .textbox').html($('#item-' + item_id + ' .input[name="text"]').val());
      $('#item-' + item_id + ' h1').show();
      $('#item-' + item_id + ' .textbox').show();
      $('#item-' + item_id + ' .input').hide();
      $('#item-' + (item_id + direction) + ' h1').hide();
      $('#item-' + (item_id + direction) + ' .textbox').hide();
      $('#item-' + (item_id + direction) + ' .input').show();
    }
  
  }
  
  $('#item-' + (item_id + direction) + ' input[name="' + (item_id + direction) + '-id"]').val(id);
  $('#item-' + (item_id + direction) + ' input[name="' + (item_id + direction) + '-title"]').val(title);
  $('#item-' + (item_id + direction) + ' textarea').val(text);
  $('#item-' + (item_id + direction) + ' h1').html(title);
  $('#item-' + (item_id + direction) + ' .textbox').html(text);
  
  $('#item-' + item_id + ' input[name="' + item_id + '-id"]').val(id_next);
  $('#item-' + item_id + ' input[name="' + item_id + '-title"]').val(title_next);
  $('#item-' + item_id + ' textarea').val(text_next);
  $('#item-' + item_id + ' h1').html(title_next);
  $('#item-' + item_id + ' .textbox').html(text_next);
  
  Cufon.refresh();
  inputHeight($('#item-' + item_id + ' textarea'));
  inputHeight($('#item-' + (item_id + direction) + ' textarea'));
  
}

function linksButtonEdit(dit) {
  var parent = $(dit).parent().parent();
  var item_id = parent.attr('id').substr(5)*1;
  
  if ($('#item-' + item_id + ' a').is(":visible")) {
    $('#item-' + item_id + ' a').hide();
    $('#item-' + item_id + ' .input').show();
  } else {
    var title = $('#item-' + item_id + ' .input[name="' + item_id + '-title"]').val();
    var url = $('#item-' + item_id + ' .input[name="' + item_id + '-url"]').val();
    
    if (title == '' || title == 'Titel') {
      $('#item-' + item_id + ' .input[name="' + item_id + '-title"]').focus();
    } else if (url == '' || url == 'URL' || !validateURL(url)) {
      $('#item-' + item_id + ' .input[name="' + item_id + '-url"]').focus();
    } else {
      $('#item-' + item_id + ' a').html(title);
      $('#item-' + item_id + ' a').attr('href', url);
      $('#item-' + item_id + ' a').show();
      $('#item-' + item_id + ' .input').hide();
    }
  }
  
}

function aboutButtonEdit(dit) {
  var parent = $(dit).parent().parent();
  var item_id = parent.attr('id').substr(5)*1;
  
  if ($('#item-' + item_id + ' h1').is(":visible")) {
    $('#item-' + item_id + ' h1').hide();
    $('#item-' + item_id + ' .textbox').hide();
    $('#item-' + item_id + ' .input').show();
    inputHeight($('#item-' + item_id + ' textarea'));
  } else {
    var title = $('#item-' + item_id + ' .input[name="' + item_id + '-title"]').val();
    var text = $('#item-' + item_id + ' textarea').val();
    //text = text.replace(/&lt;/gi, '<')
    
    if (title == '' || title == 'Titel') {
      $('#item-' + item_id + ' .input[name="' + item_id + '-title"]').focus();
    } else if (text == '') {
      $('#item-' + item_id + ' textarea').focus();
    } else {
      $('#item-' + item_id + ' h1').html(title);
      $('#item-' + item_id + ' .textbox').html(text);
  
      Cufon.refresh();
      $('#item-' + item_id + ' h1').show();
      $('#item-' + item_id + ' .textbox').show();
      $('#item-' + item_id + ' .input').hide();
    }
  }
  
}

function linksButtonDelete(dit) {
  var parent = $(dit).parent().parent();
  var item_id = parent.attr('id').substr(5)*1;
  
  if (parent.attr('class') == 'item') {
    var delete_id = $('#item-' + item_id + ' input[name="' + item_id + '-id"]').val();
    $('#delete_block').val($('#delete_block').val() + delete_id + ',');
  
  }
  
  $('#item-' + item_id).remove();
  item_id++;
  while ($('#item-' + item_id).length > 0) {
    $('#item-' + item_id + ' input').each(function (i) {
      $(this).attr('name', (item_id - 1) + $(this).attr('name').substr((item_id + '').length));
    });
    $('#item-' + item_id).attr('id', 'item-' + (item_id - 1));

    item_id++;
  }
  
  $('.list .button-up').show();
  $('.list .button-down').show();
  $('#item-1 .button-up').hide();
  $('.list .button-down').last().hide();

}

function aboutButtonDelete(dit) {
  var parent = $(dit).parent().parent();
  var item_id = parent.attr('id').substr(5)*1;
  
  if (parent.attr('class') == 'item') {
    var delete_id = $('#item-' + item_id + ' input[name="' + item_id + '-id"]').val();
    $('#delete_block').val($('#delete_block').val() + delete_id + ',');
  
  }
  
  $('#item-' + item_id).remove();
  item_id++;
  while ($('#item-' + item_id).length > 0) {
    $('#item-' + item_id + ' input, #item-' + item_id + ' textarea').each(function (i) {
      $(this).attr('name', (item_id - 1) + $(this).attr('name').substr((item_id + '').length));
    });
    $('#item-' + item_id).attr('id', 'item-' + (item_id - 1));

    item_id++;
  }
  
  $('.list .button-up').show();
  $('.list .button-down').show();
  $('#item-1 .button-up').hide();
  $('.list .button-down').last().hide();
  $('.divider').show();
  $('.divider').last().hide();

}

function resetLinksActions(item) {
  if (item != '') {
    item = item + ' ';
  }
  
  $(item + '.button-up').click(function() {
    linksButtonMove(this, -1);
  });
  
  $(item + '.button-down').click(function() {
    linksButtonMove(this, 1);
  
  });
  
  $(item + '.button-delete').click(function () {
    linksButtonDelete(this);
  });
  
  $(item + '.button-edit').click(function () {
    linksButtonEdit(this);
  });
  
  $('.list .button-up').show();
  $('.list .button-down').show();
  $('#item-1 .button-up').hide();
  $('.list .button-down').last().hide();

}

function resetAboutActions(item) {
  if (item != '') {
    item = item + ' ';
  }
  
  $(item + '.button-up').click(function() {
    aboutButtonMove(this, -1);
  });
  
  $(item + '.button-down').click(function() {
    aboutButtonMove(this, 1);
  
  });
  
  $(item + '.button-delete').click(function () {
    aboutButtonDelete(this);
  });
  
  $(item + '.button-edit').click(function () {
    aboutButtonEdit(this);
  });
  
  $('.list .button-up').show();
  $('.list .button-down').show();
  $('#item-1 .button-up').hide();
  $('.list .button-down').last().hide();
  $('.divider').last().hide();

}

function initLinks() {
  $('#item-new .input').css({color: '#afafaf', fontStyle: 'italic'});
  $('#item-new .input').focus(function() {
    if ($(this).css('color') != 'rgb(0, 0, 0)' && ($(this).val() == 'Titel' || $(this).val() == 'URL')) {
      $(this).val('');
    }
    $(this).css({color: '#000', fontStyle: 'normal'});
  });
  
  $('#item-new .button-add').click(function () {
    var inner = $('#item-new').html();
    var position = $('.list').children().size() + 1;
    var title = $('#item-new .input[name="title"]').val();
    var url = $('#item-new .input[name="url"]').val();
    
    if (title == '' || title == 'Titel') {
      $('#item-new .input[name="title"]').focus();
    } else if (url == '' || url == 'URL' || !validateURL(url)) {
      $('#item-new .input[name="url"]').focus();
    } else {
      inner = inner.replace(/name="/g,  'name="' + position + '-');
      inner = '<li id="item-' + position + '">' + inner + '</li>';
      $('.list .button-down').last().show();
      $('.list').append(inner);
      $('.list .button-down').last().hide();
      
      $('#item-' + position + ' .button-add').remove();
      $('#item-' + position + ' a').html(title);
      $('#item-' + position + ' a').attr('href', url);
      $('#item-' + position + ' .input[name="' + position + '-title"]').val(title);
      $('#item-' + position + ' .input[name="' + position + '-url"]').val(url);
      $('#item-new .input[name="title"]').val('Titel');
      $('#item-new .input[name="url"]').val('URL');
      
      var item = '#item-' + position + ' ';
  
      $(item + '.button-up').click(function() {
        linksButtonMove(this, -1);
      });
      
      $(item + '.button-down').click(function() {
        linksButtonMove(this, 1);
      
      });
      
      $(item + '.button-delete').click(function () {
        linksButtonDelete(this);
      });
      
      $(item + '.button-edit').click(function () {
        linksButtonEdit(this);
      });
      
      $('#item-new .input').css({color: '#afafaf', fontStyle: 'italic'});
      $('#item-new .input').focus(function() {
        if ($(this).css('color') != 'rgb(0, 0, 0)' && ($(this).val() == 'Titel' || $(this).val() == '<p></p>')) {
          $(this).val('');
        }
        $(this).css({color: '#000', fontStyle: 'normal'});
      });
    }
    
  });
  
  resetLinksActions('');
  
}

function initAbout() {
  $('#item-new .input').css({color: '#afafaf', fontStyle: 'italic'});
  $('#item-new .input').focus(function() {
    if ($(this).css('color') != 'rgb(0, 0, 0)' && ($(this).val() == 'Titel' || $(this).val() == '<p></p>')) {
      $(this).val('');
    }
    $(this).css({color: '#000', fontStyle: 'normal'});
  });
  
  inputHeight($('#item-new textarea'));
  
  $('#item-new .button-add').click(function () {
    var inner = $('#item-new').html();
    var position = $('.list').children().size() + 1;
    var title = $('#item-new .input[name="title"]').val();
    var text = $('#item-new textarea').val();
    
    if (title == '' || title == 'Titel') {
      $('#item-new .input[name="title"]').focus();
    } else if (text == '' || text == '<p></p>') {
      $('#item-new textarea').focus();
    } else {
      inner = inner.replace(/name="/g,  'name="' + position + '-');
      inner = '<div id="item-' + position + '" class="item">' + inner + '</div>';
      $('.list .button-down').last().show();
      $('.list').append(inner);
      $('.list .button-down').last().hide();
      
      $('#item-' + position + ' .button-add').remove();
      $('#item-' + position + ' h1').html(title);
      $('#item-' + position + ' .textbox').html(text);
      $('#item-' + position + ' .input[name="' + position + '-title"]').val(title);
      $('#item-' + position + ' textarea').val(text);
      $('#item-new .input[name="title"]').val('Titel');
      $('#item-new textarea').val('<p></p>');
      
      var item = '#item-' + position + ' ';
  
      $(item + '.button-up').click(function() {
        aboutButtonMove(this, -1);
      });
      
      $(item + '.button-down').click(function() {
        aboutButtonMove(this, 1);
      });
      
      $(item + '.button-delete').click(function () {
        aboutButtonDelete(this);
      });
      
      $(item + '.button-edit').click(function () {
        aboutButtonEdit(this);
      });
      $('.divider').show();
      $('.divider').last().hide();
      $('#item-new .input').css({color: '#afafaf', fontStyle: 'italic'});
      $('#item-new .input').focus(function() {
        if ($(this).css('color') != 'rgb(0, 0, 0)' && ($(this).val() == 'Titel' || $(this).val() == '<p></p>')) {
          $(this).val('');
        }
        $(this).css({color: '#000', fontStyle: 'normal'});
      });
    }
    
  });
  
  resetAboutActions('');
  
}

function initHome() {
  $('#item-1 .button-up').hide();
  $('.button-down').last().hide();
  $('.news-title, .news-time, .youtube-title, .youtube-content, .image-title').css({fontSize: '10px', color: '#afafaf', fontStyle: 'italic'});
  $('.news-day, .news-month, .news-year').css({color: '#afafaf', fontStyle: 'italic'});
  $('.news-day, .news-month, .news-year').focus(function() {
    $(this).css({color: '#000', fontStyle: 'normal'});
  });
  $('.news-title, .news-time, .youtube-title, .youtube-content, .image-title').focus(function() {
    if ($(this).css('color') != 'rgb(0, 0, 0)' && $(this).val() == 'Titel') {
      $(this).val('');
    }
    $(this).css({fontSize: '14px', color: '#000', fontStyle: 'normal'});
  });
  
  $('.news-item').hover(function () {
    $('#' + $(this).attr('id') + ' .controls').fadeIn('fast');
  },
  function () {
    $('#' + $(this).attr('id') + ' .controls').fadeOut('fast');
    
  });
  
  $('.news-item p').hover(function () {
    $(this).animate({ backgroundColor: '#e8e6da' }, 'fast' );
  },
  function () {
    $(this).animate({ backgroundColor: '#faf8eb' }, 'fast' );
    
  });
  
  $('.news-time').keypress(function (e) {
    if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
      return false;
    }
  });
  
  resetActions('');
  
  $('#publish').click(function () {
    $('#draft').val('0');
  });
  
  $('#add-text').click(function () {
    addNewsField('text');
    
    return false;
  });
  
  $('#add-youtube').click(function () {
    addNewsField('youtube');
    
    $('.youtube-title, .youtube-content').focus(function() {
      if ($(this).css('color') != 'rgb(0, 0, 0)') {
        $(this).val('');
      }
      $(this).css({fontSize: '14px', color: '#000', fontStyle: 'normal'});
    });
    
    return false;
  });
  
  $('#add-image').click(function () {
    addNewsField('image');

    $('.image-title').focus(function() {
      if ($(this).css('color') != 'rgb(0, 0, 0)') {
        $(this).val('');
      }
      $(this).css({fontSize: '14px', color: '#000', fontStyle: 'normal'});
    });
    
    return false;
  });
}

function initAgenda() {
  $('.divider').last().hide();
  $('.agenda-edit-title').css({fontSize: '24px', color: '#afafaf', fontStyle: 'italic'});
  $('.agenda-edit-time, .agenda-edit-field').css({fontSize: '10px', color: '#afafaf', fontStyle: 'italic'});
  $('.agenda-edit-textarea').css({color: '#afafaf', fontStyle: 'italic'});
  $('.agenda-edit-day, .agenda-edit-month, .agenda-edit-year').css({color: '#afafaf', fontStyle: 'italic'});
  $('.agenda-edit-day, .agenda-edit-month, .agenda-edit-year').focus(function() {
    $(this).css({color: '#000', fontStyle: 'normal'});
  });
  $('.agenda-edit-title').focus(function() {
    if ($(this).css('color') != 'rgb(0, 0, 0)' && $(this).val() == 'Titel') {
      $(this).val('');
    }
    $(this).css({fontSize: '36px', color: '#000', fontStyle: 'normal'});
  });
  $('.agenda-edit-time, .agenda-edit-field').focus(function() {
    if ($(this).css('color') != 'rgb(0, 0, 0)' && ($(this).val() == 'Titel' || $(this).val() == 'Subtitel' || $(this).val() == 'Lokatie' || $(this).val() == 'Beschrieving')) {
      $(this).val('');
    }
    $(this).css({fontSize: '14px', color: '#000', fontStyle: 'normal'});
  });
  $('.agenda-edit-textarea').focus(function() {
    if ($(this).css('color') != 'rgb(0, 0, 0)' && ($(this).val() == 'Beschrieving')) {
      $(this).val('');
    }
    $(this).css({color: '#000', fontStyle: 'normal'});
  });
  
  inputHeight($('.agenda-edit-textarea'));
  
  $('.agenda-edit-time').keypress(function (e) {
    if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
      return false;
    }
  });
  
  $('#publish').click(function () {
    $('#draft').val('0');
  });
}

function initContact() {
  $('.content-send').dblclick(function () {
    var obj = $(this)[0];
    var first = $('.content-send').first()[0];
    var last = $('.content-send').last()[0];
    
    var id = $(this).children().first().val();
    
    $(this).remove();
    $.get('ajax/functions.php?function=set_read&id=' + id);

    if (obj == first && obj == last) {
    } else if ($('.content-send').length == 1) {
      $('.content-send').first().attr('id', 'one');
    } else if (obj == first) {
      $('.content-send').first().attr('id', 'first');
    } else if (obj == last) {
      $('.content-send').last().attr('id', 'last');
    }
  });

  if ($('#name').val() != '') {
    $('#name').css({backgroundColor: '#f0f0f0'});
  }
  if ($('#mail').val() != '') {
    $('#mail').css({backgroundColor: '#f0f0f0'});
  }
  if ($('#message').val() != '') {
    $('#message').css({backgroundColor: '#f0f0f0'});
  }
  if ($('#control').val() != '') {
    $('#control').css({backgroundColor: '#f0f0f0'});
  }
  
}

function initLogin() {
  if ($('#username').val() != '') {
    $('#username').css({backgroundColor: '#f0f0f0'});
  }
  if ($('#password').val() != '') {
    $('#password').css({backgroundColor: '#f0f0f0'});
  }
  
  $('#username').keyup(function() {
    if ($('#username').val() != '') {
      $('#username').css({backgroundColor: '#f0f0f0'});
    }
    if ($('#password').val() != '') {
      $('#password').css({backgroundColor: '#f0f0f0'});
    }
  });
  
  $('#password').keyup(function() {
    if ($('#username').val() != '') {
      $('#username').css({backgroundColor: '#f0f0f0'});
    }
    if ($('#password').val() != '') {
      $('#password').css({backgroundColor: '#f0f0f0'});
    }
  });
  
}
      
function inputHeight(object) {
  object.height(0);
  $('#new-text').height(0);
  object.height(object.attr('scrollHeight'));
  $('#new-text').height(object.attr('scrollHeight') + 14);
  
}

$(document).ready(function() {
  $('#keyword-search').focus(function() {
    inputFocus($('#keyword-search'));
    
  });

  $('#keyword-search').blur(function() {
    inputBlur($('#keyword-search'));
    
  });
  
  $('#keyword-search').keydown(function() {
    inputClear($('#keyword-search'));
    
  });
  
  $('#clear-search').mousedown(function () {
    $('#keyword-search').val('');
    inputClear($('#keyword-search'));
    $('#keyword-search').focus();
    
  });
  
});	
