
/* Main
 * --------------------------------------------------------------- */

Shadowbox.loadSkin('skin', '/');

$(document).ready(function() {
    setupShadowbox();
    scrollToAndPlay();
});






/* Flash callback method
 * --------------------------------------------------------------- */

// Allow Flash video player to resize Shadowbox
function resize(w, h) {
    $('#shadowbox_content').width(w);
    Shadowbox.adjustWidth(w+2, true);
}



/* Scroll-to-and-play bookmarkable links
 * --------------------------------------------------------------- */

function scrollToAndPlay() {
    if (location.hash) {
        // Ugly two-liner to turn #/client/work/ to client:work
        var id_components = location.hash.split(/[#/]/);
        var id = $.trim(id_components.join(' ')).replace(' ', ':')
$("#hashtext").html(id);
        // Animate scroll to target, trigger click event in callback
        target = $('a[id=' + id + ']');
        $('html,body').animate({
            scrollTop: target.offset().top - 150
        }, function() {
            target.click();
        });
    }
}



/* Shadowbox setup for each page
 * --------------------------------------------------------------- */

function setupShadowbox() {
    var page = $(document.body);
    if (page.is('.radio')) {
        setupRadioShadowbox();
    } else if (page.is('.tv')) {
        setupTVShadowbox();
    } else if (page.is('.online')) {
        setupOnlineShadowbox();
    } else {
        setupOtherShadowbox();
    }
}


// The radio page is the one exception in that it doesn't use Shadowbox, but
// for consistency's sake, the method is named as if it did use it.
function setupRadioShadowbox() {

    // If they have at least Flash 8, add the radio player.
    // Flash 9 is really required, but Flash 8 can use expressinstall to update.
    if (swfobject.hasFlashPlayerVersion('8.0.0')) {
        var flashvars = {
            clipURL:  $('.row a').attr('href')
        }
        var params = {
            bgcolor:  '#111111'
        }
        swfobject.embedSWF('/swf/radio.swf', 'player', '573', '177', '9.0.0', '/swf/expressInstall.swf', flashvars, params);
    }

    // If they have Flash 9+, make all the mp3 links play in the Flash player
    if (swfobject.hasFlashPlayerVersion('9.0.0')) {
        $('.row a').each(function() {
            var link = $(this);
            var link_href = link.attr('href');

            // Play mp3 in Flash player
            link.click(function() {
                var flashvars = {
                    clipURL:  link_href,
                    play:     true
                }
                var params = {
                    bgcolor:  '#111111'
                }
                swfobject.embedSWF('/swf/radio.swf', 'player', '573', '177', '9.0.0', '/swf/expressInstall.swf', flashvars, params);
            });

            // Change link href to something nice (and bookmarkable)
            var id_components = link.attr('id').split(':');
            link.attr('href', '#/' + id_components.join('/') + '/');
        });
    }
}


function setupTVShadowbox() {
    // We will trigger Shadowbox manually
    Shadowbox.init({ skipSetup: true });

    // Diddle with each thumbnail link
    $('.slider a').each(function() {
        var link = $(this);
        var link_href = link.attr('href');
        var link_title = link.attr('title');

        // Open Flash video player in Shadowbox on click
        link.click(function() {
            Shadowbox.open({
                player:   'swf',
                title:    link_title,
                content:  '/swf/video.swf',
                width:    384,
                height:   324,
                options:  {
                    animSequence:    'sync',
                    overlayOpacity:  0.9,
                    flashBgColor:    '#111111',
                    clipURL:         link_href
                }
            });
        });

        // Change link href to something nice (and bookmarkable)
        var id_components = link.attr('id').split(':');
        link.attr('href', '#/' + id_components.join('/') + '/');

        // Remove title to prevent annoying tooltip
        link.removeAttr('title');
    });


}


function setupOnlineShadowbox() {
    $('.online_item').attr('rel', 'shadowbox');
    Shadowbox.init({
        animSequence:    'sync',
        overlayOpacity:  0.9
    });
}


function setupOtherShadowbox() {
    // We will trigger Shadowbox manually
    Shadowbox.init({ skipSetup: true });

    // Diddle with each thumbnail link
    $('.slider a').each(function() {
        var link = $(this);
        var link_href = link.attr('href');
        var link_title = link.attr('title');

        // Open image in Shadowbox on click
        link.click(function() {
            Shadowbox.open({
                player:   'img',
                title:    link_title,
                content:  link_href,
                options:  {
                    animSequence:    'sync',
                    overlayOpacity:  0.9
                }
            });
        });

        // Change link href to something nice (and bookmarkable)
        var id_components = link.attr('id').split(':');
        link.attr('href', '#/' + id_components.join('/') + '/');

        // Remove title to prevent annoying tooltip
        link.removeAttr('title');
    });
}



