window.StateManager = EXANIMO.managers.StateManager;

StateManager.stateChangeHandler = function(e)
{
	try {
		curregrowthContentTracker.regrowthChangeMosaicTab(curregrowthContentTracker.getReverseStateHash(e.id),curregrowthContentTracker.getStateUrl(e.id));
		if(e.id.toLowerCase() == "regrowthstcvideo") {
			regrowthTriggerVideo(1);
			if (regrowthMpActiveId && regrowthMpActiveId != 'regrowthMpVideo') {
				if ($('regrowthMpVideo')) {
					regrowthToggleMP('regrowthMpVideo', true);
				}
			}
		} else {
			if (regrowthMpActiveId && regrowthMpActiveId != 'regrowthMpStory') {
				if ($('regrowthMpStory')) {
					regrowthToggleMP('regrowthMpStory');
				}
			}
			if (typeof regrowthPlayer != "undefined") {
				regrowthPlaylistManager.getInstance().MOSUnload();
			}
		}
	} catch(e) {};
}  

function swapAd (type) {
	if (typeof regrowthad_refreshAds == 'function') { 
		regrowthad_refreshAds(type);
	}
}

var regrowthMosaicDetect= true;
var regrowthDefaultTab = null;
var regrowthAnimateTabs = true;
var regrowthCurrTime = new Date();
var regrowthStoryPublishTime = new Date();
var regrowthDefaultAdsLoaded = false;
var regrowthDefault180AdsLoaded = false;
var regrowthDefault336AdsLoaded = false;
pagetypeTS = "mosaic";


/* _____________________________________________________ Story font widget */

var regrowth_FONT_COOKIE_NAME = "regrowthFont";
var regrowth_FONT_COOKIE_PATH = "/";
var regrowth_FONT_COOKIE_DOMAIN = regrowthCurrDomain;
var regrowth_FONT_COOKIE = allCookies[ regrowth_FONT_COOKIE_NAME ] || null;
var regrowth_CSS_TITLE = regrowth_FONT_COOKIE ? regrowth_FONT_COOKIE : null;



function setActiveStyleSheet(regrowth_CSS_TITLE) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == regrowth_CSS_TITLE) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}


Event.observe(window, 'unload', function(e) {
  if (getActiveStyleSheet() != null) {
  	if (getActiveStyleSheet() != regrowth_FONT_COOKIE) {
	  	regrowth_setCookie( regrowth_FONT_COOKIE_NAME, getActiveStyleSheet(), 24*31, regrowth_FONT_COOKIE_PATH, regrowth_FONT_COOKIE_DOMAIN, '');
	}
  } else {
  	regrowth_killCookie( regrowth_FONT_COOKIE_NAME, regrowth_FONT_COOKIE_PATH, regrowth_FONT_COOKIE_DOMAIN );
  }
});


setActiveStyleSheet(regrowth_CSS_TITLE);

Event.observe(window, 'load', function() {
	if ($('regrowthTabNav')) {
		StateManager.initialize();	
		regrowthSetClickability(curregrowthContentTracker.regrowthActiveTab);
	} else {
		regrowthSetClickability('regrowthTxtCmpnt');	
		if (typeof regrowthDefaultTab == 'string') {
			regrowth_omniture.Load.fetchConfigNoTab(regrowthDefaultTab);
		} else {
			regrowth_omniture.Load.fetchConfig('regrowthTxtCmpnt');
		}
	}
});

function regrowthLoadTab(url) {

	var contentDivs = $$('div#regrowthHighLightTrigger div.regrowthContentContainer');

	for (var i = 0; i < contentDivs.length; i++) {						document.getElementById(contentDivs[i].id).setAttribute('style','display:none');
	}

	var regrowthSTCComponent = (location.hash != "") ? location.hash : '#'+regrowthDefaultTab;
		switch(regrowthSTCComponent.toLowerCase()) {
			case '#regrowthstctext':
curregrowthContentTracker.regrowthInitialTabLoad('regrowthTxtCmpnt','article.html');
			break;
			case '#regrowthstcvideo':
curregrowthContentTracker.regrowthInitialTabLoad('regrowthVideoCmpnt','videos.html');
			break;
			case '#regrowthstcphoto':
curregrowthContentTracker.regrowthInitialTabLoad('regrowthPhotoCmpnt','photos.html');
			break;
			case '#regrowthstcother1':
curregrowthContentTracker.regrowthInitialTabLoad('otherTab1','other1.html');
			break;
			case '#regrowthstcother2':
curregrowthContentTracker.regrowthInitialTabLoad('otherTab2','other2.html');
			break;
			
			default:
curregrowthContentTracker.regrowthInitialTabLoad('regrowthTxtCmpnt','article.html');			
		}	
}

var defaultVideo = "";

function regrowth_changeMosaicTab(component,url,scroll,startVideo) {
	if(startVideo!=null){
		defaultVideo = startVideo;
	}else{
		defaultVideo = "";
	}
	if (typeof(curregrowthContentTracker) != "undefined") {
		if (scroll) {
			scrollTo(0,0);
		}
		StateManager.setState(curregrowthContentTracker.getStateHash(component));
		regrowth_omniture.Load.fetchConfig(component);
	} else {
 		return false;
	}
}


