if (!window.National_Trust_AGM_2007_Silverlight_Player_v1_251007)
	window.National_Trust_AGM_2007_Silverlight_Player_v1_251007 = {};

var ScreenModeSetttings = {Mini:0,Max:1};

National_Trust_AGM_2007_Silverlight_Player_v1_251007.Page = function(){
	this.screenmode = ScreenModeSetttings.Mini;
}

var MediaChangeChapter;

National_Trust_AGM_2007_Silverlight_Player_v1_251007.Page.prototype = {
	handleLoad: function(control, userContext, rootElement) {
		this.control = control;
		if(!window.$Ag)
			window.$Ag = function(name){
				var a = arguments; var al = a.length;
				if(al==1)
					return control.content.findName(name);	
				else{
					var o=[];
					for(var i=0;i<al;i++)
						o.push(control.content.findName(a[i]));
					return o;
				}
			}
		window.AGM2007Player = this;
		//LOAD Transcript
		//this.getTranscript();

		//Mouse event handler		
		rootElement.addEventListener("MouseMove", Silverlight.createDelegate(this, this.handleMouseMove));
		
		//CHAPTERS SCROLL
		var ChapterScrollbar = new Twofour.Usercontrols.Scrollbar($Ag("Chapters"), $Ag("ChaptersTrack"), $Ag("ChaptersMarker"), false, true);
		var ChapterScrollCanvas = new Twofour.Usercontrols.ScrollCanvas($Ag("chapter_scroll_canvas"), ChapterScrollbar);
		ChapterScrollCanvas.add($Ag("ChaptersScrollContent"));
		
		//WEBCHAT SCROLL
		var WebchatScrollbar = new Twofour.Usercontrols.Scrollbar($Ag("Webchat"), $Ag("WebchatTrack"), $Ag("WebchatMarker"), true, false);
		var WebchatScrollCanvas = new Twofour.Usercontrols.ScrollCanvas($Ag("WebchatCanvasScroll"), WebchatScrollbar);
		WebchatScrollCanvas.add($Ag("WebchatScrollContent"));

	},
	
	handleMouseMove: function(sender, eventArgs)
	{
	    $Ag("FadeControls").begin();
	},

	changeLayout:function(newState){
		var StateValues = {MiniNormal:0,MaxNormal:1,MiniFullscreen:2,MaxFullscreen:3};
		var state = (this.screenmode)+(!this.control.content.fullscreen ?0:2)	
		switch(state){
			case StateValues.MiniNormal:
				$Ag('GoNormalscreen').begin();
				break;
			case StateValues.MaxNormal:
				$Ag('GoFullscreen').begin();
				break;
			case StateValues.MiniFullscreen:
				break;
			case StateValues.MaxFullscreen:
				break;	
		}
	},
	
	getTranscript:function(url){
		url=url||'transcript.txt';
		var textElement = $Ag('TranscriptText');
		if(textElement){
			var cl = new Twofour.Web.ContentLoader(url);
			cl.onComplete = function(s,e){
				textElement.Text = e.response;
			}
			cl.onError = function(s,e){
				textElement.Text = 'There was an error loading the transcript.';
			}
			cl.load();
		}
	}
}


function VidControlMouseEnter(sender, eventArgs) {
	if(sender.name == "PlayButton"){	    
		$Ag("PlayButton_MouseEnter").begin();
	}
	if(sender.name == "RewindButton"){	    
		$Ag("RewindButton_MouseEnter").begin();
	}
	if(sender.name == "FastForwardButton"){	    
		$Ag("FastForwardButton_MouseEnter").begin();
	}
	if(sender.name == "MuteButton"){	    
		$Ag("MuteButton_MouseEnter").begin();
	}
	if(sender.name == "FullscreenButton"){	    
		$Ag("FullscreenButton_MouseEnter").begin();
	}
}

function VidControlMouseLeave(sender, eventArgs) {
	if(sender.name == "PlayButton"){
		$Ag("PlayButton_MouseLeave").begin();
	}
	if(sender.name == "RewindButton"){
		$Ag("RewindButton_MouseLeave").begin();
	}
	if(sender.name == "FastForwardButton"){
		$Ag("FastForwardButton_MouseLeave").begin();
	}
	if(sender.name == "MuteButton"){
		$Ag("MuteButton_MouseLeave").begin();
	}
	if(sender.name == "FullscreenButton"){
		$Ag("FullscreenButton_MouseLeave").begin();
	}
}

