function switchTabs(tabGroup, paneGroup, button) {
    // hide all galleries
    $(paneGroup).hide();
    // deselect all tabs
    $(tabGroup + ' a').removeClass('selected');

    // show the proper pane (by removing Btn from the tab's id)
    $('#' + button.attr('id').replace('Btn', '')).show();
    // select the tab
    button.addClass('selected');

    // don't scroll
    return false;
}
function initTabs() {
    $('.cast:not(.selected)').hide();
    $('#castButtons a').bind('click', function(ev) {
        hideInitialOverlay('bl');
        return switchTabs('#castButtons', '.cast', $(this));
    });
    
    $('.videoPlaylist:not(.selected)').hide();
    $('#playlistButtons a').bind('click', function(ev) {
        hideInitialOverlay('tr');
        return switchTabs('#playlistButtons', '.videoPlaylist', $(this));
    });
}
function hideInitialOverlay(id) {
    if (id == 'tr') {
        $('#tr').css('overflow-y', 'scroll');
    }
    $('#' + id + ' .initialOverlay').fadeOut(200);
}
function initInlineVideoPlayerHoverAndClick() {
    $('.inlineVideoPlayer').bind('mouseenter', function(ev) {
        var este = $(this);
        if (!(este.children('object, a')).html()) {
            este.prepend('<a href="#" id="playOver"></a>');
        }
        $('#playOver').bind('click', function(e) {
            var id = $(this).parent().parent().attr('id');
            $('#' + id + ' .inlineVideoPlayer').prepend('<div id="replaceMeWithVideo"><p>You need flash.</p></div>');
            swfobject.embedSWF('/media/swf/player_flv_maxi.swf', 'replaceMeWithVideo', '113', '130', '9.0.0', '', false, {FlashVars:'config=/media/swf/intern-flv-conf.txt&flv=/media/flv/interns/' + id + '.flv'}, false);
            return false;
        });
    });
    $('.inlineVideoPlayer').bind('mouseleave', function(ev) {
        $('#playOver').remove();
    });
}
function initInternHover() {
    $('.expandable').bind('mouseenter', function(ev) {
        var e = $(this);
        // was just e
        if ($('.expandable').hasClass('galleryPopUp')) {
            return;
        }
        offset = e.offset();

        e.parent().attr('id', e.attr('id') + 'LiContainer');
        e.addClass('galleryPopUp').appendTo('body').css({
            top: (offset.top) + 'px',
            left: (offset.left) + 'px'
        });

        e.children('.moreInfo').show();

        // BROWSER SPECIFIC HACK (i hate microsoft)
        if (jQuery.browser.msie) {
            e.css({backgroundPosition: 'top left'});
        }
        e.animate({
            width:'103px',
            height:'160px',
            paddingLeft:'10px',
            top: (offset.top - 10) + 'px',
            left: (offset.left - 10) + 'px'
        }, 200);
    });

    $('.expandable').bind('mouseleave', function(ev) {
        var e = $(this);
        ev.preventDefault();
        var id = '#' + e.attr('id');
        // e.children('.inlineVideoPlayer object').remove();
        $(id + ' .inlineVideoPlayer object').remove();
        e.children('.moreInfo').hide();
        e.stop(); // (stop animating)
        e.removeClass('galleryPopUp');
        e.css({
            width: 94,
            height: 130,
            top: 0,
            left: 0,
            paddingLeft: 0
        });
        // BROWSER SPECIFIC HACK (i hate microsoft)
        if (jQuery.browser.msie) {
            e.css({backgroundPosition: 'top center'});
        }
    
        $('#playOver').remove();
    
        e.appendTo('#' + e.attr('id') + 'LiContainer');
        return false;
    });
}
function initSlideControllers() {
    function slideContent(este, delta) {
        delta += '=379px';
        $('#tl').scrollTo(delta, {duration: 500, easing:'easeInOutQuad', axis:'x'});
        return false;
    }
    $('.advance').bind('click', function(e) {
        e.stopPropagation();
        return slideContent(this, '+');
    });
    $('.retreat').bind('click', function(e) {
        e.stopPropagation();
        return slideContent(this, '-');
    });
}
function initTeamHover() {
    $('.team').hover(
        function() {
            $(this).children('.castOver').fadeIn(200).one('click', function(e) {
                este = $(this); // the a element
                team = este.parent();
                team.prepend('<div id="theTeamPlayer"></div>');
                swfobject.embedSWF('/media/swf/player_flv_maxi.swf', 'theTeamPlayer', '379', '130', '9.0.0', '', false, {FlashVars:'config=/media/swf/team-flv-conf.txt&flv=/media/flv/team' + team.attr('id') + '.flv'}, false);
                este.hide();
            });
            $(this).animate(
                { height: '130px' },
                200,
                function(e) {
                    return;
                }
            );
        },
        function() {
            $(this).children('.castOver').unbind().fadeOut(200);
            $(this).children('#theTeamPlayer').remove();
            $(this).animate(
            { height: '88px' },
                200,
                function(e) {
                    $(this).find('img').show();
                }
            );
        }
    );
}
function initInitialOverlay() {
    function initialOverlay(h1, h2) {
        return '<div class="initialOverlay"><h1>' + h1 + '</h1><h2>' + h2 + '</h2></div>';
    }
    $('#tl').prepend(initialOverlay('学ぶ', '番組について'));
    $('#tr').css('overflow', 'hidden').prepend(initialOverlay('見る', 'ビデオを'));
    $('#bl').prepend(initialOverlay('会う', '出演者に'));
    $('#br').prepend(initialOverlay('参加する', 'アロハチャレンジに'));
    $('.contentBox').one('mouseenter',
        function() {
            hideInitialOverlay(this.id);
        }
    );
}
function initVideoPlaylist() {
    $('.videoPlaylist td a:has(img)').lightBox();
    $('.videoPlaylist td a:has(img)').wrapInner('<div class="wrap"></div>');
    $('.videoPlaylist td a:has(img)').hover(
        function() {
            $(this).children('.wrap').append('<div id="playOverlay"></div>');
        },
        function() {
            $('#playOverlay').remove();
        }
    );
    $('.videoPlaylist td a:not(:has(img))').click(function() {
        window.open(this.href);
        return false;
    });
}

