﻿function createSilverlight()
{
	var scene = new SilverlightDetectionTest.Page();
	Silverlight.createObjectEx({
		source: "wmvplayer.xaml",
		parentElement: document.getElementById("silverlightControlHost"),
		id: "SilverlightControl",
		properties: {
			width: "100%",
			height: "100%",
			version: "1.0",
			isWindowless: 'true',       // Display as windowed plug-in.
			background: '#00000000'
		},
		events: {
			onLoad: Silverlight.createDelegate(scene, scene.handleLoad),
			onError: function(sender, args) {
				var errorDiv = document.getElementById("errorLocation");
				if (errorDiv != null) {
					var errorText = args.errorType + "- " + args.errorMessage;
							
					if (args.ErrorType == "ParserError") {
						errorText += "<br>File: " + args.xamlFile;
						errorText += ", line " + args.lineNumber;
						errorText += " character " + args.charPosition;
					}
					else if (args.ErrorType == "RuntimeError") {
						errorText += "<br>line " + args.lineNumber;
						errorText += " character " +  args.charPosition;
					}
					errorDiv.innerHTML = errorText;
				}	
			}
		}
	});
}

function showhide(id){ 
	if (document.getElementById){ 
		obj = document.getElementById(id); 
		if (obj.style.display == "none"){ 
			obj.style.display = ""; 
		} else { 
			obj.style.display = "none"; 
		} 
	} 
} 

if (!window.Silverlight) 
	window.Silverlight = {};

Silverlight.createDelegate = function(instance, method) {
	return function() {
		return method.apply(instance, arguments);
	}
}

function playSilverlight(id,movie,preview,w,h,autostart)
{
	//playNonSilverlight(id,movie,preview,w,h,autostart);
	
	showhide('silverlight');
	var cnt = document.getElementById(id);
	var src = '/videoPlayer/wmvplayer.xaml';
	var cfg = {
		file:movie,
		image:preview,
		height:h,
		width:w,
        autostart: autostart
	};
	var ply = new jeroenwijering.Player(cnt,src,cfg);
		
}
function playNonSilverlight(id,movie,preview,w,h,autostart,movieID)
{
	showhide('nonsilverlight');
	//jQuery('#'+id).append("<object id='player' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' name='player' width='"+w+"' height='"+h+"'><param name='movie' value='/videoPlayer/player.swf' /><param name='allowfullscreen' value='true' /><param name='allowscriptaccess' value='always' /><param name='flashvars' value='file="+movie+"&image="+preview+"&autostart="+autostart+flashVars+"'/><embed type='application/x-shockwave-flash' id='player2' name='player2' src='/videoPlayer/player.swf' width='"+w+"' height='"+h+"' allowscriptaccess='always' allowfullscreen='true' flashvars='file="+movie+"&image="+preview+"&autostart="+autostart+flashVars+"' /></object>");
	//'skin': '/videoPlayer/skins/lulu/lulu.zip'	
	var objUrl = parseUrl(movie);
	if ( movieID == undefined )
		movieID = objUrl.file;
		
	jwplayer(id).setup({
		'flashplayer': '/videoPlayer/player.swf',
		'movie': '/videoPlayer/player.swf',
		'id': 'playerID',
		'width': w,
		'height': h,
		'file': movie,
		'image': preview,
		's.movieID': movieID,
		's.mediaName': movieID,
		's.Media.playerName': movieID,
		's.charSet': 'UTF-8',
		's.currencyCode': 'USD',
		's.account': 'mswlsrccrmdbingsiteccom',
		's.visitorNamespace': 'microsoftwlsearchcrm',
		's.trackingServer': 'microsoftwlsearchcrm.112.2o7.net',
		's.trackClickMap': true,
		's.Media.autoTrack': true,
		's.Media.trackWhilePlaying': true,
		's.Media.trackSeconds': 10		
		});	
}

function parseUrl(data) {
    var e=/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?$/;

    if (data.match(e)) {
        return  {url: RegExp['$&'],
                protocol: RegExp.$2,
                host:RegExp.$3,
                path:RegExp.$4,
                file:RegExp.$6,
                hash:RegExp.$7};
    }
    else {
        return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
    }
}


