/* Do not remove this commented code. This is compiled by Expression Encoder at template compile time.
*/
//var JStxtbTitle = '7-Luncheon Keynote: Role of the United Nations, International Labour Organization and other Global Bodies '; //'Sample demo title';
//var TStxtbPresenters = 'Tim Brewer, Brian Ward, Shelley Clark, Ron Labonte'; // //'McGill John Doe';
//var JStxtDateTime = '12 Feb 2009 12:00';
//var JSMedia1 = 'mms://media1.campus.mcgill.ca:80/COOLContent/2008-FALL/CHEM-110-001/20080929_1230_02_96437/20080929_1230_02_96437.VGA.asf.wmv'; //'mms://cool02.campus.mcgill.ca:8000/PULL_VGA_2'; //'mms://media1.campus.mcgill.ca:80/COOLCONTENT/200801/BIOL-202-001/20080130_0830_99694/20080130_0830_99694.VGA.asf.wmv';	//
//var JSMedia2 = 'mms://media1.campus.mcgill.ca:80/COOLContent/2008-FALL/CHEM-110-001/20080929_1230_02_96437/20080929_1230_02_96437.VID.wmv'; //'mms://cool02.campus.mcgill.ca:8000/PULL_VID_2'; //'mms://media1.campus.mcgill.ca:80/COOLCONTENT/200801/BIOL-202-001/20080130_0830_99694/20080130_0830_99694.VID.wmv';	//"20090211_2023_25_53551.VID.wmv";
//var JSEnableFeedback = true;
//var JSEventState = 'NOTSTARTED'; //'NOTSTARTED'; //'PUBLISHING'; //'TEMP' 'AAA' //'BROADCASTING' //'ONLINE'
//var JSDescription = 'Helloao asodjpaos dopasjdpo asjpodjasopjdpas das dsa odjpoasjd asjdposajd poasjdpo asjdpojasopd jpaso jdposajd pojasopdjas odjpasojdposjapod';

//var JSChapters = 'new Sys.UI.Silverlight.MediaChapter("1",15.19,"2008-FALL_32-QA-CHWR_20081009_1610_31_57670[1].VGA_15.190.jpg"),' +
//	'new Sys.UI.Silverlight.MediaChapter("2",30.828,"2008-FALL_32-QA-CHWR_20081009_1610_31_57670[1].VGA_30.828.jpg"),' +
//	'new Sys.UI.Silverlight.MediaChapter("3",48.645,"2008-FALL_32-QA-CHWR_20081009_1610_31_57670[1].VGA_48.645.jpg"),' +
//	'new Sys.UI.Silverlight.MediaChapter("4",69.346,"2008-FALL_32-QA-CHWR_20081009_1610_31_57670[1].VGA_69.346.jpg"),' +
//	'new Sys.UI.Silverlight.MediaChapter("5",85.241,"2008-FALL_32-QA-CHWR_20081009_1610_31_57670[1].VGA_85.241.jpg")';
//	

function rgbToHex(rgb)
{
	var strHex="0123456789ABCDEF";
	var ichBracket=rgb.indexOf("(");
	if (ichBracket!=-1) {
		var rgstr = rgb.substr(ichBracket+1, rgb.length-ichBracket-2).split(',');
		rgb = "#"+ strHex[parseInt(rgstr[0])>>4]+strHex[parseInt(rgstr[0])%16]+
			   strHex[parseInt(rgstr[1])>>4]+strHex[parseInt(rgstr[1])%16]+
			   strHex[parseInt(rgstr[2])>>4]+strHex[parseInt(rgstr[2])%16];
	}
	return rgb;
}

