/***************************************************************************
 *	Copyright 2007 Creative Tank Ltd, All Rights Reserved
 *
 *	Any violation of copyright should be reported immediately to:
 *			Creative Tank Ltd
 *			150 Fleet Street
 *			London, EC4A 2DQ, United Kingdom
 *			+44 (0) 207 936 2888
 *
 ***************************************************************************/


// Call this function for standarad html content
function webtv_RunRealContent(){
  webtv_GenerateObj("webtv_RunRealContent()", false, "clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA", arguments);
}

// Call this function for XHTML content
function webtv_RunRealContentX(){
  webtv_GenerateObj("webtv_RunRealContent()", true, "clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA", arguments);
}

// Call this function for standarad html content
function webtv_RunQuickContent(){
  webtv_GenerateObj("webtv_RunQuickContent()", false, "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B", arguments);
}

// Call this function for XHTML content
function webtv_RunQuickContentX(){
  webtv_GenerateObj("webtv_RunQuickContent()", true, "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B", arguments);
}

// Call this function for standarad html content
function webtv_RunWMPContent(){
  webtv_GenerateObj("webtv_RunWMPContent()", false, "clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6", arguments);
}

// Call this function for XHTML content
function webtv_RunWMPContentX(){
  webtv_GenerateObj("webtv_RunWMPContent()", true, "clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6", arguments);
}

var  _mdply = null;
var  _mdtyp = null;
var  _skipSecs = 30;
var  _seeker = null;
var  _real_seek_correction_secs = 0;
var  _winm_seek_correction_secs = 0;

function mpInit(p,type) { _mdply = p; _mdtyp=type; }
function mpInitEx(type) { mpInit(document.player, type); }

function rmPlay()  { _mdply.DoPlay(); }
function rmStop()  { if (_mdply.CanStop()) _mdply.DoStop(); }
function rmPause() { _mdply.DoPlayPause(); }
function rmMute()  { _mdply.SetMute(!_mdply.GetMute()); }
function rmFull()  { if (!_mdply.GetFullScreen()) _mdply.SetFullScreen(); }
function rmLoud()  { _mdply.SetVolume( Math.min(_mdply.GetVolume()+16, 100) ); }
function rmQuiet() { _mdply.SetVolume( Math.max(_mdply.GetVolume()-16, 000) ); }
function rmFF()	   { if (_mdply.GetCanSeek()) _mdply.SetPosition( _mdply.GetPosition() + _skipSecs * 1000 ); }
function rmRW()	   { if (_mdply.GetCanSeek()) _mdply.SetPosition( _mdply.GetPosition() - _skipSecs * 1000 ); }
function rmSeek(s) { if (_mdply.GetCanSeek()) {clearTimeout(_seeker); _mdply.SetPosition( (s+_real_seek_correction_secs) * 1000 );} else eval('setTimeout("rmSeek('+s+')",100)');}
function rmDL()    {
	var w = 'http://www.real.com/realplayer.html';
	var m = 'http://www.apple.com/downloads/macosx/internet_utilities/realplayer.html';
	mac = ( navigator.appVersion.indexOf( 'Mac' ) != -1 );
	window.open(mac?m:w);
}


function wmPlay()  { _mdply.controls.play(); }
function wmStop()  { _mdply.controls.stop(); }
function wmPause() { _mdply.controls.pause(); }
function wmMute()  { _mdply.settings.mute = (_mdply.settings.mute ? false : true) }
function wmFull()  { _mdply.fullScreen=true; }
function wmLoud()  { _mdply.settings.volume = Math.min(_mdply.settings.volume+16, 100); }
function wmQuiet() { _mdply.settings.volume = Math.max(_mdply.settings.volume-16, 0); }
function wmFF()	   { _mdply.controls.fastForward(); }
function wmRW()	   { _mdply.controls.fastForward(); }
function wmSeek(s) { _mdply.controls.currentPosition = s+_winm_seek_correction_secs; }
function wmDL()    { window.open('http://www.microsoft.com/windows/windowsmedia/player/download/download.aspx'); }

