if (!window.console) {console = {log:function(str){if ($('console')) {$('console').innerHTML += str;}}};}
function cookieValue(key) {
  var cookies = document.cookie;
  var pos = cookies.indexOf(key + '=');
  if (pos != -1) {
    var start = pos+key.length+1;
    var end = cookies.indexOf(';',start);
    if (end == -1) {end = cookies.length;}
    return decodeURIComponent(cookies.substring(start,end));
  }
  return null;
}
function setCookie(key, value, expires) {
  var expires = expires*1000*60*60*24*365;
  var expiryDate = new Date((new Date()).getTime()+expires);
  document.cookie = key+'='+value+';expires='+expiryDate.toGMTString()+';path=/;';
}
     
var ImageAdjuster = {
  adjustImage: function(img, idealWidth, idealHeight) {
    img = $(img);
    var imgWidth = img.width;
    var imgHeight = img.height;
    if (imgWidth/idealWidth < imgHeight/idealHeight) {
      img.width = idealWidth;
      img.height = imgHeight*idealWidth/imgWidth;
      var offset = (img.height-idealHeight)/2;
      img.style.marginTop = -offset + 'px';
      img.style.marginLeft = '0px';
      img.style.clip = 'rect(' + offset + 'px, ' + idealWidth + 'px, ' + (offset+idealHeight) + 'px, 0px)';
    }
    else {
      img.height = idealHeight;
      img.width = imgWidth*idealHeight/imgHeight;
      var offset = (img.width-idealWidth)/2;
      img.style.marginTop = '0px';
      img.style.marginLeft = -offset+'px';
      img.style.clip = 'rect(0px, ' + (offset+idealWidth) + 'px, ' + idealHeight + 'px, ' + offset + 'px)';
    }
    img.style.width = img.width + 'px';
    img.style.height = img.height + 'px';
    img.style.position = 'absolute';
    img.style.overflow = 'hidden';
  },
  adjustImageClass: function(className, idealWidth, idealHeight) {
    $$('img.'+className).each(function(img) {
      img.onload = function() {ImageAdjuster.adjustImage(this, idealWidth, idealHeight);};
    });
  }
};

function appendScript(url) {
  var head = document.getElementsByTagName("head")[0];
  var script = document.createElement("script");
  script.charset = "utf-8";
  script.src = url;
  script.type = 'text/javascript';
  head.appendChild(script);
}

function initUserHeader() {
  var uName = cookieValue('display_name');
  if (uName) {
    $('account_welcome').innerHTML += uName;
    $('login').hide();
    $('account_nav').show();
  }
  else {
    $('account_nav').hide();
    $('login').show(); 
  }
}  