if (!window.mic_1395_rr)
	mic_1395_rr = {};

mic_1395_rr.Page = function() {}

mic_1395_rr.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		
		// Sample event hookup:	
		//rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
	}
}

var clips = ['/uk/dynamics/change/inc/round-table.wvx']; /* change this to inc/round-table.wvx */

var chapter_1 = '00:00:22'; 
var chapter_2 = '00:01:23'; 
var chapter_3 = '00:02:40'; 
var chapter_4 = '00:04:11'; 
var chapter_5 = '00:00:32'; 
var chapter_6 = '00:03:49'; 
var chapter_7 = '00:06:06'; 
var chapter_8 = '00:07:31'; 


function video_one()	{
	setScreenSource("");
	
	document.getElementById("ERP_TrackingImg").src = "ukmbuk_FY09ERPVideoCaseStudyTab_1";
	clips = ['/uk/dynamics/change/inc/dynamics-change.wvx'];
	content.findName("Tab_5").Visibility = 'Collapsed';
	content.findName("Tab_6").Visibility = 'Collapsed';
	content.findName("Tab_7").Visibility = 'Collapsed';
	content.findName("Tab_8").Visibility = 'Collapsed';
	
	content.findName("Tab_1").Visibility = 'Visible';
	content.findName("Tab_2").Visibility = 'Visible';
	content.findName("Tab_3").Visibility = 'Visible';
	content.findName("Tab_4").Visibility = 'Visible';
	
	//setScreenSource(clips[current_clip]);
	pageLoad("","");
	
	
}

function video_two()	{
	setScreenSource("");

	document.getElementById("ERP_TrackingImg").src = "ukmbuk_FY09ERPVideoRoundTableVodcastTab_1";
	clips = ['/uk/dynamics/change/inc/round-table.wvx'];
	content.findName("Tab_1").Visibility = 'Collapsed';
	content.findName("Tab_2").Visibility = 'Collapsed';
	content.findName("Tab_3").Visibility = 'Collapsed';
	content.findName("Tab_4").Visibility = 'Collapsed';
	
	content.findName("Tab_5").Visibility = 'Visible';
	content.findName("Tab_6").Visibility = 'Visible';
	content.findName("Tab_7").Visibility = 'Visible';
	content.findName("Tab_8").Visibility = 'Visible';
	
	//setScreenSource(clips[current_clip]);
	pageLoad("","");
	
}



/********************************/
// GLOBAL VARIABLES
/********************************/
var host;
var content;

var sl_screen;
var sl_blackFade;
var sl_time;
var sl_scrubberScaleTransform;
var sl_thumb;
var sl_btnPlay;
var sl_btnPause;
var sl_buffering;
var sl_btnReplay;
var sl_btnSound;
var sl_btnNosound;
var playback_polling_id;
var current_position;
var current_duration;
var sound_on = true;
var sl_btnStart;

/*var sl_slider;
var sl_btnBack;
var sl_btnBackDisabled;
var sl_btnForward;
var sl_btnForwardDisabled;*/

var mouseDownPosition = 0;
var mouseDownValue = -1;
var thumbCenter = 10.5;

var current_state;

var current_clip = 0;
var popwin;


var vid1_firstPlay = false;
var vid2_firstPlay = false;

var host = "";
var content = "";

/********************************/
// PAGE INIT
/********************************/