var qtMuteState=false;
var qtResumeWindow=null;
var qtResumeTime=0;
var qtMonTimer = null;
var qtMonHeartBeat = 0;
var qtMonQuantum=300; //ms
var qtMonBuffer=2000; //ms
function qtPlay()  { qtMonParentCancel(); if (qtIsFFRW) _mdply.SetMute(qtMuteState); _mdply.Play(); }
function qtStop()  { qtMonParentCancel(); if (qtIsFFRW) _mdply.SetMute(qtMuteState); _mdply.Stop(); }
function qtPause() { qtMonParentCancel(); if (qtIsFFRW) _mdply.SetMute(qtMuteState); _mdply.SetRate( _mdply.GetRate()!=0?0:1 ); }
function qtMute()  { qtMonParentCancel(); _mdply.SetMute( _mdply.GetMute() ? false : true ); qtMuteState=_mdply.GetMute(); }
function qtFull()  { qtMonParentCancel(); url=getParam('url', window.location.href); if (_mdply != null) { _mdply.SetRate(0); fullScreen('/?id=18&t='+parseInt(_mdply.GetTime()/_mdply.GetTimeScale())+'&url='+url,'wtvqtfull'); qtMonHeartBeat=100*(qtMonBuffer/qtMonQuantum); qtMonTimer=setInterval('qtMonParent()',qtMonQuantum); _mdply.Stop(); } }
function qtLoud()  { qtMonParentCancel(); if( _mdply.GetMute() ) _mdply.SetMute(false); _mdply.SetVolume(Math.min(_mdply.GetVolume()+40, 255)); }
function qtQuiet() { qtMonParentCancel(); if( _mdply.GetMute() ) _mdply.SetMute(false); _mdply.SetVolume(Math.max(_mdply.GetVolume()-40, 0)); }
function qtFF()	   { qtMonParentCancel(); _mdply.SetRate(_mdply.GetRate()<=1?2.0:1.0);  _mdply.SetMute(qtIsFFRW()?true:qtMuteState); }
function qtRW()	   { qtMonParentCancel(); _mdply.SetRate(_mdply.GetRate()>=0?-2.0:1.0); _mdply.SetMute(qtIsFFRW()?true:qtMuteState); }
function qtIsFFRW(){ return ( _mdply.GetRate()<0 || _mdply.GetRate()>1 ); }
function qtSeek(s) { qtMonParentCancel(); _mdply.controls.currentPosition = s+_winm_seek_correction_secs; }
function qtDL()    { qtMonParentCancel(); window.open('http://www.apple.com/quicktime/download'); }
function qtRegResume(p,o) { _mdply=p; qtResumeWindow=o; qtResumeWindow.qtMonHeartBeat=qtMonBuffer/qtMonQuantum; qtMonTimer=setInterval('qtMonChild()',qtMonQuantum); window.onunload=qtActResume;}
function qtResume(t) 	  { qtMonParentCancel(); loadingStart('wtvLoading'); if (_mdply != null) { _mdply.SetTime(Math.max(t-_mdply.GetTimeScale(),0)); qtPlay(); } }
function qtActResume()	  { qtResumeWindow.qtMonHeartBeat=-1; }
function qtMonChild()     { qtResumeWindow.qtMonHeartBeat++; if (_mdply != null) qtResumeWindow.qtResumeTime=_mdply.GetTime(); }
function qtMonParent()    { qtMonHeartBeat--; if (qtMonHeartBeat<0) { qtMonParentCancel(); qtResume(qtResumeTime); } }
function qtMonParentCancel() { if ( qtMonTimer != null ) { clearInterval(qtMonTimer); qtMonTimer=null; } }

function xmPlay()  { if(_mdtyp=='WIN') wmPlay();  else if (_mdtyp=='REAL') rmPlay();	else if (_mdtyp=='QT') qtPlay();  }
function xmStop()  { if(_mdtyp=='WIN') wmStop();  else if (_mdtyp=='REAL') rmStop();	else if (_mdtyp=='QT') qtStop();  }
function xmPause() { if(_mdtyp=='WIN') wmPause(); else if (_mdtyp=='REAL') rmPause();	else if (_mdtyp=='QT') qtPause(); }
function xmMute()  { if(_mdtyp=='WIN') wmMute();  else if (_mdtyp=='REAL') rmMute();	else if (_mdtyp=='QT') qtMute();  }
function xmFull()  { if(_mdtyp=='WIN') wmFull();  else if (_mdtyp=='REAL') rmFull();	else if (_mdtyp=='QT') qtFull();  }
function xmLoud()  { if(_mdtyp=='WIN') wmLoud();  else if (_mdtyp=='REAL') rmLoud();	else if (_mdtyp=='QT') qtLoud();  }
function xmQuiet() { if(_mdtyp=='WIN') wmQuiet(); else if (_mdtyp=='REAL') rmQuiet();	else if (_mdtyp=='QT') qtQuiet(); }
function xmFF()	   { if(_mdtyp=='WIN') wmFF();    else if (_mdtyp=='REAL') rmFF();		else if (_mdtyp=='QT') qtFF();    }
function xmRW()	   { if(_mdtyp=='WIN') wmRW();    else if (_mdtyp=='REAL') rmRW();		else if (_mdtyp=='QT') qtRW();    }
function xmDL()    { if(_mdtyp=='WIN') wmDL();    else if (_mdtyp=='REAL') rmDL();	    else if (_mdtyp=='QT') qtDL();    }
function xmSeek(s) { if(_mdtyp=='WIN') wmSeek(s); else if (_mdtyp=='REAL') rmSeek(s);	else if (_mdtyp=='QT') qtSeek(s); }

