﻿Agility.RegisterNamespace("BNN.Controls.VideoPlayer");

(function(VideoPlayer) {

    var properties = null;
    var clipId, instanceId, width, height, mode;

    VideoPlayer.Load = function(p) {

        properties = p;

        clipId = properties.ClipId;
        instanceId = properties.InstanceId;
        width = properties.Width;
        height = properties.Height;
		mode = properties.Mode;

        if (width == undefined || width == null) width = 300;
        if (height == undefined || height == null) height = 225;

        var container = $("#" + instanceId);

        if (container.size() > 0) container.html("");

        if (clipId != null && clipId.length > 0) {

            container.show();
            templateUrl = Agility.ResolveUrl("~/ClientTemplates/VideoPlayer.htm");
            container.setTemplateURL(templateUrl);
            container.setParam("ClipId", clipId);
            container.setParam("Width", width);
            container.setParam("Height", height);
			container.setParam("Mode", mode);
            container.processTemplate(new Array());

            Interface.PermalinkClicked = function(url) {
                if (Playlist.GetInstance().Current.IsAd) {
                    window.open(url, "player");
                }
            }

            Interface.GetInstance().PlayerViewer = document.getElementById(instanceId);
                       

            Playlist.FeaturedVideos = new Array();
            Playlist.FeaturedVideos.push(new Video({ Title: properties.Description, Format: 'FLV', ClipId: clipId, Permalink: 'http://watch.bnn.ca/Redirect/?ClipId=' + clipId }));

			// Determine if there's a showname for the dart tag
			var dartSiteId = "ctv.bnnwatch.ca";
			var pUri = parseUri(window.location);
            var directoryPath = pUri.directory.toLowerCase().split("/");
			
			if (pUri.file.length > 0 && pUri.file.toLowerCase() == 'videoplayer.aspx') {
                pUri = parseUri(window.top.location);
				directoryPath = pUri.directory.toLowerCase().split("/");
			}
			
            if (((directoryPath[1] == "shows") || (directoryPath[1] == "features")) && pUri.file.length > 0) {
				dartSiteId += "/" + pUri.file.toLowerCase().replace(".aspx", "").replace(/-/g, "");
			}
			
            new Framework(Format.FlashVideo, 3, "chumtvbnn", "One Clip Player", dartSiteId, "/Controls/OneClipTheme.aspx?Size=" + width + "," + height, "/Handlers/VideoThumb.ashx?Clip=" + clipId + "," + width + "," + height + "," + mode);
        }
        else {
            container.hide();
        }
    };

})(BNN.Controls.VideoPlayer); 
