var txDur = 1000;
var featuredShow = {
    time: 0,
    featured: 0,
    show_slug: "",
    ep_slug: ""
};
var latestEpisodes = false;
var episodesPerBox = 4; // needs to match operator.php AND noscript.php
var allowPrivate = true;
var baseURL = window.location.href.substring(0, window.location.href.lastIndexOf("/") + 1);



// http://diveintohtml5.org/detect.html
function supports_video() {
    return !!document.createElement('video').canPlayType;
}
function supports_h264_baseline_video() {
    if (!supports_video()) { return false; }
    var v = document.createElement("video");
    return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
}

var videoTagSupport = supports_h264_baseline_video();
var isChrome = navigator.userAgent.indexOf('Chrome') > -1;
var isSafari = navigator.userAgent.indexOf('Apple') > -1;
var isIPad = navigator.userAgent.indexOf("iPad") > -1;
var isIPhone = navigator.userAgent.indexOf("iPhone") > -1;
var isIOS = isIPad || isIPhone;

if (isChrome) {
    videoTagSupport = false;
}
if (isSafari) {
    var isSafari4 = parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf("Version") + 8)) == 4;
    if (isSafari4 && !isIOS) {
        videoTagSupport = false;
    }
}



function videoPlay() {
    if (!$.browser.msie) {
        $("#fade-container").stop().show().animate({ "opacity": "1" }, { duration: txDur / 2 });
    }
    if (videoTagSupport) {
        $(".player .loading-graphic").remove();
    }
}
function videoPause() {
    if (!$.browser.msie) {
        $("#fade-container").stop().animate({ "opacity": "0" }, { duration: txDur / 2, complete: function(){ $(this).hide(); } });
    }
}
function videoStateChange(event){
    if (event.newstate == "PLAYING") {
        videoPlay();
    } else if (event.newstate == "PAUSED" || event.newstate == "IDLE") {
        videoPause();
    }
}
function videoLoaded(event) {
    var player = (typeof(event) == "object") ? $("#" + event.id) : $("#" + event);
    var placeholder = $("#" + player.attr("id").replace("-player", "-placeholder"));
    // vimeo player doesn't fire callbacks if autostart is defined in flashvars, so we manually fire api_play
    // (this means we also have to ensure the vid is still toggled.)
    if (placeholder.hasClass("vimeo")) {
        if (player.hasClass("toggled")) {
            try {
                player[0].api_addEventListener('onPlay', 'videoPlay');
                player[0].api_addEventListener('onPause', 'videoPause');
                player[0].api_addEventListener('onFinish', 'videoPause');
                player[0].api_play();
            } catch(e){}
        }
    } else {
        try {
            player[0].addModelListener("STATE", "videoStateChange");
        } catch(e){}
    }
    player.addClass("loaded");
}



var loadingGraphic = $('<div class="loading-graphic"><img src="/media/loading-static.gif" /><div>LOADING...</div></div>');



