$(document).ready(function() {          
         
         var onetimeKeyEvt = false;
         
         // ALT - g Grid control easter egg
         $(document).keydown(function(e) {                      
                if(onetimeKeyEvt 
                   || 
                   /input|textarea|select/.test(e.target.tagName.toLowerCase()) 
                   ||       
                   !(e.altKey && String.fromCharCode(e.keyCode ? e.keyCode : e.which).toLowerCase() == 'g')) {
                        return true;
                };
                
                onetimeKeyEvt = true;                 
                document.body.className += " gridified";
        });         
        
        $(document).keyup(function(e) { 
                onetimeKeyEvt = false;                     
                document.body.className = document.body.className.replace(/(\s)?gridified/, "");
        }); 
        
        // Kick the required CSS into place
        $("body").addClass("js-active");
        
        // Animate the teaser
        if($("#teaser")) {
                $("#teaser").hide();
                setTimeout(function() { $("#teaser").fadeIn('slow').addClass("animate-in"); }, 3000);                        
        };       
        
        // Email
        if($("#david")) {
                $("#alex").append('&#160;<span class="email" id="alex-email"></span>');
                $("#david").append('&#160;<span class="email" id="david-email"></span>');
                
                $("#alex-email").hide();
                $("#david-email").hide();
                
                $("#alex-email").append('(<a href="mailto:alexandre@modernartgalerie.fr">alexandre@modernartgalerie.fr</a>)');
                $("#david-email").append('(<a href="mailto:david@modernartgalerie.fr">david@modernartgalerie.fr</a>)');   
                
                // Wait 3 seconds to try to avoid spambots who have JS activated from scraping the addresses
                setTimeout(function() { 
                        $("#alex-email").fadeIn("slow");
                        $("#david-email").fadeIn("slow");
                },3000);
        };
        
        try{
                Hyphenator.config({
                        remoteloading : false, 
                        onerrorhandler: function (e) {}
                        });
                Hyphenator.run();
        } catch(err) {};
                
        // Are we in a gallery?         
        if(!$("#header") || !($("#header").hasClass("gallery-view"))) {                 
                return;
        };
        
        // Lazy load gallery images
        $("#content-body img").lazyload({ threshold : 900 });
        
        var galleryView = {                  
                current:0,
                chunks:$(".content-block"),
                calculateCurrentElement: function() {
                        var scrollTop = $(window).scrollTop();
                        if(scrollTop != undefined) {  
                                var tmp = galleryView.current;                                
                                galleryView.current = Math.min(galleryView.chunks.length - 1, Math.floor(scrollTop / 992));
                                if(galleryView.current == 0 && tmp > 0) {
                                        $("#header").stop().animate({ 
                                                top:"0px",                                 
                                                height:"100px"
                                        }, 800);   
                                };
                        };
                },                
                updateImagePositions: function() {
                        var sh = parseInt(window.innerHeight ? window.innerHeight : $(window).height(), 10);
                        
                        $("ol.gallery-list img").each(function() {
                                var h = parseInt(this.offsetHeight, 10);                                
                                $(this).css("margin-top", (h && h < sh ? Math.round((sh - h) / 2) : 0) + "px");                               
                        });
                }
        };
        
        $("ol.gallery-list img").each(function() { $(this).load(function() {
                var sh = parseInt(window.innerHeight ? window.innerHeight : $(window).height(), 10),
                    h  = parseInt(this.offsetHeight, 10); 
                $(this).css("margin-top", (h && h < sh ? Math.round((sh - h) / 2) : 0) + "px");$              
        }) });
        
        $(window).bind('resize', galleryView.updateImagePositions);
        
        $("#header").hover(
                function() {                                
                        $(this).stop().animate({ 
                                top:"0px",
                                height:"100px"
                        }, 800);
                },
                function() {
                        $(this).stop().animate({ 
                                top:"-90px",
                                height:"110px"
                        }, 800);
                });
        $("#header a").bind("focus",
                function(e) {                                
                        $("#header").stop().animate({ 
                                top:"0px",                                 
                                height:"100px"
                        }, 800);
                });
                
        $(document).bind("keydown",
                function(e) { 
                        var k = e.which;     
                        if(!(k == 39 || k == 37)) return;
                        
                        galleryView.current += k == 39 ? 1 : -1;
                        
                        if( galleryView.current > galleryView.chunks.length - 1) galleryView.current = 0
                        else if( galleryView.current < 0) galleryView.current = galleryView.chunks.length - 1;
                         
                        galleryView.current = Math.max(0, Math.min(galleryView.current, galleryView.chunks.length));
                        
                        $.scrollTo(galleryView.chunks[galleryView.current]);
                        
                        if(galleryView.current == 0) {
                                $("#header").stop().animate({ 
                                        top:"0px",                                 
                                        height:"100px"
                                }, 800);   
                        } else {
                                $("#header").stop().animate({ 
                                        top:"-90px",                                 
                                        height:"110px"
                                }, 800); 
                        }                                               
                });
                
        $(document).bind("scroll", galleryView.calculateCurrentElement);
        $(document).mousewheel(galleryView.calculateCurrentElement);          
        $(window).load(galleryView.calculateCurrentElement);
        
        $("#header-inner").append('<p id="navigation-aid" lang="en">Press the arrow keys &larr; <span class="amp">&amp;</span> &rarr; to navigate through the gallery</p>');
});