function pageLoad(sender, args) {
	

	
	// store the host and content
	if(host.length == 0)	{
		host = sender.getHost();
		content = host.content;
	}
	
	// store objects
	sl_screen 					= content.findName("screen");
	sl_blackFade 				= content.findName("blackFade");
	sl_time 					= content.findName("time");
	sl_scrubberScaleTransform 	= content.findName("scrubberScaleTransform");
    sl_thumb 					= content.findName("sliderThumb");
	sl_btnPlay 	 				= content.findName("btnPlay");
    sl_btnPause 				= content.findName("btnPause");
    sl_buffering 				= content.findName("buffering");
	sl_btnReplay 	 			= content.findName("btnReplay");
	sl_btnStart					= content.findName("btnStart");
	sl_btnSound					= content.findName("btnSound");
	sl_btnNosound				= content.findName("btnNosound");
	/*sl_slider 	 				= content.findName("slider");
   	sl_btnBack 					= content.findName("btnBack");
    sl_btnBackDisabled 			= content.findName("btnBackDisabled");
    sl_btnForward 				= content.findName("btnForward");
    sl_btnForwardDisabled 		= content.findName("btnForwardDisabled");*/
	
	// start buffer anim
	content.findName("bufferLoop").Begin();
	
	// add event handlers to the screen
	sl_screen.AddEventListener("MediaOpened", mediaOpenedHandler);
	sl_screen.AddEventListener("CurrentStateChanged", mediaStateChangeHandler);
	sl_screen.AddEventListener("MediaEnded", mediaEndedHandler);
	//sl_screen.AddEventListener("MediaFailed", mediaFailHandler);
	
	// set screen source
	setScreenSource(clips[current_clip]);
	
	
	
	// set link text
	//setLinkText();
	
	// init slider
   //slider_SetValue(sl_slider, sl_slider.width);
   
   
	
}		


/********************************/
// EVENT HANDLERS
/********************************/	
			
function mediaOpenedHandler(sender, eventArgs) {
	
	// show time
	sl_time.Visibility = 'Visible';
	
	// get the current duration
	current_duration = Math.floor(sender.NaturalDuration.seconds);
		
	// clear any polling
	stopPollingPosition();
		
	// start polling position	
	startPollingPosition();
	
	// start the content
	//sl_screen.play();	
	//if(content.findName("btnStart").Visibility == "Collapsed") { sl_screen.play(); };

	sl_screen.Position = '00:00:01';

}

			
function mediaStateChangeHandler(sender, eventArgs) {
	
	// hide buffering
	sl_buffering.Visibility = 'Collapsed';
	
	// store the current state
	current_state = sl_screen.CurrentState;
	
	switch (current_state) {
		
		case 'Playing':
			setButtonState('Playing');
			break;
		
		case 'Paused':
			setButtonState('Paused');
			//if (isAtEnd()) forwardHandler();
			break;
		
		case 'Buffering':
			sl_btnStart.Visibility = 'Collapsed';
		case 'Opening':
			sl_btnStart.Visibility = 'Collapsed';
			sl_buffering.Visibility = 'Visible';
			break;
	}
	
}

function mediaEndedHandler(sender, eventArgs) {
		
	// clear any polling
	stopPollingPosition();
	
	// set scrubber width
	sl_scrubberScaleTransform.ScaleX = 0;
	
	// call next clip
	forwardHandler();
	
	// show end frame
	showEndFrame();
  
}	

function mediaFailHandler(sender, errorArgs) {
	// The error message to display.
	
	var debugDiv = document.getElementById("errorLocation");
	var errorMsg = "Silverlight Error: \n\n";
    
    // Error information common to all errors.
    errorMsg += "Error Type:    " + errorArgs.errorType + "\n";
    errorMsg += "Error Message: " + errorArgs.errorMessage + "\n";
    errorMsg += "Error Code:    " + errorArgs.errorCode + "\n";
	
	if (debugDiv != null) {
		debugDiv.innerHTML = errorMsg;
	} else {
		alert(errorMsg);
	}
}

/********************************/
// SET VIDEO SOURCE
/********************************/	

function setScreenSource(source, calledFromChapter) { 
	
	// hide time
	sl_time.Visibility = 'Collapsed';
		
	// set the video source
	sl_screen.source = source; 
	
	// set disabled state
	setDisabledState();
	
	// call the jquery chapter function
	//if (!calledFromChapter) fakeChapterClick(current_clip);
	
}


/********************************/
// BUTTONS
/********************************/	
			