function showVideo(placeholder, dir) {
    var bgImgWidth = 960;
    var bgTiles = 4;

    var videoWidth = 600;
    var videoHeight = 338;
    var videoLeft = videoWidth + 25;
    var videoMiddle = 0;
    var videoRight = videoLeft * -1;
    var videoBtm = videoHeight + 25;
    var videoTop = videoBtm * -1;
    
    var start, end, offset;
    
    var player = $("#" + placeholder.attr("id").replace("-placeholder", "-player"));
    if (!player.length) {
        $("#player-container").append('<div class="player" id="' + placeholder.attr("id").replace("-placeholder", "-player") + '"></div>'); // ridiculous ipad overflow bug
        player = $("#" + placeholder.attr("id").replace("-placeholder", "-player"));
    }
    var playBtn = placeholder.children(".play-btn");
    var episodeLink = $("#" + placeholder.attr("id").replace("-placeholder", "-link"));
    var episodeBox = episodeLink.parent(".episode-box");
    var episodes = episodeBox.parent();
    var show = $("#" + episodes.attr("id").replace("-episodes", "") + "-link");
    
    // toggle link
    $(".episode-box .video-link.toggled").removeClass("toggled");
    episodeLink.addClass("toggled");
    
    // reset autoplay
    $(".placeholder.autoplay").not(placeholder).removeClass("autoplay");
    
    // toggle episode box
    if (!episodeBox.hasClass("toggled")) {
        var height = ($(".video-link", episodeBox).length * 25) + ($(".episode-box-link", episodeBox).length * 35);
        
        // animate if show is toggled
        if (show.hasClass("toggled")) {
            $(".episodes, .episode-box").stop(true, true);
            var index = episodeBox.attr("id").split("-");
            index = index[index.length - 1];
            
            var oldEpisodeBox = $(".episode-box.toggled");
            var oldHeight, oldIndex;
            if (oldEpisodeBox.length) {
                oldHeight = ($(".video-link", oldEpisodeBox).length * 25) + ($(".episode-box-link", oldEpisodeBox).length * 35);
                oldIndex = oldEpisodeBox.attr("id").split("-");
                oldIndex = oldIndex[oldIndex.length - 1];
            }
            
            if (index > oldIndex) {
                // up
                start = oldHeight + 15;
                end = "-" + parseInt(oldHeight + 15, 10);
            } else {
                // down
                start = "-" + parseInt(height + 15, 10);
                end = height + 15;
            }
            episodeBox.siblings().not(oldEpisodeBox).css("left", "-100%");
            episodeBox.addClass("toggled").css("top", start + "px").css("left", "0").animate({ top: 0 }, txDur / 2);
            oldEpisodeBox.removeClass("toggled").animate({ top: end + "px" }, txDur / 2);
            episodes.animate({ height: height + "px" }, txDur / 2);
        } else {
            episodeBox.addClass("toggled").css("left", 0).css("top", 0);
            episodeBox.siblings().css("left", "-100%");
            $(".episode-box").not(episodeBox).removeClass("toggled");
            episodes.height(height + "px");
        }
    }
    
    // toggle show
    if (!show.hasClass("toggled")) {
        $("#shows .episodes").stop(true, true);
        if ($("#shows .episodes:visible").length) {
            $("#shows .episodes:visible").slideUp(txDur / 2);
        }
        episodes.animate({ "line-height": 25 }, txDur / 2) // hacky method to delay .5 * dxDur - can't use slideUp() callback
            .slideDown(txDur / 2);
        
        dir = dir ? dir : ($("#shows .show-link.toggled").index("#shows > a") > show.index("#shows > a")) ? "right" : "left";
        
        // toggle bg img
        if (dir == "left") {
            start = 0;
            offset = -1 * bgImgWidth * bgTiles;
        } else {
            start = -1 * bgImgWidth * bgTiles;
            offset = 0;
        }
        $("#bg-img").stop(true, true).css("left", start)
            .animate({ left: offset }, { duration: txDur });
        
        show.addClass("toggled").siblings().removeClass("toggled");
    }
    
    // toggle description
    $("#video-details > .desc").stop(true, true);
    var desc = $("#" + placeholder.attr("id").replace("-placeholder", "-desc"));
    if (!desc.is(":visible")) {
        if ($("#video-details .desc:visible").length) {
            $("#video-details .desc:visible").slideUp(txDur / 2);
        }
        desc.animate({ "line-height": 18 }, txDur / 2).slideDown(txDur / 2); // aforementioned hack
    }
    
    // fade play buttons
    if (playBtn.length) {
        $("#placeholder-container .play-btn").not(playBtn).stop(true, true).fadeOut(txDur / 2);
        playBtn.stop(true, true).show().animate({ height: 75 }, txDur / 2).animate({ "opacity": 0.6 }, txDur / 2);  // aforementioned hack
    }
    
    var toggled = $("#video-browser .toggled");
    var from, to;
    if (placeholder.hasClass("autoplay")) {
        // jump immediately to player (typically from "the latest" links)
        from = toggled;
        to = player;
    } else if (placeholder.hasClass("toggled")) {
        from = placeholder;
        to = player;
        if (placeholder.hasClass("vimeo") && isIOS) {
            // prevent loading player, forward to vimeo
            window.open("http://vimeo.com/" + placeholder.data("vimeo_id"));
            return false;
        }
    } else if (player.hasClass("toggled")) {
        from = player;
        to = placeholder;
    } else {
        from = toggled;
        to = placeholder;
    }        
    from.removeClass("toggled");
    to.addClass("toggled");
    
    var from_id = from.attr("id");
    var to_id = to.attr("id");
    
    // pause all videos
    $.each($(".player.loaded"), function(){
        if ($("#" + $(this).attr("id").replace("-player", "-placeholder")).hasClass("vimeo")) {
            try{ this.api_pause(); } catch(e){}
        } else {
            if ($(this).is("video")) {
                $(this)[0].pause();
            } else {
                try { this.sendEvent("PLAY", false); } catch(e){}
            }
        }
    });
    // unload other videos
    setTimeout(function(){
        $(".player:not(.toggled)").each(function(){
            $(this).remove();
        });
    }, txDur);
    
    // load video
    // (we stagger the video load from the player load so the player exists before animating...)
    if (to == player && !player.hasClass("loaded")) {
        var params = {
            allowscriptaccess: 'always',
            allowfullscreen: 'true',
            wmode: 'transparent'
        };
        var attributes = {
            "class": "player-child"
        };
        var flashvars;
        var playerChild;
        if (placeholder.hasClass("vimeo")) {
            // load vimeo player
            flashvars = {
                clip_id: placeholder.data("vimeo_id"),
                show_portrait: 1,
                show_byline: 1,
                show_title: 1,
                js_api: 1,
                js_onLoad: 'videoLoaded',
                js_swf_id: player.attr("id"),
                color: 'ffffff'
            };
            player.html('<span id="' + player.attr("id").replace("-player", "") + '-flash-object"></span>');
            playerChild = player.children();
            swfobject.embedSWF("http://vimeo.com/moogaloop.swf", playerChild.attr("id"), "600", "338", "9.0.0","expressInstall.swf", flashvars, params, attributes);
        } else {
            if (videoTagSupport) {
                // load <video> player
                player.html('<video controls="controls"></video>'); // nesting doesn't work with iPad, FUCK
                playerChild = player.children();
                playerChild.attr(attributes).bind("playing", videoPlay).bind("pause", videoPause).bind("ended", videoPause);
                
                // (replaceWith workaround for iPad play() bug)
                /*player.html('<span id="' + player.attr("id").replace("-player", "") + '-video-tag"></span>');
                playerChild = player.children();
                playerChild.replaceWith('<video controls="controls"></video>');
                playerChild = player.children();
                playerChild.attr(attributes).bind("playing", videoPlay).bind("pause", videoPause).bind("ended", videoPause);*/
            } else {
                // load jw player
                var ga_id = show.attr("id").replace("-link", ""); // maybe we could use the $.address() value?
                ga_id += "/" + placeholder.attr("id").replace("-placeholder", "").replace(ga_id + "-", "");
                flashvars = {
                    id: player.attr("id"),
                    controlbar: "over",
                    icons: false,
                    autostart: true,
                    playerready: "videoLoaded",
                    plugins: "gapro-1",
                    "gapro.accountid": "UA-12389862-1",
                    "gapro.idstring": ga_id,
                    skin: "/lib/mediaplayer/glow.zip"
                };
                player.html('<span id="' + player.attr("id").replace("-player", "") + '-flash-object"></span>');
                playerChild = player.children();
                swfobject.embedSWF("/lib/mediaplayer/player.swf", playerChild.attr("id"), "600", "338", "9.0.0","expressInstall.swf", flashvars, params, attributes);
            }
            player.append(loadingGraphic);
            
            // get token
            $.getJSON("/operator.php?action=get-video-token", function(result){
                var episode_id = placeholder.data("ep_id");
                var src = '/watch-test?ts=' + result.ts + '&episode_id=' + episode_id + '&itwtoken=' + result.token;
                if (videoTagSupport) {
                    // <video> player
                    playerChild.attr("src", src);
                    playerChild[0].load();
                    playerChild[0].play();
                    player.addClass("loaded");
                } else {
                    // jw player
                    var jwKickstart = function(){
                        try {
                            playerChild[0].sendEvent("load", {'file': src, 'type': 'video' });
                        } catch(e) {
                            setTimeout(jwKickstart, 250);
                        }
                    };
                    setTimeout(jwKickstart, 250);
                }
            });
        }
    } else if (to == player && player.hasClass("loaded")) {
        videoLoaded(player.attr("id"));
    }
        
    // animate
    to = $("#" + to_id);
    from = $("#" + from_id);
    var toggledPlaceholder = toggled.length ? $("#" + toggled.attr("id").replace("-player", "-placeholder")) : $();
    dir = dir ? dir : (toggledPlaceholder.index() > placeholder.index()) ? "down" : "up";
    switch (dir) {
    case "left":
        start = videoLeft + "px";
        end = videoRight + "px";
        break;
    case "right":
        start = videoRight + "px";
        end = videoLeft + "px";
        break;
    case "up":
        start = videoBtm + "px";
        end = videoTop + "px";
        break;
    case "down":
        start = videoTop + "px";
        end = videoBtm + "px";
        break;
    }
    $("#placeholder-container > .placeholder, #player-container > .player").stop(true, true);
    if (dir == "left" || dir == "right") {
        from.animate({ left: end }, { duration: txDur });
        to.show().css("left", start).css("top", videoMiddle)
            .animate({ left: videoMiddle + "px" }, { duration: txDur });
    } else if (dir == "up" || dir == "down") {
        from.animate({ top: end }, { duration: txDur });
        to.show().css("top", start).css("left", videoMiddle)
            .animate({ top: videoMiddle + "px" }, { duration: txDur });
    } else if (dir == "scale") {
        if (from[0] == placeholder[0]) {
            to.css("top", videoMiddle).css("left", videoMiddle).css("z-index", 30);
            from.css("z-index", 35).hide("scale", { percent: 0, origin: ['middle', 'center'] }, txDur / 5);
        } else {
            from.css("z-index", 30);
            to.css("top", videoMiddle).css("left", videoMiddle).css("z-index", 35)
                .show("scale", { percent: 100, origin: ['middle', 'center'] }, txDur / 5, function(){
                    from.css("left", "100%").css("top", "100%");
                });
        }
    }
}



