(function($){

$('div[@id^="insertBefore"]').each(function() {
    var node = $(this);
    var target = $('input[@id$="'+node.attr('id').split('-')[1]+'"]');
    if (target.length) {
        target.parents('div.field').before(node.html());
    }
});

$('.largetooltiped').click(function() {
  var modal = $('#modal');
  if (modal.length == 0) {
    $('body').append('<div id="modal" style="display:none"></div>');
    var modal = $('#modal');
  }
  modal.empty();
  var tooltip = $('#' + $(this).attr('tooltip'));
  tooltip.clone().show().appendTo('#modal');
  modal.modal();
});

$('.tooltipped').tooltip({
    bodyHandler: function() {
        var node = $('#' + $(this).attr('tooltip'));
        if (/^\//.exec(node.text())) {
            return $.ajax({
              url: node.text(),
              async: false
            }).responseText;
        } else {
            return node.html();
        }
    },
    showURL: false
});

$('.pdftooltipped').click(function() {
  location = $('#' + $(this).attr('tooltip')).html();
});

var Field = function(node) {
    if (node && !node.hasClass('field')) {
        node = node.parents('div.field');
    }
    this.node = node;
}

Field.prototype = {
    html: function(label, value) {
        this.node = $(''+
                '<div class="field">'+
                '  <div class="label_wrapper">'+
                '    <label>'+label+' :</label>'+
                '  </div>'+
                '  <div class="readonly_field_wrapper">'+
                value+
                '  </div>'+
                '  <div class="field_errors">'+
                '  </div>'+
                '</div>');
        return this.node;
    },
    id: function(value) {
        if (!value) return $('input', this.node).attr('id');
        this.node.attr('id', value);
    },
    val: function(value) {return $('input', this.node).val(value)},
    error: function(message) {
        $('div.field_error', this.node).remove();
        if (message && message.error)
            message = message.error;
        if (message)
            $('div.field_errors', this.node)
                .html('<div class="field_error">'+message+'</div>');
    }
}

$.extend({
    getField: function(node) {
        return new Field(node);
    },
    formatFloats: function() {
        $('div.FloatFieldRenderer').each(function(){
            $('input', this).toFloat();
            $('input', this).change(function() {
                if (! /^[0-9.,]+$/.test($(this).val())) {
                    var p = $(this).parents('.field_wrapper');
                    $('.field_errors', p.parent()).append('<div>La valeur doit être un nombre</div>');
                } else {
                    var p = $(this).parents('.field_wrapper');
                    $('.field_errors', p.parent()).empty();
                    $(this).toFloat();
                }
            });
        });
    },
    toFloat: function(val, as_float){
        val = val.replace(',', '.');
        val = val.split('.');
        var before = val[0];
        var after = '00';
        if (val.length > 1) {
            var after = val[1];
            if (after.length == 1) {
                after = after.substring(0,1) + '0';
            } else if (after.length > 1) {
                after = after.substring(0,2);
            } else {
                after = '00';
            }
        }
        if (as_float) {
            return parseFloat(''+before+'.'+after);
        } else {
            return ''+before+','+after;
        }
    }
});

$.fn.extend({
    toFloat: function() {
        $(this).each(function() {
            var node = $(this);
            node.val($.toFloat(node.val()));
        });
        return this;
    },
    floatVal: function() {
        if (this.length==0) return 0.0
        node = $(this[0]);
        return $.toFloat(node.val(), true);
    },
    toInt: function() {
        $(this).each(function() {
            var node = $(this);
            node.val(''+parseInt($.toFloat(node.val())));
        });
        return this;
    },
    intVal: function() {
        if (this.length==0) return 0.0
        node = $(this[0]);
        return parseInt($.toFloat(node.val()));
    }
});

$('#main_bar_search').submit(function(){
    $.get('/entreprises/search', {name: $('#main_bar_search input.search').val()},
        function(r) {
            if (r.length > 0) {
                document.location.pathname = r;
            }
            else {
                input = $('#main_bar_search input.search');
                input.val('Non trouv\u00e9').select();
            }
        });
    return false;
});

$('#main_bar_search input.search').focus(function(){
    $(this).val('').addClass('selected');
});

$('.datepicker').datepicker(
    $.extend({}, $.datepicker.regional["fr"], {
        'showStatus': true,
        'showOn': 'button'
}));

$('#reset_forms').click(function() {
    $("form input[type=text], form input[type=password]").val('');
});

$.formatFloats();

$('div.form_controls').show();

$('div.form_controls input[@value="Annuler"]').addClass('cancel');

if ($('#modal_block').length != 0) {
  $('#modal_block').modal();
  $('#modal_block').show();
  $('.modalClose').hide();
}

})(jQuery);

function load_main_bar($, id) {
    $.get('/entreprises/main_bar/' + id, function(r) {$('#main_bar').html(r)});
}
