﻿$(function () {
    $("div.box:not(:has(td img))").addClass("noimg").filter(":odd").addClass("alt");

    $(".story").click(function () {

        closeStory();

        $(this)
			.hide()
			.clone()
			.data("icon", this)
			.hide()
			.attr("class", "story-detail")
			.addClass("block")
			.insertAfter($(this))
			.fadeIn("fast");

        $(".story-detail h3").addClass("block");
        $(".story-detail .story-video").show();

        var detailUrl = encodeURIComponent("http://" + window.location.host + $(this).attr("data-url"));
        var src = "http://www.facebook.com/plugins/like.php?href=" + detailUrl + "&amp;layout=standard&amp;show_faces=true&amp;width=320&amp;action=like&amp;font=arial&amp;colorscheme=light";
        var iframe = $('<iframe src="' + src + '" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:320px; height:50px"></iframe>');
        iframe.appendTo(".story-detail .like-frame");
    });


    $(".story-video").live("click", function (e) {
        e.preventDefault();

        var sd = $(this).closest(".story-detail");
        var id = $(this).attr("data-id");
        var watchUrl = $(this).attr("href");
        if (id && watchUrl) {
            var video = findVideo(watchUrl);
            if (video) {
                var vid = 'vid-' + id;
                var html = "<div class='story-video-player'><div id='" + vid + "' "
                            + "style='width:" + video.Width + "px;height:" + video.Height + "px;'>"
                            + "</div><span class='close'></span></div>";
                $("table", sd).before(html);

                flashembed(vid, video.SourceUrl, {
                    menu: "false",
                    allowFullScreen: true,
                    allowscriptaccess: "always"
                });

                $(this).hide();
            }
        }

        return false;
    });


    $(".story-detail .close").live("click", closeStory);
});

function closeStory() {
    var d = $(".story-detail");
    if (d) {
        $(d.data("icon")).show();
        d.remove();
    }
}

function findVideo(watchUrl) {
    var index = -1;
    $.each(videos, function (i, v) {
        if (v.WatchUrl == watchUrl) {
            index = i;
        }
    });
    return videos[index];
}
