﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="js/Silverlight.js"/>

function SilverlightHost(playerParent, featuredContentParent, relatedMediaParent, top10Parent) {
    this._playerParent = playerParent;
    this._featuredContentParent = featuredContentParent;
    this._relatedMediaParent = relatedMediaParent;
    this._top10Parent = top10Parent;

    if (Silverlight.isInstalled('1.0')) {
        this._create();
    } else {
        this._toggleInstallInfo();
	    this._interval = window.setInterval(Function.createDelegate(this, this._checkInstall), 3000);
    }   
}

SilverlightHost.prototype = {
    _playerParent: null,
    _featuredContentParent: null,
    _relatedMediaParent: null,

    _playerLoaded: false,
    _featuredContentLoaded: false,
    _relatedMediaLoaded: false,
    _top10Loaded: false,
    
    _toggleInstallInfo: function() {
        var show = ($get('noSilverlight').style.visibility == "visible") ? false : true;
        
        $get(this._playerParent).className = show ? 'off' : '';
        $get(this._featuredContentParent).className = show ? 'off' : '';
        $get(this._relatedMediaParent).className = show ? 'off' : '';
        
	    $get('noSilverlight').style.visibility = show ? "visible" : "hidden";
	    /*
	    if (Silverlight.ua.Browser == "Firefox" || Silverlight.ua.Browser == "Safari") {
	        $get("restart").style.visibility = 'visible';
	    }
	    
        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();
        }
    },
    
    _playerLoad: function() {
        this.player = $create(Insys.LechTV.Player, 
                                  { // properties
                                    autoPlay    : true, 
                                    volume      : 1.0,
                                    muted       : false
                                  },{ mediaFailed: Function.createDelegate(this, this._onMediaFailed) }, null, $get(this._playerHostname)  ); 
        var playlist = new Insys.LechTV.Playlist($get(this._playerHostname), "Playlist");     
        playlist.set_items([]);
        this.player.set_playlist(playlist);
        
        this._playerLoaded = true;
        this._init();
    },
    
    _featuredContentLoad: function() {
        this.featuredContent = new Insys.LechTV.FeaturedContent($get(this._featuredContentHostname), "FeaturedContent");
        Insys.LechTV.MediaInfoService.GetFeaturedContent(Function.createDelegate(this, this._setFeaturedContentCallback), null);
        
        this._featuredContentLoaded = true;
        this._init();
    },
    
    _relatedMediaLoad: function() {
        this.relatedMedia = new Insys.LechTV.MediaList($get(this._releatedMediaHostname), "MediaList", "Zobacz również");
        Insys.LechTV.MediaInfoService.GetHomeRelatedMedia(Function.createDelegate(this, this._setHomeRelatedMediaCallback), null);        

        this._relatedMediaLoaded = true;
        this._init();
    },
    
    _top10Load: function() {
        this.top10 = new Insys.LechTV.MediaList($get(this._top10Hostname), "MediaList", "Najczęściej oglądane");
        Insys.LechTV.MediaInfoService.GetTop10Media(Function.createDelegate(this, this._setTopMedia10Callback), null);

        this._top10Loaded = true;
        this._init();
    },
        
    _onMediaFailed: function() {
        alert('Nie można odtworzyć materiału video.')
    },
    
    _create: function(playerParent, featuredContentParent, relatedMediaParent) {
        this._playerHostname = EePlayer.Player._getUniqueName("xamlHost");
        this._featuredContentHostname = EePlayer.Player._getUniqueName("xamlHost");
        this._releatedMediaHostname = EePlayer.Player._getUniqueName("xamlHost");
        this._top10Hostname = EePlayer.Player._getUniqueName("xamlHost");

        Silverlight.createObjectEx( {   source: 'xaml/Player.xaml', 
                                            parentElement: $get(this._playerParent), 
                                            id: this._playerHostname, 
                                            properties:{ width:'486px', height:'344px', version:'1.0', background:'#05143B', isWindowless:'true', inplaceInstallPrompt:true }, 
                                            events:{ onLoad:Function.createDelegate(this, this._playerLoad) } } );
        
        Silverlight.createObjectEx( {   source: 'xaml/featuredcontent.xaml', 
                                            parentElement: $get(this._featuredContentParent), 
                                            id: this._featuredContentHostname, 
                                            properties:{ width:'452px', height:'344px', version:'1.0', background:'#05143B', isWindowless:'true'}, 
                                            events:{ onLoad:Function.createDelegate(this, this._featuredContentLoad) } } );

        Silverlight.createObjectEx( {   source: 'xaml/medialist.xaml', 
                                            parentElement: $get(this._relatedMediaParent), 
                                            id: this._releatedMediaHostname, 
                                            properties:{ width:'482px', height:'150px', version:'1.0', background:'#05143B', isWindowless:'false'}, 
                                            events:{ onLoad:Function.createDelegate(this, this._relatedMediaLoad) } } );
                                            
        Silverlight.createObjectEx( {   source: 'xaml/medialist.xaml', 
                                            parentElement: $get(this._top10Parent), 
                                            id: this._top10Hostname, 
                                            properties:{ width:'482px', height:'150px', version:'1.0', background:'#05143B', isWindowless:'false'}, 
                                            events:{ onLoad:Function.createDelegate(this, this._top10Load) } } );
    },
    
    _init: function() {
        if (this._playerLoaded && this._featuredContentLoaded && this._relatedMediaLoaded && this._top10Loaded) {
            this.featuredContent.onItemMediaClick = Function.createDelegate(this.player, this.player.playMedia);
            this.featuredContent.onActionMediaClick = Function.createDelegate(this.player, this.player.addToPlaylist);        

            this.player.set_relatedMediaList(this.relatedMedia);
            
            this.top10.onItemMediaClick = Function.createDelegate(this.player, this.player.playMedia);
            this.top10.onActionMediaClick = Function.createDelegate(this.player, this.player.addToPlaylist);
            
            if (typeof(getStartupMedia) != 'undefined') {
                this.player.playMedia(getStartupMedia());
            }
            
            if (typeof(hideFeaturedContentOnStartup) != 'undefined') {
                this.hideFeaturedContent();
            }
        }
    },
    
    _setHomeRelatedMediaCallback: function(result, eventArgs)
    {
        if (this.relatedMedia) {
            this.relatedMedia.set_items(result);
        }
    },
    
    _setFeaturedContentCallback: function(result, eventArgs)
    {
        if (this.featuredContent) {
            this.featuredContent.set_items(result);
        }
    },
    
    _setTopMedia10Callback: function(result, eventArgs) 
    {
        if (this.top10) {
            this.top10.set_items(result);
        }
    },
    
    _wsErrorCallback: function(error) {
        alert(error.get_message());
    },
    
    hideFeaturedContent: function() {
        $get(this._featuredContentParent).style.display = 'none';
        this.featuredContent.stopRotate();
    },
    
    showFeaturedContent: function() {
        $get(this._featuredContentParent).style.display = 'block';
        this.featuredContent.startRotate();
    }
}

function play(id) {
    slHost.player.playMedia(fetchMedia(id));
}

function add(id) {   
    slHost.player.addToPlaylist(fetchMedia(id));
    $get('vid' + id + '_added').style.display = 'block';
    window.setTimeout('hide(' + id + ')', 1600);
}

function hide(id) {
    $get('vid' + id + '_added').style.display = 'none';
}

function selectMenuItem(id) {
    var links = $get('menu').getElementsByTagName('a');
    for (i in links) {
        if (links[i].className == 'mainButton_on') {
            links[i].className = 'mainButton_off';
            break;
        }
    }
    var link = $get(id);
    if(link!=null){
      if (link.className == 'mainButton_off') {
          link.className = 'mainButton_on';
      }
    }
}