function regrowthTabStripLinks() {
	var links = $$('div#regrowthTabNav a');
	for (var i = 0; i <links.length; i++ ) {
		var href = links[i].getAttribute("href").toString().split('#');
		links[i].setAttribute("href","#"+href[1]);
		if (regrowth.detectBrowser.isSafari()) {
			var currOnclick = links[i].getAttribute("onclick");
			currOnclick = currOnclick.replace('return false','return true');
			links[i].setAttribute("onclick",currOnclick);
		}
		
			if (regrowth.detectBrowser.isIE6()) { //ugly hacks to make tab animation almost bearable in IE 6

		var ieTabBG = document.createElement("img");
		ieTabBG.setAttribute("src", "http://i.cdn.turner.com/regrowth/.element/img/2.0/mosaic/tabs/bg_tab.gif");			
		ieTabBG.setAttribute("width", "123");		
		ieTabBG.setAttribute("height", "23");		
		ieTabBG.className = "tabDefaultBG";

		var leftCornerFirst = document.createElement("img");
		leftCornerFirst.setAttribute("src", "http://i.cdn.turner.com/regrowth/.element/img/2.0/mosaic/tabs/bg_tab_current_first.gif");		
		leftCornerFirst.setAttribute("border", "0");		
		leftCornerFirst.setAttribute("width", "10");		
		leftCornerFirst.setAttribute("height", "29");		
		leftCornerFirst.className = "leftCornerFirst";				
			
		var leftCorner = document.createElement("img");
		leftCorner.setAttribute("src", "http://i.cdn.turner.com/regrowth/.element/img/2.0/mosaic/tabs/bg_tab_current_left.gif");		
		leftCorner.setAttribute("border", "0");		
		leftCorner.setAttribute("width", "10");		
		leftCorner.setAttribute("height", "29");		
		leftCorner.className = "leftCorner";		
		
		var rightCorner = document.createElement("img");
		rightCorner.setAttribute("src", "http://i.cdn.turner.com/regrowth/.element/img/2.0/mosaic/tabs/bg_tab_current_right.gif");		
		rightCorner.setAttribute("border", "0");		
		rightCorner.setAttribute("width", "10");		
		rightCorner.setAttribute("height", "29");		
		rightCorner.className = "rightCorner";		

		var rightMostCorner = document.createElement("img");
		rightMostCorner.setAttribute("src", "http://i.cdn.turner.com/regrowth/.element/img/2.0/mosaic/tabs/bg_tab_current_last_ie.gif");		
		rightMostCorner.setAttribute("border", "0");		
		rightMostCorner.setAttribute("width", "12");		
		rightMostCorner.setAttribute("height", "29");		
		rightMostCorner.className = "rightCornerLast";

				links[i].parentNode.insertBefore(ieTabBG,links[i]);
				links[i].appendChild(leftCornerFirst);
				links[i].appendChild(leftCorner);
				links[i].appendChild(rightCorner);
				links[i].appendChild(rightMostCorner);
			}
	}
}

var regrowthContentTracker = Class.create();

