(function() {
    var so, player, videos, videoSrc, current,
        playingClass = 'playing', defaultVideo = '/swf/tutorial/homepage.swf', playerId = 'playing';

    function writeSwfObj(override) {

        videoSrc = (typeof override == 'string') ? override : $(this).attr('href');

        if (videoSrc != '' && videoSrc != '#' && videoSrc != current) {
            player.html('');

            so = new SWFObject(videoSrc, "tutorial", "640", "398", "8", "#4a4a4a");
            so.addParam('scale','exactfit');
            so.addParam('wmode','transparent');
            so.addParam('allowfullscreen','true');
            so.write(playerId);

            current = videoSrc;
            
            pageTracker._trackEvent('Video Tutorials', 'User started watching ' + videoSrc);

            if (videoSrc === override) {
                assignPlayingClass($('a[href="' + videoSrc + '"]').get(0));
            } else {
                assignPlayingClass(this);
            }
        }

        return false;
    }

    function assignPlayingClass(clicked) {
        $(clicked).parent('li').addClass(playingClass).siblings().removeClass(playingClass);
    }

    function bindNavClicks() {
        videos.click(writeSwfObj);
        return true;
    }

    function closePopup() {
        window.close();
    }

    $(document).ready(function() {

        player = $('#' + playerId), videos = $('#videoNav ul li a');

        if (bindNavClicks()) {
            videos.eq(0).click();
        }

        if ($('body').attr('id') === 'tutorialPopup') {
            $('.closePop').click(closePopup);
        }
    });

})();