var $bc = {};

$bc.findPos = function (obj) {
	var curleft = curtop = 0;

  if (obj && obj.offsetParent) {
    do {
    curleft += obj.offsetLeft;
    curtop += obj.offsetTop;
  
  } while (obj = obj.offsetParent);

  return [curtop, curleft];
}
}








$bc.emergencyCallBack = function(data) {

  var item = data.getElementsByTagName('item')[0];
  
  var descriptionItems = item.getElementsByTagName('description');
  var description = '';
  
  if (descriptionItems.length > 0) {
    description = descriptionItems[0].firstChild.nodeValue;
  }  

  if (jQuery.trim(description) != '' && jQuery.trim(description.toUpperCase()) != 'INACTIVE') {
    $bc.addBanner(description);
  } else {
    
    /* change back to /rss/emergency.json when going to production */
    
    jQuery.getJSON('/rss/emergency.json?'+ (new Date()).getTime(), '', function(d) {
      if (d[0].status == 'on') {
        $bc.addBanner(d[0].headline + '. ' + d[0].message);
      }
    });
    
    
  }

}


$bc.addBanner = function(rave_message) {
   
    var div = document.createElement('div');
    var span1 = document.createElement('span');
    var span2 = document.createElement('span');
  
    div.appendChild(span1);
      
    span1.appendChild(document.createTextNode(rave_message));
  
    span1.style.color = 'red';
    span1.style.padding = '3px';
    span1.style.fontSize = '14px';

    var style = div.style;
    style.overflow = 'hidden';
    
    style.border = '2px solid black';
    style.backgroundColor = 'white';
    style.zIndex = '9999999';
    style.fontFamily = 'arial';
    style.fontSize = '14px';
    style.textAlign = 'left';
    style.lineHeight = '20px';
    style.height = '60px';
  
    /* BC INFO PAGE */
    
    if (document.title.indexOf('BCInfo') != -1 && $bc.bcinfoDiv) {
      var pos = $bc.findPos($bc.bcinfoDiv);
      style.position = 'absolute';
      style.top = pos[0] + 'px';
      style.left = (pos[1] + 110) + 'px';
      style.width = '448px';
      
      document.body.appendChild(div);
      
    } else if ((location.pathname == '/' && $bc.featuresDiv) || (location.pathname == '/hometest.html' && $bc.featuresDiv)) { /* BC HOME PAGE */
      style.width = '100%';
      $bc.featuresDiv.insertBefore(div, $bc.featuresDiv.firstChild);
    } else if (location.pathname == '/prepare/' && $bc.primaryDiv) { /* BC PREPARE HOME */
      style.width = ($bc.primaryDiv.offsetWidth - 10) + 'px';
      style.marginLeft = '10px';
      $bc.primaryDiv.parentNode.insertBefore(div, $bc.primaryDiv);
    }
}


$bc.addEventSimple = function (obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}


$bc.addEventSimple(window, 'load', function() {
  $bc.bcinfoDiv = document.getElementById('bcinfo');
  $bc.featuresDiv = document.getElementById('features');
  $bc.primaryDiv = document.getElementById('primary');
  
  if (
      (document.title.indexOf('BCInfo') != -1 && $bc.bcinfoDiv) ||
      (location.pathname == '/' && $bc.featuresDiv) ||
      (location.pathname == '/hometest.html' && $bc.featuresDiv) ||
      (location.pathname == '/prepare/' && $bc.primaryDiv)
      
     ) {
    
    jQuery.get('/rss/rave.xml?'+ (new Date()).getTime(), '', $bc.emergencyCallBack, 'xml');
  }
});
