$(document).ready(function(){
    //prepare navbar
    $('.sitenavli:last').addClass('last');
    $('.sitenavli:first').addClass('first active');
    
    $.localScroll({duration:600});
    
    $('.toplink').click(function(){
        setTimeout("moveFollower();",600); //ipad fail
    });
    
    
    //make follower draggable
    $( "#follower" ).draggable({ 
        axis: 'y' , 
        containment: 'parent'
    });
    
    
    $('.sitenavli').droppable({
      drop: function() { 
          if($(window).scrollTop() == $($('a',$(this)).attr('href')).offset().top){
            $.scrollTo(($(window).scrollTop()+ 1));  
          }
          $.scrollTo($($('a',$(this)).attr('href')));  
      }
    });
    
});


$(window).load(function(){
    //window.scrollTo(0, 1);
    //window.scrollTo(0, 0);//hide bars from mobile safari
    
    makeViewers();
    getRatio();
    
    
});

$(window).resize(function(){
   getRatio();
});

$(window).scroll(function(){
    moveFollower();
});

function lazyLoad(viewer,all){
    
    lazyimg = all? $('img.lazy', viewer) : $('img.lazy:first', viewer);
    if(lazyimg.size()>0){
        lazyimg.each(function(){
            src= $(this).attr('data-src');
            $(this).attr('src',src).load(function(){
                            $(this).removeClass('lazy');
            }); 
        });
           
     }
}

function getRatio(){
    mainH = $('#main').height();
    navH = ($(window).height())/2.5; //how much of the window height is needed for menu height
       
    $('section').each(function() {
       h = $(this).outerHeight();
       ratio = (navH * h/mainH);
       ratio = ratio>15? ratio : 15;
       $('#sitenav_'+ $(this).attr('id')).stop().animate({
            height: ratio
          }, 200, function(){moveFollower()});
    });
}

function moveFollower(){
  
    var cutoff = $(window).scrollTop();
    $('section').removeClass('top');
  
      $('.sectionBlock').each(function(){

        if ($(this).offset().top > cutoff) {
            $(this).parents('section').addClass('top');

            //set menu
            $('.sitenavli').removeClass('active');
            $('#sitenav_'+ $(this).parents('section').attr('id')).addClass('active');

            return false; // stops the iteration after the first one on screen

        }

    });
    
    
    //when scrolling too fast to bottom
    if($('section.top').size()==0){
        $('section:last').addClass('top');
        //set menu
            $('.sitenavli').removeClass('active');
            $('#sitenav_'+ $('section:last').attr('id')).addClass('active');    
    }
  
  
    //set position
    
    followerY = $('.sitenavli.active').position().top;
        $('#follower').stop().animate({
            top: followerY
          }, 300);   
      
    
}

function makeViewers(){
   
    
    //slides
     $('.viewerinit').each(function(){
         
         

         //init the viewer navigation
         if($('.viewerPage',$(this)).size() > 1){
                          
             $(this).jshowoff({ 
                controls:true, 
                autoPlay: false,
                links: false,
                effect: 'none'
            })
                      
         } 
         
         //load all the images
         lazyLoad($(this), 0);
         
         
         $(this).removeClass('viewerinit');  
          
     });
            
        
}

//puls-specific



/*
 * jQuery outerHTML
 *
 * Copyright (c) 2008 Ca-Phun Ung <caphun at yelotofu dot com>
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://yelotofu.com/labs/jquery/snippets/outerhtml/
 *
 * outerHTML is based on the outerHTML work done by Brandon Aaron
 * But adds the ability to replace an element.
 */

(function($){
	$.fn.outerHTML = function(s) {
		return (s) 
			? this.before(s).remove() 
			: $('<p>').append(this.eq(0).clone()).html();
	}
})(jQuery);

startListeners();

function startListeners() {
    $('.loadMore').unbind();
    $('.loadMore').click(function() { 
        getMoreContent($(this));
    });
}

function getMoreContent(moreButton) {
    idsOnScreen = new Array();
    i = 0
    $("div[data-type='" +moreButton.data('type')+"']").each(function() {
        idsOnScreen[i++] = $(this).data('id');
    });
    
    $.ajax({
        type: "POST",
        dataType: 'json',
        data: {
            'idsOnScreen': idsOnScreen
        },
        url:'/ajaxrequest/'+moreButton.data('type')+'/more/',
        beforeSend: function() {
            moreButton.html('bezig...');
            
        },
        success: function(data) {
            moreButton.outerHTML(data['htmlcontent']);
            startListeners();
            moveFollower();
            makeViewers();
            getRatio();
        },
        error: function() {
            moreButton.outerHTML('gegevens opvragen mislukt');
        }
    });
}
