/* jQuery++ */
(function($){
  $.fn.setCursorPosition = function(pos) {
    var obj = $(this).get(0);
    obj.focus();
	if (obj.setSelectionRange) {
		obj.setSelectionRange(pos, pos);
    } else if (obj.createTextRange) {
      var range = obj.createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
    }
  };
  
  encodeEntities = function (s) {
	    return $("<u/>").text(s).html(); // jQuery magic
  };
  
  $.fn.stabilo = function(filtre){
	  if (filtre !== "") {
		terms = filtre.split(' ');
		for (var i=0; i<terms.length; i++){
			term = new RegExp("(" + terms[i].replace('/', '\/') + ")", "gi");
	        var highlighted = "<em class=\"stabilo\">$1</em>";
	        this.find(":not(iframe, option, script, textarea)").contents().each(function () {
	          if (this.nodeType === 3) {
	            var s = encodeEntities(this.nodeValue).replace(term, highlighted);
	            $(this).replaceWith(s);
	          }
	        });
		}
      }
  }
  
})(jQuery);

/* javascript */
(function($){
	$(document.documentElement).addClass('hasJavascript');
})(jQuery);

/* Onload */
(function($){
	$(function(){
		$('.shy').parent()
			.bind(
					'mouseover',
					function(e){
						if (!e) var e = window.event;
						e.cancelBubble = true;
						if (e.stopPropagation) e.stopPropagation();

						//$(this).parent().parent().children('.shy').css('visibility', 'hidden');
						$(this).children('.shy').css('visibility', 'visible');
					}
				  )
			.bind(
					'mouseout',
					function(){
						$(this).children('.shy').css('visibility', 'hidden');
					}
				  );
		
		/* Autolabel */
		$('div.autolabel :text, div.autolabel :password')
			.bind('focus',
				function(){
					var $this = $(this);
					if ($this.val() === $this.parent().find('label').text()){
						$this
							.val('')
							.removeClass('labeled');
					}
				}
			)
			.bind('blur',
				function(){
					var $this = $(this);
					var labeled;
					if ($this.val() === ''){
						$this.val($this.parent().find('label').text());
						labeled = true;
					}
					if (labeled || $this.val() === $this.parent().find('label').text()){
						$this.addClass('labeled');
					}
				}
			)
			.each(
				function(){
					$(this).triggerHandler('blur');
				}
			);
		
		$('TABLE.cake-sql-log, TABLE.cake-sql-log caption')
			.bind(
					'mouseover',
					function(){
						$(this).css('opacity', '1');
					}
				  )
			.bind(
					'mouseout',
					function(){
						$(this).css('opacity', '0.3');
					}
				  );
	});
})(jQuery);