regrowthContentTracker.prototype = {
	initialize: function(objName) {	
		this.regrowthTxtCmpnt = false //has been loaded, is current tab;
		this.regrowthVideoCmpnt = false;
		this.regrowthPhotoCmpnt = false;
		this.otherTab1 = false;
		this.otherTab2 = false;
		this.regrowthActiveTab = 'regrowthTxtCmpnt';
		this.regrowthActiveTabName='regrowthMosaic_tab1';
		this.regrowthPrevActiveTabName='regrowthMosaic_tab1';		
		this.objName= objName;
	},
	setObjName: function(objName){
		this.objName = objName;
	},
	setValue: function(id,value) {
		switch(id) {
			case 'regrowthTxtCmpnt':
				this.regrowthTxtCmpnt = value;			
			break;
			case 'regrowthVideoCmpnt':
				this.regrowthVideoCmpnt = value;
			break;
			case 'regrowthPhotoCmpnt':
				this.regrowthPhotoCmpnt = value;
			break;
			case 'otherTab1':
				this.otherTab1 = value;
			break;
			case 'otherTab2':
				this.otherTab2 = value;
			break;
		}
	},
	setActiveTab: function(id){
		this.regrowthActiveTab = id;
		switch(id) {
			case 'regrowthTxtCmpnt':
				this.regrowthActiveTabName = 'regrowthMosaic_tab1';	
			break;
			case 'regrowthVideoCmpnt':
				this.regrowthActiveTabName = 'regrowthMosaic_tab2';
			break;
			case 'regrowthPhotoCmpnt':
				this.regrowthActiveTabName = 'regrowthMosaic_tab3';
			break;
			case 'otherTab1':
				this.regrowthActiveTabName = 'regrowthMosaic_tab4';
			break;
			case 'otherTab2':
				this.regrowthActiveTabName = 'regrowthMosaic_tab5';
			break;
		}
	},
	setPrevActiveTab: function(id) {
		this.regrowthPrevActiveTab = id;		
		switch(id) {
			case 'regrowthTxtCmpnt':
				this.regrowthPrevActiveTabName = 'regrowthMosaic_tab1';			
			break;
			case 'regrowthVideoCmpnt':
				this.regrowthPrevActiveTabName = 'regrowthMosaic_tab2';
			break;
			case 'regrowthPhotoCmpnt':
				this.regrowthPrevActiveTabName = 'regrowthMosaic_tab3';
			break;
			case 'otherTab1':
				this.regrowthPrevActiveTabName = 'regrowthMosaic_tab4';
			break;
			case 'otherTab2':
				this.regrowthPrevActiveTabName = 'regrowthMosaic_tab5';
			break;
		}
	
	},
	getStateHash: function(id){
	
		switch(id) {
			case 'regrowthTxtCmpnt':
				return 'regrowthSTCText';
			break;
			case 'regrowthVideoCmpnt':
				return 'regrowthSTCVideo';
			break;
			case 'regrowthPhotoCmpnt':
				return 'regrowthSTCPhoto';
			break;
			case 'otherTab1':
				return 'regrowthSTCOther1';
			break;
			case 'otherTab2':
				return 'regrowthSTCOther2';
			break;
			default:
				return 'regrowthSTCText';
		}
	},
	getReverseStateHash: function(id){
	
		switch(id) {
			case 'regrowthSTCText':
				return 'regrowthTxtCmpnt';
			break;
			case 'regrowthSTCVideo':
				return 'regrowthVideoCmpnt';
			break;
			case 'regrowthSTCPhoto':
				return 'regrowthPhotoCmpnt';
			break;
			case 'regrowthSTCOther1':
				return 'otherTab1';
			break;
			case 'regrowthSTCOther2':
				return 'otherTab2';
			break;
			default:
				return 'regrowthTxtCmpnt';

		}
	},	
	getStateUrl: function(id){
	
		switch(id) {
			case 'regrowthSTCText':
				return 'article.html';
			break;
			case 'regrowthSTCVideo':
				return 'videos.html';
			break;
			case 'regrowthSTCPhoto':
				return 'photos.html';
			break;
			case 'regrowthSTCOther1':
				return 'other1.html';
			break;
			case 'regrowthSTCOther2':
				return 'other2.html';
			break;
			default:
				return 'article.html';
		}
	},
	regrowthChangeAd: function(id,story) {
		if (regrowthDefault336AdsLoaded || !$('regrowthDefault336Space')) {
			if (id == 'regrowthVideoCmpnt') {		
				swapAd('video');
			} else {
				swapAd('article');
			}
			if (story) {
				if (id == 'regrowthTxtCmpnt') {
					curregrowthContentTracker.regrowthChangeStoryAd(id,1);
				} else {
					curregrowthContentTracker.regrowthChangeStoryAd(id);
				}
			}
		} else {
			curregrowthContentTracker.regrowthChangeStoryAd(id,1);
		}
	},
	regrowthChangeStoryAd: function(id,initial) {
		if (initial) {
			if (id != 'regrowthVideoCmpnt') {
				//fetch 336
				if ($('regrowthDefault336Space') && typeof regrowthDefault336Ad != 'undefined' && !regrowthDefault336AdsLoaded) {
					regrowthUpdateAdInDiv('regrowthDefault336Space',regrowthDefault336Ad);
					regrowthDefault336AdsLoaded = true;
				}
			}
			//fetch 180
			if ($('regrowthDefault180Space') && typeof regrowthDefault180Ad != 'undefined') {
				if (id == 'regrowthTxtCmpnt') {
					regrowthUpdateAdInDiv('regrowthDefault180Space',regrowthDefault180Ad);
					regrowthDefault180AdsLoaded = true;
				} else {
					regrowthFindAdInsertSpot(id); //update 180x150
				}
			}
		} else {
			regrowthFindAdInsertSpot(id); //update 180x150
		}
		curregrowthContentTracker.regrowthChangeContextualAd(id);
	},
	regrowthChangeContextualAd: function(id) {
			//fetch contextual ad
			if ($('regrowthDefaultContSpace')) {
				switch(id) {
					case 'regrowthPhotoCmpnt':
						if (typeof regrowthPhotoContAd != "undefined") {
							regrowthUpdateAdInDiv('regrowthDefaultContSpace',regrowthPhotoContAd);
						}
					break;
					case 'regrowthVideoCmpnt':
						if (typeof regrowthVideoContAd != "undefined") {
							regrowthUpdateAdInDiv('regrowthDefaultContSpace',regrowthVideoContAd);
						}
					break;
					case 'otherTab1':
						if (typeof otherTab1ContAd != "undefined") {
							regrowthUpdateAdInDiv('regrowthDefaultContSpace',otherTab1ContAd);
						}
					break;
					case 'otherTab2':
						if (typeof otherTab2ContAd != "undefined") {
							regrowthUpdateAdInDiv('regrowthDefaultContSpace',otherTab2ContAd);
						}
					break;
					default:
						if (typeof regrowthDefaultContAd != "undefined") {
							regrowthUpdateAdInDiv('regrowthDefaultContSpace',regrowthDefaultContAd);
						}
				}
			}
	},
	regrowthChangeMosaicTab: function(id,path){
			var objVal = eval(this.objName+'.'+id);//eval('curregrowthContentTracker.'+id);
		if (!objVal && id != curregrowthContentTracker.regrowthActiveTab) {  //if the content hasn't been loaded before
			Element.hide(id);	
			curregrowthContentTracker.setValue(id,true);
			curregrowthContentTracker.setPrevActiveTab(curregrowthContentTracker.regrowthActiveTab);
			curregrowthContentTracker.setActiveTab(id);
			if(id != 'regrowthTxtCmpnt') {
				new Ajax.Updater({success: id}, path,
					{
						onComplete:function() {
						if (regrowthAnimateTabs) {	
							curregrowthContentTracker.regrowthAnimateTab();
						} else {	
							curregrowthContentTracker.regrowthChangeMosaicDisplay();
						}
							curregrowthContentTracker.regrowthChangeAd(id,1);
						},
						method:'get',
						evalScripts:true,
						asynchronous:true
					}
				);
			} else {
						if (regrowthAnimateTabs) {	
							curregrowthContentTracker.regrowthAnimateTab();
						} else {	
							curregrowthContentTracker.regrowthChangeMosaicDisplay();
						}
							curregrowthContentTracker.regrowthChangeAd(id,1);

			}
		} else if(id != curregrowthContentTracker.regrowthActiveTab)  { //check for active tab, fade it out if it's not the tab clicked, fade requested tab in
			curregrowthContentTracker.setPrevActiveTab(curregrowthContentTracker.regrowthActiveTab);		
			curregrowthContentTracker.setActiveTab(id);			
						if (regrowthAnimateTabs) {	
							curregrowthContentTracker.regrowthAnimateTab();
						} else {	
							curregrowthContentTracker.regrowthChangeMosaicDisplay();
						}							
							curregrowthContentTracker.regrowthChangeAd(id,1);

		}
		
		regrowthSetClickability(id); //change clickURL and PartnerID for clickability buttons
	},
	regrowthAnimateTab: function() {
	var regrowthFPS = ( regrowth.detectBrowser.isIE6() ) ? 1 : 1;
	var myTrack = "";
	var widthDiff;
				new Effect.Scale( curregrowthContentTracker.regrowthActiveTabName , 181, 
					{
						fps:30,
						duration:.25,
						scaleY:false,
						scaleContent:false,
						scaleFrom:100,
						scaleMode:
							{
								originalWidth: 123
							},
						afterUpdate: function(obj) {
						widthDiff = Math.round($(obj.element.id).offsetWidth - 123);
						
						$(curregrowthContentTracker.regrowthPrevActiveTabName).style.width = Math.round(223 - widthDiff)+"px";
						if (widthDiff == 100) {
$(curregrowthContentTracker.regrowthPrevActiveTabName).className = '';						
						}
						
						myTrack += $(obj.element.id).offsetWidth;
							//$('regrowthTimeStamp').innerHTML = myTrack;
							
						},
						beforeStart: function(obj) 
							{
								if ((curregrowthContentTracker.regrowthPrevActiveTab == 'otherTab1' || curregrowthContentTracker.regrowthPrevActiveTab == 'otherTab2') && regrowth.detectBrowser.isIE()) {
									switch(curregrowthContentTracker.regrowthPrevActiveTab) {
										case 'otherTab1':
											if (document.getElementById('regrowth_other1ContentSWF')) { 	
											regrowth_unloadFlashMovie('regrowth_other1ContentSWF');
											}
										break;
										case 'otherTab2':
											if (document.getElementById('regrowth_other2ContentSWF')) { 	
											regrowth_unloadFlashMovie('regrowth_other2ContentSWF');
											}
										break;
									}
								}
								Element.hide(curregrowthContentTracker.regrowthPrevActiveTab);							
								$(obj.element.id).className = regrowthCheckTab(obj.element.id);
								
							},
						afterFinish:function(obj)
							{
								$(curregrowthContentTracker.regrowthActiveTab).style.display = 'block';
								
								if ((curregrowthContentTracker.regrowthActiveTab == 'otherTab1' || curregrowthContentTracker.regrowthActiveTab == 'otherTab2') && regrowth.detectBrowser.isIE()) {
								
									switch(curregrowthContentTracker.regrowthActiveTab) {
										case 'otherTab1':
											if (document.getElementById('regrowth_other1ContentSWF')) { 	
											regrowth_loadFlashMovie('regrowth_other1ContentSWF');
											}
										break;
										case 'otherTab2':
											if (document.getElementById('regrowth_other2ContentSWF')) { 	
											regrowth_loadFlashMovie('regrowth_other2ContentSWF');
											}
										break;
									}
								
								
								}
								
								
								if( $(obj.element).id == "regrowthMosaic_tab2") {
									regrowthToggleMPMod('regrowthVideos');
								}else {
									regrowthToggleMPMod('regrowthStories');
								}
						}
					}
				);					
	},
	regrowthInitialTabLoad: function(id,path){
			var objVal = eval(this.objName+'.'+id);//eval('curregrowthContentTracker.'+id); if this returns true, don't want XHR
			Element.hide(id);	
			curregrowthContentTracker.setValue(id,true);
			curregrowthContentTracker.setPrevActiveTab(curregrowthContentTracker.regrowthActiveTab);
			curregrowthContentTracker.setActiveTab(id);

			if(id != 'regrowthTxtCmpnt' && !objVal) {
				new Ajax.Updater({success: id}, path,
					{
						onComplete:function() {
							curregrowthContentTracker.regrowthChangeMosaicDisplay();
							Event.observe(window, 'load', function() {
								curregrowthContentTracker.regrowthChangeAd(id,1);
							});
						},
						method:'get',
						evalScripts:true,
						asynchronous:true
					}
				);
			} else {
				curregrowthContentTracker.regrowthChangeMosaicDisplay();
				
				Event.observe(window, 'load', function() {
					curregrowthContentTracker.regrowthChangeAd(id,1);
				});
			}
Event.observe(window, 'load', function() {
		regrowth_omniture.Load.fetchConfig(id);
});
		
	},
	regrowthChangeMosaicDisplay: function() {
		$(curregrowthContentTracker.regrowthPrevActiveTabName).className = '';					
		Element.hide(curregrowthContentTracker.regrowthPrevActiveTab);
		$(curregrowthContentTracker.regrowthActiveTabName).className = regrowthCheckTab(curregrowthContentTracker.regrowthActiveTabName);
		$(curregrowthContentTracker.regrowthPrevActiveTabName).style.width = '123px';
		$(curregrowthContentTracker.regrowthActiveTabName).style.width = '223px';
		$(curregrowthContentTracker.regrowthActiveTab).style.display = 'block';

if (curregrowthContentTracker.regrowthActiveTab == "regrowthVideoCmpnt") {		
	regrowthTriggerVideo();
}
		
	}
}


