window.addEvent('domready',function(){
	
	try
	{
		$('showTVFadeWrapper').fade('hide');
		$('showTVFadeWrapper').removeClass('hide');
	}
	catch(err)
	{}
	 

});

function SetShowTVPopupShown()
{
	Set_Cookie("popupShowTVShown",'1',0, '/','','');
	
	
}

var blTVFadedIn = false;

function FadeInShowTV()
{		
	$('showTVFadeWrapper').fade('in'); 
	$('showTVFadeWrapper').setStyle('visibility','visible');
}

function LoadIFrame()
{
	new IFrame({
			src: '/shared/iframehidden/tv-fade-in.html',
			border: 0, frameborder: 0, id: 'iframetv',
			styles: {width:430, height: 305},
			onload: function (doc) {
					//$('test').setStyle('height', this.getScrollSize().y);
					//$('test').setStyle('width', this.getScrollSize().x);
			}
	}).inject($('iFrameTVContainer'));
	
}

function FadeOutShowTV()
{
	$('showTVFadeWrapper').fade('out');
	
	SetShowTVPopupShown();
}


var elapsedShowTVTime = 0;//this will hold the elapsed time;
var initialShowTVTimerLength = 30 * 1000;
var timerShowTVObject;

window.onunload = function()
{
    //elapsed value gets pushed to a cookie here
    Set_Cookie("elapsedShowTVTime",elapsedShowTVTime,0, '/','','');	
	Set_Cookie("PopupTimerStarted","NotStarted",0, '/','','');
}

function IsTVPopupAllowed()
{
	var strCurrentURL = window.location.href;
	strCurrentURL = strCurrentURL.toLowerCase();
	
	//alert(strCurrentURL);
	
	var returnValue = true;
	
	if(strCurrentURL.indexOf("/exercises/") != -1)
	{
		strCurrentURL = strCurrentURL.replace("http://localhost:81","");
		strCurrentURL = strCurrentURL.replace("http://www.workoutbox.com","");
		
		switch(strCurrentURL)
		{
			//case "/exercises/":
			
			case "/exercises/ab-exercises/":
				case "/exercises/ab-exercises/lower-ab-exercises/":
				case "/exercises/ab-exercises/upper-ab-exercises/":
				case "/exercises/ab-exercises/oblique-exercises/":
			
			case "/exercises/arm-exercises/":
				case "/exercises/arm-exercises/bicep-exercises/":
				case "/exercises/arm-exercises/tricep-exercises/":
			
			case "/exercises/back-exercises/":
				case "/exercises/back-exercises/lower-back-exercises/":
				case "/exercises/back-exercises/upper-back-exercises/":
				case "/exercises/back-exercises/lat-exercises/":
			
			case "/exercises/chest-exercises/":
				case "/exercises/chest-exercises/lower-chest-exercises/":
				case "/exercises/chest-exercises/upper-chest-exercises/":
			
			case "/exercises/core-exercises/":
			
			case "/exercises/leg-exercises/":
				case "/exercises/leg-exercises/thigh-exercises/":
				case "/exercises/leg-exercises/butt-exercises/":
				case "/exercises/leg-exercises/hamstring-exercises/":
				case "/exercises/leg-exercises/calf-exercises/":
			
			case "/exercises/shoulder-exercises/":
			
			case "/exercises/home-exercises/":
			case "/exercises/fat-burning-exercises/":
			case "/exercises/weight-lifting-exercises/":
			
			case "/exercises/cardio-exercises/":
			case "/exercises/stretching-exercises/":
				
				//alert("yes");
				returnValue = true;
				break;
			
			default:
				
				//alert("no");
				//must be some other exercise page therefore not allows
				returnValue = false;
				break;
		}
	}
	
	return returnValue;
}

//Run this on page load.
function startTVTimerCounter()
{	
	if(IsTVPopupAllowed())
	{
		popupShowTVShown = null;
		popupShowTVShown = Get_Cookie('popupShowTVShown');
		
		elapsedShowTVTime = Get_Cookie('elapsedShowTVTime');
		
		if(elapsedShowTVTime == null || elapsedShowTVTime == "NaN")
		{
			elapsedShowTVTime = 0;
		}
		else
		{
			//alert("Previous time is " + elapsedTime);
		}
		//TESTING
		//popupShown = null;
		
		if (popupShowTVShown==null)
		{			
			timerShowTVObject = setTimeout("timerShowTVTick()", 1000);
		}
	}
}

var iFrameLoaded = false;

//Executes every second and checks to see if the timer has hit threshold
function timerShowTVTick()
{
	//alert(elapsedTime);

	
		elapsedShowTVTime = (elapsedShowTVTime * 1) + 1000;
		
		if(elapsedShowTVTime > initialShowTVTimerLength - 7000)
		{
			if(!iFrameLoaded)
			{
				LoadIFrame();
				iFrameLoaded = true;
			}
		}
		
		if(elapsedShowTVTime > initialShowTVTimerLength)
		{
			try
			{
				//alert("fading in");
				FadeInShowTV();
			}
			catch(err)
			{
				alert(err);
				//popup code not loaded yet
				setTimeout("timerShowTVTick()",1000);
			}
			
		}
		else
		{
			setTimeout("timerShowTVTick()",1000);
		}
}