function StartPlayer_0(parentId) {
    try {
    	eval('document.body.style.backgroundColor="#000000"');
    } catch(e){}

    this._hostname = ExpressionPlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx( {   source: 'player.xaml', 
                                    parentElement: $get(parentId ||"divPlayer_0"), 
                                    id:this._hostname, 
                                    properties:{ width:'100%', height:'100%', version:'1.0', background:rgbToHex(document.body.style.backgroundColor), isWindowless:'false', inplaceInstallPrompt:true }, 
                                    events:{ onLoad:Function.createDelegate(this, this._handleLoad) } } );
    this._currentMediainfo = -1;     
}
StartPlayer_0.prototype = {
    _handleLoad: function() {
        this._player = $create(ExtendedPlayer.Player,
                                  { // properties
                                      autoPlay: this.autoPlayParam(),
                                      autoLoad: this.autoLoadParam(),
                                      scaleMode: this.scaleModeParam(),
                                      muted: this.mutedParam(),
                                      enableCaptions: this.enableCaptionsParam(),
                                      volume: 1.0
                                  },
                                  { // event handlers
                                      mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                      mediaFailed: Function.createDelegate(this, this._onMediaFailed),
                                      playPreviousVideo: Function.createDelegate(this, this._onPlayPreviousVideo),
                                      playNextVideo: Function.createDelegate(this, this._onPlayNextVideo)
                                  },
                                  null, $get(this._hostname));

        this._playlist = [];

        try {
            if ((JSEventState == 'BROADCASTING') && (JSMedia2 != '')) {
                var tempmedia = JSMedia1;
                JSMedia1 = JSMedia2;
                JSMedia2 = tempmedia;

                var nhost = document.getElementById("xamlHost0");
                var CMP1 = nhost.content.findName("VideoWindow");
                var CMP2 = nhost.content.findName("VideoWindow2");
                var newBuf = CMP1.BufferingTime.Seconds;
                newBuf = 0;
                CMP1.BufferingTime.Seconds = newBuf;
                CMP2.BufferingTime.Seconds = newBuf;
            }
        } catch (e) { }

        try {
            eval('this._playlist=[' +
		'{"mediaSource":"' + JSMedia1 + '",' +
		'"placeholderSource":"",' +
		'"chapters":' +
			'[' +
	            JSMediaChapters +
			']}' +
		'];');
        } catch (e) {
            alert(e);
        }

        this._galleryItems = [];
        //        try {
        //            eval('this._galleryItems=[' +
        //		'new ExpressionPlayer.GalleryItem("TEST","2008-FALL_32-QA-CHWR_20081009_1610_31_57670[1].VGA_Thumb.jpg")' +
        //		'];');
        //        }
        //        catch (e) { }

        this._player.set_galleryInfo(this._galleryItems, Function.createDelegate(this, this._onClickGalleryItem));
        this._onPlayNextVideo(null, null);

        this._loadEventsUI(null);
        this._loadEventsMetaData(null);
        if (JSEventState) {
            if ((JSEventState == 'BROADCASTING') && (JSMedia2 != '')) { this.BroadCastingSwap(null, null); }
        }

        this.BroadCastingSwap(null, null);

    },

    _loadEventsMetaData: function() {
        try {
            var nhost = document.getElementById("xamlHost0");
            this.txtbTitle = nhost.content.findName("txtbTitle");
            this.txtbPresenters = nhost.content.findName("txtbPresenters");
            this.txtbDateTime = nhost.content.findName("txtbDateTime");

            this.txtbTitle.text = JStxtbTitle;
            this.txtbPresenters.text = JStxtbPresenters;
            this.txtbDateTime.text = JStxtDateTime;

            if (JSDescription) {
                this.txtbDescription = nhost.content.findName("textRecordingDetails");
                this.txtbDescription.text = JSDescription;
            }

        } catch (e) { }
    },

    _loadEventsUI: function() {
        try {
            var nhost = document.getElementById("xamlHost0");
            var Placeholder1 = nhost.content.findName("Placeholder1");
            var Placeholder2 = nhost.content.findName("Placeholder2");
            var CMP1 = nhost.content.findName("VideoWindow");
            var CMP2 = nhost.content.findName("VideoWindow2");

            if (JSEventState == 'PUBLISHING') {
                if (Placeholder1) { Placeholder1.Source = "PUBLISHING.jpg"; }
                if (Placeholder2) { Placeholder2.Source = "PUBLISHING.jpg"; }
            } else {
                if (Placeholder1) { Placeholder1.Source = "NOTSTARTED.jpg"; }
                if (Placeholder2) { Placeholder2.Source = "NOTSTARTED.jpg"; }
            }

            if (CMP2) {
                CMP2.source = JSMedia2;
                this.btnSwap = nhost.content.findName("buttonSwap");
                this.btnSwap.addEventListener("MouseLeftButtonUp", Function.createDelegate(this, this.handleswap));
            }

            this.btnFullScreen = nhost.content.findName("buttonFullScreen");
            this.btnFullScreen.addEventListener("MouseLeftButtonUp", Function.createDelegate(this, this.handlebtnFullScreen));


            this.ButtonAsk = nhost.content.findName("ButtonAsk");
            this.ButtonAsk.addEventListener("MouseLeftButtonUp", Function.createDelegate(this, this.handleButtonAsk));
            if (JSEnableFeedback == true) {
                this.ButtonAsk.Visibility = 'Visible';
            } else {
                this.ButtonAsk.Visibility = 'Collapsed';
            }

            this.ButtonDetails = nhost.content.findName("buttonDetails");
            this.ButtonDetails.addEventListener("MouseLeftButtonUp", Function.createDelegate(this, this.handleButtonDetails));

            this.buttonDetailsClose = nhost.content.findName("buttonDetailsClose");
            this.buttonDetailsClose.addEventListener("MouseLeftButtonUp", Function.createDelegate(this, this.handlebuttonDetailsClose));


        } catch (e) { alert(e); }
    },

    handlebtnFullScreen: function(sender, eventsArgs) {
        try {
            var nhost = document.getElementById("xamlHost0");
            nhost.content.fullscreen = !nhost.content.fullscreen;
        } catch (e) {
            alert(e);
        }
    },

    handleButtonAsk: function(sender, eventArgs) {
        // The following line of code shows how to find an element by name and call a method on it.
        //alert("going to open new page");
        //window.open('../SubmitFeedback.aspx', 'Feedback', 'width=900,height=300,toolbar=no, location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no, resizable=yes');
    },

    handlebuttonDetailsClose: function(sender, eventArgs) {
        var nhost = document.getElementById("xamlHost0");
        var DescriptionPanel = nhost.content.findName("DescriptionPanel");
        //Make Sure Panel Exists
        if (DescriptionPanel) {
            if (DescriptionPanel.Visibility == "Collapsed") {
                DescriptionPanel.Visibility = "Visible";
            } else {
                DescriptionPanel.Visibility = "Collapsed";
            }
        }
    },

    handleButtonDetails: function(sender, eventArgs) {
        var nhost = document.getElementById("xamlHost0");
        var DescriptionPanel = nhost.content.findName("DescriptionPanel");
        //Make Sure Panel Exists
        if (DescriptionPanel) {
            if (DescriptionPanel.Visibility == "Collapsed") {
                DescriptionPanel.Visibility = "Visible";
            } else {
                DescriptionPanel.Visibility = "Collapsed";
            }
        }
    },

    handleswap: function(sender, eventArgs) {
        try {
            var VB1BV = sender.findName("VB1BV");
            var VB1FS = sender.findName("VB1FS");
            var VB2BV = sender.findName("VB2BV");
            var VB2FS = sender.findName("VB2FS");
            if (VB2BV) {

                if (VB1BV.SourceName == "VideoWindow") {
                    VB1BV.SourceName = "VideoWindow2";
                    VB1FS.SourceName = "VideoWindow2";
                    VB2BV.SourceName = "VideoWindow";
                    VB2FS.SourceName = "VideoWindow";
                } else {
                    VB1BV.SourceName = "VideoWindow";
                    VB1FS.SourceName = "VideoWindow";
                    VB2BV.SourceName = "VideoWindow2";
                    VB2FS.SourceName = "VideoWindow2";
                }
            }
        } catch (e) { alert(e); }
    },

    BroadCastingSwap: function(sender, eventArgs) {
        try {
            var nhost = document.getElementById("xamlHost0");
            var VB1BV = nhost.content.findName("VB1BV");
            var VB1FS = nhost.content.findName("VB1FS");
            var VB2BV = nhost.content.findName("VB2BV");
            var VB2FS = nhost.content.findName("VB2FS");
            if (VB2BV) {
                if (VB1BV.SourceName == "VideoWindow") {
                    VB1BV.SourceName = "VideoWindow2";
                    VB1FS.SourceName = "VideoWindow2";
                    VB2BV.SourceName = "VideoWindow";
                    VB2FS.SourceName = "VideoWindow";
                } else {
                    VB1BV.SourceName = "VideoWindow";
                    VB1FS.SourceName = "VideoWindow";
                    VB2BV.SourceName = "VideoWindow2";
                    VB2FS.SourceName = "VideoWindow2";
                }
            }
        } catch (e) { alert(e); }
    },

    _onClickGalleryItem: function(galleryItemIndex) {
        this._player.set_mediainfo(this._playlist[galleryItemIndex]);
        this._currentMediainfo = galleryItemIndex + 1;
    },

    _onMediaEnded: function(sender, eventArgs) {
        window.setTimeout(Function.createDelegate(this, this._onPlayNextVideo), 1000);
    },

    _onMediaFailed: function(sender, eventArgs) {
        alert(String.format(Sys.UI.Silverlight.MediaPlayer.Res.mediaFailed, this._player.get_mediaSource()));
    },


    _onPlayPreviousVideo: function(sender, eventArgs) {
        if (this._playlist != null) {
            if (this._currentMediainfo > 0) {
                this._player.set_mediainfo(this._playlist[--this._currentMediainfo]);
            }
        }
    },

    _onPlayNextVideo: function(sender, eventArgs) {
        if (this._playlist != null) {
            if (this._currentMediainfo < this._playlist.length - 1) {
                this._player.set_mediainfo(this._playlist[++this._currentMediainfo]);
            }
        }
    },

    autoLoadParam: function() {
        var autoLoad = true;
        try {
            eval("autoLoad=('True'!=='False')");
        } catch (e) { }
        return autoLoad;
    },

    autoPlayParam: function() {
        var autoPlay = true;
        try {
            eval("autoPlay=('True'!=='False')");
        } catch (e) { }
        return autoPlay;
    },

    scaleModeParam: function() {
        var scaleMode = 1/*Normal*/;
        try {
            eval("scaleMode=1;");
        } catch (e) { }
        return scaleMode;
    },

    enableCaptionsParam: function() {
        var enableCaptions = true;
        try {
            eval("enableCaptions=('True'!=='False');");
        } catch (e) { }
        return enableCaptions;
    },

    mutedParam: function() {
        var muted = false;
        try {
            eval("muted=('False'!=='False');");
        } catch (e) { }
        return muted;
    }
}

function StartWithParent(parentId, appId) {
    new StartPlayer_0(parentId);
}