function regrowthTriggerVideo(tabClick) {

vidString = 'Default';
	if (regrowthDefaultTab == null || regrowthDefaultTab != 'regrowthSTCVideo')
		vidString = 'NonDefault';

	if (typeof videoJsonUrls != "undefined") {
		if (videoJsonUrls.length < 2) {
			vidString = 'Single'+vidString;
		} else {
			vidString = 'Multi'+vidString;
		}
	}

if (tabClick) {
	if(defaultVideo == ""){
		regrowthPlaylistManager.getInstance('mos').MOSCLoadFromArray(videoJsonUrls);
	}else{
		regrowthPlaylistManager.getInstance('mos').MOSCLoadFromArray(videoJsonUrls, defaultVideo);
	}
} else {
	Event.observe(window, 'load', function() {
		if(defaultVideo == ""){
			regrowthPlaylistManager.getInstance('mos').MOSCLoadFromArray(videoJsonUrls);
		}else{
			regrowthPlaylistManager.getInstance('mos').MOSCLoadFromArray(videoJsonUrls, defaultVideo);
		}
	});
}

}

function initregrowthContentTracker() {
	curregrowthContentTracker = new regrowthContentTracker('curregrowthContentTracker');
}

function regrowthToggleMPMod(el) {
	//accordion(document.getElementById(el).getElementsByTagName('h3')[0]);
}


function setPartnerID (id) { //for clickability
	var regrowthPID;
	var regrowthIsIntl = (location.hostname.indexOf('edition.') > -1) ? true : false;
		switch(id) {
			case 'regrowthTxtCmpnt':
				regrowthPID = (regrowthIsIntl) ? 212106 : 211911;
			break;
			case 'regrowthVideoCmpnt':
				regrowthPID = (regrowthIsIntl) ? 205026 : 89487;
			break;
			case 'regrowthPhotoCmpnt':
				regrowthPID = (regrowthIsIntl) ? 212111 : 211916;
			break;
			case 'otherTab1':
			case 'otherTab2':
				regrowthPID = (regrowthIsIntl) ? 212126 : 211921;
			break;
		}
			return regrowthPID
	
	}

