/*  ------------------------------------------------------------------------
                            NiFTy Global Javascript
    ------------------------------------------------------------------------  */

// NEW WINDOW
function targetBlank (url) 
{
	blankWin = window.open(url,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
}

// DISPLAY INSTRUCTIONS
function display_instructions()
{
	(document.getElementById('dl_instructions').style || document.getElementById('dl_instructions')).visibility = "visible";
	(document.getElementById('dl_instructions').style || document.getElementById('dl_instructions')).position = "static";
	document.getElementById('dl_instructions').style.display='block';
	document.getElementById('dl_instructions').scrollIntoView(true);
}

function hide_instructions()
{
	document.getElementById('dl_instructions').style.display='none';
}

// POPUP
var win = null;
function NewWindow( mypage, myname, w, h, scroll )
{
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
	win = window.open( mypage, myname, settings );
}



/* Smooth scrolling
 *
 * Changes links that link to other parts of this page to scroll
 * smoothly to those links rather than jump to them directly, which
 * can be a little disorienting.
 * sil, http://www.kryogenix.org/
 * v1.0 2003-11-11
 * v1.1 2005-06-16 wrap it up in an object
 */

var ss = {
	fixAllLinks: function()
	{
		// Get a list of all links in the page
		var allLinks = document.getElementsByTagName('a');

		// Walk through the list
		for( var i=0;i<allLinks.length;i++ )
		{
			var lnk = allLinks[i];
			if( (lnk.href && lnk.href.indexOf('#') != -1) && (lnk.pathname == location.pathname || '/'+lnk.pathname == location.pathname) && lnk.search == location.search )
			{
				/* If the link is internal to the page (begins in #)
				 * then attach the smoothScroll function as an onclick
				 * event handler.
				 */
				ss.addEvent(lnk,'click',ss.smoothScroll);
			}
		}
	},

	smoothScroll: function( e )
	{
		/* This is an event handler; get the clicked on element,
		 * in a cross-browser fashion.
		 */
		if( window.event )
			target = window.event.srcElement;
		else if( e )
			target = e.target;
		else
			return;

		/* Make sure that the target is an element, not a text node
		 * within an element.
		 */
		if( target.nodeType == 3 )
			target = target.parentNode;

		// Paranoia; check this is an A tag
		if( target.nodeName.toLowerCase() != 'a' )
			return;

		// Find the <a name> tag corresponding to this href
		// First strip off the hash (first character)
		anchor = target.hash.substr( 1 );

		// Now loop all A tags until we find one with that name
		var allLinks = document.getElementsByTagName( 'a' );
		var destinationLink = null;

		for( var i=0; i < allLinks.length; i++ )
		{
			var lnk = allLinks[i];
			if( lnk.name && lnk.name == anchor )
			{
				destinationLink = lnk;
				i = allLinks.length;
			}
		}

		// If we didn't find a destination, give up and let the browser do its thing.
		if( !destinationLink )
			return true;

		// Find the destination's position.
		var destx = destinationLink.offsetLeft; 
		var desty = destinationLink.offsetTop;
		var thisNode = destinationLink;
		while( thisNode.offsetParent && thisNode.offsetParent != document.body )
		{
			thisNode = thisNode.offsetParent;
			destx += thisNode.offsetLeft;
			desty += thisNode.offsetTop;
		}

		// Stop any current scrolling
		clearInterval( ss.INTERVAL );
		cypos = ss.getCurrentYPos();

		ss_stepsize = parseInt( (desty-cypos)/ss.STEPS );
		ss.INTERVAL = setInterval( 'ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10 );
	
		// And stop the actual click happening
		if( window.event )
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if( e && e.preventDefault && e.stopPropagation )
		{
			e.preventDefault();
			e.stopPropagation();
		}
	},
	
	scrollWindow: function( scramount,dest,anchor )
	{
		wascypos = ss.getCurrentYPos();
		isAbove = (wascypos < dest);
		window.scrollTo(0,wascypos + scramount);
		iscypos = ss.getCurrentYPos();
		isAboveNow = (iscypos < dest);

		if( isAbove != isAboveNow || wascypos == iscypos )
		{
			/* If we've just scrolled past the destination, or
			 * we haven't moved from the last scroll (i.e., we're at the
			 * bottom of the page) then scroll exactly to the link.
			 */
			window.scrollTo(0,dest);

			// Cancel the repeating timer...
			clearInterval(ss.INTERVAL);

			// ...and jump to the link directly so the URL's right.
			location.hash = anchor;
		}
	},

	getCurrentYPos: function()
	{
		if( document.body && document.body.scrollTop )
			return document.body.scrollTop;
		else if( document.documentElement && document.documentElement.scrollTop )
			return document.documentElement.scrollTop;
		else if( window.pageYOffset )
			return window.pageYOffset;
		return 0;
	},

	addEvent: function( elm, evType, fn, useCapture )
	{
		/* addEvent and removeEvent
		 * cross-browser event handling for IE5+,  NS6 and Mozilla
		 * By Scott Andrew
		 */
		if( elm.addEventListener )
		{
			elm.addEventListener(evType, fn, useCapture);
			return true;
		}
		else if( elm.attachEvent )
		{
			var r = elm.attachEvent("on"+evType, fn);
			return r;
		}
		else
			alert("Handler could not be removed");
	}
}

//VOD function

function VOD_firefox_change_content(input)
{
	var Element=document.getElementById('Player');
	Element.URL = input;
	Element.controls.play();
}

function VOD_ie_change_content(input)
{
	var Element=document.Player;
	Element.filename = input; 
	Element.filename = input;
}

function graph_init()
{
	document.getElementById("viewership_graph").style.display="block";
	document.getElementById("viewership_graph").style.opacity="0";
	document.getElementById("viewership_graph").style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(opacity=0)';
	
	set_scroll();
	setTimeout('fade_in()', '100');
}

function set_scroll()
{
	var xpos = document.getElementById("xpos").value;
	var ypos = document.getElementById("ypos").value;

	window.scrollTo(xpos,ypos);
}

function fade_in()
{

	var element = document.getElementById("viewership_graph");
	var next_opac = (parseFloat(element.style.opacity) + .1)*100;
	
	SetOpacity(element, next_opac);
	
	if(next_opac < 100)
	{
		setTimeout('fade_in()', '100');
	}
	
	else if(next_opac == 100)
	{
		setTimeout('fade_out()', '28000');
	}
}

function fade_out()
{

	var element = document.getElementById("viewership_graph");
	var next_opac = (parseFloat(element.style.opacity) - .1)*100;
	
	SetOpacity(element, next_opac);
	
	if(next_opac > 0)
	{
		setTimeout('fade_out()', '100');
	}

}


function show_player()
{
	var player_div = document.getElementById("NiFTyPlayer");
	player_div.style.display="block";
	document.getElementById("pleasechoose").style.display="none";
	document.getElementById("patientvideo").style.display="block";
	document.getElementById("patientvideo").style.opacity="1";
	document.getElementById("patientvideo").style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(opacity=1)';

	setTimeout('hide_buffering()', '7000');
	window.scrollTo(0,0);
}

function hide_buffering()
{

	var element = document.getElementById("patientvideo");
	var next_opac = (parseFloat(element.style.opacity) - .1)*100;
	
	SetOpacity(element, next_opac);
	
	if(next_opac > 0)
	{
		setTimeout('hide_buffering()', '100');
	}

}

function SetOpacity(obj, opacity)
{
	opacity = (opacity < 0)?0:opacity;
	opacity = (opacity > 100)?100:opacity;
	opacity=opacity/100;

	//FF,OP,SF
	obj.style.opacity = 	opacity;

	//IE
	obj.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(opacity=' + opacity + ')';
}

function init_player()
{
	/*if(navigator.userAgent.indexOf("MSIE") != -1)
	{
		document.write('<OBJECT ID="MediaPlayer1" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab# Version=5,1,52,701" STANDBY="Loading Microsoft Windows® Media Player components..." TYPE="application/x-oleobject" width="320" height="240">');
	}*/
	
	if (navigator.userAgent.indexOf("Firefox") != -1)
	{
		document.writeln('<OBJECT id="Player"'); 
		document.write(' type="application/x-ms-wmp"'); 
		document.write(' width=320 height=300> \n');
	}  
	else
	{
		document.writeln('<OBJECT ID="Player" ')
		document.write('CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" ');
		document.write('CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#" ');
		document.write('Version=5,1,52,701" ');
		document.write('STANDBY="Loading Microsoft Windows® Media Player components..." ');
		document.write('TYPE="application/x-oleobject" ');
		document.write('width="320" height="240"> \n');
	}
	
	document.writeln('<PARAM name="animationatStart" value="true">');
	document.writeln('<PARAM name="transparentatStart" value="true">');
	document.writeln('<PARAM name="autoStart" value="true">');
	document.writeln('<PARAM name="is " value="true">');
	document.writeln('<PARAM name="Volume" value="-300">');
	document.writeln('<PARAM name="stretchToFit" value="true"> ');
	document.writeln('<PARAM name="dock" value="fill" />');	
	
	document.writeln('</OBJECT>');
}

function switch_live_display()
{
	var ptr = document.getElementById('live_banner');
	if(ptr)
		ptr.style.display = "none";
	ptr = document.getElementById('vodmore_button');
	if(ptr)
		ptr.style.display = "none";
	ptr = document.getElementById('live_banner2');
	if(ptr)
		ptr.style.display = "block";
	ptr = document.getElementById('live_button');
	if(ptr)
		ptr.style.display = "block";
	ptr = document.getElementById('vod_button');
	if(ptr)
		ptr.style.display = "block";
	ptr = document.getElementById('invite_button');
	if(ptr)
		ptr.style.display = "block";
	ptr = document.getElementById('reminder_button');
	if(ptr)
		ptr.style.display = "block";
	ptr = document.getElementById('cancel_button');
	if(ptr)
		ptr.style.display = "block";
}

function switch_back_display()
{
	var ptr = document.getElementById('live_banner');
	if(ptr)
		ptr.style.display = "block";
	ptr = document.getElementById('vodmore_button');
	if(ptr)
		ptr.style.display = "block";
	ptr = document.getElementById('live_banner2');
	if(ptr)
		ptr.style.display = "none";
	ptr = document.getElementById('live_button');
	if(ptr)
		ptr.style.display = "none";
	ptr = document.getElementById('vod_button');
	if(ptr)
		ptr.style.display = "none";
	ptr = document.getElementById('invite_button');
	if(ptr)
		ptr.style.display = "none";
	ptr = document.getElementById('reminder_button');
	if(ptr)
		ptr.style.display = "none";
	ptr = document.getElementById('cancel_button');
	if(ptr)
		ptr.style.display = "none";
}

function getScrollX()
{
	var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
	return scrollLeft;
}


function getScrollY()
{
	var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0; 
	return scrollTop;
}

/*function VODsearch()
{
	//window.location = (document.URL); 
	//alert (document.URL);
	alert(document.getElementById("brandPage").value);
	alert(document.getElementById("page").value);
	alert(document.getElementById("section").value);
	return true;
}*/
function VODsearch()
{
	var path = location.pathname;
	var pathArray = path.split("/");
	var location_URL = window.location.hostname;
	var str="http://"+location_URL+"/"+pathArray[1]+"/video_on_demand";
	var current_td = null; 
	var div = document.getElementById("parentSearch");
	var temp_name = "";
	var temp_sel = "";
	current_td = div.firstChild; 

	do 
		{ 
			current_select=current_td.firstChild;
			while(current_select)
			{
				if(current_select.nodeName == "SELECT")
				{
					temp_sel = current_select;
				}
				else if(current_select.nodeName == "STRONG")
				{
					temp_name = current_select.innerHTML;
				}
				current_select=current_select.nextSibling;
			}
			if(temp_sel != "" && temp_name != "")
			{
				var temp_doc = document.getElementById(temp_sel.id);
//				str += "/" + encodeHTML(temp_name) + "/" + encodeHTML(temp_doc.value);
				str += "/" + encodeHTML(temp_name) + "/" + encodeHTML(temp_doc.options[temp_doc.selectedIndex].text);
			}			
			temp_sel = "";
			temp_name = "";
			current_td = current_td.nextSibling;
		}
	while(current_td)
	
	//check second row if it exists for mobile
	div = null;
	div = document.getElementById("parentSearch2");
	
	if(div != null)
	{
		current_td = div.firstChild; 
		
		do 
		{ 
			current_select=current_td.firstChild;
			while(current_select)
			{
				if(current_select.nodeName == "SELECT")
				{
					temp_sel = current_select;
				}
				else if(current_select.nodeName == "STRONG")
				{
					temp_name = current_select.innerHTML;
				}
				current_select=current_select.nextSibling;
			}
			if(temp_sel != "" && temp_name != "")
			{
				var temp_doc = document.getElementById(temp_sel.id);
				//str += "/" + encodeHTML(temp_name) + "/" + encodeHTML(temp_doc.value);
				str += "/" + encodeHTML(temp_name) + "/" + encodeHTML(temp_doc.options[temp_doc.selectedIndex].text);
			}			
			temp_sel = "";
			temp_name = "";
			current_td = current_td.nextSibling;
		}
		while(current_td)
	}

	window.location = (str);
}


function encodeHTML(encodedHtml) 
{
     encodedHtml = escape(encodedHtml);
     encodedHtml = encodedHtml.replace(/\//g,"%7C");
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     return encodedHtml;
} 
	
