/*
*************************************************

DESIGN SWAP
JavaScript Functions (requires jQuery)

Created by the ridiculous Dan Mall
http://www.danielmall.com/

*************************************************
*/


/*-------------------------------------------    
    Global Variables
-------------------------------------------*/
var currentSection, 
    currentSectionWidth = 'narrow';

var LARGE_WIDTH = 980;
var SMALL_WIDTH = 682


/*-------------------------------------------    
    Initial Actions
-------------------------------------------*/

function init() {
    
    //resizeContainer();
    createTabs();
    wrapChronologicalTable();
    initApps();
    //initCDRollovers();
       
}

// this runs EVERYTHING
$(document).ready(function() { init(); });



/*-------------------------------------------    
    Custom Functions
-------------------------------------------*/

/** 
*   Resizes content on mouseover
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 0.1
*   @copyright Dan Mall
*
*/

function resizeContainer(){
    
    var LARGE_WIDTH = 980;
    var SMALL_WIDTH = 682
	
	$('#chronological table').mouseenter(function(){
	    $('#outer-wrap').animate({
            width: LARGE_WIDTH
        }, 250, 'linear');
	}).mouseleave(function(){
	    $('#outer-wrap').animate({
            width: SMALL_WIDTH
        }, 250, 'linear');
	});
	
}



/** 
*   Creates tabset of different browsing options
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 0.1
*   @copyright Dan Mall
*
*/

function createTabs(){
    
    // stupid Firefox won't style the <nav>, so I have to wrap it in a <div>
    $('#intro').after('<div id="nav"><nav><ul><li id="nav-random" class="current"><a href="#random">Random</a></li></ul></nav></div>');
    
    $('section.major').each(function(){
        var _id = $(this).attr('id');
        $('#nav ul').append('<li id="nav-' + _id + '"><a href="#' + _id + '">' + $(this).find('header h2').html() + '</a></li>');
    });
    
    createTheRandomSection();
    
    $('#nav a').click(function(){
        if($(this).parent().hasClass('current') == false){
            $('#nav li').removeClass('current');
            $(this).parent().addClass('current');
            displayCurrentSection($(this).attr('href'));            
        }
        return false;
    });
	
}


function createTheRandomSection(){
    $('#nav').after('<section id="random" class="major narrow"><div class="content"></div></section>'); 
    
    var _randomSections = 0, _randomFact, _id;   
    
    $('#chronological tr.eligible').each(function(n){
        _id = $(this).attr('id');
        var _activity = $(this).find('td:eq(0)').html();
        var _time = $(this).find('th:eq(0)').html();
        $('#random div.content').append('<article id="' + _id + '"><p>' + _activity + '</p>' + _time + '</article>');        
        _randomSections++;
    });   
    
    
    // hide all <article>s, but show a random one    
    $('#random article').hide();
    
    // insert Flash movie to one that will be shown
    var _randomFact = Math.floor(Math.random() * _randomSections);    
    var _showMe = $('#random article').eq(_randomFact);
    $(_showMe).prepend('<div id="flash-' + $(_showMe).attr('id') + '"></div');
    swfobject.embedSWF('/s/' + $(_showMe).attr('id') + '.swf', 'flash-' + $(_showMe).attr('id'), '500', '230', '9.0.0', '/s/expressInstall.swf', {}, { menu: 'false', wmode: 'transparent' }, {});
    $(_showMe).show();
    
    currentSection = $('#random');
    
}


function displayCurrentSection($sec){
    
    // 1. fade out current section
    $(currentSection).find('.content').fadeTo('normal', 0);
    
    // 2. resize height to future section
    $($sec).fadeIn('normal');
    $($sec).find('.content').show();
    $(currentSection).hide();
    
    // 3. resize container width
    if($($sec).hasClass('narrow')){
        if(currentSectionWidth != 'narrow'){        
            $('#outer-wrap, #credit ul').animate({
                width: SMALL_WIDTH
            }, 250, 'linear', function(){
                $($sec).find('.content').css({'opacity' : 1});
                $($sec).fadeIn('normal');
            });
            currentSectionWidth = 'narrow';
        }else{
            $($sec).find('.content').css({'opacity' : 1});
            $($sec).fadeIn('normal');
        }
    }else{
        if(currentSectionWidth == 'narrow'){        
            $('#outer-wrap, #credit ul').animate({
                width: LARGE_WIDTH
            }, 250, 'linear', function(){
                $($sec).find('.content').css({'opacity' : 1});
                $($sec).fadeIn('normal');
            });
            currentSectionWidth = 'wide';
        }else{
            $($sec).find('.content').css({'opacity' : 1});
            $($sec).fadeIn('normal');
        }
    }
    
    //  reset currentSection
    currentSection = $($sec);
    
}