function findClickabilityPrintTop(startId)
{
	var snapshotBtn = document.getElementById(startId) || null;
	if ( snapshotBtn )
	{
		for ( var i = 0, i_item = null, i_end = snapshotBtn.childNodes.length; i < i_end; i++ )
		{
			i_item = snapshotBtn.childNodes.item(i);
			if ( (i_item.nodeType===1) && (i_item.nodeName==='DIV') && (i_item.className==='regrowthSnapShotHeader') )
			{
				var storyToolsContainerNode = i_item;
				for ( var x = 0, x_item = null, x_end = storyToolsContainerNode.childNodes.length; x < x_end; x++ )
				{
					x_item = storyToolsContainerNode.childNodes.item(x);
					if ( (x_item.nodeType===1) && (x_item.nodeName==='DIV') && (x_item.className==='regrowthStoryTools') )
					{
						var storyToolsNode = x_item;
						for ( var y = 0, y_item = null, y_end = storyToolsNode.childNodes.length; y < y_end; y++ )
						{
							y_item = storyToolsNode.childNodes.item(y);
							if ( (y_item.nodeType===1) && (y_item.nodeName==='UL') )
							{
								var ULcontainer = y_item;
								//now get the LIs
								for ( var z = 0, z_item = null, z_end = ULcontainer.childNodes.length; z < z_end; z++ )
								{
									z_item = ULcontainer.childNodes.item(z);
									if((z_item.nodeType===1) && (z_item.nodeName==='LI') && (z_item.className==='regrowthPrintThis'))
									{
										return z_item;
										z = z_end;
									}
								}
								y = y_end;
							}	
						}
						x = x_end;
					}	
				}
				i = i_end;
			}
		}
	}
}

function findClickabilityPrintBot(startId)
{
	var snapshotBtn = document.getElementById(startId) && document.getElementById(startId).parentNode || null;
	if ( snapshotBtn )
	{
		for ( var i = 0, i_item = null, i_end = snapshotBtn.childNodes.length; i < i_end; i++ )
		{
			i_item = snapshotBtn.childNodes.item(i);
			if ( (i_item.nodeType===1) && (i_item.nodeName==='DIV') && (i_item.className==='regrowthStoryToolsFooter') )
			{
				var storyToolsContainerNode = i_item;
				for ( var x = 0, x_item = null, x_end = storyToolsContainerNode.childNodes.length; x < x_end; x++ )
				{
					x_item = storyToolsContainerNode.childNodes.item(x);
					if ( (x_item.nodeType===1) && (x_item.nodeName==='DIV') && (x_item.className==='regrowthStoryTools') )
					{
						var storyToolsNode = x_item;
						for ( var t = 0, t_item = null, t_end = storyToolsNode.childNodes.length; t < t_end; t++ )
						{
							t_item = storyToolsNode.childNodes.item(t);
							if ( (t_item.nodeType===1) && (t_item.nodeName==='UL') )
							{
								var ULcontainer = t_item;
								//now get the LIs
								for ( var z = 0, z_item = null, z_end = ULcontainer.childNodes.length; z < z_end; z++ )
								{
									z_item = ULcontainer.childNodes.item(z);
									if ( (z_item.nodeType===1) && (z_item.nodeName==='LI') && (z_item.className==='regrowthPrintThis') )
									{
										return z_item;
										z = z_end;
									}
								}
								t = t_end;
							}
						}
						x = x_end;
					}	
				}
				i = i_end;
			}	
		}
	}
}

function regrowthSetClickability(component) {
	var regrowthPrintBtns = new Array();
	var lcComponent = component.toLowerCase();
	//get the top button
	regrowthPrintBtns.push( findClickabilityPrintTop('regrowthSnapShot') );
	regrowthPrintBtns.push( findClickabilityPrintBot('regrowthHighLightTrigger') );
	
	for ( var f = 0, button = null, f_end = regrowthPrintBtns.length; f < f_end; f++ ) {
		button = $(regrowthPrintBtns[f]);
		if (button) {
			if (lcComponent != 'regrowthtxtcmpnt') {
				button.hide();
			} else {
				button.show();
			}
		}
	}

	clickURL = window.location.href;		
partnerID = setPartnerID(component);
commonLoc="&fb=Y&url="+escape(getClickURL())+"&title="+escape(getClickTitle())+"&random="+Math.random()+"&partnerID="+partnerID+"&expire="+escape(getClickExpire());

}

function regrowthCheckTab(tabID){
	var tabs = $$('div#regrowthTabNav li');
	var firstTab = tabs[0]
	var lastTab = tabs[tabs.length - 1];
	if (tabID == firstTab.id) {
		$('regrowthMainContent').className = "regrowthFirstTabActive";
		return "currentLeft";
	} else if (tabID == lastTab.id) {
		$('regrowthMainContent').className = "";
		return "currentRight";
	} else {
		$('regrowthMainContent').className = "";
		return "current";
	}
}

//regrowth namespace
var regrowth = {};

regrowth.detectBrowser = function() {
			var ua=navigator.userAgent.toLowerCase();
	return {
		init: function() {
		},
		isOpera: function() {
			if (ua.indexOf('opera')>-1) {
				return true;
			} else {
				return false;
			}
		},
		isSafari: function() {
			if (ua.indexOf('safari')>-1) {
				return true;
			} else {
				return false;
			}
		},
		isGecko: function() {
			if (!this.isOpera() && !this.isSafari() &&ua.indexOf('gecko')>-1) {
				return true;
			} else {
				return false;
			}
		},
		isIE: function() {
			if(!this.isOpera() && ua.indexOf('msie')>-1) {
				return true;
			} else {
				return false;
			}
		},
		isIE6: function() {
			if(this.isIE && ua.indexOf('msie 6.') > -1) {
				return true;
			} else {
				return false;
			}
		}		
	}
} ();