function playPauseToggleHandler() {
	//content.findName("btnStart").Visibility = "Collapsed";
	
		
		if (clips[current_clip] == "inc/dynamics-change.wvx")	{
			
				if (vid1_firstPlay == false)	{
					
					document.getElementById("ERP_TrackingImg").src = "http://view.atdmt.com/action/ukmbuk_FY09ERPVideoRapidCost_1";
					vid1_firstPlay = true;
				
				}
		}

		if (clips[current_clip] == "inc/round-table.wvx")	{
				
				if (vid1_firstPlay == false)	{
					
					document.getElementById("ERP_TrackingImg").src = "http://view.atdmt.com/action/ukmbuk_FY09ERPRTVodcastPlayFull_1";
					vid2_firstPlay = true;
	
				}
				
		}


	switch (current_state) {
		
		case 'Playing':
			sl_screen.pause();	
			break;
		
		case 'Paused':	
			if (isAtEnd()) sl_screen.Position = '00:00:01';
			sl_screen.play();		
			stopPollingPosition();
			startPollingPosition();	
			hideEndFrame();
			break;
		
	}
	

	
}
	
function soundToggleHandler() {
	
	switch (sound_on) {
		
		case true:
			setButtonState('Sound off');
			sl_screen.Volume = 0;
			sound_on = false;
			break;
		
		case false:	
			setButtonState('Sound on');
			sl_screen.Volume = 100;
			sound_on = true;
			break;
		
	}
	
}	
			
		
function backHandler() {
	
	// if position is more than 5 secs, go back to start of current clip
	if (sl_screen.Position.Seconds > 5 || current_clip == 0) {
		sl_screen.stop();
		sl_screen.play();		
		stopPollingPosition();
		startPollingPosition();	
	} 
	
	// otherwise go back a clip if a previous clip exists
	else if (current_clip > 0) {
		current_clip--;
		setScreenSource(clips[current_clip]);
	}
	
}	
			
function forwardHandler() {	
	
	// if there is a next clip, go to it
	if (current_clip < clips.length-1) {
		current_clip++;
		setButtonState('Paused');
		setScreenSource(clips[current_clip]);
	}
	
}
	
// roll handlers
function playOverHandler() { content.findName("playOver").Begin(); }
function playOutHandler() { content.findName("playOut").Begin(); }
function replayOverHandler() { content.findName("replayOver").Begin(); }
function replayOutHandler() { content.findName("replayOut").Begin(); }
function download1OverHandler() { content.findName("download1Over").Begin(); }
function download1OutHandler() { content.findName("download1Out").Begin(); }
function download2OverHandler() { content.findName("download2Over").Begin(); }
function download2OutHandler() { content.findName("download2Out").Begin(); }
function moreinfoOverHandler() { content.findName("moreinfoOver").Begin(); }
function moreinfoOutHandler() { content.findName("moreinfoOut").Begin(); }
function soundOverHandler() { content.findName("soundOver").Begin(); }
function soundOutHandler() { content.findName("soundOut").Begin(); }
function startOverHandler() { content.findName("startOver").Begin(); }
function startOutHandler() { content.findName("startOut").Begin(); }
/*function backOverHandler() { content.findName("backOver").Begin(); }
function backOutHandler() { content.findName("backOut").Begin(); }	
function forwardOverHandler() { content.findName("forwardOver").Begin(); }
function forwardOutHandler() { content.findName("forwardOut").Begin(); }	*/
function startTab1_Over() { content.findName("Tab1_Over").Begin(); }
function startTab1_Out() { content.findName("Tab1_Out").Begin(); }
function startTab2_Over() { content.findName("Tab2_Over").Begin(); }
function startTab2_Out() { content.findName("Tab2_Out").Begin(); }
function startTab3_Over() { content.findName("Tab3_Over").Begin(); }
function startTab3_Out() { content.findName("Tab3_Out").Begin(); }
function startTab4_Over() { content.findName("Tab4_Over").Begin(); }
function startTab4_Out() { content.findName("Tab4_Out").Begin(); }

