Shadowbox.loadSkin('classic', 'http://www.elon.edu/campaign/Scripts/shadowbox/skin');
Shadowbox.loadLanguage('en', 'http://www.elon.edu/campaign/Scripts/shadowbox/lang');
Shadowbox.loadPlayer(['flv', 'iframe'], 'http://www.elon.edu/campaign/Scripts/shadowbox/player');

function getPlayer(gid) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[gid];
	} else {
		return document[gid];
	}
};

$(document).ready(function() {
 
    $(".entrydesc").jTruncate( {
        length: 100,
        minTrail: 10,
        moreText: '',
        lessText: ''
    });
    
    $(".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",""));
    var options = {
        flvPlayer:     'http://www.elon.edu/campaign/Scripts/player.swf'
    };
    
    Shadowbox.init(options);
    
    loadVideos();
    checkNextPrev();
    
    var numVideos = 5;
    var rand=Math.floor(Math.random()*numVideos) + 1;
    
    var videoUrl = "";
    var posterUrl = "";
    
    switch(rand)
    {
        case 1:
            videoUrl = "http://www.elon.edu/campaign/Scripts/player.swf?file=http://stream.elon.edu/ur/ever_elon/campaign_themes/ever_elon/A1_danieley_2.flv";
            posterUrl = "http://www.elon.edu/campaign/posters/danieley320240.jpg";
            break;
        case 2:
            videoUrl = "http://www.elon.edu/campaign/Scripts/player.swf?file=http://stream.elon.edu/ur/ever_elon/campaign_themes/ever_elon/A6_gant_1.flv";
            posterUrl = "http://www.elon.edu/campaign/posters/gant320240.jpg";
            break;
        case 3:
            videoUrl = "http://www.elon.edu/campaign/Scripts/player.swf?file=http://stream.elon.edu/ur/ever_elon/campaign_themes/ever_elon/A7_young_2.flv";
            posterUrl = "http://www.elon.edu/campaign/posters/young320240.jpg";
            break;
        case 4:
            videoUrl = "http://www.elon.edu/campaign/Scripts/player.swf?file=http://stream.elon.edu/ur/ever_elon/campaign_themes/ever_elon/A8_lambert_2.flv";
            posterUrl = "http://www.elon.edu/campaign/posters/lambert320240.jpg";
            break;
        case 5:
            videoUrl = "http://www.elon.edu/campaign/Scripts/player.swf?file=http://stream.elon.edu/ur/ever_elon/campaign_themes/ever_elon/A5_mahaffey_1.flv";
            posterUrl = "http://www.elon.edu/campaign/posters/mahaffey320240.jpg";
            break;
    }
	
    var flashvars = false;
    var params = {menu: "false",flashvars: "bufferlength=1.0&overstretch=false&displayheight=320&controlbar=none&autostart=false&image=" + posterUrl + "&skin=http://www.elon.edu/campaign/Scripts/default.swf"};
    var attributes = {id: "top_video_player",name: "top_video_player"};
    swfobject.embedSWF(videoUrl, "top_video_container", "320", "240", "9.0.0","http://www.elon.edu/campaign/Scripts/expressInstall.swf", flashvars, params, attributes);
    
    $("#shadowbox_container").css("display","none");
    
    $("#shadowbox_overlay, #shadowbox_nav_close").click(function(){
        $("#shadowbox_container").css("display","none");
    });
    
    $("#ButtonSubmit").click(function(){
        $("#shadowbox_container").css("display","none");
    });
    
    $("#prev").click(function(){
        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(){
        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;
    });
    
    $("#tab_all").click(function(){
        loadVideos();
        $(this).addClass("tab_active");
        return false;
    });
    
    $("#tab_alumni").click(function(){
        loadVideos("1");
        $(this).addClass("tab_active");
        return false;
    });
    
    $("#tab_facstaff").click(function(){
        loadVideos("2");
        $(this).addClass("tab_active");
        return false;
    });
    
    $("#tab_parent").click(function(){
        loadVideos("4");
        $(this).addClass("tab_active");
        return false;
    });
    
    $("#tab_student").click(function(){
        loadVideos("5");
        $(this).addClass("tab_active");
        return false;
    });
    
    $(".top_video").click(function(){
        var vid = $(this).attr("href");

        var playerRef = getPlayer("top_video_player");
        
        var obj = {file:vid};
        playerRef.sendEvent("LOAD",obj);
        playerRef.sendEvent("PLAY","true");

        return false;
    });
    
    $("#videos_container").click(function(){
        var playerRef = getPlayer("top_video_player");
        playerRef.sendEvent("PLAY","false");
    });
});

function loadVideos(category)
{
    $("#tab_all, #tab_alumni, #tab_facstaff, #tab_parent, #tab_student").removeClass("tab_active");
    var categoryS = ((category) ? "?c=" + category : "");
    $("#videos").css("margin-left","0px");
    checkNextPrev();
    $.ajax({
        type: "GET",
        url: "http://www.elon.edu/campaign/GetVideos.aspx" + categoryS,
        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" rel="shadowbox;width=320;height=240" href="' + video_url + '"><img src="' + thumb_url + '"/>' + '<br />' + name + '</a>';
                    $('<li></li>').html(div).appendTo('#videos');
                });
                Shadowbox.setup($("li > a[rel^=shadowbox]"));
                $(".sb_link, #submit_story_link").click(function(){
                    $("#shadowbox_container").css("display","block");
                });
            });
        },
        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");
    }
}