$(function() {
  function showCoverLink() {
    var coverLink = $('#cover-link');      
    coverLink.animate({
     left: 0
    });  
  }

  function hideCoverLink() {
    var coverLink = $('#cover-link');      
    coverLink.animate({
     left: 26
    });  
  }
  
  function bringIntroToFront() {
    $("#intro-text").css('z-index', 12);
  }
  function sendIntroToBack() {
    $("#intro-text").css('z-index', 8);
  }
	
	$(document).ready(function(){	  
	  $('#ebook').booklet({
  		width:  442,
  		height: 400,
  		pageNumbers: false,
  		shadows: false,
  		pagePadding: 0, 
  		manual: false,
  		overlays: false,
  		closed: true,
  		covers: true,
  		speed: 500,
  		prev: '#ebook-prev', 
  		next: '#ebook-next',
  	  before: function(opts){
        // console.log("before: opts.curr", opts.curr);
        if (opts.curr == 0) {
          hideCoverLink();
          $("#prev-next-wrapper").hide();
          $("#ebook-prev").css('opacity', .25);
        } else {
          sendIntroToBack();
        }
      },               
      after: function(opts){      
        // console.log('after:  index is : '+opts.curr + ' of ' +opts.pTotal);
        if (opts.curr > 0) {
          
          showCoverLink();
          $("#ebook-prev").css('opacity', 1);
          $("#intro-text").css('z-index', 8);
          $("#prev-next-wrapper").show();
        } 
        else {
          bringIntroToFront();
        }

        if (opts.curr + 2 >= opts.pTotal) {
          $("#ebook-next").css('opacity', .25);
        }
      }                
  	});
  	
  	$('#ebook').show();

    $('#ebook-chapters li a').each(function(index, link) {
      $(link).click(function(e){
        e.preventDefault();
        $('#ebook').booklet($(this).data('pg')); // go to page X
      });      
    });
    
    $("#cover-link").click(function(e){
      e.preventDefault();
      $('#ebook').booklet(1); // go to cover page
    });
    
	});
	
  $("#email-edit").focus(function() {
    if(this.value == this.defaultValue) {
      this.value = "";
    }
    }).blur(function() {
      if(!this.value.length) {
        this.value = this.defaultValue;
      }
    });
	
});

function externalLinks()
{
	if(!document.getElementsByTagName) return ;
	var anchors = document.getElementsByTagName("a");
	for(var i=0;i<anchors.length;i++)
	{
		var anchor = anchors[i];
		if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
		{
			anchor.target = "_blank";	
		}
	}
}
window.onload = externalLinks
