function snTagWord(string, word, tag)
{
  return string;
  
  if(string.length == 0 || word.length == 0)
    return string;
    
  var tagOpen = tag;
  var tagEnd = tag.split(' ')[0];
  var $j = jQuery.noConflict();
  
  if(links = $j(string).find('a'))
  //if(false)
  {
    //alert(links); return string;
    links.each(function(){
      var regReplace = new RegExp('([^=&"/])(' + word + ')([^&"/])', 'ig');// i = caseless, g = all matches
      this.innerHTML = this.innerHTML.replace(regReplace, '$1<' + tagOpen + '>$2</' + tagEnd + '>$3');
    });
  }
  else
  {
	  var regReplace = new RegExp('([^=&"/])(' + word + ')([^&"/])', 'ig');// i = caseless, g = all matches
	  string = string.replace(regReplace, '$1<' + tagOpen + '>$2</' + tagEnd + '>$3');
	}
	
	return string;
}

function snApplyEditIcon(id)
{
  $j('#' + id + ' tbody > tr > td.texte4').find('a').not('a.icon').each(function(){
    $j(this).attr('class', 'edit');
  });
}

function snTableOverOut(id)
{
	$j('#' + id).find('tr.value').each(function(){
	  $j(this).mouseover(function(){
	    var cb = $j(this).find('input:checkbox').eq(0);
	    $j(this).attr('class', 'value-over');
	  }).mouseout(function(){
	    var cb = $j(this).find('input:checkbox').eq(0);
	    if(cb.attr('checked'))
	      $j(this).attr('class', 'value-selected');
	    else
	      $j(this).attr('class', 'value');
	  })
	});
}

function snOpenDialog(id, options, callback)
{
  if(!callback)
    callback = function() {
      alert("valider");
    };

  var baseOptions = {
    modal: true, 
    overlay: { 
      opacity: 0.5, 
      background: "black" 
    } ,
    buttons: { 
      "Valider": callback, 
      "Annuler": function() { 
        $j(this).dialog("close"); 
      } 
    } 
  };
  baseOptions = merge(baseOptions, options);
  
  $j("#" + id).dialog(baseOptions);
}

function merge(obj1, obj2)
{
  for(var i in obj2)
    obj1[i] = obj2[i];
    
  return obj1;
}

function snFillCombo(id, options, params)
{
  var cbOpts = $(id).options;
  cbOpts.length = options.length;
  var i = 0;
  
  if(params && params.include_custom)
  {
    cbOpts.length++;
    cbOpts[i].value = params.include_custom.value;
    cbOpts[i].text  = params.include_custom.text;
    i++;
  }
  
  for(var j = 0; j < options.length; j++, i++)
  {
    if(options[j].text)
    {
      cbOpts[i].value = options[j].value;
      cbOpts[i].text  = options[j].text;
    }
    else
    {
      cbOpts[i].value = options[j];
      cbOpts[i].text  = options[j].toString();//.toUpperCase();
    }
    if(params.selected && cbOpts[i].value == params.selected)
      cbOpts.selectedIndex = i;
  }
  
  if(!params.selected)
    cbOpts.selectedIndex = 0;
}

snMaskParams = { backgroundColor: '#000000' };
function snShowMask(id)
{
  var box = $j('#' + id);
  $j('body').append('<div id="mask-' + id + '" class="snmask"></div>');
  var offset = box.offset();
  offset.left+= snWindowHaveScroll() ? 1 : 0;
  
  //--o Prepare mask
  var mask = $j('#mask-' + id);
  var css = {
    backgroundColor: snMaskParams.backgroundColor,
    position: 'absolute',
    top: offset.top,
    left: offset.left,
    zIndex: 1000,
    opacity: 0
  };
  mask.css(css);
  mask.width(box.innerWidth());
  mask.height(box.innerHeight());
  
  mask.fadeTo('fast', 0.75);
}
function snHideMask(id)
{
  var mask = $j('#mask-' + id);
  mask.remove();
}
function snWindowHaveScroll()
{
  if($j('html').css('overflow-y') == 'scroll') return false;
  return $j('body').height() > $j(window).height() ? true : false;
}

function snAnchorElement(el, elSrc, offsetX, offsetY)
{
  offsetX = offsetX ? offsetX : 0;
  offsetY = offsetY ? offsetY : 0;
  var _elSrc = $j("#" + elSrc);
  var offset = _elSrc.position(); 
  $j("#" + el).css({
    top: offset.top + offsetY, 
    left: offset.left + _elSrc.width() + offsetX});
}

