$(window).load(function(){







/* INITIALIZE */
    // strapline
    var strapline = $('#strapline').text().split(" ");
    var new_strapline = [];
    for( var i in strapline){
        new_strapline[i] = '<b>'+strapline[i]+'</b><br />';
    }
    $('#strapline').html(new_strapline.join(''));    
    // target="blank"
    $('#bottom-menu .submenu a').attr('target', '_blank');
    // fix font if @font-face is not supported
    if(!$.fontAvailable('Typewriter Regular')) {
        $('.homepage-menu a.menu').css('font-size', '1.1em').css('line-height', '2em');
        $('#strapline').css('font-size', '1.1em');
    }
    // homepage video
    function addVideo(){
        var date = new Date();
        var hour = date.getHours();
        var video = (hour>0 && hour<12) ? 'night' : 'day';
        $('#tv').flash({
			swf: '/wp-content/themes/openthread/images/homepage_video_'+video+'.swf',
			height: 241,
			width: 187,
			wmode: 'transparent'
        });     
    }


                    
                        





/* POSITION IN THE MIDDLE OF THE PAGE */
    correctMiddlePosition();
    $(window).resize(function() {
        correctMiddlePosition();
    });
    function correctMiddlePosition() {
        if($(window).height()>=$('#page').height()) {
            $('#page').css('margin-top', ($(window).height()-$('#page').height())/2+'px');  // vertical
        }
        if($(window).height()>=$(window).height()) {
            $('#page').css('margin-left', ($(window).width()-$('#page').width())/2+'px');   // horizontal
        }
    }
    
    
    
    
    
    
    
/* SITE SLIDE UP AND INITIAL CONTENT */
    $('#page').css('display', 'none').show('slide', {direction: 'down'}, 1000, function(){
        addVideo();
        if(location.hash.length>1){
            $('#top-menu li.menu a.menu').each(function(){
                if($(this).attr('href')=='/'+location.hash){
                    $(this).trigger('click');
                }
            });
        }
    });
    
    
    
  
    

/* CONTENT */
    $('#top-menu > .o1 a.menu, .o2 a.menu, .o6 a.menu').bind('click',  function() {
        var page = $(this).attr('href').replace('/', '');
        var li = $(this).parent();
        if(!li.data('disabled')){
            $('#top-menu li a').data('disabled', 1);
            // hide open pages
            $('.content').not(':hidden').each(function(){
                var pageWidth = $(this).width();
                var linkWidth = $(this).parent().width();
                $(this).slideUp(500, function() {
                    $(this).parent().animate({ width: (linkWidth-pageWidth-156) }, function(){
                        $(this).removeClass('open')
                            .removeData('active');
                        $('#top-menu li').removeData('disabled');
                        location.hash = 'home';
                    });
                });
            });
            // open page
            if(!li.data('active')){
                li.data('active', 1)
                    .addClass('open')
                    .animate({ width: $(page).width()+li.width()+156 }, 1000, function(){
                        li.append($(page));
                        $(page).slideDown(1000);
                        $('#top-menu li').removeData('disabled');
                        location.hash = page;
                    });
            }
        }
    });







/* WHAT IS OPENTHREAD: navigation */
    $('#what-is-open-thread .excerpt:gt(0)').hide();
    $('#what-is-open-thread .navigation a').bind('click', function(){
        $(this).parent().find('a').removeClass('active');
        $(this).addClass('active');
        var show = parseInt($(this).html())-1;
        $(this).parent().parent().find('.excerpt').hide();
        $(this).parent().parent().find('.excerpt:eq('+show+')').show();
        return false;
    });
/* WHO IS OPENTHREAD: navigation */
    $('#who-is-open-thread .excerpt:gt(0)').hide();
    $('#who-is-open-thread .left div.name').live('click', function() {
        $('#who-is-open-thread .right .excerpt').hide();
        $('#who-is-open-thread .right .excerpt:eq('+$('#who-is-open-thread .left div.name').index(this)+')').show();
    });
        
        
        
        
        
        

/* ACTIVE OBJECTS */
    var activeObjectCoords      = [];
    activeObjectCoords['o1']    = '0 -284px';
    activeObjectCoords['o2']    = '0 -493px';
    activeObjectCoords['o2b']   = '0 -383px';
    activeObjectCoords['o4']    = '0 -150px';
    activeObjectCoords['o5']    = '0 0';
    activeObjectCoords['o6']    = '0 -107px';
    activeObjectCoords['hide']  = '5000px 0';

    $('#active-objects a').hover(function(){
        if($(this).attr('class').indexOf('2')!=-1){
            $('#active-objects .o2').css('background-position', activeObjectCoords['o2']);
            $('#active-objects .o2b').css('background-position', activeObjectCoords['o2b']);
            $('#top-menu li.o2 a.menu').trigger('mouseover');
        } else {
            $(this).css('background-position', activeObjectCoords[$(this).attr('class')]);
            $('#top-menu li.'+$(this).attr('class')+' a.menu').trigger('mouseover');
        }
    }, function(){
        if($(this).attr('class').indexOf('2')!=-1){
            $('#active-objects .o2').css('background-position', activeObjectCoords['hide']);
            $('#active-objects .o2b').css('background-position', activeObjectCoords['hide']);
            $('#top-menu li.o2 a.menu').trigger('mouseout');
        } else {
            $(this).css('background-position', activeObjectCoords['hide']);
            $('#top-menu li.'+$(this).attr('class')+' a.menu').trigger('mouseout');
        }
    }).bind('click', function(){
        if($(this).attr('class').indexOf('2')!=-1){
            $('#top-menu li.o2 a.menu').trigger('click');
        } else {
            $('#top-menu li.'+$(this).attr('class')+' a.menu').trigger('click');
        }
    });
    
    
    
    
    
    

/* MENU */
    $('#top-menu li.menu').hover(function(){
        $(this).addClass('active').find('.submenu').not(':animated').slideDown(300);
        if($(this).hasClass('o2')){
            $('#active-objects .o2').css('background-position', activeObjectCoords['o2']);
            $('#active-objects .o2b').css('background-position', activeObjectCoords['o2b']);
        } else {
            var id = $(this).attr('class').replace('menu ', '');
            $('#active-objects a.'+id+'').css('background-position', activeObjectCoords[id]);
        }
    }, function(){
        $(this).removeClass('active').find('.submenu').not(':animated').slideUp(300);
        if($(this).hasClass('o2')){
            $('#active-objects a.o2, #active-objects a.o2b').css('background-position', activeObjectCoords['hide']);
        } else {
            var id = $(this).attr('class').replace('menu ', '').replace(' active', '');
            $('#active-objects a.'+id+'').css('background-position', activeObjectCoords['hide']);
        }
    });
    $('#bottom-menu li.menu').hover(function(){
        $(this).addClass('active').find('.submenu').not(':animated').slideDown(300);
    }, function(){
        $(this).removeClass('active').find('.submenu').not(':animated').slideUp(300);
    });
    
    
    
    
    
    
    
/* STRAPLINE */
    $('#strapline').hover(function(){
       $('#top-menu li:eq(0)').trigger('mouseover');
    }, function(){
        $('#top-menu li:eq(0)').trigger('mouseout');
    });
    $('#strapline').bind('click', function(){
        $('#top-menu li:eq(0) a').click();
    });
    
    
    
    
   
    
});