var strCookieString = new String();
								
//------------------------------------------------------------------------------
function AddCookieString( strName, strValue )
{
	if (strCookieString.indexOf(strName) != -1) {
		var oldval = getValue(strName, strCookieString);
		var s = strName + '|' + oldval + '|';
		strCookieString = strCookieString.replace(s, strName + "|" + strValue + "|");
	}
	else {
		strCookieString += ( strName + "|" + strValue + "|" );
	}
}

//------------------------------------------------------------------------------
function setCookie( theName, theValue )
{
	var theCookie = theName + "=" + escape(theValue) + ";";
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+ 360);
	theCookie += ";expires="+exdate.toGMTString();
  
	//theCookie += ";secure";
  
	document.cookie = theCookie;

  
}

//------------------------------------------------------------------------------
function getCookie( cookieName )
{
  var strResults = 'unset';
  var offset = 0;
  var end = 0;
  var strSearch = new String(cookieName) + "="; 
  if (document.cookie.length > 0) 
  { 
	offset = document.cookie.indexOf( strSearch);

	if (offset != -1) 
	{ 
	  offset += strSearch.length;
	  end = document.cookie.indexOf(";", offset);
	  if (end == -1)
	  {
		end = document.cookie.length;
	  }
	  strResults = unescape(document.cookie.substring(offset, end));             

	}
  }

 return strResults;
}




//------------------------------------------------------------------------------



function getValue( strName, strCookieString )
{
  var strResults = new String( "" );
  var offset = 0;
  var end = 0;
  var strSearch = new String( strName + "|" );

  if ( strCookieString.length > 0 ) 
  { 
	offset = strCookieString.indexOf( strSearch );
	if ( offset != -1 ) 
	{ 
	  offset += strSearch.length;
	  end = strCookieString.indexOf( "|", offset );
	  if ( end == -1 )
	  {
		end = strCookieString.length;
	  }
	  strResults = strCookieString.substring( offset, end );
	}
  }
  return strResults;
}


function deleteCookie(name,path)
 { 
	if(getCookie(name))
	{ 
		document.cookie = name+"="+((path)?"; path="+path:"") + ";secure"  //+"; expires=Thu, 01-Jan-70 00:00:01 GMT"; 
	} 

}

/* room to numeric tab index map */
hlth_ehm_tab_map = {
	'kitchen'		: 1,
	'childsroom'	: 4,
	'familyroom'	: 5,
	'bathroom'		: 3,
	'bedroom'		: 0,
	'laundryroom'	: 2,
	'garage'		: 0,
	'yard'			: 0,
	'library'		: 0,
	'index'			: 0
};

/* room to ad segment index map */
hlth_ehm_ads_map = {
	'kitchen'		: 1,
	'childroom'		: 4,
	'familyroom'	: 2,
	'bathroom'		: 3,
	'bedroom'		: 5,
	'laundry'		: 6,
	'garage'		: 7,
	'yard'			: 8,
	'library'		: 9 ,
	'index'			: 9 
};

/* wait until the full page is loaded to set this true */
var health_ehome_page_loaded = false;

function tabsInit() {
	health_ehome_page_loaded = false;
	/* wrap text inside tab headers with links */
	$('.hlth_ehm_tab_head_fmt h3').each(function(i) {
		$(this).html( '<a href="">' + $(this).html() + '</a>' );
	});
	
	/* add onclick functions to each header link */
	$('.hlth_ehm_tab_head_fmt a').each(function(i) {
		$(this).click(function() {
			wmdPageLink('sb-rr-0300-tc-02_' + (i+1));
			showTab(i);
			return false;
		});
	});
	
	/* default the tabs to the kitchen */
	showTabByRoomName('kitchen');
	
	/* after initial page load, now we set this true so ads will refresh on tab clicks */
	health_ehome_page_loaded = true;
}

/* function to show a particular tab by its numeric index */
function showTab(n) {	
	$('.hlth_ehm_tab_head_fmt').each(function(i) {
		if (i == n) {
			/* hide all tabs, show the selected tab */
			hideTabs();
			$(this).parent().removeClass('hlth_ehm_tab_hide_fmt').addClass('hlth_ehm_tab_show_fmt');
		}
	});
}