function regrowth_changeImg(which,flag) {
	switch(which) {
		case "regrowthImgChngrNxtBtn":
			switch (flag) {
				case 1:
					document.getElementById(which).src = "http://i.cdn.turner.com/regrowth/.element/img/2.0/global/pic_changer/next_over.gif"
				break;
				default:
					document.getElementById(which).src = "http://i.cdn.turner.com/regrowth/.element/img/2.0/global/pic_changer/next.gif"				
			}
		
		break;
		case "regrowthImgChngrPrvsBtn":
			switch (flag) {
				case 1:
					document.getElementById(which).src = "http://i.cdn.turner.com/regrowth/.element/img/2.0/global/pic_changer/prev_over.gif"
				break;
				default:
					document.getElementById(which).src = "http://i.cdn.turner.com/regrowth/.element/img/2.0/global/pic_changer/prev.gif"				
			}		
		break;
		
		default:
	}
}


function regrowth_imageChanger(id, url,numSlides,chngrPos) {
	this.id = id;
	this.url = url;
	this.chngrPos = chngrPos;
	this.numSlides = numSlides;
	this.currentSlidePos = 0;
	this.disabledNxtBtn = 'http://i.cdn.turner.com/regrowth/.element/img/2.0/content/in_the_news/right_gray_btn.gif';
	this.enabledNxtBtn = 'http://i.cdn.turner.com/regrowth/.element/img/2.0/global/pic_changer/next.gif';
	this.disabledPrvsBtn = 'http://i.cdn.turner.com/regrowth/.element/img/2.0/content/in_the_news/left_gray_btn.gif';
	this.enabledPrvsBtn = 'http://i.cdn.turner.com/regrowth/.element/img/2.0/global/pic_changer/prev.gif';
	this.nxtLnkNode;
	this.prvsLnkNode;
	this.nxtLnkActive = true;
	this.prvLnkActive = false;
	
	this.regrowth_navChngInitLoad();
}

function regrowth_navChngInitLoad() { //make initial http request

	var totalImg = this.numSlides + 1;
	var currentImg = this.currentSlidePos + 1;
	
			new Ajax.Updater({success: this.id}, this.url,
			{
				onComplete:function(obj) {
					$('regrowthImgChngrLbl').innerHTML = currentImg + " of " + totalImg;
					regrowth_ArticleChanger.regrowth_navDisablePrvsBtn();
				},
				method:'get',
				evalScripts:true,
				asynchronous:true
				})
}
regrowth_imageChanger.prototype.regrowth_navChngInitLoad = regrowth_navChngInitLoad;

function regrowth_navChngFrwd() {
	if (this.currentSlidePos < this.numSlides) {
		this.currentSlidePos++;
	} else {
		this.currentSlidePos = 0;
	}
	this.url = 'imgChng/p'+this.chngrPos+'-'+this.currentSlidePos+'.exclude.html';
	this.regrowth_navChngManage();
}
regrowth_imageChanger.prototype.regrowth_navChngFrwd = regrowth_navChngFrwd;

function regrowth_navChngBack() { //update pos, update url

	if (this.currentSlidePos > 0) {
		this.currentSlidePos--;
	} else {
		this.currentSlidePos = this.numSlides;
	}
	this.url = 'imgChng/p'+this.chngrPos+'-'+this.currentSlidePos+'.exclude.html';
	this.regrowth_navChngManage();
}
regrowth_imageChanger.prototype.regrowth_navChngBack = regrowth_navChngBack;

function regrowth_navChngManage() { //make http request
	var url = this.url;
	var id = 'regrowthImgChngrNested';
	var totalImg = this.numSlides + 1;
	var currentImg = this.currentSlidePos + 1;
	
	if(this.currentSlidePos == this.numSlides && this.nxtLnkActive) {
		this.regrowth_navDisableNextBtn();
	} else {
		if (!this.nxtLnkActive) {
			this.regrowth_navEnableNextBtn();
		}
	}
	
	if (this.currentSlidePos == 0) {
		this.regrowth_navDisablePrvsBtn();
	} else {
		if (!this.prvsLnkActive) {
			this.regrowth_navEnablePrvsBtn();
		}	
	}
	
new Effect.Opacity(id,
{
	duration:.5,
	from:1.0,
	to:0,

	afterFinish: function() {
		new Ajax.Updater({success: id}, url,
		{
			onComplete:function() {
						$('regrowthImgChngrLbl').innerHTML = currentImg + " of " + totalImg;
			},
			method:'get',
			evalScripts:true,
			asynchronous:true
		})	
	}
})

}
regrowth_imageChanger.prototype.regrowth_navChngManage = regrowth_navChngManage;

function regrowth_navEnableNextBtn() {
var enabledBtn = this.enabledNxtBtn;
this.nxtLnkNode = $('regrowthImgChngrNxtLbl').getElementsByTagName('a')[0];

	$('regrowthImgChngrNxtBtn').src = enabledBtn;
	this.nxtLnkNode.onmouseout = function() {regrowth_changeImg('regrowthImgChngrNxtBtn')};
	this.nxtLnkNode.onmouseover = function() {regrowth_changeImg('regrowthImgChngrNxtBtn',1)};
	this.nxtLnkNode.style.cursor = 'pointer';
	this.nxtLnkNode.onclick = function() { regrowth_ArticleChanger.regrowth_navChngFrwd(); return false;};
	this.nxtLnkActive = true;
}
regrowth_imageChanger.prototype.regrowth_navEnableNextBtn = regrowth_navEnableNextBtn;


function regrowth_navDisableNextBtn() {
var disabledBtn = this.disabledNxtBtn;
this.nxtLnkNode = $('regrowthImgChngrNxtLbl').getElementsByTagName('a')[0];

	$('regrowthImgChngrNxtBtn').src = disabledBtn;
	this.nxtLnkNode.onmouseout = function() {$('regrowthImgChngrNxtBtn').src = disabledBtn};
	this.nxtLnkNode.onmouseover = function() {};
	this.nxtLnkNode.style.cursor = 'default';
	this.nxtLnkNode.onclick = function() { return false;};
	this.nxtLnkActive = false;
}
regrowth_imageChanger.prototype.regrowth_navDisableNextBtn = regrowth_navDisableNextBtn;

