﻿$(function() {
    var bioCtr = 0;

    $(".closeWindow, .fancy_close").click(function() { 
        $(".site_bioWrapper:visible").trigger("click");
        return false;
    });

    $(".site_bodyCover").fadeTo(1, 0.01).css("height", $("#site_wrapper").height() + "px").hide().click(function() {
        $(".site_bioWrapper:visible").trigger("click");
    });
    
    $(".site_bioWrapper").hide().click(function() {
        $(this).fadeOut("slow");
        $(".site_bodyCover").fadeTo("slow", 0.01, function() {
            $(this).hide();
        });
    });

    $(".site_bioContainer").click(function(e) {
        return false;
    });

    $(".site_bioContainer a").click(function(e) {
	open($(this).attr("href"), "_blank");
    });

    $("#site_team ol li a").each(function() {
        $(this).data("idx", bioCtr);
        bioCtr++;
    }).click(function() {
        window.scrollTo(0,0);
    
        var idx = $(this).data("idx");
        
        if ($(".site_bioWrapper").length > idx) {
            $(".site_bodyCover").show().fadeTo("slow", 0.85);
            $(".site_bioWrapper").eq(idx).fadeIn("slow", function() {
                try { this.style.removeAttribute('filter'); }
                catch(e) { }
            });
        }
        
        return false;
    });
    
    setNews();
    setTwitter();
    
    $("#site_news .content_listing, #site_twitter .content_listing, #site_blogs .content_listing, .site_twitterListContainer .content_listing").each(function() {
        $(this).children(":odd").addClass("alt");
    });
    
    setFeatures();
});

function setFeatures() {
    $("#vidPrev, #vidNext").click(function() { 
       var curChild = $("#vids").children("div:visible");

       try {
           var swfObj = curChild.find("object[id*=flvLink], embed[id*=flvLink]");
           
           if (swfObj.length > 0) {
                swfObj = swfObj.eq(0);
                swfObj.outerHTML(gCurrentFLVClick);
                bindFLVLinks();
           } else {
                swfObj = curChild.find("object, embed")
                
                if (swfObj.length > 0) {
                    var myId = swfObj.attr("id");
                    var ytplayer = document.getElementById(myId);
                    ytplayer.pauseVideo();
                }
           }

       } catch(e) {  }
    });

    $("#vids").slideshow(0, 0, "vidPrev", "vidNext");
}

function setNews() {
    var firstNews = $("#site_news .content_listing li:first");
    $("#site_news .site_listingFeatured").each(function() {
        var a = firstNews.find("a");
        var em = a.find("em");
        
        try {
            $(this).find("h4 a").attr("href", a.attr("href")).text(a.text().replace(" " + em.text(), ""));
            $(this).find(".content_listingMeta").html("<strong>" + em.text().split(" ")[0] + "</strong>");
        } catch(e) { }
    });
    firstNews.parent().remove();
}

function setTwitter() {
    var firstTweet = $("#site_twitter .content_listing li:first, .site_twitterListContainer .content_listing li:first").eq(0);
    $("#site_twitter .site_listingFeatured, .site_twitterListContainer .site_listingFeatured").each(function() {
        var a = firstTweet.find("a").eq(0);
        var em = firstTweet.find("em");
        var img = firstTweet.find("img");
        var user = firstTweet.find("strong");
        var tweet = firstTweet.find("span");

        $(this).find(".content_profileImage").attr("href", a.attr("href")).attr("target", "_blank").find("img").attr("alt", user.text()).attr("src", img.attr("src"));        
        $(this).find("h4").html(tweet.html());
        $(this).find(".content_listingMeta").html("<strong>" + user.text() + "</strong> - " + em.text());
        
        var tweetText = tweet.text();
        if (tweetText.indexOf("RT ") == 0) {
            tweetText ="RT @" + user.text() + " " + tweetText.substring(3);
        } else {
            tweetText = "RT @" + user.text() + " " + tweetText;
        }
        
        var rtLink = "http://twitter.com/home/?status=" + URLEncode(tweetText);
        $(this).find(".content_retweet").attr("target", "_blank").attr("href", rtLink);
    });
    firstTweet.remove();

    $("#site_twitter .content_listing li, .site_twitterListContainer .content_listing li").each(function() {
        var img = $(this).find("img").attr("src");
        var user = $(this).find("a:first");
        
        $(this).find("img").remove();
        
        var newhtml = '<img src="' + img + '" /><span>' + $(this).html() + '</span>';
        $(this).html(newhtml);
    });
}
