﻿Type.registerNamespace('Insys.LechTV');

Insys.LechTV.PlayerLite = function(domElement) {
    Insys.LechTV.PlayerLite.initializeBase(this, [domElement]);
    
    this._playOverlayButton = new Ee.UI.Xaml.Media._Button(domElement, 'PlayOverlayButton', null, this._startPlayer, null, this, false);

    this.add_mediaOpened(Function.createDelegate(this, this._onMediaOpened));
    this.add_mediaEnded(Function.createDelegate(this, this._onMediaEnded));
    this.add_stateChanged(Function.createDelegate(this, this._onStateChanged));
    this.add_xamlInitialized(Function.createDelegate(this, this._clearEvents));
}

    function Insys$LechTV$PlayerLite$playMedia(media) 
    {
        Insys.LechTV.MediaInfoService.GetMediaUrl(media.id, Function.createDelegate(this, this._setMediaUrlCallback), this._zonkCallback);
    }

    function Insys$LechTV$PlayerLite$_setMediaUrlCallback(result, eventArgs)
    {
        this.set_mediaUrl(result)
    }

    function Insys$LechTV$PlayerLite$_zonkCallback(error) {
        alert(error.get_message());
    }


Insys.LechTV.PlayerLite.prototype =  {
    _currentMedia: null,
    _overlays: {},
    _playlist: null,
    _currentVideoIndex: -1,
    _relatedMediaList: null,
    playMedia: Insys$LechTV$PlayerLite$playMedia,
    _setMediaUrlCallback: Insys$LechTV$PlayerLite$_setMediaUrlCallback,
    _zonkCallback: Insys$LechTV$PlayerLite$_zonkCallback,
    
	set_playlist: function(playlist)
	{
	    this._playlist = playlist;
	    this._onMediaEnded();
	},
	
	_onStateChanged: function(sender, eventArgs) {
	    var newState = this.get_playState();
	     if (newState === "Playing") {
	        this._playOverlayButton.setVisible(false);
	     } else if (newState === "Paused") {
	        this._playOverlayButton.setVisible(true);
	     }
	},
	
	
	_onMediaEnded: function(sender, eventArgs) {
        //this._currentVideoIndex++;
        //var nextMedia = this._playlist[this._currentVideoIndex % this._playlist.length];
        //this.playMedia(nextMedia);
        this.play();
    },
    
    _onMediaOpened: function(sender, eventArgs) {
        if (!this.get_autoPlay()) {
            this._playOverlayButton.setVisible(true);
        }
    },
    
    _clearEvents: function() {
        // change default click events on media element
        this._domElements[0]._doubleClickDelegate = null;
        //this._domElements[0]._clickDelegate = Function.createDelegate(this, this._openLechTVSite);
    },
    
    _startPlayer: function() {
        this.togglePlayPause();
        this._playOverlayButton.setVisible(false);
    },
    
    _openLechTVSite: function() {
        window.top.location = 'http://www.lechpoznan.tv/';
    }
}
Insys.LechTV.PlayerLite.registerClass('Insys.LechTV.PlayerLite', EePlayer.Player);


function StartPlayer(parentId, autoplay) {
    this._parentId = parentId;
    this._autoplay = autoplay;
    this._hostname = EePlayer.Player._getUniqueName("xamlHost");
    
    if (Silverlight.isInstalled('1.0')) {
        this._create();
    } else {
        this._toggleInstallInfo();
	    this._interval = window.setInterval(Function.createDelegate(this, this._checkInstall), 3000);
    }    
}
StartPlayer.prototype= {
    _handleLoad: function() {
        this.player = $create(Insys.LechTV.PlayerLite, 
                          { // properties
                            autoPlay    : this._autoplay, 
                            volume      : 1.0,
                            muted       : false
                          },{}, null, $get(this._hostname)); 
        
        //Insys.LechTV.MediaInfoService.GetPromoMedia(Function.createDelegate(this, this._setPlaylistCallback), null);
        this.player.set_mediaUrl('http://87.255.34.98/lsw070518/energialecha/spot2.wmv');
    },    
    _setPlaylistCallback: function(result, eventArgs) 
    {
        this.player.set_playlist(result);
    },
    
    _toggleInstallInfo: function() {
        var noSL = $get('noSilverlight');
        var show = (!noSL.style.display || noSL.style.display == "none") ? true : false;
        
        $get('playerContent').style.display = show ? "none" : "block";
	    noSL.style.display = show ? "block" : "none";
	    
        var sys = Silverlight.ua.OS
        if (sys == "MacIntel") {
           d = "92808";
           l = "92804";
           p = "92806"
        } else if (sys == "MacPPC") {
           d = "92807";
           l = "92815";
           p = "92816"
        }
        else {
           d = "92799";
           l = "92803";
           p = "92805"
        }

        $get('lnkInstall').href = 'http://go.microsoft.com/fwlink/?LinkID=' + String(d);	    
        $get('lnkLicense').href = 'http://go.microsoft.com/fwlink/?LinkID=' + String(l);
        $get('lnkPrivacy').href = 'http://go.microsoft.com/fwlink/?LinkID=' + String(p);
    },
    
    _checkInstall: function() {
        if (Silverlight.isInstalled('1.0')) {
            window.clearInterval(this._interval);
            this._toggleInstallInfo();
            this._create();
        }
    },
    
    _create: function() {
        Silverlight.createObjectEx( {   source: 'xaml/PlayerLite.xaml', 
                                    parentElement: $get(this._parentId), 
                                    id: this._hostname, 
                                    properties:{ width:'302px', height:'217px', version:'1.0', background:'transparent', isWindowless:'true', inplaceInstallPrompt:true }, 
                                    events:{ onLoad: Function.createDelegate(this, this._handleLoad) } } );   
    }
}