function getVideoIndex(val) {
    var links = getLinks();
    var index = false;
    jQuery.each(links, function(i, link) {
        if (link.id == val) {
            index = i;
        }
        if (link.url == val) {
            index = i;
        }
    });
    return index;
}

function popLinks(vindex) {
    var links = getLinks();
    var lastindex = links.length - 1;
    jQuery('#PrevVid').unbind("click");
    jQuery('#NextVid').unbind("click");
    if (vindex == 0) {
        if (links[lastindex].type == 'yt') {
            jQuery('#PrevVid').click(function() { loadYouTube(links[lastindex].id, links[lastindex].title); });
            //alert('prev yt: '+links[lastindex].id);
        } else if (links[lastindex].type == 'av') {
            jQuery('#PrevVid').click(function() { loadAppleVideo(links[lastindex].url, 480, 288, links[lastindex].title); });
            //alert('prev av: '+links[lastindex].url);
        } else if (links[lastindex].type == 'uv') {
            jQuery('#PrevVid').click(function() { loadUstreamVideo(links[lastindex].url, links[lastindex].title); });
        }
    } else {
        if (links[vindex-1].type == 'yt') {
            jQuery('#PrevVid').click(function() { loadYouTube(links[vindex-1].id, links[vindex-1].title); });
            //alert('prev yt: '+links[vindex-1].id);
        } else if (links[vindex-1].type == 'av') {
            jQuery('#PrevVid').click(function() { loadAppleVideo(links[vindex-1].url, 480, 288, links[vindex-1].title); });
            //alert('prev av: '+links[vindex-1].url);
        } else if (links[vindex-1].type == 'uv') {
            jQuery('#PrevVid').click(function() { loadUstreamVideo(links[vindex-1].url, links[vindex-1].title); });
        }
    }
    if (vindex == lastindex) {
        if (links[0].type == 'yt') {
            jQuery('#NextVid').click(function() { loadYouTube(links[0].id, links[0].title); });
            //alert('next yt: '+links[0].id);
        } else if (links[0].type == 'av') {
            jQuery('#NextVid').click(function() { loadAppleVideo(links[0].url, 480, 288, links[0].title); });
            //alert('next av: '+links[0].url);
        } else if (links[0].type == 'uv') {
            jQuery('#NextVid').click(function() { loadUstreamVideo(links[0].url, links[0].title); });
        }
    } else {
        if (links[vindex+1].type == 'yt') {
            jQuery('#NextVid').click(function() { loadYouTube(links[vindex+1].id, links[vindex+1].title); });
            //alert('next yt: '+links[vindex+1].id);
        } else if (links[vindex+1].type == 'av') {
            jQuery('#NextVid').click(function() { loadAppleVideo(links[vindex+1].url, 480, 288, links[vindex+1].title); });
            //alert('next av: '+links[vindex+1].url);
        } else if (links[vindex+1].type == 'uv') {
            jQuery('#NextVid').click(function() { loadUstreamVideo(links[vindex+1].url, links[vindex+1].title); });
        }
    }
}

function loadYouTube(id, title) {
    var vidindex = getVideoIndex(id);
    jQuery.ajax({
        type: "POST",
        url: "/index/you-tube/",
        data: "id="+id+"&title="+title,
        beforeSend: function() {
            
        },
        success: function(html){
            jQuery('#VideoContainer').attr('style', 'background: #fff url(/i/video-youtube-bg.jpg);');
            jQuery("#Videos").html(html);
            popLinks(vidindex);
        }
    });
}

function loadAppleVideo(url, width, height, title) {
    var vidindex = getVideoIndex(url);
    jQuery.ajax({
        type: "POST",
        url: "/index/apple-video/",
        data: "url="+url+"&width="+width+"&height="+height+"&title="+title,
        beforeSend: function() {
            
        },
        success: function(html){
            jQuery('#VideoContainer').attr('style', 'background: #fff url(/i/video-applevid-bg.jpg);');
            jQuery("#Videos").html(html);
            popLinks(vidindex);
        }
    });
}

function loadUstreamVideo(url, title) {
    var vidindex = getVideoIndex(url);
    jQuery.ajax({
        type: "POST",
        url: "/index/ustream-video/",
        data: "url="+url+"&title="+title,
        beforeSend: function() {
            
        },
        success: function(html){
            jQuery('#VideoContainer').attr('style', 'background: #fff url(/i/video-youtube-bg.jpg);');
            jQuery("#Videos").html(html);
            popLinks(vidindex);
        }
    });
}

function loadFirstVideo() {
    var links = getLinks();
    if (links[0].type == 'av') {
        loadAppleVideo(links[0].url, 480, 288, links[0].title);
    }
    if (links[0].type == 'yt') {
        loadYouTube(links[0].id, links[0].title);
    }
    if (links[0].type == 'uv') {
        loadUstreamVideo(links[0].url, links[0].title);
    }
}

