var weekday=new Array(7);
weekday[0]="Sun";
weekday[1]="Mon";
weekday[2]="Tue";
weekday[3]="Wed";
weekday[4]="Thu";
weekday[5]="Fri";
weekday[6]="Sat";


/*function timestamp2str(t) {
    var d = new Date();
    d.setTime(t*1000);
    var t = d.toLocaleTimeString().split(':');

    var day = (d.getDay()-1)<0?6:(d.getDay()-1);

    return weekday[day]+" "+t[0]+":"+t[1]+t[2].substr(3);
}

function formattitle(s) {
    var t = s.split(' ');
    return timestamp2str(t[0])+' '+t[1]+' '+timestamp2str(t[2]);
}*/

var checkPlayerInterval;
function checkPlayer() {
    try {
        var type = typeof document.getElementById('ply').sendEvent;
    } catch (err) {
        var type = "";
    }

    if (type=="function") {
        $('.videoitem:first').click();
        document.getElementById('ply').addModelListener('TIME', 'positionListener');
        document.getElementById('ply').addModelListener('STATE','playerStateChanged');
        clearTimeout(checkPlayerInterval);
    }
}

function playerStateChanged(obj) {
        if (obj.newstate=="COMPLETED") {
            $('li.playing').next('li.videoitem').click();
        }
    }

var new_vid_update_cycle;
function update_new_vids() {
    var url = '/channel/'+chanid+'/is_updated/'+now_ts+'/?'+(new Date().getTime());
    $('#newvids').load(url,function (data) { 
            if (parseInt(data)>=3) {
                $('#newvids').text('Multiple');
                clearInterval(new_vid_update_cycle);
            }
        });
}

$(document).ready(function () {
    //new_vid_update_cycle = setInterval("update_new_vids()",25000);

    checkPlayerInterval = setInterval("checkPlayer();",1000);

    $('.thumbnail').hover(function () {
        if ($('#animate_all_checkbox').attr('checked'))
            return;
        if ($(this).parents('.videoitem').attr('deleted')=='true')
            return;
        $(this).attr('src',$(this).attr('animated'));
        setTimeout("$('#"+this.id+"').attr('src',$('#"+this.id+"').attr('static'));",10000);
    });

    $('.show').mousedown(function () {
        document.getElementById('ply').sendEvent('PLAY','false');
    });

    $('#animate_all_checkbox').change(function () {
        if ($('#animate_all_checkbox').attr('checked')) {
            $('.thumbnail:not([src$="empty.PNG"])').each(function(){$(this).attr('src',$(this).attr('animated'));});
        } else {
            $('.thumbnail:not([src$="empty.PNG"])').each(function(){$(this).attr('src',$(this).attr('static'));});
        }
    });


    $('.delete').click(function () {
        l = $('li.playing');
        if (l.attr('deleted')!='true') {
            // delete vid
            $(this).html('Deleting&hellip;');

            $.get("/delete/video/"+l.attr('id')+"/",function (data) {
                if (parseInt(data)==1) {
                    l = $('li.playing');
                    l.attr('deleted','true');
                    l.find('.recording_title').attr('old-title',l.find('.recording_title').html()); // backup title
                    l.find('.recording_title').html('DELETED VIDEO');
                    l.find('.thumbnail').attr('src','/site_media/empty.PNG');
                    select(l.get(0));
                }
            });


        } else {
            // undelete vid
            $.get("/undelete/video/"+l.attr('id')+"/",function (data) {
                if (parseInt(data)==1) {
                    l = $('li.playing');
                    l.find('.recording_title').html(l.find('.recording_title').attr('old-title')); // restore title
                    l.find('.thumbnail').attr('src',l.find('.thumbnail').attr('static'));
                    l.attr('deleted','false');
                    select(l.get(0));
                }
            });
        }
    });

    
});



function select(elem) {
    l = $(elem);
    $('.videoitem').removeClass("playing");
    l.addClass("playing");

    if (l.attr('deleted')=='true') {
       document.getElementById('ply').sendEvent("LOAD",{"file":"","image":""});
       $('.delete').html('&larr; Undo delete');
    } else {
        $('.delete').html('&times; Delete this recording');
        document.getElementById('ply').sendEvent("LOAD",{"file":l.attr('highlight'),"image":l.attr('image')});
        document.getElementById('ply').sendEvent("PLAY","true");
    }

    $('.video_title').html(l.find('.recording_title').html());
    $('.show').attr('href',l.attr('yt_url'));

    $('#description').html(l.attr('description'));


    $('#download_button').attr('href',l.attr('highlight'));

    $('#embed_vid').val("<div id='highlightcam-container' style='width:350px;height: 360px;'>  <div id='highlightcam-player'>        <embed width='332' height='350' flashvars='&file="+l.attr('highlight')+"&image="+l.attr('image')+"&abouttext=HighlightCam&aboutlink="+hostname+"/about/&stretching=uniform&autostart=true&skin="+hostname+"/site_media/skin.swf' wmode='opaque' allowscriptaccess='always' allowfullscreen='true' quality='high' bgcolor='#ffffff' name='ply' id='highlightcamplayer' style='' src='"+hostname+"/site_media/player.swf' type='application/x-shockwave-flash'></embed> </div></div>");
    

}

var currentPosition = 0;
function positionListener(obj) {
    currentPosition = parseInt(obj.position*1000);
}

function watch_original() {
    document.getElementById('ply').sendEvent("PLAY","false");
    $('.show').text('Back to highlights');
    $.get("/jump/"+$('li.playing').attr('id')+"/"+currentPosition+"/",function (jumpPosition) {
        document.getElementById('ply').sendEvent("LOAD",{"file":$('li.playing').attr('original'),"streamer":stream_server,"image":$('li.playing').attr('image'),"start":jumpPosition});
        document.getElementById('ply').sendEvent("PLAY","true");
        $('.show').text('Back to highlights');
    });
}

function watch_highlight() {
    $('.show').text('Show this point in original');
    select($('li.playing').get(0));
}