function startTab5_Over() { content.findName("Tab5_Over").Begin(); }
function startTab5_Out() { content.findName("Tab5_Out").Begin(); }
function startTab6_Over() { content.findName("Tab6_Over").Begin(); }
function startTab6_Out() { content.findName("Tab6_Out").Begin(); }
function startTab7_Over() { content.findName("Tab7_Over").Begin(); }
function startTab7_Out() { content.findName("Tab7_Out").Begin(); }
function startTab8_Over() { content.findName("Tab8_Over").Begin(); }
function startTab8_Out() { content.findName("Tab8_Out").Begin(); }



function skipChapter(sender, args)	{
	
	var chapter_number = sender.name.substring(4,5);
	var chapter = eval("chapter_"+chapter_number);
	sl_screen.Position = chapter;
	sl_screen.play();
	
	switch (chapter_number) {
		case '1':
			document.getElementById("ERP_TrackingImg").src = "http://view.atdmt.com/action/ukmbuk_FY09ERPVideoRapidCost_1";
			break;
		case '2':
			document.getElementById("ERP_TrackingImg").src = "http://view.atdmt.com/action/ukmbuk_FY09ERPVideoRapidROI_1";
			break;
		case '3':
			document.getElementById("ERP_TrackingImg").src = "http://view.atdmt.com/action/ukmbuk_FY09ERPVideoAccurateData_1";
			break;
		case '4':
			document.getElementById("ERP_TrackingImg").src = "http://view.atdmt.com/action/ukmbuk_FY09ERPVideoLessAdmin_1";
			break;
		case '5':
			document.getElementById("ERP_TrackingImg").src = "http://view.atdmt.com/action/ukmbuk_FY09ERPRTVodcast20thcentury_1";
			break;
		case '6':
			document.getElementById("ERP_TrackingImg").src = "http://view.atdmt.com/action/ukmbuk_FY09ERPRTVodcastSmlvsLargeBusiness_1";
			break;
		case '7':
			document.getElementById("ERP_TrackingImg").src = "http://view.atdmt.com/action/ukmbuk_FY09ERPRTVideoPioneersvsReactor_1";
			break;
		case '8':
			document.getElementById("ERP_TrackingImg").src = "http://view.atdmt.com/action/ukmbuk_FY09ERPRTVideoTechnology_1";
			break;
	}
}

function setButtonState(state) {
	
	switch (state) {
		
		case 'Playing':
			sl_btnStart.Visibility = 'Collapsed';
			sl_btnPlay.Visibility = 'Collapsed';
			sl_btnPause.Visibility = 'Visible';
			break;
		
		case 'Paused':
			sl_btnStart.Visibility = 'Visible';
			sl_btnPlay.Visibility = 'Visible';
			sl_btnPause.Visibility = 'Collapsed';
			break;
			
		case 'Sound off':
			sl_btnNosound.Visibility = 'Visible';
			sl_btnSound.Visibility = 'Collapsed';
			break;
			
		case 'Sound on':
			sl_btnNosound.Visibility = 'Collapsed';
			sl_btnSound.Visibility = 'Visible';
			break;
		
		/*case 'ForwardDisabled':
			sl_btnForwardDisabled.Visibility = 'Visible';
			sl_btnForward.Visibility = 'Collapsed';
			break;
		
		case 'ForwardEnabled':
			sl_btnForwardDisabled.Visibility = 'Collapsed';
			sl_btnForward.Visibility = 'Visible';
			break;*/
			
	}			
	
}

function setDisabledState() {
	
	/*if (current_clip == clips.length-1) setButtonState('ForwardDisabled');
	else setButtonState('ForwardEnabled');*/
	
}


/********************************/
// PLAYBACK POSITION AND POLLING
/********************************/	

function startPollingPosition() { playback_polling_id = setInterval(pollPlaybackPosition, 50); }
function stopPollingPosition() { clearInterval(playback_polling_id); }

