// This makes animations run a bit smoother
jQuery.fx.interval = 10;

// A function to check whether an element is visible
function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop()-50;
    var docViewBottom = docViewTop + $(window).height() - 200;

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
      && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop) );
}

// Slide the header to the first visible slice

function slideHeader(){
	var header = $("#header");
	var slices = $(".slice");

	if(isScrolledIntoView(header)==false){
		// We need to change the position of the header to the closest visible slice
		var docViewTop = $(window).scrollTop(); // Current scroll position
		var docViewBottom = docViewTop + $(window).height();
		
		$(slices).each(function(index){
			var offset = $(this).offset(); // Slice coordinates
			
			if(docViewTop <= offset.top && offset.top < docViewBottom && $(this).hasClass("currentSlice") == false){ // If the slide is visible

			$(".currentSlice").removeClass("currentSlice").children(".wrapper").stop(false,false).animate({paddingTop: 0});
				$(this).addClass("currentSlice").children(".wrapper").stop(false,false).animate({paddingTop: '55px'},{speed:"fast",complete: function(){
					$(header).css({
						top: $(this).offset().top,
						opacity: 0
					}).animate({
						opacity: 1
					},{speed: "fast"});
				}
				});
				$(".selected-slice").removeClass("selected-slice");
				$("#navigation li a[href='#"+ this.id +"']").addClass('selected-slice');
				return false;
				}
			
		});
		
	}

}
$(document).ready(function(){

	// Lightbox functionality
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_rounded'});
	
	// Display zoom icon on hover
	$("a[rel^='prettyPhoto']").hover(function(){

		$(this).children("img").stop(false,false).animate({opacity:0.4},"fast");
	
	},function(){
	
		$(this).children("img").stop(false,false).animate({opacity:1},"fast");
	
	});
	
	// Slide scroll to the clicked slice
	$("a[rel^='tn-slide']").click(function() {

          if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {

            var $target = $(this.hash);

            $target = $target.length && $target

            || $('[name=' + this.hash.slice(1) +']');

            if ($target.length) {

              var targetOffset = $target.offset().top;
			var docViewTop = $(window).scrollTop();
			
			if(docViewTop > targetOffset){
				//sliding upwards
				$('html,body').animate({scrollTop: targetOffset}, 1000);
			}else{
				//sliding downwards
				$('html,body').animate({scrollTop: targetOffset-55}, 1000);
			}
             return false;
            }

          }

        });
	
	// Slide the header to the first visible slice.
	$(window).scroll(function(){
		slideHeader();
	});
	// Cufon font replacement rules
		Cufon.replace("h1,h2,h3,h4,h5,#slogan,#navigation li a,.cta2 em,.cta1 em,.delimiter, .forms label, .read-more em");
		Cufon('#navigation li a',{textShadow: '0px -1px 2px #000'});                
		Cufon('#footer h3',{textShadow: '0 -1px 1px rgba(0,0,0, 0.3)'});
		Cufon('.slice h1, .slice h2, .dark-post h2, .cta1 em, .cta2 em, #footer h2',{textShadow: '1px 1px 1px rgba(0,0,0, 0.6)'});
		Cufon('.slice-light h1, .slice-light h2',{textShadow: '1px 1px 1px rgba(256,256,256, 0.6)'});
        
    $('#slice4-link').click(function(){
        $('.flipbook').flipbook.playSlideshow();
        $('#flipbook_btn_play').toggle();  
        $('#flipbook_btn_pause').toggle();
    });
    $('#flipbook_btn_play').click(function(){
        $('.flipbook').flipbook.playSlideshow();
        $('#flipbook_btn_play').toggle();  
        $('#flipbook_btn_pause').toggle();
    });
    $('#flipbook_btn_pause').click(function(){
        $('.flipbook').flipbook.pauseSlideshow();
        $('#flipbook_btn_play').toggle();  
        $('#flipbook_btn_pause').toggle();
    });
    
    $('a').click(function() {
      var $a = $(this);
      var href = $a.attr('href');

      // see if the link is external
      if ( (href.match(/^http/)) && (! href.match(document.domain)) ) {

        // if so, register an event
        var category = 'outgoing'; // set this to whatever you want
        var event = 'click'; // set this to whatever you want
        var label = href; // set this to whatever you want

        pageTracker._trackEvent(category, event, href);
      }
    });
});

