// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function isIE() {
  return new RegExp("MSIE").test(navigator.userAgent);
}

function showEditUserDiv(divId) {
  editUserDivs.each(function(div) {
    if (div.id == divId) {
      div.show();
    } else {
      div.hide();
    }
  });
}

function jscss(a,o,c1,c2) {
  switch (a) {
    case 'swap':
      o.className = jscss('check',o,c1) ? o.className.replace(c1,c2) : o.className.replace(c2,c1);
      break;
    case 'add':
      if (!jscss('check',o,c1)) {
        o.className += o.className ? ' '+c1 : c1;
      }
      break;
    case 'remove':
      var rep = o.className.match(' '+c1) ? ' '+c1 : c1;
      o.className = o.className.replace(rep, '');
      break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className);
      break;
  }
}

var _width = 0; var _height = 0;

function showWindowDimensions() {
  if (wd = $('windowDimensions')) {
    wd.innerHTML = _width + " x " + _height;
  }
}

function setWindowDimensions() {
  if (typeof(window.innerWidth) == "number") {
    _width = window.innerWidth; _height = window.innerHeight;
  } else if (document.documentElement && document.documentElement.clientWidth) {
    _width = document.documentElement.clientWidth;
    _height = document.documentElement.clientHeight;
  } else if (document.body && document.body.clientWidth) {
    _width = document.body.clientWidth;
    _height = document.body.clientHeight;
  }
}

function resizeBulletin(div) {
  var imgArray = $(div).select('img');
  if (imgArray.length > 0) {
    imgArray.each(function(img) {
      if (img.className == "bulletin-picture") {
        var divBody = $(div.id + '_body');
        if (divBody.getHeight() < img.getHeight()) {
          divBody.style.height = img.getHeight() + "px";
        }
      }
    });
  }
}

function resizeContainer(div) {
  div.childElements().each(function(c) {
    if (c.getHeight() > div.getHeight()) {
      div.style.height = c.getHeight() + "px";
    }
    if (navigator.userAgent.indexOf("MSIE") != -1 && c.className.indexOf('right-column') != -1) {
      c.style.width = (c.getWidth() - 8) + "px";
    }
  });
}

function resizeMainDiv() {
  var toSubtract = isIE() ? 160 : 130;
  if (_height > 155)  {
    $('main').style.height = (_height - toSubtract) + "px";
  }
}

function blankTargetizeRightBarLinks() {
  var foo = [];
  $('bulletin-list').select('span.bulletin-body-text').each(function(text) {
    text.select('a').each(function(anchorTag) {
      if (anchorTag.className != "shortened") {
        anchorTag.target = "_blank";
        foo.push(anchorTag);
      }
    });
  });
  return foo;
}

window.onresize = function() {
  setWindowDimensions();
  showWindowDimensions();
  resizeMainDiv();
  if (isIE()) {
    // $('right-bar').style.width = "27.%";
    // $('right-bar').style.width = ($('right-bar').getWidth() - 13) + "px";
  }
}

window.onload = function() {
  setWindowDimensions();
  showWindowDimensions();
  resizeMainDiv();
  $A(document.getElementsByTagName('div')).each(function(element) {
    if (element.className == "bulletin") {
      resizeBulletin(element);
    }
  });
  $A(document.getElementsByClassName('two-column-container')).each(function(element) {
    resizeContainer(element);
  });
  if (isIE()) {
    $('main').style.padding = "8px 25px 8px 1%";
    $('right-bar').style.width = ($('right-bar').getWidth() - 13) + "px";
    $('right-bar-ie').show();
  }
  blankTargetizeRightBarLinks();
}
