
// remap jQuery to $
(function($){
    $(document).ready(function(){        
        // Home page slideshow
        var rotations = 0;
        var activeContainer = 1;    
        var currentImg = 1;
        var animating = false;
        var currentZindex = 2;
        var navigate = function(direction) {
            // Check if no animation is running. If it is, prevent the action
            if(animating) {return;}
            // Check which current image we need to show
            if(direction == "next") {
                currentImg++;
                if(currentImg == rotations+1)
                    currentImg = 1;
            } else {
                currentImg--;
                if(currentImg == 0)
                    currentImg = rotations;
            }
            // Check which container we need to use
            var currentContainer = activeContainer;
            activeContainer = (activeContainer == 1) ? 2 : 1;
            showImage(currentImg, currentContainer, activeContainer);
        }
        var showImage = function(imageNum, currentContainer, activeContainer) {
            animating = true;
            // Make sure the new container is always on the background
            $("#heroImg" +currentContainer).css({'z-index':currentZindex});
            // Set the background image of the new active container
            $("#heroImg" + activeContainer).css({
                'background':'url('+$("#splash img:eq("+(imageNum-1)+")").attr("src")+') no-repeat 0 0 transparent',
                'display':'block',
                "z-index": currentZindex-1
            });
            // Fade out the current container
            $("#heroImg" + currentContainer).fadeOut(1500,function() {animating = false;});
        }
        if($('#splash img').length>1){
            // hide splash
            $("#splash ul").hide();
            // container structures
            $("<div id=\"heroImg1\" class=\"heroImg\" />").appendTo("#splash");
            $("<div id=\"heroImg2\" class=\"heroImg\" />").appendTo("#splash");
            rotations = $("#splash img").length;
            currentImg = rotations;
            // set the first image
            if(rotations>0)
                navigate("next");
            // Start playing the animation
            var interval;
            var slideshowSpeed = 6000;
            interval = setInterval(function() {navigate("next");}, slideshowSpeed);
        }

    });
})(this.jQuery);



// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};



// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);