function snApplyTabs(callback)
{
	$j('div.tab-topbar').find('a.tab-item, a.tab-item-selected').each(function(){
	  $j(this).click(function(){
	    snOpenTab(this);
	    if(callback != undefined)
	     callback();
	    return false;
	  });
	  if($j(this).attr('class') == 'tab-item')
	    $j('#' + $j(this).attr('rel')).attr('style', 'display: none;');
	});
	
	function snOpenTab(tabItem) {
	  tabItem.blur();
	  $j('div.tab-topbar').find('.tab-item-selected').each(function(){
	    $j(this).attr('class', 'tab-item');
	    $j('#' + $j(this).attr('rel')).attr('style', 'display: none;');
	  });
	  tabItem.className = 'tab-item-selected';
	  $j('#' + $j(tabItem).attr('rel')).attr('style', 'display: block;');
	}
}

function snStringToNumber(strNumber)
{
  return parseFloat(strNumber.replace(' ', '').replace(',', '.'));
}
function snNumberFormat(number, decimals, dec_point, thousands_sep)
{
  //----o Conversion du nombre en chaine
  //--o Calcul d'arrondi
  number = (isNaN(number) ? '0' : (Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals)) + '');

  //----o Séparation entier et décimals
  arrIntegerDecimal = number.split('.');
  integer = arrIntegerDecimal[0];
  decimal = (arrIntegerDecimal.length > 1 ? arrIntegerDecimal[1] : '');

  //----o Formate décimals
  strDecimal = (decimal.length > 0 ? decimal.substr(0, decimals) : '');
  decimalLength = strDecimal.length
  if (decimalLength < decimals)
    for (i = 0; i < decimals - decimalLength; i++)
      strDecimal+= "0";

  //----o Formate entier
  strInteger = '';
  reste = integer + '';
  pos = reste.length - 3;
  while (pos > 0)
  {
    strInteger+= thousands_sep + reste.substr(pos);
    reste = reste.substr(0, pos);
    pos-= 3;
  }
  strInteger = reste + strInteger;

  //----o Assemblage number
  dec_point = (decimals > 0 ? dec_point : '');
  return strInteger + dec_point + strDecimal;
}

function getCookieVar(name)
{
  var cookie = document.cookie;
  var cookie_vars = document.cookie.split(' ');
  for(i in cookie_vars)
    if(cookie_vars[i].indexOf('symfony') > -1){
      return cookie_vars[i];
    }
}

function snMakeMinusPlusBox(handlerElement_id, contentElement_id, bulletElement_id)
{
  var handler = $j('#' + handlerElement_id);
  var domHandler = $(handlerElement_id);
  
  domHandler.contentId = contentElement_id;
  domHandler.bulletId  = bulletElement_id;
  
  handler.mouseover(function(){
    var bullet  = $j('#' + this.bulletId);
    
    if(bullet.attr('class') == 'minus')
      bullet.attr('class', 'minus-over'); 
    else 
      bullet.attr('class', 'plus-over');
  })
  .mouseout(function(){
    var bullet  = $j('#' + this.bulletId);
    
    if(bullet.attr('class') == 'minus-over') 
      bullet.attr('class', 'minus'); 
    else 
      bullet.attr('class', 'plus');
  })
  .click(function(){
    var content = $j('#' + this.contentId);
    var bullet  = $j('#' + this.bulletId);
  
    if(content.attr('tagName').toLowerCase() != 'div') {
      content.toggle();
      if(content.css('display') == 'none') { 
        bullet.attr('class', 'plus-over');
      } else {
        bullet.attr('class', 'minus-over');
      } 
    } else {
      if(content.css('display') == 'none') { 
        content.slideDown('normal');
        bullet.attr('class', 'minus-over');
      } else {
        content.slideUp('normal');
        bullet.attr('class', 'plus-over');
      }
    }
  });
}

