$(function(){
    var usingFlash = Flash.init();
    if(!usingFlash){
        try {
            Typekit.load({
                active: function(){
                    $('#wrap').fadeIn();
                    BG.resize();
                    $('body').css({
                        'font-size': '75%',
                        'font-weight': '100'
                    });
                    if(window.location.pathname == '/'){
                        NewsTicker.init();
                    }
                },
                inactive: function() {
                    $('#wrap').fadeIn();
                    BG.resize();
                    if(window.location.pathname == '/'){
                        NewsTicker.init();
                    }
                }
            });
        } catch(e){}
    }
    Video.init();
    Offices.init();
    ProjectMenu.init();
    BG.init();
    ListLinks.init();
})

Flash = {
    init: function(){
        if(version != 'flash'){
            return false;
        }
        
        if (swfobject.hasFlashPlayerVersion("9.0.115")) {
            Flash.loadFlash();
            return true
        
        } else if (swfobject.hasFlashPlayerVersion("5")) {
            $('#switch-version').text('/ upgrade flash')
            .attr('href', 'http://get.adobe.com/flashplayer/')
            .attr('title', 'You need to upgrade your installed version of Flash Player to view the flash version of this site.')
            return false;
        } else {
            // tried to load flash but not found at all...
            $('#switch-version').remove();
            if(version == 'flash'){
                $('a').each(function(){
                    if($(this).attr('href').slice(0, 4) != 'http'){
                        $(this).attr('href', $(this).attr('href') + '?version=html')
                    }
                })
            }
            return false;
        }
        
    },
    loadFlash: function(){
        
        // I tried removing images in the html here as they're not really needed, 
        // but they still get loaded anyway.
        
        //var flashvars = now global...
        var params = {
            allowFullScreen: 'true', 
            wmode: 'transparent',
            menu: false
        }
        var attrs = {
            id: 'flash'
        }
        var src = "/assets/site/swf/loader.swf?v=1";
        swfobject.embedSWF(src, "wrap", "100%", "100%", "9.0.28", "/assets/lib/swf/expressInstall.swf", flashvars, params, attrs)
    }
}

Video = {
    init: function(){
        if($('video').length){
            
            if (Video.canPlayVideo()){
                return false;
            }
            else if(swfobject.hasFlashPlayerVersion("9.0.115")){
                Video.embedFlash();
            }
            else {
                Video.showDownloadLink();
            }
        } 
    },
    canPlayVideo: function(){
        
        // detect support for video, inspired by modernizr
        var elem = window.document.createElement('video');
                
        if(elem['canPlayType'] && elem['canPlayType']('video/mp4; codecs="avc1.42E01E, mp4a.40.2"')){
            // we can play h264 with the video tag so no need to do anything- yay!
            return true;
        } else {
            return false;
        }
    },
    embedFlash: function(){
        
        $('video').each(function(i){
            
            var id = 'video-' + i
            $(this).attr('id', id)
            
            var width = $(this).attr('width');
            var height = $(this).attr('height');
            var src = $(this).find('source').attr('src');
        
            var flashvars = {
      		    src: src,
      		    width: width,
      		    height: height,
      		    duration: '389',
      		    color: color
            };
            var params = {
                menu: 'false',
                bgcolor: '#000000',
                allowFullScreen: 'true'
            };
             var attrs = {
                 id: id
             };
             
            swfobject.embedSWF('/assets/site/swf/video_player.swf', id, width, Number(height) + 36, '9.0.115', '/assets/lib/swf/expressInstall.swf', flashvars, params, attrs);
        })
    },
    showDownloadLink: function(){
        
        $('video').each(function(i){
            $(this).replaceWith('<a href="'+$(this).find('source').attr('src')+'">Download Video</a>')
        });
    }
}