/** 
*   Adds multiple elements to the table to style
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 0.1
*   @copyright Dan Mall
*
*/

function wrapChronologicalTable(){
    
   $('#chronological table').wrap('<div id="chronological-wrap-outer"><div id="chronological-wrap-inner"></div><div id="hour-hand"><div id="anchor"></div></div></div>');
   
   assignPositions();
   createChronologicalNav();
   	
}


function assignPositions(){
    
    $('#chronological table tr').each(function(){
        
        var _haystack = $(this).find('time:eq(0)').text();
        var _colonIndex = _haystack.indexOf(':');
        
        var _hour = _haystack.substring(0, _colonIndex);
        var _minute = _haystack.substring(_colonIndex + 1, _colonIndex + 3);
        
        $(this).addClass('hour' + _hour).addClass('minute' + _minute);
        
    });
    
    
    $('#chronological table tr').mouseenter(function(){
        if(!$.browser.webkit){
            $(this).fadeTo('normal', 1);
        }
        
        $('#hour-hand').fadeTo('fast', 0.5);
        
    }).mouseleave(function(){
        if(!$.browser.webkit){
            $(this).fadeTo('normal', 0.2);
        }
        
        $('#hour-hand').fadeTo('fast', 1);
        
    });
    
}


function createChronologicalNav(){
    
    $('#chronological-wrap-outer').after('<div id="clock-nav"><nav><ul><li id="cn-hour7"><a href="#hour7">7AM</a></li><li id="cn-hour8"><a href="#hour8">8AM</a></li><li id="cn-hour9"><a href="#hour9">9AM</a></li><li id="cn-hour10"><a href="#hour10">10AM</a></li><li id="cn-hour11"><a href="#hour11">11AM</a></li><li id="cn-hour12"><a href="#hour12">12PM</a></li><li id="cn-hour1"><a href="#hour1">1PM</a></li><li id="cn-hour2"><a href="#hour2">2PM</a></li><li id="cn-hour3"><a href="#hour3">3PM</a></li><li id="cn-hour4"><a href="#hour4">4PM</a></li><li id="cn-hour5"><a href="#hour5">5PM</a></li><li id="cn-hour6"><a href="#hour6">6PM</a></li></ul></nav></div>');
    
    $('#clock-nav a').click(function(){         
        highlightClockNav($(this));
        
        var _href = $(this).attr('href');
        var _target = _href.substring(5, _href.length);
        showActivities(_target);
        turnClock(_target);        
        return false;
    });
    
    
    // first run
    $('#cn-hour7 a').trigger('click');
    
}

function highlightClockNav($el){
    
    // turn off all current states
    $('#clock-nav li').removeClass('current');
    
    // highlight passed in id
    $($el).parent().addClass('current');
    
}

function showActivities($hour){
    
    var _combo = ".hour" + $hour;
    
    // clear out currently showing    
    $('#chronological tr').each(function(){
       if($(this).is(':visible')){
           $(this).fadeOut('fast');
       } 
    });
    
    $('#chronological tr').filter(_combo).fadeIn('normal');
    
}

function turnClock($hour){
    
    $('#hour-hand').attr('class', 'clock' + $hour);
    
}


/** 
*   Dock-style animation for icons
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 0.1
*   @copyright Dan Mall
*
*/

function initApps(){
    
   $('#apps li a, #cloud li a').wrapInner('<div class="tip"></div>');
   
   $('#apps a, #cloud a').mouseenter(function(){
       $(this).find('.tip').fadeIn('normal');
   }).mouseleave(function(){
        $(this).find('.tip').fadeOut('fast');
   });
   	
}


/** 
*   CD Animation with JavaScript, cuz CSS animation still sucks
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 0.1
*   @copyright Dan Mall
*
*/

function initCDRollovers(){

    $('#yeasayer a').mouseover(function(){
        
    });
    
    $('#tool a').mouseover(function(){
        $(this).parent().animate({
            left: '120px',
            rotate: '+=45deg'
        }, 1000);
    });
   	
}




/** 
*   JavaScript alerts a message from Flash
*   
*   @author Dan Mall <dan@danielmall.com>
*   @version 1.0
*   @copyright Dan Mall
*
*/

function outputMessage(s){
	alert(s);
}



