/* 

Author: Yves Van Broekhoven
Date: 2009-06-13

Fueled by jQuery

*/

// Note: No conflict function

// This function must be called after including the jQuery javascript file, but before including any other conflicting library, 
// and also before actually that other conflicting library gets used, in case jQuery is included last. noConflict can be called at 
// the end of the jQuery.js file to globally disable the $() jQuery alias. jQuery.noConflict returns a reference to jQuery, so it 
// can be used to override the $() alias of the jQuery object.
// Use jQuery via jQuery(...)
//jQuery.noConflict();

jQuery(document).ready(function(){
    
    /* Local scroll
    -----------------------------------------------------*/
    $("#nav a, .btn-top a").click(function(){
        var target = $(this).attr("href");
        $(document).scrollTo(target, 1500, {easing:'easeOutCirc'});
        return false;
    });
    
     $("#btns-slv a").click(function () {  
        var target = $(this).attr("href");
        $(document).scrollTo(target, 1500, {easing:'easeOutCirc'});
        return false;
   	});

    
    
    /* Toggle Refs
    -----------------------------------------------------*/
    var toggle_btn      = '<div id="btn-alle-refs" class="btn slide"><a href="#" class="slider btn-slide">bekijk alle referenties</a></div>';
    var total_height    = $("#refs-list #panel").height();
    $("#refs-list #panel").height(total_height/4);
    $("#refs-list").append(toggle_btn);
    
    $("#btn-alle-refs a").click(function(){
        if (!($(this).hasClass("active"))){
            $("#refs-list #panel").animate({ height: total_height }, 1000);
        } else {
            $("#refs-list #panel").animate({ height: (total_height/4) }, 1000);
        }
        $("#refs-list-box").toggle();
        $(this).toggleClass("active");
        return false;
    });
    
    
    /* Slideshow
    -----------------------------------------------------*/
    var slideshow_duration  = 3000  /* in milliseconds */
    var slideshow_btn       = '<a href="#" class="btn-pauze btn"></a>';
    
    $(".slideshow").each(function(){
        var length          = $("img", this).length;
        if (length > 1) {
            $("img", this).hide();
            $("img:first", this).show();
            $(this).prepend(slideshow_btn);
            $(".btn", this).css({ "opacity":"0" });
        }
    });
    
    $(".slideshow").hover(
        function(){
            var btn = $(this).children(".btn");
            if (btn.hasClass("btn-pauze")){
                $(".btn", this).stop().animate({ opacity:1 }, 500);
            }
        }, 
        function(){
            var btn = $(this).children(".btn");
            if (btn.hasClass("btn-pauze")){
                $(".btn", this).stop().animate({ opacity:0 }, 500);
            }
        }
    );
    
    $(".slideshow .btn").click(function(){
        if ($(this).hasClass("btn-pauze")){
            var state   = 1;
        } else {
            var state   = 0;
        }
        if (state == 1) { 
            $(this).closest(".slideshow").addClass("paused");
            $(this).removeClass("btn-pauze").addClass("btn-play");
        }
        if (state == 0) { 
            $(this).closest(".slideshow").removeClass("paused");
            $(this).removeClass("btn-play").addClass("btn-pauze");
        }
        return false;
    });
    
    function slideTheShow()
    {
        $(".slideshow").each(function(){
            var length      = $("img", this).length;
            if (length > 1 && (!($(this).hasClass("paused"))) && $(this).is(":visible") ){
                var curr_img        = $("img:visible", this);
                var curr_img_pos    = $("img", this).index(curr_img);
                curr_img.css({ "z-index":"10" });
                if (curr_img_pos == (length - 1)){
                    var next_img    = $("img:first", this);
                } else {
                    var next_img    = curr_img.next();
                }
                next_img.css({ "display":"block" });
                curr_img.fadeOut(1000, function(){
                    curr_img.css({ "z-index":"0" });
                    next_img.css({ "z-index":"10" });
                }); 
            }
        });
        setTimeout(slideTheShow, slideshow_duration);
    }
    setTimeout(slideTheShow, slideshow_duration);
    
    
    /* Event slider
    -----------------------------------------------------*/
    var btn_back    = '<a href="#" class="btn-back">vorige</a>';
	var btn_next    = '<a href="#" class="btn-next">volgende</a>';
	
    $(".book-item").each(function(){
        if ($(".event", this).length > 1){
            $(".event", this).hide().children(".slideshow").addClass("paused");
            $(".event:first", this).show().children(".slideshow").removeClass("paused");
            $(this).append(btn_back).append(btn_next);
            $(".btn-back", this).animate({ opacity: 0 });
        }
    });
    
    $(".book-item .btn-back, .book-item .btn-next").click(function(){
            var parent          = $(this).closest(".book-item");
            var length          = $(".event", parent).length;
            var curr_event      = $(".event:visible", parent);
            var curr_event_pos  = $(".event", parent).index(curr_event);
            if ($(this).hasClass(".btn-next") && (curr_event_pos) < (length - 1)){
                var next_event  = curr_event.next();
                curr_event_pos  += 1;
            } else if ($(this).hasClass(".btn-back") && (curr_event_pos - 1) >= 0){
                var next_event  = curr_event.prev();
                curr_event_pos  -= 1;
            }
            if (next_event) {
                curr_event.children(".slideshow").addClass("paused");
                curr_event.fadeOut(500, function(){
                    next_event.fadeIn(500).children(".slideshow").removeClass("paused").children(".btn-play").removeClass("btn-play").addClass("btn-pauze").animate({ opacity:0 }, 1);
                });
            }
            sliderNavigation(curr_event_pos, length);
        return false;
    });
    
    function sliderNavigation(curr_event_pos, length, parent)
    {
        if (curr_event_pos >= (length - 1)){
            $(".btn-next", parent).animate({ opacity: 0 }, function(){ $(this).hide(); });
        } else {
            $(".btn-next", parent).show().animate({ opacity: 1 });
        }
        if (curr_event_pos - 1 < 0){
            $(".btn-back", parent).animate({ opacity: 0 }, function(){ $(this).hide(); });
        } else {
            $(".btn-back", parent).show().animate({ opacity: 1 });
        }
    }

});


/* IE 7 Cleartype fix
-----------------------------------------------------*/
jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
};