function process_episode(ep) {
    var latestIndex = $.inArray(ep.id, latestEpisodes);
    if (latestIndex > -1) {
        latestEpisodes[latestIndex] = ep;
    }
    
    var time = Date.parse(ep.release);
    if (featuredShow.featured != 1 && (ep.featured == 1 || time > featuredShow.time)) {
        featuredShow.show_slug = ep.show_slug;
        featuredShow.ep_slug = ep.slug;
        featuredShow.time = time;
        featuredShow.featured = ep.featured;
    }
    
    var date = new Date(Date.parse(ep.release));
    ep.release = date = (date.getMonth() + 1) + "." + date.getDate() + "." + String(date.getFullYear()).slice(2, 4);
    
    return ep;
}
function build_video_details(ep) {
    var out = '<div id="' + ep.show_slug + '-' + ep.slug + '-desc" class="desc">';
    out += '<div style="float: left; width: 285px; padding-right: 15px;">';
    out += '<span style="color: #ddd;">' + ep.release + '</span> &ndash; <span>' + ep.desc + '</span>';
    out += '</div>';
    
    out += '<div style="float: left; width: 300px;">';
    out += '<div class="url"><span>COPY LINK:</span> <input type="text" class="url" readonly="readonly" value="http://intothewoods.tv/' + ep.show_slug + '/' + ep.slug + '" /></div>';
    if (ep.vimeo_id == "") {
        embedCode = '<script src=&quot;http://intothewoods.tv/player?show=' + ep.show_slug + '&ep=' + ep.slug + '&id=' + ep.id + '&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;></script>';
        out += '<div class="url"><span>EMBED VIDEO:</span> <input type="text" class="url" readonly="readonly" value="' + embedCode + '" /></div>';
    }
    //out += '<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fintothewoods.tv%2F' + ep.show_slug + '%2F' + ep.slug + '&amp;layout=standard&amp;show_faces=false&amp;width=300&amp;action=like&amp;colorscheme=dark" scrolling="no" frameborder="0" allowTransparency="true" style="border: none; overflow: hidden; width: 300px; height: 23px; visibility: hidden;" onload="this.style.visibility = \'visible\';" class="fb-like-btn"></iframe>';
    out += '</div>';
    out += '</div>';
    $("#video-details").append($(out).hide());
}
function build_video(ep) {
    var out = '<div id="' + ep.show_slug + '-' + ep.slug + '-placeholder" class="placeholder' + (ep.vimeo_id ? ' vimeo' : '') + '" style="background-image: url(\'/media/video-placeholders/' + ep.show_slug + '-' + ep.slug + '.jpg\');">';
    out += '<div class="show-title">' + ep.show_title.toUpperCase() + '</div>';
    out += '<div class="ep-title">';
    out += (ep.number ? '#' + ep.number + ' ' : '');
    out += ep.title.toUpperCase() + '</div>';
    out += '<div class="play-btn"></div>';
    out += '</div>';
    
    var ep_title = "";
    ep_title += ep.number ? " #" + ep.number : "";
    ep_title += " - " + ep.title;
    $(out).appendTo("#placeholder-container")
        .data("show_title", ep.show_title)
        .data("ep_title", ep_title)
        .data("vimeo_id", ep.vimeo_id)
        .data("ep_id", ep.id);
}
function build_episode_link(ep) {
    var out = '<a href="' + ep.show_slug + '/' + ep.slug + '" id="' + ep.show_slug + '-' + ep.slug + '-link" class="video-link"><span>';
    if (ep.number) {
        out += '#' + ep.number + ' ';
    }
    out += ep.title.toUpperCase() + '</span></a>';
    
    var parentBox = $("#" + ep.show_slug + "-episode-box-" + ep.parent_box);    
    var olderLink = $(".episode-box-link.older", parentBox);
    if (olderLink.length) {
        olderLink.before(out);
    } else {
        parentBox.append(out);
    }
}
function add_episode(ep) {
    ep = process_episode(ep);
    build_episode_link(ep);
    build_video(ep);
    build_video_details(ep);
}
function build_episode_box(box) {
    var ep = box.episodes[0];
    var episodes = $("#" + ep.show_slug + "-episodes");
    var out = '<div id="' + ep.show_slug + '-episode-box-' + ep.parent_box + '" class="episode-box" style="left: -100%;">';
    if (box.newer_ep !== false) {
        out += '<a href="' + box.newer_ep + '" class="episode-box-link newer"><span>+' + (episodesPerBox * (ep.parent_box - 1)) + ' NEWER</span></a>';
    }
    if (box.older_ep !== false) {
        out += '<a href="' + box.older_ep + '" class="episode-box-link older"><span>+' + (episodes.data("episodes_total") - (ep.parent_box * episodesPerBox)) + ' OLDER</span></a>';
    }
    out += '</div>';
    episodes.append(out);
    
    $(box.episodes).each(function(){  
        add_episode(this);
    });
}
function build_show_link(show) {
    var out = '<a href="' + show.slug + '" id="' + show.slug + '-link" class="show-link"><span>' + show.title.toUpperCase() + '</span></a>';
    out += '<div id="' + show.slug + '-episodes" class="episodes"></div>';
    $("#shows").append(out);
    $("#" + show.slug + "-episodes").data("episodes_total", show.episodes_total);
}

