// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Activate all facebox links
$(document).ready(function(){
                    $('a[rel*=facebox]').facebox();
                  });

// Paint zebras
function paint_zebras(elements){
  if(!elements || typeof(elements) == 'undefined'){
    elements = $('[rel*=zebra]');
  }

  elements.each(function(i){
                  if(i%2 == 0){
                    $(this).removeClass("odd");
                    $(this).addClass("even");
                  } else {
                    $(this).removeClass("even");
                    $(this).addClass("odd");
                  }
                });
}

// Paint tooltips for help
function geo_help_tooltip( id, content_html, width ){
  var options = { content: content_html,
                  show: 'mouseover',
                  hide: 'mouseout',
                  position:{ corner: { target: 'topRight',
                                        tooltip: 'bottomLeft'
                                      }
                  },
                  style:{ background:'#ddd6d2',
                          name: 'light',
                          color: '#664533',
                          tip: true,
                          width:width,
                          border:{ width: 1,
                                   radius: 3,
                                   color: '#ff6800'
                                   }
                           }
                  };
  $('#'+id).qtip(options);
}

$(document).ready(function(){
                    paint_zebras();
                  });

// Date picker options
function load_datepickers(){
    var options = {
        changeYear:true,
        changeMonth:true,
        dateFormat:'dd/mm/yy',
        yearRange:'-100:+20',
        onSelect: function(dateText, inst){
          var new_val = dateText.split('/').reverse().join('-');
          $(this).siblings('input[type=hidden]').val(new_val);
          if($(this).attr('onchange')){
            $(this).attr('onchange')();
          }
          return true;
        }
      };

    $("[rel^=datepicker]").each(function(){
      var original_id = $(this).attr('id');
      var original_name = $(this).attr('name');
      var curr_val = $(this).val();

      $(this).attr('readonly', true);
      $(this).attr('id', original_id+'_datepicker');
      $(this).attr('name', original_name+'_datepicker');

      if (curr_val.length > 0){
        curr_val = curr_val.replace(/^(\d{4}-\d{2}-\d{2}).*$/, "$1"); // remove time 00:00:00 if exists
      }
      $(this).parent().append('<input type="hidden" id="'+original_id+'" name="'+original_name+'" value="'+curr_val+'" />');

      $(this).datepicker(options);

      if (curr_val.length > 0){
        $(this).val(curr_val.replace(/^(\d{4})-(\d{2})-(\d{2}).*$/, "$3/$2/$1"));
      }
    });
}

$(document).ready(function(){
                    load_datepickers();
                  });

