﻿/*
 * Generic page functions
 */

//Main Nav function
mainNav = function()
{
	var nav_elements = document.getElementById("topnav");

	if (nav_elements.hasChildNodes)
	{
		nav_elements = nav_elements.getElementsByTagName('li');

		for (var count = 0; count < nav_elements.length; count++)
		{
			if (nav_elements[count].className.indexOf("nav_") >= 0)
			{
				nav_elements[count].onmouseenter = function()
				{
					this.className += "_over over";
				}

				nav_elements[count].onmouseleave = function()
				{					
					this.className = this.className.replace("_over over", "");
				}
			}
		}
	}
}

/**
 * @namespace Encapsulates image preload and buffer functionality
 */
var imageBuffer = {
	/**
	 * @public
	 * @static
	 */
	images : {},
	/**
	 * Preloads the given images - assigns them to the images object
	 * as a 'buffer'.  Images are stored as a key-value object pair
	 * in K:Name/V:Image object format.  Thus, assigned names must
	 * be unique to avoid collisions and the ensuing overwrites and
	 * chaos (unless you really want that...)
	 *
	 * @param {Array} Image(s) to process - each entity is an object of the
	 * format { Name : {String}, Source: {URL} }
	 *
	 * @public
	 * @static
	 */
	preload : function(images)
	{	
		var 
			length = 0,
			img;
			
		while (length < images.length)
		{		
			img = new Image();
			img.src = images[length].Source;
			imageBuffer.images[images[length].Name] = img;
			length ++;
		}
	},
	/**
	 * Destroy the images buffer.
	 *
	 * @public
	 * @static
	 */
	destroy : function()
	{
		var
			image;
			
		for (image in imageBuffer)
		{
			imageBuffer[image] = null;
		}
	}
};

/**
* @namespace Encapsulates functionality related to shimming for IE6
*/
var IE6Shim = {
	/**
	* Gets the position of a given element relative to the
	* top and left sides of the page.
	*
	* @static
	* @param {HTMLElement} element HTML Element that needs position determined.
	* @returns {Array} An 2-tuple (Array coordinate pair) in {x,y} format.
	*/
	getOffsets: function(element)
	{
		/** @private */
		var left = 0,
				top = 0,
				currentElement = element;

		do
		{
			left += currentElement.offsetLeft;
			top += currentElement.offsetTop;
		} while ((currentElement = currentElement.offsetParent) && (currentElement.id !== "ctl00_pnlContent"));

		return [left, top];
	},
	/**
	* Formats all of the shims to match the selects they cover.
	*
	* @static 
	*/
	formatShims: function()
	{
		/** @private */
		var count = -1,
				shims = document.getElementsByTagName("iframe"),
				menus = document.getElementById("topnav").getElementsByTagName("div"),
				currentCoordinates;

		// Loop through all shims:
		// Format each shim
		count = (shims.length === 0) ? -1 : shims.length;

		while (--count >= 0)
		{
			if (menus[count].className.indexOf("menu") >= 0)
			{
				shims[count].style.height = menus[count].offsetHeight;
				shims[count].style.width = menus[count].offsetWidth;

				currentCoordinates = IE6Shim.getOffsets(menus[count]);

				shims[count].style.left = currentCoordinates[0] - menus[count].style.marginLeft - menus[count].style.borderLeft;
				shims[count].style.top = currentCoordinates[1] - 10 - menus[count].style.marginTop - menus[count].style.borderTop;
			}
		}
	},
	/**
	* Hides shims when drop menus retract - otherwise, there would be some funky
	* stuff going down in terms of user interaction and event propagation / bubbling.
	*
	* @static
	*/
	hideShims: function()
	{
		/** @private */
		var count = -1,
			shims = document.getElementsByTagName("iframe");

		// Loop through all shims:
		// Hide each shim
		count = (shims.length === 0) ? -1 : shims.length;

		while (--count >= 0)
		{
			// Hide this shim
			shims[count].style.display = "none";
		}
	},
	/**
	* Displays all shims on menu drop.
	*
	* @static
	*/
	displayShims: function()
	{
		/** @private */
		var count = -1,
			shims = document.getElementsByTagName("iframe");

		// Loop through all shims:
		// Display each shim
		count = (shims.length === 0) ? -1 : shims.length;

		while (--count >= 0)
		{
			// Display this shim
			shims[count].style.display = "inline";
			shims[count].style.border = "1px solid #FF0000";
		}
	}
}