function build_latest_episode(ep) {
    var out = '<div class="video">';
    out += '<a href="' + ep.show_slug + '/' + ep.slug + '" id="' + ep.show_slug + '-' + ep.slug + '-latest-link" class="latest-link">';
    out += '<div class="date">' + ep.release + '</div>';
    out += '<img class="thumbnail" src="/media/video-placeholders/thumbnails/' + ep.show_slug + '-' + ep.slug + '.jpg" />';
    out += '<div class="play-btn"><img src="/media/play-btn.png" /></div>';
    out += '<div class="ep-desc"><span>' + ep.title.toUpperCase() + '</span></div>';
    out += '</a></div>';
    $("#the-latest").append(out);
}



$(function(){
    // pre-init
    $("#footer, #the-latest-text, #shows").hide();
    $(".episode-box").css("left", "-100%");
    
    // URL handler
    var address = false;
    function addressChange(event, reset){
        if (address) {
            var show_only = false;
            var vid;
            var vid_id = event.pathNames[1];
            var show_id = event.pathNames[0];
            if (event.value == "/") {
                show_id = defaultPage.show_slug;
                vid_id = defaultPage.ep_slug;
            }
            if (!$("#" + show_id + "-link").length) {
                show_id = featuredShow.show_slug;
                vid_id = featuredShow.ep_slug;
            }
            if (!vid_id || reset) {
                show_only = true;
                vid = $("#" + $("#" + show_id + "-episode-box-1").find(".video-link:first").attr("id").replace("-link", "-placeholder"));
            } else if (!$("#" + show_id + "-" + vid_id + "-placeholder").length) {
                // search for episode on server
                $.getJSON("/operator.php?action=find-episode&show_slug=" + show_id + "&slug=" + vid_id + (allowPrivate ? "&allow_private=true" : ""), function(result){
                    $(".episode-box-link .loading").remove();
                    if (result.episodes.length > 0) {
                        build_episode_box(result);
                        reset = false;
                    } else {
                        reset = true;
                    }
                    addressChange({ pathNames: $.address.pathNames(), value: $.address.value() }, reset);
                });
                return false;
            } else {
                vid = $("#" + show_id + "-" + vid_id + "-placeholder");
            }
            $.address.title("INTOTHEWOODS.TV - " + vid.data("show_title") + (show_only ? "" : vid.data("ep_title")));
            //var placeholder = $("#" + vid.attr("id").replace("video-", "") + "-placeholder");
            var player = $("#" + vid.attr("id").replace("-placeholder", "-player"));
            if (!vid.hasClass("toggled") && !player.hasClass("toggled")) {
                showVideo(vid);
            }
        }
        address = event.value;
    }
    $.address.change(addressChange);
    $("#logo-link, .show-link, .video-link, .episode-box-link, .latest-link").live("click", function(){
        // custom link behaviors
        if ($(this).hasClass("episode-box-link")) {
            if (!$(".episodes .video-link[href=" + $(this).attr("href") + "]").length) {
                $(this).append('<span class="loading">...</span>');
            }
        } else if ($(this).hasClass("latest-link")) {
            var id = $(this).attr("id").replace("-latest-link", "");
            var placeholder = $("#" + id + "-placeholder");
            if (placeholder.hasClass("toggled")) {
                placeholder.click();
            } else {
                // "autoplay" in this context means to skip the placeholder
                placeholder.addClass("autoplay");
            }
        }
        
        $.address.value($(this).attr("href").replace(baseURL, ""));
        return false;
    });
    
    // window resize
    function refresh() {
        if ($(window).width() <= 1054) {
            $("#footer, #bg-container, #fade-container").addClass("small-window");
        } else {
            $("#footer, #bg-container, #fade-container").removeClass("small-window");
        }
        var min_height = $("#left-col").outerHeight() > $("#right-col").outerHeight() ? $("#left-col").outerHeight() : $("#right-col").outerHeight();
        if ($(window).height() <= min_height) {
            $("#bg-container, #fade-container").height(min_height);
        } else {
            $("#bg-container, #fade-container").height("100%");
        }
        var btm = -1 * $(window).scrollTop();
        $("#footer").css("bottom", btm);
    }
    if (!isIOS) {
        $(window).resize(refresh);
        $(window).scroll(refresh);
    }
    
    // init routine
    $(window).load(function(){
        $.getJSON("/operator.php?action=get-episodes,get-latest-episodes,get-featured-episode" + (allowPrivate ? "&allow_private=true" : ""), function(result){
            latestEpisodes = result.latest_episodes;
            
            $(result.shows).each(function(showIndex){
                build_show_link(this);
                build_episode_box(this);
            });
            
            $(latestEpisodes).each(function(){  
                build_latest_episode(this);
            });
            
            $("#shows .episodes, #the-latest .play-btn, #the-latest").hide();
                        
            $(window).resize();
            
            addressChange({ pathNames: $.address.pathNames(), value: $.address.value() });
            $("#shows").slideDown(txDur / 2);
            $("#footer, #the-latest-text, #the-latest").slideDown(txDur);
        });
    });
    
    // show/episode links
    $(".video-link, .show-link, .episode-box-link").live("hover", function(){
        $(this).toggleClass("link-hover-state");
    });
    
    // the latest
    $(".latest-link").live("mouseenter", function(){
        $(this).find(".play-btn").stop(true, true).fadeIn(txDur / 4);
    }).live("mouseleave", function(){
        $(this).find(".play-btn").stop(true, true).fadeOut(txDur / 4);
    });
                        
    // video player
    $("#fade-container").click(function(){
        $.each($(".player.loaded"), function(){
            if ($(this).is("video")) {
                $(this)[0].pause();
            } else if ($("#" + $(this).attr("id").replace("-player", "-placeholder")).hasClass("vimeo")) {
                try{ this.api_pause(); } catch(e){}
            } else {
                try{ this.sendEvent("PLAY", "false"); } catch(e){}
            }
        });
    });
    $(".placeholder").live("click", function(){
        showVideo($(this), "scale");
    });
    $(".placeholder").live("mouseenter", function(){
        $(this).children(".play-btn").stop(true, true).animate({ "opacity": 1 }, txDur / 4);
    });
    $(".placeholder").live("mouseleave", function(){
        $(this).children(".play-btn").stop(true, true).animate({ "opacity": 0.6 }, txDur / 4);
    });
    
    $("input.url").live("click", function(){
        $(this).focus().select();
    });
        
    // wampire donation promo
    $("#wampire-promo-link").click(function(){
        $(this).hide();
        $("#donate-btn, #donate-form").show();
        $("#donate-btn").load("/operator.php?action=get-checkout-btn&file_id=0001", function(){
            $(this).find("form").submit(function(){
                var amt = $("#donate-amt").val();
                if (isNaN(amt) || amt === 0) {
                    alert("Amount must be at least 1\u00A2.");
                    $("#donate-amt").focus().select();
                    return false;
                } else {
                    $(this).attr("action", $(this).attr("action") + "&amt=" + amt);
                }
            });
        });
    });
        
    // contact icons
    $("#facebook-icon, #twitter-icon, #rss-icon").hover(function(){
        $(this).stop(true, true).animate({ "opacity": 1 }, txDur / 4);
    }, function(){
        $(this).stop(true, true).animate({ "opacity": 0.6 }, txDur / 4);
    });
});