String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g,"");
}


function getCookie( check_name ) {

  var a_all_cookies = document.cookie.split( ';' );
  var a_temp_cookie = '';
  var cookie_name = '';
  var cookie_value = '';
  var b_cookie_found = false; // set boolean t/f default f
  
  for ( i = 0; i < a_all_cookies.length; i++ )
  {
    
    a_temp_cookie = a_all_cookies[i].split( '=' );
    
    cookie_name   = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
    
    if ( cookie_name == check_name )
    {
      b_cookie_found = true;
    
      if ( a_temp_cookie.length > 1 ) {
        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
      }
    
      return cookie_value;
      break;
    }
    a_temp_cookie = null;
    cookie_name = '';
  }
  if ( !b_cookie_found ) 
  {
    return null;
  }
}

function setCookie( name, value, expires, path, domain, secure ) {
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires )  {
    expires = expires * 1000 * 60 * 60 * 24;
  }

  var expires_date = new Date( today.getTime() + (expires) );

  document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

document.observe('dom:loaded', function() {
  
  
  var search_active = getCookie('search_active');
  
  if ( search_active !== null ) {
    $(search_active).addClassName('active');
  }
  
  $$('.search_click').each( function (search_click) {

    search_click.observe('click', function (e) {
      
      //console.log( Event.element(e));
      $$('.search_click').invoke("removeClassName", "active");
      
      var clicked     = Event.element(e);
      var active_id   = clicked.id;
      clicked.addClassName("active"); 
      $('customsearch').setAttribute("action", clicked.getAttribute('default'));
      
      
      setCookie('search_active', active_id);
  
    });
  });
  
  

  
});

Event.observe(window, 'load', function () {

  //alert($$('img')[0]);
  
  $$('img').each(function (_img) {
    var _isrc = _img.src.replace('http://www.diet.com', ''), 
    //var _isrc = _img.src.replace('http://xdev01a:8050', ''), 
        _nsrc = 'http://new.diet.com' + _isrc;
    
    if ( _isrc.indexOf('/manage') >= 0 ) {
      _img.src = _nsrc;
      _img.setAttribute('src', _nsrc);
    }
  });
  
});

