var moving = false;

$(document).ready(function() {
    
    $(".entrydesc").each(function(){
        var content = $(this).html();
        content = content.replace(/[&]lt;br[/][&]gt;/g,"<br/>");
        $(this).html(content);
    });

    var width = parseInt($("#videos_container").css("width").replace("px",""));
    
    loadVideos();
    checkNextPrev();
    
    /*
    $("#shadowbox_container").css("display","none");
    
    $("#shadowbox_overlay, #shadowbox_nav_close").click(function(){
        $("#shadowbox_container").css("display","none");
    });*/
    
    $("#prev").click(function(){
        if (moving == false) {
            moving = true;
            var amount = parseInt($("#videos").css("margin-left").replace("px","")) + width;
            $("#videos").animate({marginLeft: amount + "px"}, 700, null, function(){
                $("#videos").css("margin-left",amount + "px");
                checkNextPrev(width);
            });
        }
        return false;
    });
    
    $("#next").click(function(){
        if (moving == false) {
            moving = true;
            var amount = parseInt($("#videos").css("margin-left").replace("px","")) - width;
            $("#videos").animate({marginLeft: amount + "px"}, 700, null, function(){
                $("#videos").css("margin-left",amount + "px");
                checkNextPrev(width);
            });
        }
        return false;
    });
    
});

function loadVideos(category)
{
    
    var categoryS = ((category) ? "?c=" + category : "");
    $("#videos").css("margin-left","0px");
    checkNextPrev();
    $.ajax({
        type: "GET",
        url: "http://www.elon.edu/shell/js/alumni/alumnilinks.xml",
        dataType: "xml",
        success: function(xml) {
            $("#videos").html("");
            
            $(xml).find('videos').each(function() {
                $(this).find('video').each(function() {
                
                    var id = $(this).attr('id');
                    var category = $(this).attr('category');
                    var name = $(this).find('name').text();
                    var video_url = $(this).find('video_url').text();
                    var thumb_url = $(this).find('thumb_url').text();
                    
                    var div = '<a class="sb_link" href="' + video_url + '"><img src="' + thumb_url + '"/>' + '<br />' + name + '</a>';
                    $('<li></li>').html(div).appendTo('#videos');
                });
                
            });
        },
        error: function(xhr, message, ex) {
            alert("Please try again. Error: " + message);
        }
    });
}

function checkNextPrev(width)
{
    var nextVis = false;
    var prevVis = false;
    var marginLeft = parseInt($("#videos").css("margin-left").replace("px",""));
    var numElements = $("#videos > li").size();

    if ((numElements / 4) * width < ((marginLeft * -1) + width))
    {
        nextVis = false;
    }
    else
    {
        nextVis = true;
    }
    
    if (marginLeft == 0)
    {
        prevVis = false;
        nextVis = true;
    }
    else
    {
        prevVis = true;
    }
    
    if (prevVis)
    {
        $("#prev").show("fast");
    }
    else
    {
        $("#prev").hide("fast");
    }
    
    if (nextVis)
    {
        $("#next").show("fast");
    }
    else
    {
        $("#next").hide("fast");
    }
    
    moving = false;
}