var Util = new function()
{
  var options;
  
  this.init = function(opt)
  {
    Util.options = opt;
  }
  
  this.getHref = function(action_name)
  {
    eval("var action=Util.options.urls."+action_name);
    if (!action)  return null;
    return Util.action2href(action);
  }

  this.modal = function(msg)
  {
    alert(msg);
  }
  
  this.tip=function(dom, contexte) {
    $(dom, $(contexte)).Tooltip({track:false,delay:250,showURL:false,fixPNG:false,showBody:" - "});
  }
  
  this.action2href=function(action)
  {
    return Util.options.script_name+action;
  }
  this.subs=function(str,nb)
  {
    return str.substring(0,str.length-nb);
  }
  this._fn=function(fn)
  {
    if (!$.isFunction(fn))
      fn=new Function();
    return fn;
  }
  this.img_src=function(img)
  {
    return Util.options.relative_url_root+Util.options.image_dir+img;
  }
  this.img=function(img, alt)
  {
    var alt = alt || "";
    return '<img src="'+Util.img_src(img)+'" alt="'+alt+'" />';
  }
  this.preload = function()
  {
    for(var i = 0; i<arguments.length; i++)
    {
      $("<img>").attr("src",Util.img_src(arguments[i]));
    }
  }
  this.in_array = function(needle, haystack)
  {
    for (var i in haystack) { if (i == needle) return true;}
    return false;
  }
}


jQuery.fn.extend({
  rebind: function( type, data, fn ) {
    return this.unbind(type, fn).bind( type, data, fn );
  },
  // construit un ui.dialog
  dms_dialog: function( opt, e ) {
    // si une ancienne version du dialog existe, on la supprime
    if (opt.class_name)
      $("."+opt.class_name.replace(/ /gi, ".")).remove();
    opt.width = opt.width || 400;
    opt.height = opt.height || 0 ;
    if (!opt.position) {
      var left = ($(window).width()/2)- (opt.width/2);
      if (left < 10) left = 10;
      var top = $(window).scrollTop() + 80;
      if (top < 10) top = 10;
      opt.position = left+"|"+top;
      //opt.position = "center";
    }
    $dialog = this.dialog(opt);
    $dialog.parent().parent().addClass(opt.class_name);
    if (opt.height == 0)
      $("."+opt.class_name).height($dialog.outerHeight() + 120);
    return $dialog;
  },
  loader: function( image, texte ) {
    var image = image || "loader.gif";
    var texte = texte || "";
    return this.html("<div class='maplage_loader'>"+Util.img(image)+texte+"</div>");
  }
});

function dbg(o) {
  try { return $.log(o); }
  catch(e){alert(o);}
};
