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('learn', 'about the show'));
    $('#tr').css('overflow', 'hidden').prepend(initialOverlay('watch', 'the videos'));
    $('#bl').prepend(initialOverlay('meet', 'the cast'));
    $('#br').prepend(initialOverlay('join', 'aloha challenge'));
    $('.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: 'vUFu32KzTlM',
        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 */
    'Kf_JWnaLqV0': {
        next: 'vUFu32KzTlM',
        ad: ads.alohachallenge,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    'vUFu32KzTlM': {
        next: '28u8WU41l2A',
        ad: ads.pcc,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    '28u8WU41l2A': {
        next: '1tgIxzlKn3k',
        ad: ads.hertz,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    '1tgIxzlKn3k': {
        next: 'aZtJfT2s_oE',
        ad: ads.peterlik,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    'aZtJfT2s_oE': {
        next: '3XKzsU-I2u0',
        ad: ads.kaukahi,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    '3XKzsU-I2u0': {
        next: null,
        ad: ads.pcc,
        related: ['ztmrs4aTknk', 'D0HGbM_mqCo', 'ErG5x6N2ayM']
    },
    /* episode 2 */
    'GnVxnRqzkUg': {
        next: 'YyBoLCqLRvM',
        ad: ads.alohachallenge,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU', 'HqJz8Ye090I']
    },
    'YyBoLCqLRvM': {
        next: 'cOPdSIQaD1E',
        ad: ads.pcc,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU', 'HqJz8Ye090I']
    },
    'cOPdSIQaD1E': {
        next: 'DfMYxEQIKB8',
        ad: ads.pacbio,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU', 'HqJz8Ye090I']
    },
    'DfMYxEQIKB8': {
        next: '2WOzcxQLwCY',
        ad: ads.hertz,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU', 'HqJz8Ye090I']
    },
    '2WOzcxQLwCY': {
        next: null,
        ad: ads.kaukahi,
        related: ['9N0z_QvnhAE', '2etBF9X7KBU', 'HqJz8Ye090I']
    },
    /* episode 3 */
    'v-Tbdl21P8w': {
        next: 'dmJyprpkmNU',
        ad: ads.alohachallenge,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'dmJyprpkmNU': {
        next: 'j1Y0JFaff60',
        ad: ads.hti,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'j1Y0JFaff60': {
        next: 'n07fNJyqAhI',
        ad: ads.hertz,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'n07fNJyqAhI': {
        next: 'cC3EhhC3MT4',
        ad: ads.pcc,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'cC3EhhC3MT4': {
        next: null,
        ad: ads.pacbio,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    /* episode 4 */
    '-RJSkZjueo8': {
        next: 'RcUxb6pO0Vg',
        ad: ads.alohachallenge,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'RcUxb6pO0Vg': {
        next: 'Jpwpdmoer_g',
        ad: ads.pcc,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'Jpwpdmoer_g': {
        next: 'AcSvdrgCuyw',
        ad: ads.hti,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'AcSvdrgCuyw': {
        next: 'b1N5_PpaBp4',
        ad: ads.pacbio,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'b1N5_PpaBp4': {
        next: null,
        ad: ads.kaukahi,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    /* episode 5 */
    '9_jxyZ6XFpQ': {
        next: 'qsLiGwom2-c',
        ad: ads.alohachallenge,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'qsLiGwom2-c': {
        next: 'vOHoSKApsCA',
        ad: ads.pcc,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'vOHoSKApsCA': {
        next: 'MDfIg03ragA',
        ad: ads.hti,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'MDfIg03ragA': {
        next: 'BWi7pRoYByI',
        ad: ads.hertz,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'BWi7pRoYByI': {
        next: null,
        ad: ads.midpac,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    
    '8L4t0jMR_N4': {
        next: 'YYIRjqXkQGI',
        ad: ads.alohachallenge,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'YYIRjqXkQGI': {
        next: '-meki-J_wVE',
        ad: ads.pcc,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    '-meki-J_wVE': {
        next: '9LuJ6rGxYJQ',
        ad: ads.hti,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    '9LuJ6rGxYJQ': {
        next: 'wTH66VkoQ8U',
        ad: ads.hertz,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },
    'wTH66VkoQ8U': {
        next: null,
        ad: ads.pacbio,
        related: ['rvLQ34IV6bE', '9FqUZi1vgQo', 'frLhfinPgmI']
    },

    'eKeZ84V4Tuk': {
        next: 'VRXgXwtVE2s',
        ad: ads.alohachallenge,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg', 'spV8Wmh5ino']
    },
    'VRXgXwtVE2s': {
        next: 'PvMs4urVnN0',
        ad: ads.pcc,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg', 'spV8Wmh5ino']
    },
    'PvMs4urVnN0': {
        next: 'DW5mTUGf7qk',
        ad: ads.hti,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg', 'spV8Wmh5ino']
    },
    'DW5mTUGf7qk': {
        next: '6d0c7qiMjKo',
        ad: ads.hertz,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg', 'spV8Wmh5ino']
    },
    '6d0c7qiMjKo': {
        next: null,
        ad: ads.pacbio,
        related: ['SI2rsSm9ZS0', 'Wfny-At4RRg', 'spV8Wmh5ino']
    }
};