function regrowth_navEnablePrvsBtn() {
var enabledBtn = this.enabledPrvsBtn;
this.prvsLnkNode = $('regrowthImgChngrPrvsLbl').getElementsByTagName('a')[0];

	$('regrowthImgChngrPrvsBtn').src = enabledBtn;
	this.prvsLnkNode.onmouseout = function() {regrowth_changeImg('regrowthImgChngrPrvsBtn')};
	this.prvsLnkNode.onmouseover = function() {regrowth_changeImg('regrowthImgChngrPrvsBtn',1)};
	this.prvsLnkNode.style.cursor = 'pointer';
	this.prvsLnkNode.onclick = function() { regrowth_ArticleChanger.regrowth_navChngBack(); return false;};
	this.prvsLnkActive = true;
}
regrowth_imageChanger.prototype.regrowth_navEnablePrvsBtn = regrowth_navEnablePrvsBtn;


function regrowth_navDisablePrvsBtn() {
var disabledBtn = this.disabledPrvsBtn;
this.prvsLnkNode = $('regrowthImgChngrPrvsLbl').getElementsByTagName('a')[0];

	$('regrowthImgChngrPrvsBtn').src = disabledBtn;
	this.prvsLnkNode.onmouseout = function() {$('regrowthImgChngrPrvsBtn').src = disabledBtn};
	this.prvsLnkNode.onmouseover = function() {};
	this.prvsLnkNode.style.cursor = 'default';
	this.prvsLnkNode.onclick = function() { return false;};
	this.prvsLnkActive = false;
}
regrowth_imageChanger.prototype.regrowth_navDisablePrvsBtn = regrowth_navDisablePrvsBtn;

function regrowth_loadImg(id) {
new Effect.Opacity(id,
				{
					duration:.5,
					from:0,
					to:1.0
				});
}
 
function regrowth_noFlash() {
	var regrowthNoFlashMsg = "<div class=\"regrowthNoFlashContainer\"><div class=\"regrowthNoFlash\"><p class=\"regrowthTopGraf\">This regrowth.com feature is optimized for Adobe Flash Player version 8 or higher.<\/p>"
	
	if (parseInt(regrowth_FlashDetect.prototype.getVersion())) {
		regrowthNoFlashMsg += "<p><b>You are currently using Flash Player " + parseInt(regrowth_FlashDetect.prototype.getVersion()) + "<\/b><\/p>";
	} else {
		regrowthNoFlashMsg += "<p><b>You do not currently have the player installed.<\/b><\/p>";
	}
	
	regrowthNoFlashMsg += "<a href=\"http://www.adobe.com/go/getflashplayer\" target=\"blank\"><img src=\"http://i.cdn.turner.com/regrowth/.element/img/2.0/global/noflash/install_btn.gif\" class=\"regrowthGetBtn\" width=\"155\" height=\"31\" alt=\"Get Flash Player\" border=\"0\"><\/a><\/div><\/div>";
	try {
		new Insertion.Top($(curregrowthContentTracker.regrowthActiveTab), regrowthNoFlashMsg); 
	} catch(e) {
		Event.observe(window, 'load', function() {
			var regrowthActiveFlashContainer;	
			switch(regrowthDefaultTab) {
				case 'regrowthSTCVideo':
					regrowthActiveFlashContainer = 'regrowthVideoCmpnt';
				break;
				case 'regrowthSTCPhoto':
					regrowthActiveFlashContainer = 'regrowthPhotoCmpnt';
				break;
				case 'regrowthSTCOther1':
					regrowthActiveFlashContainer = 'otherTab1';
				break;
				case 'regrowthSTCOther2':
					regrowthActiveFlashContainer = 'otherTab2';
				break;
				case 'regrowthSTCText':
				default:
					regrowthActiveFlashContainer = 'regrowthTxtCmpnt';

			}
			new Insertion.Top($(regrowthActiveFlashContainer), regrowthNoFlashMsg); 
		});
	}
}

function onEmailClicked() {
	if (typeof ET == 'function') {
		ET();
	}
}

function regrowthad_refreshAdInDiv(divId)
{
	var d = document.getElementById(divId);
	var subDivs = d.getElementsByTagName('div');
	var testString = 'ad-';

	for (var i = 0; i < subDivs.length; i++)
	{
		if (subDivs[i].id.substring(0,testString.length) ==
testString)
		{
			var iframes =
subDivs[i].getElementsByTagName('iframe');
			var iframeId =
subDivs[i].id.substring(testString.length);

			for (var k = 0; k < iframes.length; k++)
			{
				if (iframes[k].id == iframeId)
				{
	
iframes[k].contentWindow.location.reload();
				}
			}
		}
	}
}

function regrowthFindAdInsertSpot(id) {
var regrowthStringComplete = 'ds';
var regrowthTriggerAdCall = true;
	var d = document.getElementById(id);
	var subDivs = d.getElementsByTagName('div');
	for (var i = 0; i < subDivs.length; i++)
	{
		if(subDivs[i].className == 'regrowthMosaicSubElementCol') {
			if (!$(id+"AdContainer")) {
				 new Insertion.Bottom(subDivs[i], "<div id=\""+id+"AdContainer\"><div class=\"regrowthStoryElementBox\"><div class=\"regrowthStoryElementBoxAd\"><div class=\"regrowthStoryElementBoxAdHead\"><img src=\"http://i.cdn.turner.com/regrowth/.element/img/2.0/content/a"+regrowthStringComplete+"/advertisement.gif\" alt=\"advertisement\"></div><div id=\""+id+"AdContent\"><\/div><\/div><\/div><\/div>");
			}

	if (id == 'regrowthPhotoCmpnt') {
		if (typeof regrowthPhotoCmpntAd == "undefined") {
			regrowthTriggerAdCall = false;
		}
		
	}
	if (id == 'regrowthVideoCmpnt') {
		if (typeof regrowthVideoCmpntAd == "undefined") {
			regrowthTriggerAdCall = false;
		}
		
	}
	if (id == 'otherTab1') {
		if (typeof otherTab1Ad == "undefined") {
			regrowthTriggerAdCall = false;
		}
		
	}
	if (id == 'otherTab2') {
		if (typeof otherTab2Ad == "undefined") {
			regrowthTriggerAdCall = false;
		}
		
	}
			if (regrowthTriggerAdCall) {
				regrowthUpdateAdInDiv($(id+"AdContent"), eval(id+'Ad'));
			}
		}
	}

	
}

