slideTime = 3600;		// normal slide timeout
resumeTime = 10000;		// time before slideshow resumes after nav.

function revEmail(email, subject, cc) {
    var newemail = "";
    var ccemail = "";

    subject = subject || "";
    cc = cc || "";

    email = email.split('').reverse().join('');
    cc = cc.split('').reverse().join('');

    window.location = "mailto:"+email+"?subject="+subject+"&cc="+cc;
}

// Control box visibility by setting one - and only one - to have the
// 'visible' class.  The .css currently uses that to set the display:
// property.
function showonlyone(thechosenone) {
    $('#boxes .box.visible').removeClass('visible');
    $('#boxes .box#' + thechosenone).addClass('visible');
}

$(function() {
    function changeSlide( newSlide ) {
        // cancel any slide change timeout
	if (activeSlideshow)
            clearTimeout( slideTimeout );
        
        // change the currSlide value
        currSlide = newSlide;
        
        // make sure the currSlide value is not too low or high
        if ( currSlide > maxSlide ) currSlide = 0;
        else if ( currSlide < 0 ) currSlide = maxSlide;
        
        // animate the slide reel
        $slideReel.animate({
            left : currSlide * -540
        }, 400, 'swing', function() {
            // hide / show the arrows depending on which frame it's on
            $slideLeftNav.show();
            $slideRightNav.show();
            
            // set new timeout if active
            if ( activeSlideshow ) slideTimeout = setTimeout(nextSlide, slideTime);
        });
        
        // animate the navigation indicator
        $activeNavItem.animate({
            left : currSlide * 150
        }, 400, 'swing');
    }
    
    function nextSlide() {
        changeSlide( currSlide + 1 );
    }
    
    function resumeShow() {
	activeSlideshow = true;
	clearTimeout(slideTimeout);
	nextSlide();
    }

    function pauseShow() {
	activeSlideshow = false;
	clearTimeout(slideTimeout);
	slideTimeout = setTimeout(resumeShow, resumeTime);
    }

    // define some variables / DOM references
    var activeSlideshow = true,
    currSlide = 0,
    slideTimeout,
    $slideshow = $('#slideshow'),
    $slideReel = $slideshow.find('#slideshow-reel'),
    maxSlide = $slideReel.children().length - 1,
    $slideLeftNav = $slideshow.find('#slideshow-left'),
    $slideRightNav = $slideshow.find('#slideshow-right'),
    $activeNavItem = $slideshow.find('#active-nav-item');
    
    // set navigation click events
    
    // left arrow
    $slideLeftNav.click(function(ev) {
        ev.preventDefault();
        
	pauseShow();
        
        changeSlide( currSlide - 1 );
    });
    
    // right arrow
    $slideRightNav.click(function(ev) {
        ev.preventDefault();
        
	pauseShow();
        
        changeSlide( currSlide + 1 );
    });
    
    function jumpSlide(ref) {
	pauseShow();
        
        changeSlide( ref.index() );
    }

    // main navigation
    $slideshow.find('#slideshow-nav a.nav-item').click(function(ev) {
        ev.preventDefault();
     
	jumpSlide($(this));
    });
    
    // start the animation
    slideTimeout = setTimeout(nextSlide, slideTime);

    // Someone has set the #name part of the url; make sure the right
    // things are visible.  If there's no #name, then jump to the box
    // with the "default" class.  It also displays the corresponding
    // slide.
    function updateHash(hash) {
	var tgt = hash;
	var box, slide;
	
	if (tgt.length > 1) {
	    tgt = tgt.substr(1);
	    box = "box-" + tgt;
	    slide = "slide-" + tgt;
	    // jumpSlide causes rotation to pause for a while
	    jumpSlide($("#"+slide));
	} else {
	    box = $('#boxes .box.default').prop('id');
	    slide = "slide-" + box.substr(4);

	    // changeSlide just jumps to default
	    changeSlide($('#'+slide).index());
	}

	showonlyone(box);
    }

    // Update for the initial URL
    updateHash(window.location.hash);

    // Make sure clicks on links do the right thing
    $('#slideshow-reel .slide a').each(
	function () {
	    var h = $(this).prop('hash');
	    $(this).click(function () {
		updateHash(h);
	    })
	}
    );
})

