var randomScroller;

var videos;

function buildVideoObject(url) {
    var object;

    if (!Browser.Platform.win) {
        var options = 'autoplay="true" controller="true"';
        if (!url) {
            return "<img src='../images/watchingblurb.png' width='400' height='340' />";
        }
        object = "<embed id='qt' src='" + url + "' width='400' height='316' type='video/quicktime' " + options + "></embed>";
    }
    else {
        if (!url) {
            return "<img src='../images/watchingblurb.png' width='400' height='340' />";
        }
        
        object = '<param name="enableContextMenu" value="true" /><param name="stretchToFit" value="true" /><param name="autostart" value="true" /><param name="URL" value="' + url + '" /><param name="ShowStatusBar" value="False" /><param name="uiMode" value="full" /></object>';
        if (Browser.Engine.trident) {
            object = '<object id="wmp" width="400" height="340" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6">' + object;
        }
        else {
            object = '<object id="wmp" type="video/x-ms-asf" data="' + url + '" width="400" height="340">' + object;
        }
    }

    return object;
}

function createList(sortBy) {
    if (!sortBy) {
        sortBy = $("sort_by_title").checked ? "title" : "name";
    }

    var list = videos.getValues();
    list = list.sort(function(a, b) {
        var ak = a[sortBy];
        var bk = b[sortBy];
        if (ak < bk) {
            return -1;
        }
        else if (ak > bk) {
            return 1;
        }
        return 0;
    });

    var ul = $("video_list");
    ul.empty();
    $A(list).each(function(video) {
        var html = "<div class='thumbnail'>" +
            "<a href='#' onclick='playVideo(\"" + video.id + "\"); return false'><img src='../thumbnails/" + video.id + ".jpg' width='100' height='100' /></a></div>" +
            "<div class='description'><h3><a href='#' onclick='playVideo(\"" + video.id + "\"); return false'>" + video.title + "</a></h3>" +
            "<div class='name'>" + video.name + (video.experience ? (" (Experience: " + video.experience + ")") : "") + "</div>" +
            "<div class='blurb'>" + video.blurb + "</div></div>" +
            "<div style='clear: left'></div>";
            
        ul.grab(new Element("li", { html: html }), "bottom");
    });
}

function createPlayer() {
    var list = $("random_list");
    list.empty();
    
    var randomVideos = $H(videos).getValues().sort(function(a, b) { return Math.random() * 10 - 5; });

    $A(randomVideos).each(function(video) {
        list.grab(new Element("li", { html: "<a href='#' title='" + video.title + "' onclick='playVideo(\"" + video.id + "\"); return false;'><img src='../thumbnails/" + video.id + ".jpg' width='100' height='100' /></a>" }));
    });
}

function onMetadata(data) {
    videos = $H(data);

    createList();
    createPlayer();
}

function playVideo(id) {
    scroller.toElement("watch");
    $("player").setStyle("display", "block");
    $("results").setStyle("display", "none");
    if (!Browser.Platform.win) {
        var url = "http://getanimated.iinet.net.au/videos/" + id + ".mov";
        $("player").set("html", buildVideoObject(url));
    }
    else {
        var url = "mms://openmms.iinet.net.au/mms/getanimated/high/getanimated-" + id + ".asf";
        $("player").set("html", buildVideoObject(url));
    }
    $("author").set("text", videos[id].name);
    $("title").set("text", videos[id].title);
}

function scrollDown() {
    randomScroller.start(0, $("random_list").getScroll().y + 104);
}

function scrollUp() {
    randomScroller.start(0, $("random_list").getScroll().y - 104);
}

$(window).addEvent("domready", function() {
    new Request.JSON({
        url: "/getanimated/player/metadata.json",
        onComplete: onMetadata
    }).get();

    randomScroller = new Fx.Scroll("random_list");

    if (Browser.Platform.win && Browser.Engine.gecko) {
        // Shamelessly ripped off from my BPL code.
        var foundPlugin = false;
        $A(navigator.plugins).each(function(plugin) { if (plugin.filename == "np-mswmp.dll") foundPlugin = true; });
        if (!foundPlugin) {
            $("player").setStyle("display", "none");
            $("results").setStyle("display", "block");
        }
        else {
            $("player").set("html", buildVideoObject());
        }
    }
    else {
        $("player").set("html", buildVideoObject());
    }
});

// vim: set cin ai ts=4 sw=4 et:
