﻿function onXAPError(sender, errorArgs)
{
	if (document.domain == "localhost")
	{
		// The error message to display.
		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";
		
		// Determine the type of error and add specific error information.
		switch(errorArgs.errorType)
		{
			case "RuntimeError":
				// Display properties specific to RuntimeErrorEventArgs.
				if (errorArgs.lineNumber != 0)
				{
					errorMsg += "Line: " + errorArgs.lineNumber + "\n";
					errorMsg += "Position: " +  errorArgs.charPosition + "\n";
				}
				errorMsg += "MethodName: " + errorArgs.methodName + "\n";
				break;
			case "ParserError":
				// Display properties specific to ParserErrorEventArgs.
				errorMsg += "Xaml File:      " + errorArgs.xamlFile      + "\n";
				errorMsg += "Xml Element:    " + errorArgs.xmlElement    + "\n";
				errorMsg += "Xml Attribute:  " + errorArgs.xmlAttribute  + "\n";
				errorMsg += "Line:           " + errorArgs.lineNumber    + "\n";
				errorMsg += "Position:       " + errorArgs.charPosition  + "\n";
				break;
			default:
				break;
		}
		// Display the error message.
		alert(errorMsg);
	}
}

getPlayerHTML = function(videoURL)
{
    return "\
<div id=\"silverlightControlHost\"> \
    <object data=\"data:application/x-silverlight,\" type=\"application/x-silverlight\" width=\"622\" height=\"350\"> \
        <param name=\"source\" value=\"/web/xap/WebMatrix/player.xap\"/> \
        <param name=\"onerror\" value=\"onXAPError\" /> \
        <param name=\"autoUpgrade\" value=\"true\" /> \
        <param name=\"minRuntimeVersion\" value=\"3.0.40624.0\" /> \
        <param name=\"enableHtmlAccess\" value=\"true\" /> \
        <param name=\"enableGPUAcceleration\" value=\"true\" /> \
        <param name=\"initparams\" value='playerSettings = \
                    <Playlist> \
                        <AutoLoad>true</AutoLoad> \
                        <AutoPlay>true</AutoPlay> \
                        <DisplayTimeCode>false</DisplayTimeCode> \
                        <EnableCachedComposition>true</EnableCachedComposition> \
                        <EnableCaptions>true</EnableCaptions> \
                        <EnableOffline>true</EnableOffline> \
                        <EnablePopOut>true</EnablePopOut> \
                        <StartMuted>false</StartMuted> \
                        <StretchMode>None</StretchMode> \
                        <Items> \
							<PlaylistItem> \
								<MediaSource>" + videoURL + "</MediaSource> \
								<IsAdaptiveStreaming>false</IsAdaptiveStreaming> \
								<Width>1280</Width> \
								<Height>720</Height> \
							</PlaylistItem> \
                        </Items> \
                    </Playlist>'/> \
            <div style='padding-top: 150px'> \
                <a href=\"http://go2.microsoft.com/fwlink/?LinkID=124807\"> \
                    <img src=\"http://go2.microsoft.com/fwlink/?LinkId=108181\" alt=\"Get Microsoft Silverlight\"> \
                </a> \
            </div> \
    </object> \
    <iframe id='_sl_historyFrame' style='visibility:hidden;height:0;width:0;border:0px'></iframe> \
 </div> \
    ";
}

$(document).ready(function () {
    var $dialog = $('<div style="padding: 10px; text-align: center; background: #FFF;"></div>')
                    .dialog({
	                    autoOpen: false,
	                    title: 'WebMatrix Overview',
	                    height: 500,
	                    width: 700,
	                    resizable: false,
	                    draggable: false,
	                    modal: true
                    });

    var vidElementIDsAndClasses = new Array();
    vidElementIDsAndClasses.push("#video-overview");
    vidElementIDsAndClasses.push(".watch");
    vidElementIDsAndClasses.push(".watch-action");
    vidElementIDsAndClasses.push(".video-icon");
    
    for (var i=0; i<vidElementIDsAndClasses.length; i++)
    {
        $(vidElementIDsAndClasses[i]).click(function () {
            var videoURL = $(this).context.href;
            var videoTitle = $(this).context.title
            $dialog.html(getPlayerHTML(videoURL))
                .dialog('option', 'title', videoTitle)
                .dialog('open');
            // prevent the default action, e.g., following a link
            return false;
        });
    }
});