function pollPlaybackPosition() {
	
	// store the current position
	current_position = Math.min(current_duration, Math.floor(sl_screen.Position.Seconds));
	
	// update the time
	sl_time.Text = timeString(current_position) + '/' + timeString(current_duration);
	
	// set the scrubber width
	setScrubberWidth();
	
}

function setScrubberWidth() {
	
	// ignore if we don't know the duration yet 
	if (sl_screen.NaturalDuration.seconds <= 0 ) {
		sl_scrubberScaleTransform.ScaleX = 1;
		return;
	}
	
	// calc percentage
	var percentage =  sl_screen.Position.Seconds/sl_screen.NaturalDuration.seconds;	
	
	// set the scrubber width based on playback percentage
	sl_scrubberScaleTransform.ScaleX = 1 - percentage;
	
}


/********************************/
// END FRAME
/********************************/	

function hideEndFrame() {

	sl_blackFade.Visibility = 'Collapsed';	
	sl_btnReplay.Visibility = 'Collapsed';
	/*
	sl_link1.Visibility = 'Collapsed';
	sl_link3.Visibility = 'Collapsed';
	sl_link2.Visibility = 'Collapsed';		
	*/
}

function showEndFrame() { 

	sl_blackFade.Visibility = 'Visible';	
	sl_btnReplay.Visibility = 'Visible';
	sl_btnStart.Visibility = 'Collapsed';
	/*
	sl_link1.Visibility = 'Visible';
	sl_link3.Visibility = 'Visible';
	sl_link2.Visibility = 'Visible';		
	*/
}

/*function setLinkText() {
	
	sl_link1Text.Text = links[0].text;
	content.findName("arrow1"+links[0].arrow).Visibility = 'Visible';
	
	sl_link2["Canvas.Top"] = sl_link1["Canvas.Top"] + sl_link1Text.ActualHeight + 10;
	sl_link2Text.Text = links[1].text;
	content.findName("arrow2"+links[1].arrow).Visibility = 'Visible';
	
	sl_link3["Canvas.Top"] = sl_link2["Canvas.Top"] + sl_link2Text.ActualHeight + 10;
	sl_link3Text.Text = links[2].text;
	content.findName("arrow3"+links[2].arrow).Visibility = 'Visible';
	
}*/

/********************************/
// SLIDER
/********************************/	

/*function slider_thumb_MouseLeftButtonDown(sender, args) {
	
    sender.captureMouse();
    mouseDownValue = slider_GetValue(sl_slider);
    mouseDownPosition = args.getPosition(null).x;
    
    var coordinate = args.getPosition(null).x;
    coordinate -= sl_slider["Canvas.Left"];
    slider_SetValue(sl_slider, coordinate - thumbCenter);  
	
}

function slider_thumb_MouseLeftButtonUp(sender, args) {
    sl_slider.releaseMouseCapture();
    mouseDownValue = -1;
}

function slider_thumb_MouseMove(sender, args) {
    if (mouseDownValue != -1) {
        var newValue = mouseDownValue + (args.getPosition(null).x - mouseDownPosition);    
        slider_SetValue(sl_slider, newValue);
    }   
}

function slider_GetValue(sender) {
    return sl_thumb["Canvas.Left"]; 
}

function slider_SetValue(sender, newValue) {
    
    if (newValue > sender.width - sl_thumb.width) {
        newValue = sender.width - sl_thumb.width;
        mouseDownValue = -1;
    }
	
    if (newValue < 0) {
        newValue = 0;
       	mouseDownValue = -1;
    }
    
	// set the thumb coord
    sl_thumb["Canvas.Left"] = newValue;
	
	// set the volume
	slider_SetVolume(newValue/100);
	//slider_SetVolume(0);
	
}

function slider_SetVolume(volume) {
	sl_screen.Volume = volume;
}*/


/********************************/
// UTILITIES
/********************************/	