// Menu links jumper
function MM_jumpMenu(targ, selObj, restore)
{ //v3.0
eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
if (restore) selObj.selectedIndex = 0;
}

/**************************************************************************/
// Increase Font or Decrease Font
// added extra tags 3.12.2101 FJD6. probably a more elegant way to do this

/** BEGIN INCREASE FONT**/
var min=8;
var max=18;
function increaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }

   var p = document.getElementsByTagName('strong');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }

   var p = document.getElementsByTagName('b');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }

   var p = document.getElementsByTagName('td');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }

   var p = document.getElementsByTagName('li');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   } 
   var p = document.getElementsByTagName('h2');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }    
   var p = document.getElementsByTagName('h3');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   } 
   var p = document.getElementsByTagName('h4');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }    
   var p = document.getElementsByTagName('h5');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }    
}
/** END INCREASE FONT**/

/** BEGIN DECREASE FONT**/
function decreaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
   var p = document.getElementsByTagName('strong');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   

   var p = document.getElementsByTagName('b');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   

   var p = document.getElementsByTagName('td');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
   var p = document.getElementsByTagName('li');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }         

   var p = document.getElementsByTagName('h2');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }         
      var p = document.getElementsByTagName('h3');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }         
      var p = document.getElementsByTagName('h4');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }         
   var p = document.getElementsByTagName('h5');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }         
      
   
}
/** END DECREASE FONT**/


/**************************************************************************/

/*
 * The printing and emailing stuff
 */

//Function for print page
function Clickheretoprint()
{
// Script for printer friendly page
// Liz Dones 10/04/2006
// Modified for PhotoBooks by Christopher Brett Fuller 2010/03/27
var disp_setting = "toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=650,height=600,left=100,top=25",
content_header = document.getElementById("Print_Header").innerHTML,
content_subtitle = document.getElementById("Print_Subtitle").innerHTML,
content_vlue = document.getElementById("Print_Content").innerHTML,
Url = document.location.href,
docprint = window.open("", "", disp_setting);

if (docprint != null) {
	docprint.document.open();
	docprint.document.write('<html>' + '\n');
	docprint.document.write('<head>' + '\n');
	docprint.document.write('<title>Brigham and Women\'s Hospital</title>' + '\n');
	docprint.document.write('<link rel="StyleSheet" type="text/css" href="/includes/stylesheets/20100322/App_Css/styles.css" />' + '\n');
	docprint.document.write('<link rel="StyleSheet" type="text/css" href="/includes/stylesheets/20100322/App_Css/PrintStyles.css" />' + '\n');
	docprint.document.write('</head>' + '\n');
	docprint.document.write('<body>' + '\n');
	docprint.document.write('<div class="pb_inline_left" style="padding-left: 11px;">');
	docprint.document.write('<img src="/includes/images/20100322/logo.gif" border="0" />' + '\n');
	docprint.document.write('</div>');
	docprint.document.write('<div class="pb_float_right">' + '\n');
	docprint.document.write('<img src="/includes/images/20100322/print/Print.gif" border="0" alt="Print-friendly" onClick="self.print();" />' + '\n');
	docprint.document.write('<img src="/includes/images/20100322/print/CloseBtn.gif" border="0" alt="Close Window" onClick="self.close();" />' + '\n');
	docprint.document.write('</div>' + '\n');	
	docprint.document.write('<table id="contentArea">' + '\n');
	docprint.document.write('<tr>' + '\n');
	docprint.document.write('<td width="10"></td>' + '\n');
	docprint.document.write('<td>' + '\n');
	docprint.document.write('<p>' + '\n');
	docprint.document.write(content_header);
	docprint.document.write('<br />');
	docprint.document.write(content_subtitle);
	docprint.document.write('<br />');
	docprint.document.write(content_vlue);
	docprint.document.write('\n' + '<p />' + '\n');
	docprint.document.write('<strong>Find this content at:</strong><br />' + '\n');
	docprint.document.write(Url + '\n');
	docprint.document.write('<hr />' + '\n');
	docprint.document.write('The Brigham and Women\'s Hospital web site is primarily an educational resource intended to complement, not take the place of, the advice and recommendations you receive from your health care provider. If you have or suspect that you have a health problem, please seek the services of your health care provider.' +'\n');
	docprint.document.write('<p class="pb_inline_center">Copyright &copy; 2010 Brigham and Women\'s Hospital</p>' + '\n');
	docprint.document.write('</td>' + '\n');
	docprint.document.write('</tr>' + '\n');
	docprint.document.write('</body>' + '\n');
	docprint.document.write('</html>' + '\n');
	docprint.document.close();
	docprint.focus();
}

return false;
}