function wtvChangeQuality(newQuality) {
	window.location = wtvChangeParam('quality', newQuality.toUpperCase(), window.location.href);
	return false;
}

function wtvChangeChannel(newChannel) {
	window.location = wtvChangeParam('channel', newChannel, window.location.href);
	return false;
}

function wtvChangeChannelListingContainer(newChannel) {
	ifrm = window.frames['wtvChannelListingContainer'];
	ifrm.location = wtvChangeParam('channel', newChannel, ifrm.location.href);
	return false;
}

function wtvChangeUrl(newUrl) {
	window.location = wtvChangeParam('url', newUrl, window.location.href);
	return false;
}

function wtvChangeUrlChannel(newUrl,channel) {
	window.location = wtvChangeParam('url', newUrl, wtvChangeParam('channel', channel, window.location.href));
	return false;
}

function wtvChangeActiveTab(b) {
	for( var t=b; t.getAttribute('tabvariable')==null && t!=document; t = t.parentNode);
	if ( t != document ) {
		var imgs = t.getElementsByTagName("img");
		for (var i = 0; i < imgs.length; i++)
			if ( null != imgs[i].getAttribute('value') )
				if (imgs[i] == b) imgs[i].src = imgs[i].alt; else imgs[i].src = imgs[i].getAttribute('normal');
	}
}

function wtvInitTabs(t) {
	t = document.getElementById(t);
	if ( null != t ) {
		var tvariable = t.getAttribute('tabvariable');
		var tdefault  = t.getAttribute('tabdefault');
		var tparamvar = unescape(getParam(tvariable, window.location.href));
		var tset      = (tparamvar=="")?tdefault:tparamvar;
		var imgs      = t.getElementsByTagName("img");

		for (var i = 0; i < imgs.length; i++) {
			val = imgs[i].getAttribute('value');
			if (val!=null) {
				// Save the regular image
			    imgs[i].setAttribute('normal', imgs[i].getAttribute("src"));
			    // Enable the indicated tab
			    if ( val == tset ) imgs[i].src = imgs[i].alt;
			}
		}
	}
}

function wtvChangeChannelTab(o) {
	wtvChangeActiveTab(o);
	wtvChangeChannelListingContainer(o.getAttribute('value'));
}

/***************************
	2007-03-14 RBW
    This function handle multiple overlapping tabs
    The images/buttons for layered tabs are all transparent gifs
    On click we change the background image of the div in which
    they are contained.
***************************/

function wtvChangeChannelLayeredTab(o) {
	//change tabs image
	o.parentNode.style.backgroundImage = 'url(' + o.getAttribute('alt') + ')';

	//write the title, if necessary
	var ct = document.getElementById('channelTitle');
	if(ct) {
		ct.innerHTML = o.getAttribute('title');
	}

	wtvChangeChannelListingContainer(o.getAttribute('value'));
}


function printDate() {
	var monthtext = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
	var currentTime = new Date();
	var month = currentTime.getMonth();
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	return (day + " " + monthtext[month] + " " + year);
}

function wtvChangeParam(param, newValue, newUrl) {
	paramEq 	 = param+"=";
	currentValue = getParam(param, newUrl);

	if (""==currentValue) {
		// Always remove the #, that tends to mess things up when we append
		newUrl = newUrl.replace('#','');
		newUrl = newUrl + (newUrl.indexOf('?')>0?'&':'?') + paramEq + newValue;
	}
	else {
		newUrl = newUrl.replace(paramEq+currentValue,paramEq+newValue);
	}

	return newUrl;
}

