﻿/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5-vsdoc.js"/>
/// <reference path="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.12/jquery-ui.js"/>

var currentVideo;

$(function () {
    $("#videos ul li").first().css("margin-top", 0);

    $('#videos a').click(function (event) {
        event.preventDefault();
        getVideo(this.href, true);
        return false;
    });

    var videos = $("#videos ul > li");
    if (videos.length > 0)
        getVideo($(videos[0]).find("a").attr("href"), false);

    $("#videos-container .down").click(function () {
        scrollVideos(-156);
    });
    $("#videos-container .up").click(function () {
        scrollVideos(156);
    });

    $('.album #videos').mousemove(function (e) {
        var s_top = parseInt($(this).offset().top);

        // Roughly calculate the height of the menu by multiply height of a single LI with the total of LIs plus the spacing of 6px between.
        var mheight = parseInt($('#videos li').height() * $('#videos li').length + (($('#videos li').length - 1) * 6));
        var s_bottom = parseInt(mheight - $(this).height());

        // Calculate the top value. This equation is not the perfect, but it's very close.
        var top_value = Math.round(((s_top - e.pageY) / 200) * mheight / 2);
        if (Math.abs(top_value) > s_bottom) top_value = s_bottom * -1;

        // Animate by changing the top value.
        $('#videos ul').animate({ top: top_value }, { queue: false, duration: 500 });
    });

    $("#videos li").hover(function (e) { $(".video-details", this).fadeIn(600); }, function (e) { $(".video-details", this).fadeOut(600); });

    $("#clients a").click(function (event) {
        event.preventDefault();
        getVideo(this.href, true, "showVideo");
        return false;
    });
});

function scrollVideos(i) {
    var top = parseInt($('#videos ul').css("top"));
    if (isNaN(top)) top = 0;
    if (top % 156 != 0) top = top - (top % 156); // Make sure we're showing a full thumbnail.

    // Going down.
    if (i < 0) {
        // Get the full height to make sure we don't scroll too far.
        var mheight = parseInt($('#videos li').height() * $('#videos li').length + (($('#videos li').length - 1) * 6));
        var s_bottom = parseInt(mheight - $("#videos").height());

        if (Math.abs(top) < Math.abs(s_bottom)) top = top + i;
    } else {
        top = top + i;
        if (top > 0) top = 0;
    }

    $("#videos ul").animate({ top: top }, { queue: false, duration: 500 });
}

function getVideo(url, autoplay, callback) {
    if (url != undefined) {
        currentVideo = url;
        if (callback == null) callback = "switchVideo";
        $.getScript("http://vimeo.com/api/oembed.json?api=true&byline=false&title=false&autoplay=" + autoplay + "&portrait=false&height=435&url=" + url + "&callback=" + callback);
    }
}

function switchVideo(video) {
    $("#video .video").html(unescape(video.html).replace("js_api=1", "js_api=1&api=1"));
    $("#video .title").html(unescape(video.title) + " " + unescape(video.description));
    $(".client #video .title").html("<i>playing</i> " + unescape(video.title));

    if (window.location.pathname.indexOf("/client/") === 0 && currentVideo != undefined) {
        var iframe = $("iframe");
        iframe.load(function () {
            $f(iframe[0]).addEvent("finish", function (data) {
                getVideo($("a[href$='" + currentVideo + "']").parent().next().find("a").attr("href"), true);
            });
        });
    }
}

function showVideo(video) {
    $.fancybox({ content: unescape(video.html) });
}
