

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(400, { 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(800, { container: "#shop_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." });
    
    $("#keyword").focus(function() { $(this).val() != 'Zoeken...'||$(this).val(''); });
    $("#keyword").blur(function() { $(this).val() != ''||$(this).val('Zoeken...'); });
    
    $("#txtProductKw").focus(function() { $(this).val() != 'Zoeken...'||$(this).val(''); });
    $("#txtProductKw").blur(function() { $(this).val() != ''||$(this).val('Zoeken...'); });
    
    //$("#shop_keywords").blur(function() { $(this).val( $(this).val().replace(/&|@|\#|"/gi, '') ); });
    $("#shop_keywords, #news_keywords").blur(function() {$(this).val( cleanKeywords( $(this).val()) ); });
    
    /* newsletter */
    $("#newsletter_email").focus(function() { $(this).val() != 'E-mail...'||$(this).val(''); });
    $("#newsletter_email").blur(function() { $(this).val() != ''||$(this).val('E-mail...'); });
    
    
    $("input[name='pay_freq']").click(function() {
        showPremiumInfo($(this).attr('id'));
    });
    
    $(".form-reg #account_type").change(function() {
    
        if ($(this).val() == 1) {
            $("#premium-select-info").show();
            } else {
            $("#premium-select-info").hide();
        }
        
        showPremiumInfo('premium_type_6');
        $("#premium_type_6").attr('checked', 'checked');
    
    });
    
    $("#btn_sub_newsletter").click(function() {
    
        var s = $("#newsletter-status");
        var e = $("#newsletter_email").val();
        $.getJSON("/news-letter/signup", {email: e}, function(data) {
        
            s.html(data.msg).show();
            
            if (data.success == 1) {
                s.removeClass('status-nok').addClass("status-ok");
                $("#newsletter_email").val("");
            } else {
                s.removeClass('status-ok').addClass("status-nok");
            }
            
            $("#newsletter-status").delay(4000, function() {
                $(this).html('').hide("slow");
            });
            
        });
    });
    
    if($("#btn-show-premium-info").length||($("#btn-show-premium-info-select").length)) {
        $("#btn-show-premium-info, #btn-show-premium-info-select").fancybox({
            'scrolling'		: 'no',
            'titleShow'		: false
        });
    }
    
    $('#frmSearchTop').submit(function(){
        v = $.trim($('#keyword').val());
        if (v == 'Zoeken...') {
            return false;
        }
    });

});

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>');
    if (info) {
        item.parent().parent().find('label').append('<span class="premium-more-info">' + info + '</span>');
    }
}
    
$.fn.delay = function(time, callback)
{
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}

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;  
}

function loadPoll(id)
    {
        $.post('/poll/get', {divId: id}, function(response) {
            $('#' + id).html(response);
        }, 'json');
    }

function fav() {
var newT = document.createTextNode('');
var s = document.getElementById('fav');
if (window.sidebar) {
s.appendChild(newT);
s.style.cursor = 'pointer';
s.onclick = function() {window.sidebar.addPanel(document.title,self.location,'')};
} else if (window.external) {
s.appendChild(newT);
s.style.cursor = 'pointer';
s.onclick = function() {window.external.AddFavorite(self.location,document.title)};
} else if (window.opera) {
s.appendChild(newT);
s.style.cursor = 'pointer';
s.onclick = function() {
var e = document.createElement('a');
e.setAttribute('href',self.location);
e.setAttribute('title',document.title);
e.setAttribute('rel','sidebar');
e.click();
}
}
}

var pageLoaded = 0;
window.onload = function() {pageLoaded = 1;}
function loaded(i,f) {
if (document.getElementById && document.getElementById(i) != null) f();
else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
}
loaded('fav',fav);

$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});


