﻿// Code used only by the live pages (not Edit or Print)


var _HasHeader = false;
var _HasContent = false;
var _LeftChildren = null;
var _LeftChildrenIndex = 0;



jQuery(document).ready(function() {

  jQuery(".sideBarBlockTwo").each(HandleSideBar);

  jQuery(".sideBarBlock").each(HandleSideBar);

  jQuery(".FormerLeft").each(function(index, element) {
    var _el = jQuery(element);
    if (HideIfEmpty(_el))
      return;
  });

  jQuery(".headerContentBox").each(function(index, element) {
    var _el = jQuery(element);
    if (HideIfEmpty(_el))
      return;
  });

});

function HandleSideBar(index, element) {
  var _el = jQuery(element);
  if (HideIfEmpty(_el))
    return;

  //_el.css("background", "red");
  var _el = jQuery(element).find(".gold19Times");
  //debugger;
  if (new String(_el.html()).indexOf("Media Contacts") > 0) {
    _el.html("");
  }
  else if (_el.html() == "Related Resources") {
  // element represents the box containing the resources
  //debugger;
  var _outerWidth = 278;  //jQuery(element).width();
    var _Links = jQuery(element).find("A");
    var _Index = 0;
    var _numLinks = _Links.length;
    var _innerWidth = _outerWidth;
    var _Half = _numLinks;
    /* Uncomment to render Resources in two columns
    if (_numLinks > 5) {
    _Half = Math.floor(_numLinks / 2) + (_numLinks % 2 > 0 ? 1 : 0) - 1;
    _innerWidth = Math.round(_innerWidth / 2) - 5;
    }
    */
    var _html = new String();
    _html = "<div class=\"sideBarPadding\"><div class=\"RelatedResources\"><div class=\"gold19Times RelatedResourcesHeader\">Related Resources</div><ul style=\"width:" + _innerWidth + "px\">";

    _Links.each(function(index, innerElement) {

	  var _el = jQuery(innerElement);
	
      var _target = "";
      if (new String(_el.attr("href")).toLowerCase().indexOf(".pdf") > -1)
        _target = "target=\"_blank\" ";

	  var _t = _el.attr("target");
	  if(typeof(_t) != 'undefined')
	  {
	    if(_t.length > 0)
		  _target  = "target=\"" + _t +"\" ";
	  }
      _html += "<li><a href=\"" + _el.attr("href")
							+ "\" " +
							_target +
							"class=\"blue12ArialLink\">" + _el.text() + "</a></li>";
      if (_Index == _Half)
        _html += "</ul><ul style=\"width:" + _innerWidth + "px\">";
      _Index++;
    });
    _html += "</ul><div style=\"clear:both; height:12px;\"></div></div></div>";
    
    var _FactSheet = jQuery(".sideBarBlockTwo.FactSheet");

    if (_FactSheet.length > 0) {
      _FactSheet.parent().append("<div class=\"sideBarBlockTwo\">" + _html + "</div>");
      jQuery(element).remove();
    }
    else {
      jQuery(element).html(_html);
    }
  }
}

function HideIfEmpty(_el) {
  //debugger;
  if (jQuery.trim(new String(_el.html())).length == 0) {
    _el.hide();
    return true;
  } else if (jQuery.trim(new String(_el.text())).length == 0) {
    if (_el.find("IMG").length == 0) {
      _el.hide();
      return true;
    }
  }
}

/*
return;
jQuery(".FormerLeft").each(function(index, element) {

var _MainBody = jQuery(".FormerLeft").children();
var _MainBodyLen = _MainBody.length;
var _InsertionIndex = 1;
var _PairPart = false;

jQuery(element).children().each(function(index, element) {

var _el = jQuery(element);

if (new String(_el.html()).length == 0)
_el.hide();

});
return false; // Stops after the first one
});

var _PrevElement;
var _MainChildren = jQuery(".MainBody").children();
var _MainChildrenCount = _MainChildren.length;

_LeftChildren = jQuery(".FormerLeft").children();

if (_LeftChildren.length > 0) {

_MainChildren.each(function(index, element) {
//if (_LeftChildrenIndex >= _LeftChildren.length) return false;

var _el = jQuery(element);
// if we pass a header, we'll iterate through the elements until the next header and add a paragraph or two just before the next header
//_PrevElement = _el;

// element is header or contains header text
if (_el.hasClass("gold22Times") ||
_el.hasClass("gold19Times") ||
_el.hasClass("mainHeader") ||
_el.hasClass("callHeader") ||
_el.hasClass("subHeader")) {
InsertNext(_el);
}
else if (_el.find(".gold22Times").length > 0) {
InsertNext(jQuery(_el.find(".gold22Times")[0]));
}
else if (_el.find(".gold19Times").length > 0) {
InsertNext(jQuery(_el.find(".gold19Times")[0]));
}
else if (_el.find(".mainHeader").length > 0) {
InsertNext(jQuery(_el.find(".mainHeader")[0]));
}
else if (_el.find(".callHeader").length > 0) {
InsertNext(jQuery(_el.find(".callHeader")[0]));
}
else if (_el.find(".subHeader").length > 0) {
InsertNext(jQuery(_el.find(".subHeader")[0]));
}
else if (_el.find(".colBHeadingSerif").length > 0) {
InsertNext(jQuery(_el.find(".colBHeadingSerif")[0]));
}

else if (_el.html().length > 0) {
if (_HasHeader) {
//alert("found content" + _el.html());
_HasContent = true;
}
}

if (_MainChildrenCount == index + 1) {
// We're at the end.  Append the rest of the elements from the left column
if (_LeftChildren.length > 0) {
_el.before("<div style=\"border:1px solid red;\">MORE WOULD GO HERE</div>");
}
}

});
} // End if LeftChildren


function InsertNext(element) {
//alert("found header" + _el.html());
if (_HasHeader && _HasContent) {
// ready to insert some text after the last element
//alert(jQuery(_LeftChildren[_LeftChildrenIndex]).html());
var _found = false;
for (var i = _LeftChildrenIndex; i < _LeftChildren.length; i++) {
if (_LeftChildren[_LeftChildrenIndex] != null && jQuery(_LeftChildren[_LeftChildrenIndex]).html().length > 0) {
_found = true;
_LeftChildrenIndex = i;
break;
}
}
if (_found) {
var _html = jQuery(_LeftChildren[_LeftChildrenIndex]).html();

_LeftChildrenIndex++;
if(_LeftChildren.length > _LeftChildrenIndex)
_html += jQuery(_LeftChildren[_LeftChildrenIndex]).html();

_LeftChildrenIndex++;
if (_LeftChildren.length > _LeftChildrenIndex)
_html += jQuery(_LeftChildren[_LeftChildrenIndex]).html();

element.before("<div style=\"border:1px solid red;\">" + _html + "</div>");
      
_LeftChildrenIndex++;
}
_HasContent = false;
}
else {
_HasHeader = true;
}
}

*/



