﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Windows");

Windows.PossibilitiesTabbedContentBehavior = function(element) {
    Windows.PossibilitiesTabbedContentBehavior.initializeBase(this, [element]);
    
    this.clickDelegate = Function.createDelegate( this, this.tabClicked );
}

Windows.PossibilitiesTabbedContentBehavior.prototype = {
    initialize: function() {
        Windows.PossibilitiesTabbedContentBehavior.callBaseMethod(this, 'initialize');

        var tabs = this.get_AllTabs();
        for (var index = 0; index < tabs.length; index++) {
            // Create a closure which passes in the tab index
            var delegate = Function.createDelegate(this,
				function(value) {
				    return function() { this.tabClicked(value); }
				} (index)
			);

            $addHandler(tabs[index].get_element(), "click", delegate);
        }
        if (!Silverlight.isInstalled("2.0")) this.tabClicked(initialVideoIndex);
    },
    dispose: function() {
        Windows.PossibilitiesTabbedContentBehavior.callBaseMethod(this, 'dispose');
    },

    //******************************************
    //	Parameters
    //******************************************
    get_VideoHost: function() {
        return this._videoHost;
    },
    set_VideoHost: function(value) {
        if (this._videoHost != value) {
            this._videoHost = value;
            this.raisePropertyChanged('VideoHost');
        }
    },

    get_TopTabList: function() {
        return this._topTabList;
    },
    set_TopTabList: function(value) {
        if (this._topTabList != value) {
            this._topTabList = value;
            this.raisePropertyChanged('TopTabList');
        }
    },

    get_BottomTabList: function() {
        return this._bottomTabList;
    },
    set_BottomTabList: function(value) {
        if (this._bottomTabList != value) {
            this._bottomTabList = value;
            this.raisePropertyChanged('BottomTabList');
        }
    },

    get_AllTabs: function() {
        var _topTabs = this.get_TopTabList().get_Items();
        var _bottomTabs = this.get_BottomTabList().get_Items();
        var _totalTabs = new Array();

        for (var index = 0; index < _topTabs.length; index++) {
            _totalTabs.push(_topTabs[index]);
        }
        for (var index = 0; index < _bottomTabs.length; index++) {
            _totalTabs.push(_bottomTabs[index]);
        }
        return _totalTabs;
    },

    get_VideoGuids: function() {
        return this._videoGuids;
    },
    set_VideoGuids: function(value) {
        if (this._videoGuids != value) {
            this._videoGuids = value ? Array.parse(value) : [];
            this.raisePropertyChanged('VideoGuids');
        }
    },

    get_VideoUrls: function() {
        return this._videoUrls;
    },
    set_VideoUrls: function(value) {
        if (this._videoUrls != value) {
            this._videoUrls = value ? Array.parse(value) : [];
            this.raisePropertyChanged('VideoUrls');
        }
    },

    get_VideoGraphics: function() {
        return this._videoGraphics;
    },
    set_VideoGraphics: function(value) {
        if (this._videoGraphics != value) {
            this._videoGraphics = value ? Array.parse(value) : [];
            this.raisePropertyChanged('VideoGraphics');
        }
    },

    //******************************************
    //	Handlers
    //******************************************
    tabClicked: function(index) {
        this.selectNewVideo(index);
    },

    //******************************************
    //	Other functions
    //******************************************
    selectNewVideo: function(videoIndex) {
        if (Silverlight.isInstalled("2.0")) {
            var silverlightPlugin = document.getElementById("silverlight_object");
            silverlightPlugin.Content.Page.LoadVideoFromGuid(this.get_VideoGuids()[videoIndex]);
        }
        else {
            this.get_VideoHost().style.height = "445px";
            document.getElementById("sl_container").innerHTML = this.getWmpMarkup(this.get_VideoUrls()[videoIndex]);
            document.getElementById("ftp_wmp_video_graphic").style.backgroundImage = "url('" + this.get_VideoGraphics()[videoIndex] + "')";
        }
        this.get_VideoHost().style.backgroundImage = "url('" + this.get_VideoGraphics()[videoIndex] + "')";
    },

    getWmpMarkup: function(videoUrl) {
        var playerID = "contentPlayer";
        var width = 676;
        var height = 445;
        var mediaPlayerHtml = "<object id='wmpPlayer'  type='application/x-ms-wmp' classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'";
        mediaPlayerHtml += "width='" + width + "' height='" + height + "'>";
        mediaPlayerHtml += "<param name='url' value='" + videoUrl + "' />";
        mediaPlayerHtml += "<param name='autoStart' value='false' />";
        mediaPlayerHtml += "<param name='ShowStatusBar' value='true' />";
        mediaPlayerHtml += "<param name='windowlessVideo' value='false' />";
        mediaPlayerHtml += "<embed TYPE='application/x-mplayer2' pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' src='" + videoUrl + "' id='" + playerID +
            "' name='MediaPlayer' width='" + width + "' height='" + height + "' showcontrols='true' showdisplay='0' showstatusbar='true' autostart='0' autosize='true' />";
        mediaPlayerHtml += "</object>";
        return mediaPlayerHtml;
    }
}
Windows.PossibilitiesTabbedContentBehavior.registerClass('Windows.PossibilitiesTabbedContentBehavior', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
