var timer;
var timerFullScreen;

function loadPlayer(pre, inter, post, id, name, width, height, uimode, autostart, strechToFit, simpleSkin, channel, Url) {

	var playerCode = pre;

	if(window.ie)
		playerCode = '<object id="'+id+'" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'+width+'" height="'+height+'">';
	else
		playerCode = '<object id="'+id+'" type="application/x-ms-wmp" data="'+Url+'" width="'+width+'" height="'+height+'">';

	playerCode += '<param name="URL" value="'+Url+'" />';
	playerCode += '<param name="uiMode" value="'+uimode+'">';
	playerCode += '<param name="windowlessVideo" value="false">';
	playerCode += '<param name="enableContextMenu" value="true">';
	playerCode += '<param name="strechToFit" value="'+strechToFit+'">';
	playerCode += '<param name="currentPosition" value="0">';
	playerCode += '<param name="autostart" value="'+autostart+'" />';
	playerCode += '</object>';
	playerCode += post + getStatusBarCode(simpleSkin, channel);

	document.write(playerCode);
}

function wmpMediaChange(newState)  {
}

var playstateValues = new Array("Undefined","Stopped","Paused","Playing","Scan Forward","Scan Reverse","Buffering","Waiting","Media Ended","Transitioning","Ready","Reconnecting");

function wmpPlayStateChange(newState)  {
	if(newState == 6 || newState == 7 || newState == 11)
	{
		$('webcastPlayerStatus').setText(playstateValues[newState]+"...");
		$clear(timer);
	}

	if(newState == 3)
	{
		$('webcastPlayerStatus').setText("");
		$('playPauseButton').setProperty('src', $('playPauseButton').getProperty('src').replace('play.png', 'pause.png'));

		$clear(timer);
		if($('area') && $('knob'))
		{
			mySlide = new Slider($('area'), $('knob'), {
											steps: Math.round($('EdemoPlay').controls.currentItem.duration),
											onChange: function(step){
												seekTo(step);
											}
			});

			if($('EdemoPlay').controls.currentPosition != 0)
				mySlide.set(Math.round($('EdemoPlay').controls.currentPosition));

			timer = updateProgressBar.periodical(Math.round((Math.round($('EdemoPlay').controls.currentItem.duration) / $('area').getStyle('width').toInt()) * 1000) );
		}
	}
	if(newState == 1)
		$('webcastPlayerStatus').setText("");
}

function updateProgressBar() {
	if($('EdemoPlay').playState == 3 &&  $('knob').getStyle('left').toInt() <= $('area').getStyle('width').toInt() )
		$('knob').setStyle('left', $('knob').getStyle('left').toInt()+1);
}


function VisionMute() {
	try
	{
      if (!$('EdemoPlay').settings.mute) {
    	     $('EdemoPlay').settings.mute = true;
    	     $('muteButton').setProperty('src', $('muteButton').getProperty('src').replace('On', 'Off'));
	  }else{
             $('EdemoPlay').settings.mute = false;
             $('muteButton').setProperty('src', $('muteButton').getProperty('src').replace('Off', 'On'));
      }
	}
	catch (ex){}
}

function getStatusBarCode(simpleSkin, channel) {
	var statusBarCode;

	if(simpleSkin)
	{
		statusBarCode = '<div id="visionPlayStatusBar">'
						+'<div id="webcastPlayerStatus" style="float:left;background:url(images/'+channel+'/player/home/leftSpacer.png) no-repeat;"><img src="images/'+channel+'/player/home/leftSpacer.png" border="0" style="float:left;" /></div><a href="javascript:startPlay();" style="float:left;"><img src="images/'+channel+'/player/home/play.png" border="0" id="playPauseButton" /></a><a href="javascript:wmpstop();" style="float:left;"><img src="images/'+channel+'/player/home/stop.png" border="0"/></a>'
						+'</div>';

	}
	else
	{
		statusBarCode = '<div id="visionPlayStatusBar">'
						+'<a href="javascript:startPlay();" style="float:left;"><img src="images/'+channel+'/player/play.png" border="0" id="playPauseButton" /></a><a href="javascript:wmpstop();" style="float:left;"><img src="images/'+channel+'/player/stop.png" border="0"/></a><img src="images/'+channel+'/player/barLeft.png" border="0" style="float:left;"/>'
						+'<div id="area" style="float:left;"><span id="webcastPlayerStatus"></span>'
						+'<div id="knob" style="float:left;"></div>'
						+'</div>'
						+'<img src="images/'+channel+'/player/barRight.png" border="0" style="float:left;"/><a href="javascript:VisionMute();" style="float:left;"><img src="images/'+channel+'/player/muteOn.png" border="0" id="muteButton"/></a><a href="#" onclick="javascript:return VisionSetFullScreen();" style="float:left;"><img src="images/'+channel+'/player/fullscreen.png" border="0"/></a>'
						+'</div>';
	}

	return statusBarCode;
}


