﻿// This is the code that allows the video player to function:

function fillPlaylist() {
    // fill playlist div
    $.ajax({
        type:           'POST',
        url:            'http://'+document.domain+'/vidService2.asmx/getVid',
        data:           'id=',
        dataType:       'xml',
        contentType:    'application/x-www-form-urlencoded',
        success:        function(msg){
            $(msg).find('title').each(function(){
                var id = $(this).prev().prev().prev().prev().prev().prev().text();
                var title = $(this).text();
                var image = $(this).prev().prev().text();
                var altText = $(this).prev().text();
                var filename = $(this).next().next().next().next().next().next().next().next().text();
                
                altText = altText.replace(/\(.+\)|\n|\t/, '', altText);
                var item = '<table border="0" width="100%">';
                item += '<tr align="left">';
                
                // image
                item += '<td valign="top">';
                item += '<img src="http://www.onenewsnow.com/vid/'+image+'" border="0" id="img'+id+'" class="image" alt="news" />';
                item += "</td>";
                
                // title and altText
                item += '<td valign="top">';
                item += '<span id="span'+id+'" class="title"><strong>' +title + "</strong></span><br />";
                item += '<span class="altText">' + altText + '</span>';
                item += '<span id="hid'+id+'" style="visibility:hidden">'+filename+'</span>';
                item += "</td>";
                item += "</tr>"
                item += "</table><br />";
                $('#playlist').append(item);
                
            });
            
            // for the titles in the playlist to be clickable
            $(".title").bind("click", function() {
                var id = $(this).attr("id");
                id = id.replace(/span/,'');
                playClip(id);
            });
            
            // for the images in the playlist to be clickable 
            $(".image").bind("click", function() {
                var id = $(this).attr("id");
                id = id.replace(/img/,'');
                playClip(id);
            });
        }
    });
    
    // Fill most popular list
    $.ajax({
        type:           'POST',
        url:            'http://'+document.domain+'/vidService2.asmx/getHighest',
        dataType:       'xml',
        contentType:    'application/x-www-form-urlencoded',
        success:        function(msg){
            $(msg).find('title').each(function() {
                var id = $(this).prev().prev().prev().prev().prev().prev().text();
                var title = $(this).text();
                var image = $(this).prev().prev().text();
                var filename = $(this).next().next().next().next().next().next().next().next().text();
                var item = '<td valign="top" width="33%">';
                item += '<img src="http://www.onenewsnow.com/vid/'+image+'" border="0" id="img'+id+'" class="image" alt="news" />';
                item += '<br />';
                item += '<span id="span'+id+'" class="title"><strong>'+title+'</strong></span>';
                item += '</td>';
                $('#mostPopTbl').append(item);                       
            });
            
            // for the titles in the playlist to be clickable
            $(".title").bind("click", function() {
                var id = $(this).attr("id");
                id = id.replace(/span/,'');
                playClip(id);
            });
            
            // for the images in the playlist to be clickable 
            $(".image").bind("click", function() {
                var id = $(this).attr("id");
                id = id.replace(/img/,'');
                playClip(id);
            });
        }
    });            
}

function upPlayNum(id) {
    if(id != null) {
        $.ajax({
            type:           'POST',
            url:            'http://'+document.domain+'/vidService2.asmx/addPlay',
            data:           'id='+id,
            dataType:       'xml',
            contentType:    'application/x-www-form-urlencoded',
            success:        function(msg){
                //alert("success in uping numPlays");     // for debugging
            }
        });
    }
}

function playClip(id) {
    // this function is to directly play the clip when a user
    // clicks on a title or thumbnail of a different ap vid
    if(id != null) {
        var currentVid = '';
        var vid = ''; 
        if(id != 'daily') {
            // aparently the flowplayer crashes if you add the whole url to vid
            // before it gets to the play method. If not this would be a whole
            // log simpler
            // AP News play method
            currentVid = $f('player').getClip(0).url;
            vid = $('#hid'+id).text();
            if(currentVid != 'http://www.onenewsnow.com/vid/' + vid) {
                $f('player').play('http://www.onenewsnow.com/vid/'+vid);
                //upPlayNum(id);
            }
        } else {
            // Daily News Cast
            currentVid = $f('player').getClip(0).url;
            vid = $('#hiddaily').text();
            if(currentVid != vid) {
                $f('player').play(vid);
            }
        }
    }
}

function createPlayer(pUrl) {
    flowplayer("player", "flowplayer.commercial-3.1.2.swf", {
        key: '#@3f7f8f89f1f2b965104', 
        clip: {
            url: pUrl,
            autoPlay: true,
            autoBuffering:true,
            
            onStart: function(clip) {
                google._trackEvent("APVideos", "Play", clip.url);
            },
            
            onPause: function(clip) {
                google._trackEvent("APVideos", "Pause", clip.url.parseInt(this.getTime()));
            },
            
            onStop: function(clip) {
                google._trackEvent("APVideos", "Stop", clip.url.parseInt(this.getTime()));
            },
            
            onFinish: function(clip) {
                google._trackEvent("APVideos", "Finish", clip.url);
            }
        }
    });
    
}

$(function() {
    var videoId = $.query.get('videoId');
    var pUrl = '';
    //alert(videoId); // for debugging
    if(videoId == '') {
        pUrl = 'http://mediaserver3.afa.net/newscast/streams/_definst_/daily.flv';
        createPlayer(pUrl);
    } else {
        $.ajax({
            type:           'POST',
            url:            'http://'+document.domain+'/vidService2.asmx/getVid',
            data:           'id='+videoId,
            dataType:       'xml',
            contentType:    'application/x-www-form-urlencoded',
            success:        function(msg){
                $(msg).find('flv').each(function() {
                    pUrl = 'http://www.onenewsnow.com/vid/'+$(this).text();
                    createPlayer(pUrl);
                });
            }
        });
    }
    
    fillPlaylist();
    var google = _gat._getTracker("UA-354999-7");     
});