function openLinkInNewWindow(url) {
	var attributes = 'resizable=1,scrollbars=1,width=700,height=500';
	popwin = window.open(url, 'popwin', attributes);
	popwin.focus();
}

function openLinkInSameWindow(url) { window.location.href = url; }

function isAtEnd() { if (current_duration > 0) return (sl_screen.Position.Seconds >= current_duration); }

function timeString(seconds) {
	
	var hours =   pad(Math.floor(seconds/3600));
	var minutes = pad(Math.floor((seconds%3600)/60));
	var seconds = pad((seconds%3600)%60);
	
	if (hours > 0) return hours + ':' + minutes + ':' +seconds;
	else return minutes + ':' +seconds;
   
	function pad(num){
		num=num+"";
		if(num.length==1){
		   num="0" + num;
		}
		return num;   
	}
	
}

/********************************/
// ADD LINK TRACKING HERE
/********************************/	
/*function openLink(id, url, target){

	//add tracking etc here
	
	switch(id)
    {
        case "Video1Link1":
            var s=s_gi(s_account);s.linkTrackVars='eVar6,events';s.linkTrackEvents='event6';s.events='event6';s.eVar6='Outreach-ImpProf-OfficeFreeTrial';s.tl(this,'o','Outreach-ImpProf-OfficeFreeTrial');
            break;    
        case "Video1Link2":
            var s=s_gi(s_account);s.linkTrackVars='eVar7,events';s.linkTrackEvents='event7';s.events='event7';s.eVar7='Outreach-ImpProf-moreInfo';s.tl(this,'o','Outreach-ImpProf-moreInfo');
            break;
        case "Video1Link3":
            var s=s_gi(s_account);s.linkTrackVars='eVar6,events';s.linkTrackEvents='event6';s.events='event6';s.eVar6='Outreach-ImpProf-OfficeAccounting';s.tl(this,'o','Outreach-ImpProf-OfficeAccounting');
            break;
            
        case "Video2Link1":
            var s=s_gi(s_account);s.linkTrackVars='eVar6,events';s.linkTrackEvents='event6';s.events='event6';s.eVar6='Outreach-NewCust-OfficeFreeTrial';s.tl(this,'o','Outreach-NewCust-OfficeFreeTrial');
            break;    
        case "Video2Link2":
            var s=s_gi(s_account);s.linkTrackVars='eVar7,events';s.linkTrackEvents='event7';s.events='event7';s.eVar7='Outreach-NewCust-sign-up-office-live';s.tl(this,'o','Outreach-NewCust-sign-up-office-live');
            break;
        case "Video2Link3":
            var s=s_gi(s_account);s.linkTrackVars='eVar7,events';s.linkTrackEvents='event7';s.events='event7';s.eVar7='Outreach-NewCust-moreInfo';s.tl(this,'o','Outreach-NewCust-moreInfo');
            break;
            
        case "Video3Link1":
            var s=s_gi(s_account);s.linkTrackVars='eVar6,events';s.linkTrackEvents='event6';s.events='event6';s.eVar6='Outreach-CompAdv-OfficeFreeTrial';s.tl(this,'o','Outreach-CompAdv-OfficeFreeTrial');
            break;    
        case "Video3Link2":
            var s=s_gi(s_account);s.linkTrackVars='eVar7,events';s.linkTrackEvents='event7';s.events='event7';s.eVar7='Outreach-CompAdv-moreInfo';s.tl(this,'o','Outreach-CompAdv-moreInfo');
            break;
        case "Video3Link3":
            var s=s_gi(s_account);s.linkTrackVars='eVar6,events';s.linkTrackEvents='event6';s.events='event6';s.eVar6='Outreach-CompAdv-OfficeAccounting';s.tl(this,'o','Outreach-CompAdv-OfficeAccounting');
            break;
    }
	
	
	if(target == "_blank"){
		openLinkInNewWindow(url);
	} else {
		openLinkInSameWindow(url);
	}

}*/	