var Util = new function()
{
  
  var dialog_decoration_height = 60;
	
  this.init = function(opt)
  {
    Util.options = opt || {};
  };
  
  this.getCookie = function(key, default_val)
  {
    var val = $.cookie(key);
    return val === null ? default_val || null : val;
  };
  this.setCookie = function(key, val)
  {
    $.cookie(key, val);
  };
  
  this.action2href=function(action)
  {
    return Util.options.script_name+action;
  };
  this.light2href=function(action)
  {
    return Util.options.light_script_name+"?do="+action.replace("/", "--")+"--"+Util.options.light_code;
  };
  this.subs=function(str,nb)
  {
    return str.substring(0,str.length-nb);
  };
  this._fn=function(fn)
  {
    return $.isFunction(fn) ? fn : new Function();
  };
  this.img_src=function(img)
  {
    return Util.options.relative_url_root+Util.options.image_dir+img;
  };
  this.img=function(img, alt)
  {
    alt = alt || "";
    return '<img src="'+Util.img_src(img)+'" alt="'+alt+'" />';
  };
  this.preload = function(images)
  {
    for(var i = 0; i<images.length; i++)
      $("<img>").attr("src",Util.img_src(images[i]));
  };
  this.in_array = function(needle, haystack)
  {
    for (var i in haystack)
    {
      if (haystack[i] == needle) {
        return true;
      }
    }
    return false;
  };
	
  this.track=function(delai)
  {
    setTimeout(Util.urchinTrack, delai||300);
  };
  this.urchinTrack=function(page){
    var err="";
    try {
			var pageTracker = _gat._getTracker(Util.options.urchin_tracker);
			pageTracker._trackPageview();
		} 
		catch (e) {
			err += e;
			dbg('urchinTracker() ' + err + " " + Util.options.urchin_tracker);
		}
  };
};


jQuery.fn.extend({

  orNot: function() {
    return this.length ? this : false;
  },
	
	// pas de tabs dans ce textarea
	codeArea: function(opt, e)
	{
		var $$ = this;
    opt = opt || {};
		var tab = opt.tab || " ";
		$$.bind("keypress", function(evt)
		{
      // Tab key - insert tab expansion
      if (evt.keyCode == 9) {
        evt.preventDefault();
				
				for(var it=0; it<2; it++)  // deux espaces sont ajoutés
				{
		      var t = evt.target;
		      var ss = t.selectionStart;
		      var se = t.selectionEnd;
					
					t.selectionStart = t.selectionStart + tab;
					
					// "Normal" case (no selection or selection on one line only)
					t.value = t.value.slice(0, ss).concat(tab).concat(t.value.slice(ss, t.value.length));
					if (ss == se) {
						t.selectionStart = t.selectionEnd = ss + tab.length;
					}
					else {
						t.selectionStart = ss + tab.length;
						t.selectionEnd = se + tab.length;
					}
				}
      }
		});
		return $$;
	},
  // construit un ui.dialog
  dms_dialog: function( opt, e ) {
		var $$ = this;
    // si une ancienne version du dialog existe, on la supprime
    if (opt.class_name)
      $("."+opt.class_name.replace(/ /gi, ".")).remove();
    opt.width = opt.width || 410;
    opt.height = $$.show().outerHeight() + Util.dialog_decoration_height ;
		opt.dragStart = function()
    {
      var dialog_h = $$.parent().height();
      $$.hide();
      $$.parent().height(dialog_h);
    };
    opt.dragStop = function() {
      $$.show();
      $$.parent().css("height", "auto");
    };
    opt.autoResize = false;
    opt.position = "center";
    $$.dialog(opt);
		$.extend($$,
		{
      dmsDialogClose: function()
	    {
				$$.parent().parent().find("a.ui-dialog-titlebar-close").trigger("click");
	    }
		});
		
		var $dialog = $$.parent().parent().css({top: 150, left: ($(window).width() - 410) / 2});
		var $title = $dialog.find('div.ui-dialog-titlebar');
    var $content = $dialog.find('div.ui-dialog-content');
		
    $dialog.addClass(opt.class_name).css("position", "fixed").append('<div class="dialog-bottom">');
    
    // minify ( dblclik sur le title )
    $title.rebind("dblclick", function() {
      if ($content.is(':visible'))
        $content.hide();
      else
        $content.show();
    });
    
    $title.append('<a class="ui-dialog-titlebar-toggle dialog_toggle"><span>-</span></a>');
    $title.find("a.ui-dialog-titlebar-toggle").rebind("click", function(e) {
      e.stopPropagation();
      $dialog.find('div.ui-dialog-titlebar').trigger("dblclick");
      return false;
    });
    
    if (typeof(DmsEdit) != "undefined")
		  DmsEdit.overFlash();
    return $$;
  },
  rebind: function( type, data, fn ) {
    return this.unbind(type, fn).bind( type, data, fn );
  },
  loader: function( size, image, texte ) {
    size = size || 16;
    image = image || "loader.gif";
    texte = texte || "";
    return this.html("<div class='dms dms_loader'>"+Util.img(size+"/"+image)+texte+"</div>");
  }
});

function dbg()
{
  if (!page_options.is_dev)
  {
    return;
  }
  try {
  	if ("console" in window && 'firebug' in console && page_options.is_dev)
  	{
    	console.debug.apply('', arguments);
    }
  }
  catch(e)
  {
    for(var i in arguments)
    {
      if (i<5) alert(arguments[i]);
    }
  }
};
