﻿var noAGContent = '<div style=" text-align: center; width:640px; height:390px;background-image:url(images/im_silverlight.jpg);  background-repeat: no-repeat;background-position: center; " id="cleanInstall"><img style="margin-top:155px; cursor:pointer;" border=0 id="installBadge" src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" onclick="Silverlight.getSilverlight(\'2.0\');"  /></div>'


function StartPlayer_0(parentId) {

    this._hostname = ExpressionPlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx( {   source: 'common/xaml/player.xaml', 
                                    parentElement: $get(parentId ||"divPlayer_0"), 
                                    id:this._hostname,
                                    properties: { width: '640', height: '390', version: '1.0', background: "White", isWindowless: 'false', alt: noAGContent }, 
                                    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),
                                      mediaOpened: Function.createDelegate(this, this._onMediaOpened),
                                      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 {
            eval('this._playlist=[' +
		'{"mediaSource":"http://global.msads.net/ads/pronws/messenger/im/HowTo_1.wmv",' +
		'"placeholderSource":"images/HowTo_Thumb.jpg",' +
		'"chapters":' +
			'[' +
			']}' +
		'];');
        }
        catch (e) { }

        this._galleryItems = [];
        try {
            eval('this._galleryItems=[' +
		'new ExpressionPlayer.GalleryItem("media/HowTo.wmv","images/HowTo_Thumb.jpg")' +
		'];');
        }
        catch (e) { }
        this._player.set_galleryInfo(this._galleryItems, Function.createDelegate(this, this._onClickGalleryItem));
        this._onPlayNextVideo(null, null);
        // If play/pause button in control is pressed then hide play again button
        $get("xamlHost0").content.FindName("PlayPauseButton").AddEventListener("MouseLeftButtonUp", function() {
            $get("xamlHost0").content.FindName("EndButtonCanvas").Visibility = "Collapsed";
            $get("xamlHost0").content.FindName("bgEnd").Visibility = "Collapsed";
            $get("xamlHost0").content.FindName("buttonShadow").Visibility = "Visible";
            $get("xamlHost0").content.FindName("VideoWindow").Visibility = "Visible";
            if ($get("xamlHost0").content.FindName("VideoWindow").Position.Seconds == 0) { // make it's the start
                omnitureTracking("26", "Im Video - Begin");
            }
        });
        // If play/pause button in control is pressed then hide play again button
        $get("xamlHost0").content.FindName("playAgain").AddEventListener("MouseLeftButtonUp", function() {
            $get("xamlHost0").content.FindName("EndButtonCanvas").Visibility = "Collapsed";
            $get("xamlHost0").content.FindName("bgEnd").Visibility = "Collapsed";
            $get("xamlHost0").content.FindName("buttonShadow").Visibility = "Visible";

            $get("xamlHost0").content.FindName("VideoWindow").Visibility = "Visible";
            $get("xamlHost0").content.FindName("VideoWindow").stop();
            $get("xamlHost0").content.FindName("VideoWindow").play();
            omnitureTracking("26", "Im Video - Begin");
        });


        $get("xamlHost0").content.FindName("joinNow").AddEventListener("MouseLeftButtonUp", function() {
            omnitureTracking("2", "Im Video - Join Now");
            parent.opener.focus();
            parent.opener.location = "http://im.live.com/Messenger/IM/Join/Default.aspx";
            self.close();
        });

        $get("xamlHost0").content.FindName("playAgain").AddEventListener("MouseEnter", function() {
            $get("xamlHost0").content.FindName("PlayAgainMouseOver").Begin();
        });
        $get("xamlHost0").content.FindName("playAgain").AddEventListener("MouseLeave", function() {
            $get("xamlHost0").content.FindName("PlayAgainMouseOut").Begin();
        });
        $get("xamlHost0").content.FindName("joinNow").AddEventListener("MouseEnter", function() {
            $get("xamlHost0").content.FindName("JoinNowMouseOver").Begin();
        });
        $get("xamlHost0").content.FindName("joinNow").AddEventListener("MouseLeave", function() {
            $get("xamlHost0").content.FindName("JoinNowMouseOut").Begin();
        });

    },
    _onClickGalleryItem: function(galleryItemIndex) {
        this._player.set_mediainfo(this._playlist[galleryItemIndex]);
        this._currentMediainfo = galleryItemIndex + 1;
    },
    _onMediaOpened: function(sender, eventArgs) {
        omnitureTracking("26", "Im Video - Begin");
    },
    _onMediaEnded: function(sender, eventArgs) {
        // - Add tracking tag
        $get("xamlHost0").content.FindName("EndButtonCanvas").Visibility = "Visible";
        $get("xamlHost0").content.FindName("buttonShadow").Visibility = "Visible";
        $get("xamlHost0").content.FindName("bgEnd").Visibility = "Visible";
        var VideoWindow = $get("xamlHost0").content.FindName("VideoWindow");
        VideoWindow.Visibility = "Collapsed"
        omnitureTracking("27", "Im Video - Complete")
        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);
}