/* function show tab by room name */
function showTabByRoomName(tabname) {
	$.each(hlth_ehm_tab_map, function(i, val) {
		if (i == tabname) {
			showTab(val);
		}
	});
}

/* hide all tabs */
function hideTabs() {
	$('.hlth_ehm_tab_fmt').removeClass('hlth_ehm_tab_show_fmt').addClass('hlth_ehm_tab_hide_fmt');
}

/* function to replace all ads with contextually-relevant ads, slightly modified from wiki version to allow for custom segments */
function refreshAllAds(AdSegment) {
	function refreshAd(strAd) {			
		var objAd = document.getElementById(strAd);
		var okToLoop = 0;

		if (objAd) {

			var theObjects = objAd.getElementsByTagName("*");

			/* Makes sure there is an approved iframe before running a possible infinte loop */
			for (i=0;i<=theObjects.length-1;i++) {
				if (theObjects[i].id.indexOf("Ad_Iframe") != -1) {
					okToLoop+=1;
				}
			}
			
			/* Runs two loops that remove all other objects other then the original webmd iframe */
			if (okToLoop == 1) {
				while(theObjects.length > 1) {
					for (i=0;i<=theObjects.length-1;i++) {
						if (theObjects[i].id.indexOf("Ad_Iframe") < 0) {
							try {objAd.removeChild(theObjects[i]);} catch (e) {}
						}
					}
				}
			}

			/* Grabs the src of either the iframe or script whichever on is in the div */
			iframe = objAd.getElementsByTagName('iframe')[0];
			adScript = objAd.getElementsByTagName('script')[0];
			
			if(!iframe) {
				var strHTML = adScript.src;
			} else {
				var strHTML = iframe.src;
			}
				
			/* Refreshes the other variables in the src */ 
			strHTML = strHTML.replace(new RegExp("transactionID=[0-9]+"), "transactionID=" + transTileId);
			strHTML = strHTML.replace(new RegExp("tile=[0-9]*"), "tile=" + transTileId);
			
			strHTML.indexOf('segm=') != -1 ? strHTML = strHTML.replace(new RegExp("segm=[0-9]*"), "segm=" + AdSegment) : strHTML = strHTML + '&segm=' + AdSegment;

			if(!iframe) {
				adScript.src = strHTML;
			} else {
				iframe.contentWindow.location.replace(strHTML);
			}
		}
	}
	
	var transTileId = Math.round(99999999*Math.random());
	refreshAd('bannerAd_fmt');
	refreshAd('rightAd_fmt');
}


/* Added per Tony 2.23.09 -DRM */
function staticCustomLink(x, y){
	if (y == 'moduleClick') {
        //clicking on link stays within the flash app
        wmdPageLink(x);
    }
    if (y == 'pageClick') {
        //clicking on the link takes user to another WebMD page outside of the Flash app
        ctrs(x);
    }
    if (y == 'pageView') {

		if (x.indexOf("default.htm") != -1) {
			var roomName = "kitchen";
		} else {
			var roomName = x.substring(18);
		}

		var z = location.href.split("?")[0];
        var zz = z.split("#")[0];
        zz = zz.replace(/http:\/\/www.webmd.com/, "webmd.com");
        zz = zz.replace("default.htm", "")
        x = zz.replace(/http:\/\//, "") + x;
        wmdPageview(x);
		refreshAllAds(hlth_ehm_ads_map[roomName]);		
    }
}

function fixedSize(url,name,features) {
	win=window.open(url,name,features);
	win.focus();
}

function wmdTrackSponsorVideo(videoname,status) {
 var s_md, _page;
 s_md=s_gi(s_account);
 s_md.linkTrackVars="prop9,prop17,prop18,prop20,prop34,prop50";
 s_md.prop17="sponsorvideo";
 s_md.prop18="sponsorvideo_"+status;
 s_md.prop20= videoname+"_sponsorvideo_"+status;
 s_md.prop34= videoname+"_sponsorvideo";
 s_md.prop9 = "";
 if(status == "start") { s_md.prop9 = "allvideo_start"; }
 if(status == "100pct") { s_md.prop9 = "allvideo_100pct"; }
 void(s_md.tl(true, 'o', s_md.prop20));
}

function updateURL(str) {
	 window.location.hash = str;
}