function VidControlMouseDown(sender, eventArgs) {
    var control = sender.getHost().content;    
	var mediaElement = $Ag("MediaElement");
    var state = mediaElement.CurrentState;
    
    if(sender.name == "PlayButton"){
        if(state== "Playing"){
            mediaElement.Pause();
        }else{
            mediaElement.Play();
        }
    }
	
	var shiftVideo = function(seconds){
			if(mediaElement.CanSeek == true){
	            var mediaElementPosition = mediaElement.Position;
	            var newPosition = Math.floor(Math.max(mediaElementPosition.Seconds + seconds,0));
	            var timecode = Twofour.Usercontrols.VideoSlider.formatTime (0, 0, newPosition );
	            mediaElement.Position = timecode;
	        }
		}
	
    if(sender.name == "RewindButton"){
        shiftVideo(-5);
    }
    if(sender.name == "FastForwardButton"){
		shiftVideo(+5);
	}
    
	if(sender.name == "MuteButton"){
        if(mediaElement.IsMuted == false){
            mediaElement.IsMuted = true;
            $Ag("MuteButtonIconSound1").opacity = 0
            $Ag("MuteButtonIconSound2").opacity = 0
            $Ag("MuteButtonIconSound3").opacity = 0
            $Ag("MuteButtonIconSound4").opacity = 0
        }else{
            mediaElement.IsMuted = false;
            $Ag("MuteButtonIconSound1").opacity = 100
            $Ag("MuteButtonIconSound2").opacity = 100
            $Ag("MuteButtonIconSound3").opacity = 100
            $Ag("MuteButtonIconSound4").opacity = 100
        }
    }
    if(sender.name == "FullscreenButton"){
		var sm = ScreenModeSetttings;
		AGM2007Player.screenmode = AGM2007Player.screenmode==sm.Mini ? sm.Max : sm.Mini;
        AGM2007Player.changeLayout();
    }
}

function MediaOpenedSetChapter(sender, eventArgs){
    var control = sender.getHost().content;
    var MediaElement = control.findName("MediaElement");
    MediaElement.Position = MediaChangeChapter;
}
    

function ChapterMouseDown(sender, eventArgs) {
    var control = sender.getHost().content;    
    var MediaElement = control.findName("MediaElement");
    MediaElement.AddEventListener("MediaOpened", "MediaOpenedSetChapter");
	
    if(sender.name == "ImgChapter1"){
        if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
            MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (0, 0, 0);
        }      	
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (0, 0, 0);   
    }
	if(sender.name == "ImgChapter2"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
            MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (0, 10, 45);
        }	
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (0, 10, 45);
    }
	if(sender.name == "ImgChapter3"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
	    MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (0, 14, 55);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (0, 14, 55);
    }
	if(sender.name == "ImgChapter4"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
	    MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (0, 27, 37);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (0, 27, 37);
    }
	if(sender.name == "ImgChapter5"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
	    MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (0, 28, 32);
        }		
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (0, 28, 32);
    }
	if(sender.name == "ImgChapter6"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
	    MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (0, 39, 26);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (0, 39, 26);
    }
    if(sender.name == "ImgChapter7"){
        if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
	    MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (1, 14, 13);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (1, 14, 13);
    }
	if(sender.name == "ImgChapter8"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
	    MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (1, 15, 15);
        }
	MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (1, 15, 15);
    }
	if(sender.name == "ImgChapter9"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
	    MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (1, 41, 14);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (1, 41, 14);
    }
	if(sender.name == "ImgChapter10"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
            MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (2, 30, 06);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (2, 30, 06);
    }
    if(sender.name == "ImgChapter11"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_morning.wmv.asx";
            MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (2, 33, 28);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (2, 33, 28);
    }


	if(sender.name == "ImgChapter12"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_afternoon.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_afternoon.wmv.asx";
            MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (0, 0, 0);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (0, 0, 0);
    }
	if(sender.name == "ImgChapter13"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_afternoon.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_afternoon.wmv.asx";
            MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (0, 0, 54);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (0, 0, 54);
    }
	if(sender.name == "ImgChapter14"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_afternoon.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_afternoon.wmv.asx";
            MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (0, 9, 20);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (0, 9, 20);
    }
	if(sender.name == "ImgChapter15"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_afternoon.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_afternoon.wmv.asx";
            MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (0, 17, 56);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (0, 17, 56);
    }
	if(sender.name == "ImgChapter16"){
	    if(MediaElement.Source != "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_afternoon.wmv.asx"){
            MediaElement.Source = "http://www.twofourdigital.net/NationalTrust/NTAGM2008hi_afternoon.wmv.asx";
            MediaChangeChapter = Twofour.Usercontrols.VideoSlider.formatTime (1, 17, 59);
        }
        MediaElement.Position = Twofour.Usercontrols.VideoSlider.formatTime (1, 17, 59);
    }
}

function MediaElementStateChange(sender, eventArgs) {
    var control = sender.getHost().content;
    var MediaElementState = control.findName("MediaElement").CurrentState;
    
    if(sender.name == "MediaElement"){
        if(MediaElementState == "Playing"){
            control.findName("PlayButtonIconPause").opacity = 100;
            control.findName("PlayButtonIconPlay").opacity = 0;
        }else{
            control.findName("PlayButtonIconPause").opacity = 0;
            control.findName("PlayButtonIconPlay").opacity = 100;
        }
    }
}