jQuery().ready(function(){
  
  $('.info-link').click(function() {
    var infobox = $(this).parent().parent().parent().find('.extra-info');
    $(this).toggleClass('info-link-on');
    $(infobox).slideToggle();
  });
  
  $('ul.form li, ul.form-reg li, ul.form-login li, ul.form-search li').addClass('clearfix');
  
  $('a').click(function() { this.blur(); });
  $('a').focus(function() { this.blur(); });
  
  $('a.external').each(function() { $(this).attr('target', "_blank"); });
  
  $("#comment_body").blur(function() { $(this).val( replaceNewline( jQuery.trim($(this).val()) ) ); });  
  $("#comment_body").charCounter(600, { container: "#comment_body_count", format: "Nog <b>%1</b> karakters over." });
  
  /* Char count */
  $("#shop_description").blur(function() { $(this).val( replaceNewline( jQuery.trim($(this).val()) ) ); });
  $("#shop_description").charCounter(600, { container: "#shop_description_count", format: "Nog <b>%1</b> karakters over.</b>" });
  
    /* Char count */
  $("#product_description").blur(function() { $(this).val( replaceNewline( jQuery.trim($(this).val()) ) ); });
  $("#product_description").charCounter(600, { container: "#product_description_count", format: "Nog <b>%1</b> karakters over.</b>" });
  
  $("#news_intro").blur(function() { $(this).val( replaceNewline( jQuery.trim($(this).val()) ) ); });
  $("#news_intro").charCounter(400, { container: "#news_intro_count", format: "Nog <b>%1</b> karakters over." });
  
  $("#news_body").blur(function() { $(this).val( replaceMultiNewline( jQuery.trim($(this).val()) ) ); });
  $("#news_body").charCounter(5000, { container: "#news_body_count", format: "Nog <b>%1</b> karakters over." });
  
  var searchLabel = $("#keyword").val();
  $("#keyword").focus(function() { $(this).val().substr(0, 6) != 'Zoeken'||$(this).val(''); });
  $("#keyword").blur(function() { $(this).val() != ''||$(this).val(searchLabel); });
  
  //$("#shop_keywords").blur(function() { $(this).val( $(this).val().replace(/&|@|\#|"/gi, '') ); });
  $("#shop_keywords, #news_keywords").blur(function() {$(this).val( cleanKeywords( $(this).val()) ); });
  
  $("input[name='pay_freq']").click(function() {
        showPremiumInfo($(this).attr('id'));
    });
  
});

function showPremiumInfo(id)
{
    var item = $("#"+id);
    item.parent().parent().parent().find('.premium-more-info').remove();
    
    item.parent().parent().siblings().removeClass('selected-pay-freq');
    item.parent().parent().addClass('selected-pay-freq');
        
    //var info = item.attr('title');
    //item.parent().parent().after('<li class="clearfix premium-more-info"><div>' + info + '</div></li>');
    //item.parent().parent().find('label').append('<span class="premium-more-info">' + info + '</span>');
}

function toggleKeywords(ref, id)
{
  $('#tags-' + id).toggle(); $(ref).html( $(ref).html()=='(+)'?'(-)':'(+)' );
}

function cleanKeywords(str)
{  
  var s=str;
  
  var rExps=[ /[\xC0-\xC2]/g, /[\xE0-\xE2]/g,
  /[\xC8-\xCA]/g, /[\xE8-\xEB]/g,
  /[\xCC-\xCE]/g, /[\xEC-\xEE]/g,
  /[\xD2-\xD4]/g, /[\xF2-\xF4]/g,
  /[\xD9-\xDB]/g, /[\xF9-\xFB]/g ];
  
  var repChar=['A','a','E','e','I','i','O','o','U','u'];
  
  for(var i=0; i<rExps.length; i++)
  s=s.replace(rExps[i],repChar[i]);
  
  s = s.replace(/[^a-z0-9\s-']/gi, '');
  
  return s;
}

function replaceMultiNewline(str)
{
  s = str.replace(/\n{3,}/g,"\n\n");
  return s;
}

function replaceNewline(str)
{
  s = str.replace(/\n/g,' ');
  s = s.replace(/\s/g,' ').replace(/  ,/g,' ');
  s = s.replace(/\s+/g," ");
  return s;  
}