/**
 * ALL JAVASCRIPT NEEDS TO BE IN SEPARATE NAMESPACES
 * pdc namespace 
 */

var pdc = {

    initCentsToEuros: function() {
        
        jq(".datagridwidget-table-view td.prijs div").each( function() {

            cents = jq(this).html()
            euros = parseInt(cents) / 100;

            jq(this).html("&euro; " + euros.toFixed(2).replace('.',','));
        }) 
        
    },

    /** 
     * Shows number of characters to go
     */ 
    initTextCounters: function() {

        // initialize text fields and counters

        if( jq("form#pdcproduct-base-edit input#title").val() != undefined ) { 

            var maxTitleLength = max = jq("input#title").attr('maxlength');
            var titleCharsToGo = maxTitleLength - jq("input#title").val().length;

            jq("div#title_help").append('<span id="pn_counter"> (' + titleCharsToGo + ' te gaan)</span>');

            var descrLen = jq("textarea#description").val().length;
            jq("div#description_help").append('<span id="descr_counter"> (' + descrLen + ')</span>');

            // create event handlers for fields

            // Title field counter (count remaining)
            jq("input#title").keyup(function(event) {
                curlength = jq(this).val().length;
                jq("span#pn_counter").text(' (' + (maxTitleLength - curlength) + ' te gaan)');
            })            

            // Description field counter
            jq("textarea#description").keyup(function(event) {
                curlength = jq(this).val().length;
                jq("span#descr_counter").text(" (" + curlength + ")");
            })            
            
            jq("textarea#text").keyup(function(event) {

                curlength = jq(this).val().length;
                charstogo = maxTextLength - curlength;

                if( charstogo < 0 ) { 
                    jq("span#t_counter").addClass('toomanychars');
                } else {
                    jq("span#t_counter").removeClass('toomanychars');
                }

                jq("span#t_counter").text(' (' + (charstogo) + ' te gaan)');
            })
        }
    },

    initPDC: function() {

      jq(".pdc-header .char").click(function() {

        //Selected class zodat we duidelijk kunnen maken welke letter is aangeklikt.
        jq(".pdc-header .selected").removeClass("selected");
        jq(this).toggleClass("selected")
        
        //Hide welke visible is
        jq(".pdc-products:visible").hide();

        //Toon pdc-products die bij geklikte hoort
        jq(".pdc-products#part-" + jq(this).text()).show();

        
      });

      jq(".pdc-header .char").eq(0).click();

      jq("#archetypes-fieldname-kosten input").each(function() {
          
          var inputId = "" + jq(this).attr("id");

          if (inputId.indexOf("ingangsdatum_kosten") === 0) {
            jq(this).datepicker({dateFormat: 'dd-mm-yy'});
          } else if (inputId.indexOf("einddatum_kosten") === 0) {
            jq(this).datepicker({dateFormat: 'dd-mm-yy'});
          }
        });
    }
}

jq(document).ready(function() {
    pdc.initTextCounters();
    pdc.initCentsToEuros();
    pdc.initPDC();
})