function respondToAnchor() {
    var anchor = window.location.hash;
    if (anchor.match(/#video-.+/)) {
        var videoId = anchor.replace('#video-', '');
        
    }
}

function fixLinuxFonts() {
    if (/Linux/.test(navigator.platform)) {
        $('#castButtons').css('margin-top', '255px');
    }
}

$(document).ready(function(e) {
    initTabs();
    initInitialOverlay();
    initInlineVideoPlayerHoverAndClick();
    initSlideControllers();
    initInternHover();
    initTeamHover();
    initVideoPlaylist();
    fixLinuxFonts();
    // wtf
    $('#ui-datepicker-div').remove();
    // respondToAnchor();
});

function onYouTubePlayerReady(playerId) {
    var ytplayer = document.getElementById("myytplayer");
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
    $('#lightbox-container-image-data-box').css({
        backgroundColor: 'transparent',
        opacity: '1'
    });
    $('#ad').fadeIn();
    var hite = $('#ad img').height() + 16;
    if (hite == 16) hite = 60;

    $('#lightbox-container-image-data-box').before($('<div id="transback"></div>').css('height', hite)).css('overflow', 'hidden');
    var adId = $('#ad').data('identifier');
    if (adId) {
        $.get('/tracker/impression/' + adId + '/');
        $('#ad').click(function() {
            $.get('/tracker/click/' + adId + '/');
            ytplayer.pauseVideo();
            window.open(this.href);
            return false;
        });
    }
}

function onytplayerStateChange(newState) {
    if (newState == 0) // video finished
        showInterstitial();
}

function showInterstitial() {
    setTimeout('showInterstitialAfterTimeout()', 250);
}
function showInterstitialAfterTimeout() {
    var player = $('#myytplayer');
    var videoId = player.data('videoId');
    if (!videos[videoId]) return;
    var videoData = videos[videoId];
    player.replaceWith($('<div id="interstitial"></div>'));
    
    if (videoData.next) {
        $('#interstitial')
            .append('<h1 id="next">up next:</h1>')
            .append('<table class="videoPlaylist" id="upNext"><tr></tr></table>');
        $('#upNext tr:last').append($('#' + videoData.next).parent('td').clone(true));
    }
    
    $('#interstitial').append('<h1>you just watched:</h1>')
                .append('<table class="videoPlaylist" id="justWatched"><tr></tr></table>');
    $('#justWatched tr:last').append($('#' + videoId).parent('td').clone(true));
    
    if (videoData.related) {
        $('#interstitial')
            .append('<h1>you might like to watch:</h1>')
            .append('<table class="videoPlaylist" id="relatedVideos"><tr></tr></table>');
    }
    $.each(videoData.related, function(i, relatedId) {
        $('#relatedVideos tr:last').append($('#' + relatedId).parent('td').clone(true));
    });
    $('#interstitial table *').css('borderColor', '#000');
}

var ads = {
    hertz: {
        identifier: 'hertz',
        url: 'http://www.hertz.com/',
        src: '/media/img/logos/hertz.jpg',
        alt: 'Hertz'
    },
    pcc: {
        identifier: 'pcc',
        url: 'http://www.polynesianculturalcenter.com/',
        src: '/media/img/logos/pcc.png',
        alt: 'Polynesian Cultural Center'
    },
    kaukahi: {
        identifier: 'kaukahi',
        url: 'http://www.kaukahi.com/',
        src: '/media/img/logos/kaukahi.jpg',
        alt: 'Kaukahi'
    },
    peterlik: {
        identifier: 'peterlik',
        url: 'http://www.peterlik.com/home.html',
        src: '/media/img/logos/peterlik.jpg',
        alt: 'Peter Lik Gallery'
    },
    midpac: {
        identifier: 'midpac',
        url: 'http://www.midpacpetroleum.com/stationlistings.html',
        src: '/media/img/logos/midpac.jpg',
        alt: 'Mid Pac Petroleum, LLC.'
    },
    pacbio: {
        identifier: 'pacbio',
        url: 'http://www.biodiesel.com/',
        src: '/media/img/logos/pacbio.jpg',
        alt: 'Pacific Biodiesel, Inc.'
    },
    hti: {
        identifier: 'hti',
        url: 'http://www.hti.edu/',
        src: '/media/img/logos/hti.jpg',
        alt: 'Hawaii Technology Institute'
    },
    alohachallenge: {
        identifier: 'alohachallenge',
        url: 'http://www.alohachallenge.tv/media-kit/',
        src: '/media/img/logos/alohachallenge.png',
        alt: 'Advertise with Aloha Challenge!'
    }
}

var videos = {
    'nn8qkTKXpvk': {
        next: null,
        ad: null,
        related: ['UmA2t5focy4', '-0LNSoRwg-Y']
    },
    'UmA2t5focy4': {
        next: null,
        ad: null,
        related: ['nn8qkTKXpvk', '-0LNSoRwg-Y']
    },
    '-0LNSoRwg-Y': {
        next: null,
        ad: null,
        related: ['nn8qkTKXpvk', 'UmA2t5focy4']
    },
    /* ep 2 promo: yq5FX2cgSYY */
    'yq5FX2cgSYY': {
        next: null,
        ad: null,
        related: ['nn8qkTKXpvk', 'UmA2t5focy4', '-0LNSoRwg-Y']
    },
    /* ep 3 promo: E48Jb2HQBH4 */
    /* team intros */
    'ztmrs4aTknk': {
        next: 'D0HGbM_mqCo',
        ad: null,
        related: ['D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    'D0HGbM_mqCo': {
        next: 'l6XZnttLQG0',
        ad: null,
        related: ['ztmrs4aTknk', 'ErG5x6N2ayM']
    },
    'ErG5x6N2ayM': {
        next: null,
        ad: null,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo']
    },
    /* round 1 films */
    '9N0z_QvnhAE': {
        next: null,
        ad: null,
        related: ['2etBF9X7KBU', 'HqJz8Ye090I']
    },
    '2etBF9X7KBU': {
        next: null,
        ad: null,
        related: ['9N0z_QvnhAE', 'HqJz8Ye090I']
    },
    'HqJz8Ye090I': {
        next: null,
        ad: null,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU']
    },
    /* round 3 films */
    'jrT0x7G_9PA': {
        next: '5q-iCGslS6Q',
        ad: null,
        related: ['5q-iCGslS6Q', 'rlh2PDBq7vc']
    },
    '5q-iCGslS6Q': {
        next: 'rlh2PDBq7vc',
        ad: null,
        related: ['jrT0x7G_9PA', 'rlh2PDBq7vc']
    },
    'rlh2PDBq7vc': {
        next: null,
        ad: null,
        related: ['5q-iCGslS6Q', 'jrT0x7G_9PA']
    },
    /* 4 */
    'SI2rsSm9ZS0': {
        next: null,
        ad: null,
        related: ['Wfny-At4RRg', 'spV8Wmh5ino']
    },
    'Wfny-At4RRg': {
        next: null,
        ad: null,
        related: ['SI2rsSm9ZS0', 'spV8Wmh5ino']
    },
    'spV8Wmh5ino': {
        next: null,
        ad: null,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg']
    },

    /* episode 1 */
    'PHnKDNhgsuw': {
        next: 'l6XZnttLQG0',
        ad: ads.alohachallenge,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    'l6XZnttLQG0': {
        next: 'CVmoa681EAM',
        ad: ads.pcc,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    'CVmoa681EAM': {
        next: 'TKS_c9iYjZc',
        ad: ads.hertz,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    'TKS_c9iYjZc': {
        next: 'HZzlD6Bw0ok',
        ad: ads.peterlik,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    'HZzlD6Bw0ok': {
        next: '2ISNzKgwEzQ',
        ad: ads.kaukahi,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    '2ISNzKgwEzQ': {
        next: null,
        ad: ads.pcc,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    /* episode 2 */
    'omkYxCD1PTI': {
        next: 'y9OHJpslAo8',
        ad: ads.alohachallenge,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU', 'HqJz8Ye090I']
    },
    'y9OHJpslAo8': {
        next: 'M9czVcXrHJc',
        ad: ads.pcc,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU', 'HqJz8Ye090I']
    },
    'M9czVcXrHJc': {
        next: 'fK4hioK147U',
        ad: ads.pacbio,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU', 'HqJz8Ye090I']
    },
    'fK4hioK147U': {
        next: 'eFX5X80JGLk',
        ad: ads.hertz,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU', 'HqJz8Ye090I']
    },
    'eFX5X80JGLk': {
        next: null,
        ad: ads.kaukahi,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU', 'HqJz8Ye090I']
    },
    /* episode 3 */
    'tKhYSBsOSiM': {
        next: 'BSMLLWe7G2k',
        ad: ads.alohachallenge,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'BSMLLWe7G2k': {
        next: 'SH9BlXrgUgg',
        ad: ads.hti,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'SH9BlXrgUgg': {
        next: '2b5pqJ-yQuc',
        ad: ads.hertz,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    '2b5pqJ-yQuc': {
        next: 'HA_LH-pwMmA',
        ad: ads.pcc,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'HA_LH-pwMmA': {
        next: null,
        ad: ads.pacbio,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    /* episode 4 */
    'dpErCtY7-20': {
        next: 'alrxWzXtgTk',
        ad: ads.alohachallenge,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'alrxWzXtgTk': {
        next: 'mKnSDaBDAVM',
        ad: ads.pcc,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'mKnSDaBDAVM': {
        next: 'av-o2y_EGwQ',
        ad: ads.hti,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'av-o2y_EGwQ': {
        next: '-iTkl8cJndQ',
        ad: ads.pacbio,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    '-iTkl8cJndQ': {
        next: null,
        ad: ads.kaukahi,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    /* episode 5 */
    '_46jtwb8K5g': {
        next: 'FmHnD9TLFSg',
        ad: ads.alohachallenge,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'FmHnD9TLFSg': {
        next: 'WQKtMZbR0Vc',
        ad: ads.pcc,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'WQKtMZbR0Vc': {
        next: 'G6NNyGuW8VQ',
        ad: ads.hti,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'G6NNyGuW8VQ': {
        next: 'S1Zw64nDjF0',
        ad: ads.hertz,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'S1Zw64nDjF0': {
        next: null,
        ad: ads.midpac,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    
    'gS74TwCTCwc': {
        next: 'FDr8yWj9sWE',
        ad: ads.alohachallenge,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'FDr8yWj9sWE': {
        next: 'Abxi9dPqc7A',
        ad: ads.pcc,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'Abxi9dPqc7A': {
        next: 'W-VFpBDEXkg',
        ad: ads.hti,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'W-VFpBDEXkg': {
        next: 'aZT3ySmJuCY',
        ad: ads.hertz,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'aZT3ySmJuCY': {
        next: null,
        ad: ads.pacbio,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },

    'PJ68JN44BXw': {
        next: 'T95y0ZdbZOQ',
        ad: ads.alohachallenge,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg', 'spV8Wmh5ino']
    },
    'T95y0ZdbZOQ': {
        next: 'EkDvSsK5y-w',
        ad: ads.pcc,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg', 'spV8Wmh5ino']
    },
    'EkDvSsK5y-w': {
        next: 'iT84UdTuMSk',
        ad: ads.hti,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg', 'spV8Wmh5ino']
    },
    'iT84UdTuMSk': {
        next: '6uEeW45vQds',
        ad: ads.hertz,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg', 'spV8Wmh5ino']
    },
    '6uEeW45vQds': {
        next: null,
        ad: ads.pacbio,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg', 'spV8Wmh5ino']
    }
};