//Mail Function, will email the link  ---- Liz Dones Dec. 17, 2009
function mailThisUrl()
{
m = "Brigham and Women's Hospital - Web Page of Interest";
t = document.title;
u = "...." + window.location;

// the following expression must be all on one line...
window.location = "mailto:?subject=" + m + "&body=" + t + u;
}

// Increase Font Size - (increase) ? increase font size : decrease font size;
function changeFontSize(increase)
{
	var min = 8,
		 max = 18,
		 spans = document.getElementsByTagName('span'),
		 p = [],
		 s;

	for (i = 0; i < spans.length; i++)
	{
		if (spans[i].className.match(/textResize/i))	p.push(spans[i]);
	}	 
		 
	for (i = 0; i < p.length; i++)
	{
		if (p[i].style.fontSize)
		{
			s = parseInt(p[i].style.fontSize.replace("px", ""));
		} else
		{
			s = 12;
		}

		s = (increase) ?
			((s < max) ? (s + 1) : s) :
			((s > min) ? (s - 1) : s);
		
		p[i].style.fontSize = s + "px"
	}
}

/*
 * PhotoBooks Common Functions
 */

// 8-24-2006 : Kendall Beaman
// 2010-03-22 : Modified by Christopher Brett Fuller
//
// Function replaces all consecutive dashes with a single dash for a given form element
//
// frm is the form element you wish to process. Here's an example of how to call this function:
//   <form id="testForm" action="" method="post" onsubmit="filterDashes(getElementById('testForm'));">
//
// I recommend using the id attribute rather than name via the getElementById function.
function filterDashes(frm)
{
	var elemValue,
		 result,
		 count = frm.elements.length,
		 pattern = /-{2,}/g;

	while (--count >= 0)
	{
		elemValue = frm.elements[count].value;

		frm.elements[count].value = elemValue.replace(pattern, "-");
	}
}

// Function searches the entire document for forms and then goes through them to remove dashes.
function allFilterDashes()
{
	var forms = document.getElementsByTagName("form"),
		 count = forms.length;


	while (--count >= 0)
	{
		filterDashes(forms[count]);
	}
}

// Removes leading and trailing spaces and carriage returns.
function Trim(s)
{
	// Remove leading spaces and carriage returns
	while ((s.substring(0, 1) == ' ') || (s.substring(0, 1) == '\n') || (s.substring(0, 1) == '\r'))
	{
		s = s.substring(1, s.length);
	}

	// Remove trailing spaces and carriage returns
	while ((s.substring(s.length - 1, s.length) == ' ') || (s.substring(s.length - 1, s.length) == '\n') || (s.substring(s.length - 1, s.length) == '\r'))
	{
		s = s.substring(0, s.length - 1);
	}

	return s;
}