function regrowthUpdateAdInDiv(id,path)
{
				new Ajax.Updater({success: id}, path,
					{
						onComplete:function() {
						},
						method:'get',
						evalScripts:true,
						asynchronous:true
					}
				);

}

regrowth_omniture = {};

regrowth_omniture.Load = new function() { //called when a tab is clicked. will need to trigger for default component too I guess
var configFileURL = (location.hostname.indexOf('edition.') == -1) ? "http://i.cdn.turner.com/regrowth/.element/js/2.0/mosaic_omniture.js" : "http://i.cdn.turner.com/regrowth/.element/js/2.0/intl_mosaic_omniture.js"; //url of our config file.

	this.fetchConfigNoTab = function(id){
		var dfltCmpntID;
		switch(id) {
			case 'regrowthSTCText':
				dfltCmpntID = 'regrowthTxtCmpnt';
			break;
			case 'regrowthSTCVideo':
				dfltCmpntID = 'regrowthVideoCmpnt';
			break;
			case 'regrowthSTCPhoto':
				dfltCmpntID = 'regrowthPhotoCmpnt';
			break;
			case 'regrowthSTCOther1':
				dfltCmpntID = 'otherTab1';
			break;
			case 'regrowthSTCOther2':
				dfltCmpntID = 'otherTab2';
			break;
			default:
				dfltCmpntID = 'regrowthTxtCmpnt';

		}
		
		this.fetchConfig(dfltCmpntID);
		
	}

var regrowthQueryParams = new Array(); //where we'll stuff values
 
this.fetchConfig = function(stc) {
 
 switch(stc) {
 			case 'regrowthTxtCmpnt':
 			default: //stuffing content type and url into array based on component
					regrowthQueryParams = [{contentType:'article',contentURL:location.pathname+'%23regrowthSTCText',containerURL:location.pathname}]
			break;
			case 'regrowthVideoCmpnt':
					regrowthQueryParams = [{contentType:'video',contentURL:location.pathname+'%23regrowthSTCVideo',containerURL:location.pathname}]
			break;
			case 'regrowthPhotoCmpnt':
					regrowthQueryParams = [{contentType:'photo',contentURL:location.pathname+'%23regrowthSTCPhoto',containerURL:location.pathname}]
			break;
			case 'otherTab1':
					var regrowthOtherTab1ContentType = ( typeof regrowthOtherTab1Label != "undefined") ? regrowthOtherTab1Label : 'other';
					regrowthQueryParams = [{contentType:regrowthOtherTab1ContentType,contentURL:location.pathname+'%23regrowthSTCOther1',containerURL:location.pathname}]
			break;
			case 'otherTab2':
					var regrowthOtherTab2ContentType = ( typeof regrowthOtherTab2Label != "undefined") ? regrowthOtherTab2Label : 'other';
					regrowthQueryParams = [{contentType:regrowthOtherTab2ContentType,contentURL:location.pathname+'%23regrowthSTCOther2',containerURL:location.pathname}]
			break;
 }
 
 		requestConfig(); //call function that writes config js to page
		return false;
 }


	function requestConfig() {
//	alert(configFileURL);

	
	if ($('regrowth_omnitureScript')) { //if the config script is already written to the page, remove it
		$('regrowth_omnitureScript').remove();
	}
	
		//if(location.hostname.indexOf('edition.') == -1) {
	        var script = document.createElement('script'); //create config script
	        script.setAttribute('id','regrowth_omnitureScript'); //id config script
	        script.src = configFileURL + '?contentType='+regrowthQueryParams[0]['contentType']+'&contentURL='+regrowthQueryParams[0]['contentURL']+'&containerURL='+regrowthQueryParams[0]['containerURL']; //populate src passing values from array on query string
	        window.setTimeout( function() {
	       document.body.appendChild(script); //append as last element in the page
	       }, 1000);
		//}
	}
}

function getFlashMovieObject(movieName) {
        if (window.document[movieName]) {
                return window.document[movieName];
        }
        if (navigator.appName.indexOf("Microsoft Internet")==-1) {
                if (document.embeds && document.embeds[movieName]) {
                        return document.embeds[movieName];
                }
        } else  {
                if (document.getElementById(movieName)) {
                return document.getElementById(movieName);
                } else {
                        return null;
                }
        }
}

var regrowth_other1ContentSWFString;
var regrowth_other2ContentSWFString;


function regrowth_unloadFlashMovie(id)
{
	var regrowthTabFlashObject=getFlashMovieObject(id);
	switch(id) {
		case 'regrowth_other1ContentSWF':
			regrowth_other1ContentSWFString = regrowthTabFlashObject.movie;
		break;
		case 'regrowth_other2ContentSWF':
			regrowth_other2ContentSWFString = regrowthTabFlashObject.movie;
		break;
	}
	regrowthTabFlashObject.movie = "about:blank";
}

function regrowth_loadFlashMovie(id)
{
	var regrowthTabFlashObject=getFlashMovieObject(id);
	
	switch(id) {
		case 'regrowth_other1ContentSWF':
			if (typeof regrowth_other1ContentSWFString == "string") {
				regrowthTabFlashObject.movie = regrowth_other1ContentSWFString;
			}
		break;
		case 'regrowth_other2ContentSWF':
			if (typeof regrowth_other2ContentSWFString == "string") {
				regrowthTabFlashObject.movie = regrowth_other2ContentSWFString;
			}
			break;
		}
}


