jQuery(document).ready(function($) {
    $('a[rel*=external]').livequery(function() {
        $('a[rel*=external]').attr('target', 'external')
    })
    
    $('#content section:last-child, #team ul:not(.social) li:nth-child(2n)').addClass('last')
    
    var source = ''
    if ($('body').hasClass('home'))
        source = 'http://gdata.youtube.com/feeds/api/playlists/AA821F2D7F066FBD?v=2&alt=jsonc&max-results=4'
    else if ($('body').hasClass('clients'))
        source = 'http://gdata.youtube.com/feeds/api/playlists/58D80B3223A2E52D?v=2&alt=jsonc&max-results=4'
    else if ($('body').hasClass('future'))
        source = 'http://gdata.youtube.com/feeds/api/playlists/0D519B18EE674985?v=2&alt=jsonc&max-results=6'
        
    $('.future #videoWrapper').css('width', 60 * 16 * 6)
        
    getLatestVideos(source)
    getInterviews()
    
    $('#ms2ubetv footer').fadeIn('fast').removeClass('hide')
    $('#ms2ubetv footer ul a').click(function() {
        var currentIndex = $('#ms2ubetv footer ul a').index($('#ms2ubetv footer ul a.selected'))
        
        var ytplayer = document.getElementById('ytplayer' + currentIndex)
        ytplayer.pauseVideo()
        
        $('#ms2ubetv footer ul a').removeClass('selected')
        $(this).addClass('selected')
        
        var index = $('#ms2ubetv footer ul a').index(this)
        
        var delta = index * -960;
        
        $('#videoWrapper').animate({
            marginLeft: delta
        }, 500, 'swing')
    })
    
    $('#latestTweets .replace').replaceWith('<div class="twitterInfo"><h3></h3><img><p></p></div><ul></ul>')
    getLatestTweets(3)
    
    $('#mainNav li.current_page_item').addClass('active')
    $('#mainNav li:not(.active)').hover(function() {
        $(this).animate({ paddingTop: '20px' }, 500, 'swing')
    }, function() {
        $(this).animate({ paddingTop: 0 }, 500, 'swing')
    })
    
    $('#clientList ul li:nth-child(3n)').addClass('last')
    $('#clientList a').each(function() {
        $(this).attr('title', $(this).text())
    })
    
    $('#interviews .vidnav a').click(function() {
        var currentIndex = $('#interviews .vidnav a').index($('#interviews .vidnav a.selected'))
        var index = $('#interviews .vidnav a').index(this)
        
        var iplayer = document.getElementById('iplayer' + currentIndex)
        iplayer.pauseVideo()
        
        $('#interviews .vidnav a').removeClass('selected')
        $(this).addClass('selected')
        
        $('#interviews .interview').stop(true, true).fadeOut(500)
        $('#interviews .interview').eq(index).delay(750).fadeIn('slow')
    })
})

getInterviews = function() {
    var source = 'http://gdata.youtube.com/feeds/api/playlists/2FD4F30D75EF4ABB?v=2&alt=jsonc&max-results=4'
    
    $.getJSON(source + '&callback=?',
        function(data) {
            $.each(data.data.items, function(i, item) {
                $('#interviews .videos').append('<div class="interview hide"><div class="flash"></div><h2></h2></div>')
                $('#interviews .interview h2').eq(i).text(item.video.title)
                $('#interviews .interview .flash').eq(i).flash({
                    swf: 'http://www.youtube.com/v/' + item.video.id  + '?enablejsapi=1&version=3&playerapiid=iplayer' + i,
                    id: 'iplayer' + i,
                    height: 228,
                    width: 304,
                    params: {
                        allowFullScreen: true,
                        allowScriptAccess: 'always'
                    }
                })
            })
        }
    )
}

function getLatestVideos( source ) {
    $.getJSON(source + '&callback=?',
        function(data) {
            $.each(data.data.items, function(i, item) {
                $('#videoWrapper').append('<div class="video" class="hide"><div class="thumb"></div><div class="info"><h1><a rel="external youtube"></a></h1><p></p></div></div>')
                
                $('#videoWrapper .video .thumb').eq(i).flash({
                    swf: 'http://www.youtube.com/v/' + item.video.id  + '?enablejsapi=1&version=3&playerapiid=ytplayer'+i,
                    id: 'ytplayer' + i,
                    height: 480,
                    width: 640,
                    params: {
                        allowFullScreen: true,
                        allowScriptAccess: 'always'
                    }
                })
                //$('#videoWrapper .video:not(.can30) img').eq(i).attr('src', item.video.thumbnail['hqDefault'])
                $('#videoWrapper .video:not(.can30) h1 a').eq(i).text(item.video.title).attr('href', item.video.player['default']).attr('title', item.video.title)
                //$('#videoWrapper .video:not(.can30) .ceebox').eq(i).attr('href', item.video.player['default']).attr('title', item.video.title)
                $('#videoWrapper .video:not(.can30) p').eq(i).text(item.video.description)
                $('#videoWrapper .video:not(.can30) .info').eq(i)
                    .append('<div class="share"><iframe src="http://www.facebook.com/widgets/like.php?href=' + 
                        escape(item.video.player['default']) + '" scrolling="no" frameborder="0" style="border:none; width:60px; height:25px"></iframe>' + 
                        '<a class="twitter" href="http://twitter.com/home?status=Just watched the %23ms2ubetv video ' + escape('"' + item.video.title + '"') + 
                        ' http://youtu.be/' + item.video.id + '" title="Share on Twitter" rel="external twitter">Share on Twitter</a></div>')
                $('#videoWrapper .video:not(.can30)').eq(i).fadeIn('fast')
            })
        }
    )
    
    $('#videoWrapper').fadeIn('slow').removeClass('hide')
    $('.ceebox').ceebox()
}

function getLatestTweets(num) {
    $('#latestTweets ul').empty()
    $.getJSON('http://twitter.com/status/user_timeline/ms2weathergirl.json?count=' + num + '&callback=?', function(data) {
        $('#latestTweets .twitterInfo img').attr('src', data[0]['user']['profile_image_url'])
        $('#latestTweets .twitterInfo h3')
            .html(data[0]['user']['name'] + ' [<a href="http://twitter.com/' + data[0]['user']['screen_name'] +'" rel="external social">@' + data[0]['user']['screen_name'] + '</a>]')
        $('#latestTweets .twitterInfo p').text(data[0]['user']['description'])
        
        $.each(data, function(i, tweet) {
            $('#latestTweets ul').append('<li class="hide"><p></p><time></time></li>')
            $('#latestTweets ul img').eq(i)
            $('#latestTweets ul p').eq(i).text(tweet['text'])
            $('#latestTweets ul time').eq(i).text(tweet['created_at'].substr(0,19))
            $('#latestTweets ul li').eq(i).fadeIn('slow')
        })
    })
}