Offices = {
    init: function(){
        $('#offices a').live('click', Offices.onClick);
    },
    onClick: function(){
        $('#office-info li').fadeOut('normal')
        $.get(
            $(this).attr('href'),
            {},
            function(result){
                $('#office-detail').replaceWith(result)
                $('#office-detail').hide().fadeIn();
            },
            'html'
        )
        var curOffice = $('#offices span.current')
        curOffice.replaceWith('<a id="'+ curOffice.attr('id') +'" href="'+ curOffice.attr('id')+'">' + curOffice.text() + '</a>');

        $(this).replaceWith('<span id="'+ $(this).attr('id') +'" class="current">' + $(this).text() + '</span>');

        return false;
    }
}

ProjectMenu = {
    init: function(){
        ListLinks.linksFor('#menu .projects > li');
        ProjectMenu.description = $('#description');
        $('<a class="hide-button" href="#" title="Hide the description box"></a>')
        .appendTo('#description').click(ProjectMenu.hideDescription);
    },
    hideDescription: function(){
        ProjectMenu.description.replaceWith('<a id="closed-description" href="#">/ description</a>');
        $('#closed-description').click(ProjectMenu.showDescription);
        return false;
    },
    showDescription: function(){
        $(this).replaceWith(ProjectMenu.description);
        ProjectMenu.description.click(ProjectMenu.hideDescription);
        return false;
    }
}

BG = {
    init: function(){
        
        $('#bg').each(function(){
            
            if(this.complete){
                BG.resize();
                if(fadeBg){
                    $(this).hide();
                    $(this).fadeIn();
                }
            } else {
                if(fadeBg)
                    $(this).hide();
                $(this).load(BG.onBgLoad);
            }
        });
        
        $(window).resize(BG.resize);
    },
    onBgLoad: function(){
        BG.resize();
        if(fadeBg)
            $('#bg').fadeIn();
    },
    resize: function(){
        var tWidth = $('#wrap').width();
        var tHeight = $('#wrap').height() - $('#footer').outerHeight();
        if($('#bg.scale').length){
            var bgWidth = $('#bg').width();
            var bgHeight = $('#bg').height();
            var scaleX = tWidth / bgWidth;
            var scaleY = tHeight / bgHeight;
            var scale = Math.max(scaleX, scaleY);
            $('#bg').width(scale * bgWidth);
            $('#bg').height(scale * bgHeight);    
        }
        $('#bg').css('margin-left', (tWidth - $('#bg').width()) * 0.5);
        $('#bg').css('margin-top', (tHeight - $('#bg').height()) * 0.5);
    }
}

ListLinks = {
    init: function(){
        ListLinks.linksFor('#search-results li');
        ListLinks.linksFor('#menu ul.team li');
    },
    linksFor: function(selector){
        $(selector).css('cursor', 'pointer').click(function(){
            window.location = $(this).find('a').eq(0).attr('href');
        });
    }
}

NewsTicker = {
    left: 0,
    init: function(){
        if(!$('#news-ticker').length) return;
        
        var x = 0;
        $('#news-ticker span').each(function(){
            $(this).css({left: x, top: 0, width: $(this).width()});
            x += $(this).outerWidth();
        });
        
        $(window).resize(function(){
            NewsTicker.setTickerWidth();
        });
        this.setTickerWidth();
        this.scrollLoop();
    },
    scrollLoop: function(){
        $('#news-ticker span').each(function(){
            $(this).css('left', $(this).position().left - 1)
        });
        
        NewsTicker.left -= 1;
        
        if(NewsTicker.left % 10 == 0){
            $('#news-ticker span:first').each(function(){
                if($(this).position().left + $(this).outerWidth() < 0){
                    var lastHeadline = $('#news-ticker span:last');
                    $(this).css('left', lastHeadline.position().left + lastHeadline.outerWidth())
                    $('#news-ticker div').append(this);
                    $(this).hide();;
                    $(this).fadeIn();
                }
            })
            NewsTicker.left = 0;
        }
        
        setTimeout(NewsTicker.scrollLoop, 20);
    },
    setTickerWidth: function(){
        var w = $('#external-links').position().left - $('#news-ticker').position().left - $('#news-ticker h4').outerWidth() - 30;
        $('#news-ticker div').width(w);
    }
}
 