function startPlay() {
	if( $('playPauseButton').getProperty('src').test('pause.png') )
	{
		$('EdemoPlay').controls.pause();
		$('playPauseButton').setProperty('src', $('playPauseButton').getProperty('src').replace('pause.png', 'play.png'));
	}
	else
	{
		$('EdemoPlay').controls.play();
		$('playPauseButton').setProperty('src', $('playPauseButton').getProperty('src').replace('play.png', 'pause.png'));
	}
}

function wmpstop() {
	$clear(timer);
	$('webcastPlayerStatus').setText("");
	if($('knob'))
		$('knob').setStyle('left', 0);
	$('playPauseButton').setProperty('src', $('playPauseButton').getProperty('src').replace('pause.png', 'play.png'));
	$('EdemoPlay').controls.stop();
}

function seekTo(steps) {
	if(steps != 0 && ( steps > Math.round($('EdemoPlay').controls.currentPosition + 20) || steps < Math.round ($('EdemoPlay').controls.currentPosition - 20) ) )
		$('EdemoPlay').controls.currentPosition = steps;
}

function toggleHeight(elementId, toggler, defaultHeight) {
	if($(elementId).getStyle('display') == 'none')
		$(elementId).setStyle('display', 'block');

	if($(toggler).innerHTML == "+")
		$(toggler).setHTML("-");
	else if ($(toggler).innerHTML == "-")
		$(toggler).setHTML("+");

	if($(elementId).getStyle('height').toInt() <= defaultHeight )
		$(elementId).effect('height',{duration: 500}).start($(elementId).scrollHeight);
	else
		$(elementId).effect('height',{duration: 500}).start(defaultHeight);
}

function VisionSetFullScreen() {
	if ($('EdemoPlay').playState.toInt() == 3)
	{
		if(window.ie)
		{
			$('EdemoPlay').uiMode = 'full';
			$('EdemoPlay').fullScreen='true';
			timerFullScreen = watchFullScreenStateChange.periodical(200);
		}
	}
	return false;
}

function watchFullScreenStateChange() {
	if(!$('EdemoPlay').fullScreen) {
		$('EdemoPlay').uiMode = 'none';
		$clear(timerFullScreen);
	}
}

function toggleAuthors() {
	if($('toExpand').getStyle('display') == 'none')
		$('toExpand').setStyle('display', 'block');

	if($('toggler1').getFirst().getProperty('src').test('more'))
		$('toggler1').getFirst().setProperty('src', $('toggler1').getFirst().getProperty('src').replace('more', 'less'));
	else
		$('toggler1').getFirst().setProperty('src', $('toggler1').getFirst().getProperty('src').replace('less', 'more'));

	if($('toExpand').getStyle('height').toInt() <= 14 )
		$('toExpand').effect('height',{duration: 500}).start($('toExpand').scrollHeight);
	else
		$('toExpand').effect('height',{duration: 500}).start(14);
}

function toggleToc() {
	if($E('ul', 'tableOfContents').getStyle('height').toInt() == 0)
	{
		$('tableOfContents').setStyle('height', $E('ul', 'tableOfContents').scrollHeight.toInt() + $('tocExpandedToggler').getSize().size.y);
		$E('ul', 'tableOfContents').setStyle('visibility', 'visible');
		$E('ul', 'tableOfContents').effect('height',{duration: 700, fps: 600, transition: Fx.Transitions.Bounce.easeOut}).start( $E('ul', 'tableOfContents').getSize().scrollSize.y );
	}
	else
	{
		$E('ul', 'tableOfContents').effect('height',{duration: 500, fps: 600, transition: Fx.Transitions.Bounce.easeOut}).start(0).chain(function() {
			$E('ul', 'tableOfContents').setStyle('visibility', 'hidden');
			$('tableOfContents').setStyle('height', $('tocExpandedToggler').getSize().size.y);
		});
	}
}