function getParam( name, url )
{
  var regex = new RegExp( "[\\?&]"+name+"=([^&#]*)" );
  var params = regex.exec( url );
  if( params == null )
    return "";
  else
    return params[1];
}

scrollPos=new Array(0,0,0,0,0,0,0)
scrollUrl=new Array(null,null,null,null,null,null)
scrollSpeed=4
scrollTimer=null

barHeight = 315;
scrollerHeight = 42;

function scrollDown(nameid, instance) {
	scrollStop()
	scrollCheckDelta(nameid, instance)

	maxScroll = (window.frames[nameid].document.body.scrollHeight-document.getElementById(nameid).scrollHeight)

	if(scrollPos[instance]>=(maxScroll-scrollSpeed)){
		scrollPos[instance] = maxScroll
	}
	else {
		scrollPos[instance] += scrollSpeed
		window.frames[nameid].scrollTo(0,scrollPos[instance])
		scrollBarDown(nameid + 'ScrollBar', scrollBarStep(nameid));
		scrollTimer=setTimeout("scrollDown('"+nameid+"',"+instance+")",50)
	}
}

function scrollUp(nameid, instance) {
	scrollStop()
	scrollCheckDelta(nameid, instance);

	if(scrollPos[instance]<=scrollSpeed){
		scrollPos[instance] = 0
	}
	else {
		scrollPos[instance] -= scrollSpeed
		window.frames[nameid].scrollTo(0,scrollPos[instance])
		scrollBarUp(nameid + 'ScrollBar', scrollBarStep(nameid));
		scrollTimer=setTimeout("scrollUp('"+nameid+"',"+instance+")",50)
	}
}

function scrollBarUp(barid, amt) {
	var bar = document.getElementById(barid);
	if(bar) {
		var y = parseInt(bar.style.top);
		if((y - amt) >= 0)
			bar.style.top = (y - amt) + 'px';
		else
			bar.style.top = '0px';
	}
}

function scrollBarStep(nameid) {
	var maxScroll = (window.frames[nameid].document.body.scrollHeight-document.getElementById(nameid).scrollHeight)
	var steps = maxScroll/scrollSpeed;

	return parseInt( (barHeight) / steps );
}

function scrollBarDown(barid, amt) {
	var bar = document.getElementById(barid);
	if(bar) {
		var y = parseInt(bar.style.top);
		if(isNaN(y)) y = 0;
		if(y+amt <= (barHeight - scrollerHeight))
			bar.style.top = (y + amt) + 'px';
		else
			bar.style.top = (barHeight - scrollerHeight) + 'px';
	}
}

function scrollCheckDelta(nameid, instance) {
	newloc = window.frames[nameid].location.href
	oldloc = scrollUrl[instance]
	if ( newloc != oldloc) {
		scrollPos[instance] = window.frames[nameid].document.body.scrollTop
		scrollUrl[instance] = newloc
	}
}

function scrollStop(){
	if (scrollTimer != null) {
		clearTimeout(scrollTimer)
		scrollTimer = null
	}
}

function scrollChannelListingUp() {
	scrollUp('wtvChannelListingContainer',0)
}

function scrollChannelListingDown() {
	scrollDown('wtvChannelListingContainer',0)
}

function scrollContentUp() {
	scrollUp('wtvContentContainer',1)
}

function scrollContentDown() {
	scrollDown('wtvContentContainer',1)
}

loadingTimer = null;

function loadingStart(id) {
	elem = document.getElementById(id);
	if ( elem != null ) {
		loadingStop(id);
		loadingTimer = setTimeout("loadingStop('"+id+"')",9000);
		elem.style.visibility="visible";
	}
}

function loadingStop(id) {
	if (loadingTimer != null) {
		clearTimeout(loadingTimer);
		loadingTimer = null;

		elem = document.getElementById(id);
		if ( null != elem ) {
			elem.style.visibility="hidden";
		}
	}
}


function fullScreen(url, wname) {
	var full = "left=0,screenX=0,top=0,screenY=0,toolbar=0,scrollbars=0,status=0";
	if (window.screen)
	  full += ",height=" + screen.availHeight + ",innerHeight=" + screen.availHeight + ",width=" + screen.availWidth + ",innerWidth=" + screen.availWidth;
	else
	  full += ",resizable";

    return window.open(url, wname, full);
}