function snMakeChoices(choices)
{
  /* options init */
	var effect = 'drop';
	var options = {direction: 'right'};
	var duration = 250;
	
	/* function to close choices */
	$j('body').get(0).choicesBodyClick = function(){
    if($j('#choices-' + this.snOpenedChoiceId).css('display') != 'none') {
      $j('#choices-' + this.snOpenedChoiceId).hide(effect, options, duration);
      $j('#choice-' + this.snOpenedChoiceId).attr('class', 'choice-title');
    }
  }
	
	/* for each choice */
  for(i = 0; i < choices.length; i++) {
    $('choice-' + choices[i]).snChoices = choices;
    $('choice-' + choices[i]).snId = choices[i];
    /* click event on choice button */
    $j('#choice-' + choices[i]).click(function(){
      this.blur();
      
      /* close all opened choices */
      /*for(i = 0; i < this.snChoices.length; i++)
        if(this.snChoices[i] != this.snId && $j('#choices-' + this.snChoices[i]).css('display') != 'none') {
          $j('#choices-' + this.snChoices[i]).hide(effect, options, duration);
           $j('#choice-' + this.snChoices[i]).attr('class', 'choice-title');
        }*/   
      
      /* open or close clicked choice */
      if($j('#choices-' + this.snId).css('display') != 'none') {
        //$j('#choices-' + this.snId).hide(effect, options, duration);
        //$j('#choice-' + this.snId).attr('class', 'choice-title');
      } else {
        $j('#choices-' + this.snId).show(effect, options, duration);
        $j('#choice-' + this.snId).attr('class', 'choice-title-selected');
        
        /* add mouseup event on body to close choice */
        $j('body').get(0).snOpenedChoiceId = this.snId;
        $j('body').one('mouseup', $j('body').get(0).choicesBodyClick);
      }
      
      return false;
    });
    
    /* stop body mouseup on choices over */
    $j('#choices-' + choices[i]).mouseover(function(){
      $j('body').unbind('mouseup', $j('body').get(0).choicesBodyClick);
    })
    /* restart body mouseup on choices out */
    .mouseout(function(){
      $j('body').one('mouseup', $j('body').get(0).choicesBodyClick);
    })
    /* mouseup event on checkbox label */
    .find('label').mouseup(function(){
      if(!$j('#' + $j(this).attr('for')).attr('checked'))
        $j(this).parent().attr('class', 'choice-option-container-selected');
      else
        $j(this).parent().attr('class', 'choice-option-container');
    }).end()
    /* mouseup event on checkbox */
    .find('input:checkbox').each(function(){ this.snId = choices[i]; }).mouseup(function(){
      if(!$j(this).attr('checked'))
        $j(this).parent().attr('class', 'choice-option-container-selected');
      else
        $j(this).parent().attr('class', 'choice-option-container');
    })
    /* change event on checkbox */
    .change(function(){
      if(this.checked)
        $('choice-count-' + this.snId).innerHTML = snStringToNumber($('choice-count-' + this.snId).innerHTML) + 1;
      else
        $('choice-count-' + this.snId).innerHTML = snStringToNumber($('choice-count-' + this.snId).innerHTML) - 1;
    });
    
    /* reset functions for reset filters */
    $('choice-' + choices[i]).reset = function(){
      $j(this).attr('class', 'choice-title').find('span').attr('innerHTML', '0');
    }
    $('choices-' + choices[i]).reset = function(){
      $j(this).find('div.choice-option-container-selected').each(function(){
        $j(this).attr('class', 'choice-option-container');
      });
      if($j(this).css('display') != 'none')
        $j(this).hide(effect, options, duration);
    }
  }
}

function truncate2box($element)
{
  var original_width = $element.width();
  $element.css('width', 'auto');
  var complete_content = $element.html();
  var content = '';
  var iteration = 0;
  while($element.width() >= original_width && iteration < 100) {
    iteration++;
    content = $element.html();
    content = content.substring(0, content.length - 4) + '...';
    $element.html(content);
  }
  $element.width(original_width);
  var element = $element.get(0);
  element.fullContent = complete_content;
  element.truncateContent = $element.html();
  
  if(element.fullContent != element.truncateContent)
  $element.hover(function(){
    $j(this).prepend('<div class="truncate-tt">' + $j(this).get(0).fullContent + '</div>');
    var $tt = $j(this).find('div.truncate-tt');
    $tt.css('position', 'absolute');
    $tt.css('z-index', '10000');
    $tt.css('margin-top', -2);
    $tt.css('margin-left', -7);
  }, function(){
    $j(this).find('div.truncate-tt').remove();
  });
}

function truncate2table($element)
{
  var original_width = $element.attr('width');
  //alert(original_width);
  //return ;
  //$element.css('width', 'auto');
  var complete_content = $element.html();
  var content = '';
  var iteration = 0;
  while($element.width() > original_width && iteration < 100) {
    iteration++;
    content = $element.text();
    content = content.substring(0, content.length - 4) + '...';
    $element.html(content);
  }
  $element.width(original_width);
  var element = $element.get(0);
  element.fullContent = complete_content;
  element.truncateContent = $element.html();
  
  if(element.fullContent != element.truncateContent)
  $element.hover(function(){
    $j(this).prepend('<div class="truncate-tt">' + $j(this).get(0).fullContent + '</div>');
    var $tt = $j(this).find('div.truncate-tt');
    $tt.css('position', 'absolute');
    $tt.css('z-index', '10000');
    $tt.css('margin-top', -2);
    $tt.css('margin-left', -7);
  }, function(){
    $j(this).find('div.truncate-tt').remove();
  });
}