function LinkWMPPlugin() {
	var lLink = false;

	if (navigator.platform == "Win32")
	{
		if(window.gecko)
		{
			lLink = true;
			for (var i1 = 0 ; i1 < navigator.plugins.length ; i1++)
			{
				var n = navigator.plugins[i1].name;
				if ( n != null && n.test("Windows Media") && n.test("Microsoft"))
				{ lLink = false; break; }
			}
		}
	}

	return lLink;
}

function toggleAllEventsPanel(elWidth, elHeight) {
	var toggleAppear = new Fx.Styles($('eventsFullList'), {duration:600});

	if($('eventsFullList').getStyle('display') == "none")
	{
		$('content-menu').setStyle('display','none');
		$('itemList').setStyle('display','none');
		$('eventList').setStyle('display','none');
		$('eventsFullList').setStyles({width: 0, height: 0, display: 'block'});
		toggleAppear.start({'width': [0, elWidth], 'height': [0, elHeight]});
	}
	else
		toggleAppear.start({'width': [elWidth, 0], 'height': [elHeight, 0]}).addEvent('onComplete', function(){
				$('content-menu').setStyle('display','block');
				$('itemList').setStyle('display', 'block');
				$('eventList').setStyle('display','inline');
				$('eventsFullList').setStyle('display', 'none');
		});
}

function initializeScroll(divName) {
	if(!$(divName))
		return;
	if($E('ul', divName).getSize().scrollSize.y > $E('ul', divName).getSize().size.y) {
	    var webcastScroll = new divscroller($E('ul', divName), $E('.scrollKnob', divName), {
	        mode:'vertical',
	        scrollLinks: {
	            forward: $E('.scrollForward', divName),
	            back: $E('.scrollBack', divName)
            },
            expandKnob: true
        });
        $E('.scrollArea', divName).setStyle('visibility', 'visible');
    }
    else {
        $E('.scrollArea', divName).setStyle('visibility', 'hidden');
    }
}

function initializeToggledAuthorsSection() {
	$('toggler1').addEvent('click', function(){
		toggleAuthors();
	});
}

function initializeToggledTOC() {
	if(!LinkWMPPlugin())
	{
		if($('tocExpandedToggler'))
		{
			$('tocExpandedToggler').addEvent('click', function() { toggleToc(); } );
			$E('ul', 'tableOfContents').setStyle('visibility', 'hidden');
		}
	}
	else
	{
		$('tocExpandedToggler').setStyle('display', 'none');
	}
}

function initializeScrollDesc() {
	var height = $('webcastAuthorAndDesc').getSize().size.y - $('toExpand').getSize().size.y;
	$('txtDescription').setStyle('height', height - 5);
	$('scrollAreaDesc').setStyle('height', height);
	$('scrollBarContainerDesc').setStyle('height', height - 27);

    if($('txtDescription').getSize().scrollSize.y > $('txtDescription').getSize().size.y) {
    	var scrollKnob = $E('.scrollKnob', 'webcastAuthorAndDesc');
	    var webcastScroll = new divscroller($('txtDescription'), $E('.scrollKnob', 'webcastAuthorAndDesc'), {
	        mode:'vertical',
	        scrollLinks: {
	            forward: $E('.scrollForward', 'webcastAuthorAndDesc'),
	            back: $E('.scrollBack', 'webcastAuthorAndDesc')
            },
            expandKnob: true
        });
        $E('.scrollArea', 'webcastAuthorAndDesc').setStyle('visibility', 'visible');
        $E('.scrollArea', 'webcastAuthorAndDesc').setStyle('display', 'inline');
    }
    else {
        $E('.scrollArea', 'webcastAuthorAndDesc').setStyle('display', 'none');
    }
}

function setLastUsedTabCookie(channelName, tabId) {
	Cookie.set(channelName + '_lastUsedTab', tabId);
}

function getLastUsedTabCookie(channelName) {
	return Cookie.get(channelName + '_lastUsedTab');
}

//Set teaserSeen cookie to true, expired after 1 day
function setTeaserSeen(channelName) {
    Cookie.set(channelName + '_teaserSeen', true, {duration: 1});
}

function getTeaserSeen(channelName) {
    return Cookie.get(channelName + '_teaserSeen');
}

function loadTeaserPlayer(width, height, channelName, teaserUrl) {
    var teaserSeeen = getTeaserSeen(channelName);
    loadPlayer('', '', '','EdemoPlay', 'EdemoPlay', width, height, 'none', 'true', 'false', true, channelName, teaserUrl);
    if(teaserSeeen)
    {
         window.addEvent('load', function(){
            wmpstop();
            });
    }         
    else
    {
        setTeaserSeen(channelName);
    }
    
}