// used to prevent footnotes from showing up when jumping between sections
var suppressFootnotes = false

function revealFootnote(footnote, settings) 
{
    if (!settings)
        settings = {}
        
    $('.footnote.revealed').hide().removeClass('revealed')
    $(footnote).addClass('revealed').fadeIn(settings.duration || 'fast')
}

function hideFootnote(footnote, settings) 
{
    if (!settings)
        settings = {}
    
    $(footnote).fadeOut(settings.duration || 'fast', function () {
        $(this).removeClass('revealed')
    })
}

function generateFootnoteSummary(footnote) {
	maxlength = 80
	full_text = footnote.children('div.full').eq(0).text().replace(/^\s+|\s+$/g, "")
	if (full_text.length > maxlength)
		summary = full_text.substring(0,maxlength) + "..."
	else
		summary = full_text
	footnote.children('div.teaser').text(summary)
}

function expandFootnote(footnote, settings) {
    if (!settings)
        settings = {}
    
    $(footnote).children('.teaser').hide()
    $(footnote).children('.full').slideDown('fast')
    $(footnote).removeClass('footnote-collapsed').addClass('footnote-expanded')
    $(footnote).children('a.footnote-toggle').attr('title', "Collapse foonote")
        .removeClass('ui-icon-plusthick').addClass('ui-icon-minusthick')
        .unbind('click').bind('click', function (event) {
            collapseFootnote(footnote)
            return false
        })
}

function collapseFootnote(footnote, settings) {
    if (!settings)
        settings = {}
    
    $(footnote).children('.full').slideUp('fast')
    $(footnote).children('.teaser').show()
    $(footnote).removeClass('footnote-expanded').addClass('footnote-collapsed')
    $(footnote).children('a.footnote-toggle').attr('title', "Expand foonote")
        .removeClass('ui-icon-minusthick').addClass('ui-icon-plusthick')
        .unbind('click').bind('click', function (event) {
            expandFootnote(footnote)
            return false
        })
}

function highlightFootnote(footnote) {
    $(footnote).parent()
        .effect('highlight', {color: '#d99'}, 250)
        .effect('highlight', {color: '#d99'}, 250)
        .effect('highlight', {color: '#d99'}, 250)
        .effect('highlight', {color: '#d99'}, 250)
        
    return false
}

function jumpToFootnote(footnote) {
    suppressFootnotes = true
    $.scrollTo('#'+$(footnote).attr('id')+'-link', 500, {
        offset: { top: -100 },
        onAfter: function() { window.suppressFootnotes = false }
    })
}

function jumpToSection(section) {
    suppressFootnotes = true
    $.scrollTo($(section), 500, {
        offset: { top: -50},
        onAfter: function() { window.suppressFootnotes = false }
    })
}

function jumpToPage(pagestart) {
    suppressFootnotes = true
    $.scrollTo($(pagestart), 500, {
        offset: { top: -100 },
        onAfter: function() { window.suppressFootnotes = false }
    })
}

function expandSidebar(sidebar, settings) {
    if (!settings)
        settings = {}
    
    $(sidebar).children('.content').slideDown('fast')
    $(sidebar).removeClass('sidebar-collapsed').addClass('sidebar-expanded')
    $(sidebar).children('a.sidebar-toggle').attr('title', "Minimize sidebar")
    $(sidebar).children('a.sidebar-toggle').removeClass('ui-icon-plusthick').addClass('ui-icon-minusthick')
    $(sidebar).children('a.sidebar-toggle').unbind('click').bind('click', function (event) {
        collapseSidebar(sidebar)
        return false
    })
}

function collapseSidebar(sidebar, settings) {
    if (!settings)
        settings = {}

    $(sidebar).children('.content').slideUp('fast')
    $(sidebar).removeClass('sidebar-expanded').addClass('sidebar-collapsed')
    $(sidebar).children('a.sidebar-toggle').attr('title', "Expand sidebar")
    $(sidebar).children('a.sidebar-toggle').removeClass('ui-icon-minusthick').addClass('ui-icon-plusthick')
    $(sidebar).children('a.sidebar-toggle').unbind('click').bind('click', function (event) {
        expandSidebar(sidebar)
        return false
    })
}

