var channels = ['Nederland 1', 'Nederland 2', 'Nederland 3', 'BBC 1', 'BBC 2', 'National Geographic'];

function getNowNext() {
    var chan = "&channels=" + channels.join("&channels=");
    var url = '/cgi-bin/WebObjects/TVGuide.woa/wa/Json?template=Json_eventsOverview' + chan;
    req = new Ajax.Request(url, {
        "method": "get",
        "onComplete": processNowNext
    });
}
function processNowNext(response) {
    var eventList = null;
    pageState = eval(response.responseText);
    if (typeof(pageState) != 'undefined') {
        if (typeof(activeChannel) != "undefined") {
            eventList = $A(pageState.events).findAll(function(evt) {
                if (evt.channel[1] == activeChannel) {
                    return true;
                } else {
                    return false;
                }
            })
            if (eventList.length > 2) {
                eventList = eventList.splice(0, 2);
            }
            $("ch_ltv_now_title").innerHTML = eventList[0].title;
            $("ch_ltv_now_blurb").innerHTML = eventList[0].description;
            $("ch_ltv_next_title").innerHTML = eventList[1].title;
            $("ch_ltv_channel_nowandnext").style.visibility = "visible";
            setTimeout(function() {
                getNowNextChannel(req.options.channel)
            },
            300000);
        } else {
            $A(channels).each(function(channel) {
                eventList = $A(pageState.events).findAll(function(evt) {
                    if (evt.channel[1] == channel) {
                        return true;
                    } else {
                        return false;
                    }
                })
                if (eventList.length > 2) {
                    eventList = eventList.splice(0, 2);
                }
                if ($(channel.replace(" ", "_") + "_now") != null) {
                    $(channel.replace(" ", "_") + "_now").innerHTML = eventList[0].title;
                }
                if ($(channel.replace(" ", "_") + "_next") != null) {
                    $(channel.replace(" ", "_") + "_next").innerHTML = eventList[1].title;
                }
            })
        }
    }
}
var livetvplayer = Class.create();
livetvplayer.prototype = {
    url: '',
    width: '',
    height: '',
    options: null,
    DefaultOptions: {
        autoStart: 'true',
        stretchToFit: 'true',
        uiMode: 'full',
        volume: '100'
    },
    initialize: function(url, width, height, options) {
        this.options = Object.extend({},
        this.DefaultOptions);
        if ((Prototype.Browser.IE) || (Prototype.Browser.Opera)) {
            if (typeof options == 'object') {
                Object.extend(this.options, options);
            }
            this.options.FileName = url;
            this.options.src = url;
            this.options.URL = url;
        }
        this.width = parseInt(width);
        this.height = parseInt(height);
        document.write(this.display());
    },
    display: function() {
        var html = '';
        if ((Prototype.Browser.IE) || (Prototype.Browser.Opera)) {
            html += '<object id="LiveTVPlayer" data="' + this.url + '" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="video/x-ms-wmv"';
            html += ' width="' + this.width + '" height="' + this.height + '" standby="Bezig met laden...">\n';
            for (var name in this.options) {
                html += '<param name="' + name + '" value ="' + this.options[name] + '" />\n';
            }
            html += '</object>';
            return html;
        }
        else {
            html += '<embed id="LiveTVPlayer" type="application/x-ms-wmp"\n';
            html += ' pluginspage="http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx" filename="' + this.url + '"';
            html += ' width="' + this.width + '" height="' + this.height + '">\n';
            html += '</embed>';
            return html;
        }
    }
}
Event.observe(window, 'load',
function() {
    if (typeof livetvplayer != 'undefined') {
        livetvplayer.prototype.initialize = function(url, width, height, options) {
            if (typeof this.DefaultOptions != "undefined") {
                this.options = Object.extend({},
                this.DefaultOptions);
            }
            if ((Prototype.Browser.IE) || (Prototype.Browser.Opera)) {
                if (typeof options == 'object') {
                    Object.extend(this.options, options);
                }
                this.options.FileName = url;
                this.options.src = url;
                this.options.URL = url;
            }
            this.url = url;
            this.width = parseInt(width);
            this.height = parseInt(height);
        }
    }
    if ($('ch_ltv_player') != null) {
        if (typeof channel_url != 'undefined' && typeof livetvplayer != 'undefined') {
            var html2 = '';
            var mp = new livetvplayer(channel_url, 552, 366);
            html2 = mp.display();
            html2 += '<div id="ch_ltv_wmp_controls">';
            html2 += '<a href="' + channel_url + '" class="standalone">' + standalone_label + '</a>';
            html2 += (document.all ? '<a href="#" class="fullscreen" onclick="player=$(\'LiveTVPlayer\');if(player.playState==3){player.fullScreen=true}">' + fullscreen_label + '</a>': '');
            html2 += '</div>';
            $('ch_ltv_player').innerHTML = html2;
        }
    }
    getNowNext();
})
 function nowPlaying(sSelectedChannel) {
    if ($ && document.createElement) {
        arAllTVChannels = $(sChannelMenuID).getElementsByTagName('li');
        for (i = 0; i < arAllTVChannels.length; i++) {
            if (arAllTVChannels[i].innerHTML.search(sSelectedChannel) != -1) {
                arAllTVChannels[i].className = sActiveChannelClass;
                if (hasNowPlayingImage) {
                    sInsideAnchorTag = arAllTVChannels[i].getElementsByTagName('a')[0].innerHTML;
                    arAllTVChannels[i].innerHTML = sInsideAnchorTag;
                    arAllTVChannels[i].getElementsByTagName('span')[0].innerHTML = '<img src="/media/pc/hispeed/global/core/livetv/now_playing.gif" alt="' + sSelectedChannel + '" />'
                }
            }
        }
    }
}