// true if any part of the given element is currently visible
// taken from http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport/125106#125106
// TODO: turn this into a jQuery-esque function
function elementInView(el) {
  var top = el.offsetTop;
  var left = el.offsetLeft;
  var width = el.offsetWidth;
  var height = el.offsetHeight;

  while(el.offsetParent) {
    el = el.offsetParent;
    top += el.offsetTop;
    left += el.offsetLeft;
  }

  return (
    top < (window.pageYOffset + window.innerHeight) &&
    left < (window.pageXOffset + window.innerWidth) &&
    (top + height) > window.pageYOffset &&
    (left + width) > window.pageXOffset
  );
}

// stack to keep track of current section nesting
// e.g. ['section-1', 'section-1b'] means that we're in section 1b (nested inside section 1)
var sectionStack = []

$(document).ready(function(){
    // target="_blank" is not allowed in XHTML, so we do this to open
    // external links in a new window
    $('a[rel=external]').bind('click', function (event) {
        window.open(this.href);
        return false;
    });
    
    $('#section-menu').bind('change', function (event) {
        jumpToSection('#'+$(this).val())
    })
    
    $('div.chapter, div.section').bind('inview', function (event, visible) {
        sectionId = $(event.target).attr('id')
		
		// FIXME: for some reason this is getting triggered for footnotes and pages (!?)
		if (!(sectionId.match(/chapter-/) || sectionId.match(/section-/)))
			return

        if (visible) {
            // don't add the section to the stack if it's already in there
            if (!$(sectionStack).any(function(){return this == sectionId}))
                sectionStack.push(sectionId)
        } else {
            // remove the section from the stack
            sectionStack = $(sectionStack).reject(function(){return this == sectionId})
        }
        //console.log(sectionStack)
        $('#section-menu').val(sectionStack[sectionStack.length - 1])
    })
    
    $('*.pagestart').data('inview.viewport-offset-y', 40)
    $('*.pagestart').data('inview.viewport-scale-y', 0.3)
    
    $('*.pagestart').bind('inview', {bounds: 'test'}, function (event, visible) {
        if (visible) {
            pageNum = $(event.target).attr('name').replace('pg','')
            $('#goto-page').val(pageNum)
        }
    })
    
    $('#goto-page').bind('change', function (event) {
        jumpToPage('#pg'+$(this).val())
    })
    
    $('*.footnote-link').data('inview.viewport-offset-y', 45)
    $('*.footnote-link').data('inview.viewport-scale-y', 0.9)
    
    $('*.footnote-link').bind('inview', function (event, visible) {
        if (suppressFootnotes)
            return
        
        footnoteId = $(event.target).attr('id').replace('-link', '')
        footnote = $('#'+footnoteId)
        
        if (visible && !footnote.hasClass('revealed')) {
            revealFootnote(footnote, {duration: 'fast'})
        } else if (!visible && footnote.hasClass('revealed')) {
            hideFootnote(footnote, {duration: 'fast'})
            
            // if there's another footnote in view, show it
            $('*.footnote-link').not(this).each(function () {
                if (elementInView(this) && !$(this).hasClass('revealed')) {
                    footnote2Id = $(this).attr('id').replace('-link', '')
                    footnote2 = $('#'+footnote2Id)
                    revealFootnote(footnote2, {duration: 'fast'})
                    return false
                }
            })
        }
    })
    
    $('*.footnote-backlink > a').bind('click', function (event) {
        footnoteId = $(this).parent().parent('div.footnote').attr('id')
        highlightFootnote('#'+footnoteId+'-link')
        return false
    })
    
    $('*.sidebar-toggle').bind('click', function (event) {
        collapseSidebar($(this).parent())
        return false
    })
    
    $('*.footnote-toggle').bind('click', function (event) {
        expandFootnote($(this).parent())
        return false
    })
    
    $(window).trigger('scroll')
    
    /*new Benchmark(
      { // functions to be benchmarked
          old_way: test1,
          new_way: test2
      },
      { iterations: 1000, responders: 'Firebug' }
    )*/

})


