(function($) { 
    $(document).ready(function(){ 
        var navResponse = $('#secondaryNavResponse');
        /*
         * This is the function for the secondary navigation hover text.
         */
        var data = {
            'menu-item-35' : 'Are you the Right Fit?',
            'menu-item-34' : 'Apply Now',
            'menu-item-33' : 'Request a Quote',
            'menu-item-32' : 'Ask us Anything'
        };
        
        $.each(data,function(index,value){
            $('#' + index).mouseenter(function(){
               navResponse.html(value) 
            }).mouseleave(function(){
               navResponse.html('');
            });
        });
        
        $('a').each(function() {
           var a = new RegExp('/' + window.location.host + '/');
           if(!a.test(this.href)) {
               $(this).click(function(event) {
                   event.preventDefault();
                   event.stopPropagation();
                   window.open(this.href, '_blank');
               });
           }
        });
        /*
         * End of the secondary navigation hover text.
         */
        
        /*
         * First we engage easyslider for the regular pages
         */
        
        var hash = window.location.hash;
        if (hash == '#deploy' || hash == '#discover' || hash == '#develop') {
            $('#slider li').hide();
            $(hash).show();
            scroll(0,0); 
        }
        else {
            $('#slider:not(.stop)').easySlider({
                auto: true,
                continuous: true,
                numeric: true,
                speed:1500,
                pause : 3000
            });
        }
        
        
        /*
         * We also engage easyslider for the work slider - it's a bit more complicated
         */
        $('#workslider .carouselLeft:eq(0)').fade(); //the first one doesn't start unless we tell it to
        $('#workslider').easySlider({
            auto: true,
            continuous: true,
            controlsShow: false,
            //numeric: true,
            speed:1000, 
            pause: 14000,
            startAnimation : function(){
                $('#workslider .carouselLeft:eq(' + id + ') img:not(:first-child)').hide();
                $('#workslider .carouselLeft:eq(' + id + ') img:first-child').show();
                var id =  parseInt(this.id) +1 ;
                
                
                /*
                 * Stop the last fade!
                 */
                $('#workslider .carouselLeft:eq(' + (id + 1)%5 + ')' ).fade({
                    stop : true
                });
                
                
                /*
                 * This will select the right project in the tertiary nav (squares)
                 */
                $('#tertiaryNav > ul > li').removeClass('active');
                if (this.last == 'next') {
                    this.last = id;
                }
                this.last = this.last % 5;
                $('#tertiaryNav > ul > li:eq(' + (this.last)+ ')' ).addClass('active');
                
                
                /*
                 * Override the default selecting the current element; it does it
                 * normally after the animation is done
                 */
                $('#controls > a').removeClass('current');
                $("#controls > a:eq(" + (this.last) + ')' ).addClass('current');
            },
            finishAnimation : function(){
                var id =  (parseInt(this.id) +1) %5 ;
                /*
                 * Start the fade!
                 */
                $('#workslider .carouselLeft:eq(' + id + ')' ).fade();
            }
            
        });
        
        $(".controls a").click(function(){
            var index = $(this).index() + 1;
            var parent = $(this).closest('li')  ;
            parent.find('.carouselLeft').fade({
                select : index - 1
            });
            return false;
        });
        
        /*
         * If we click on the thumbnails in the work carousel, then we stop the action.
         */
//        $("#workslider .workThumb img").click(function(){
//           var imageNum = $(this).index();
//           var parentNum = $(this).closest('.carouselRight').prev().parent().index();
//           $("#workslider .carouselLeft:eq(" +parentNum + ")").fade({
//               id : parentNum,
//               stop : true,
//               select : imageNum
//           });
//        });
        
        /*
         * if the tertiary nav is clicked, then we just simulate the click on 
         * the built-in feature from the easySlider.
         */
//        $("#tertiaryNav > ul > li").click(function(){
//            var workNum = $(this).index();
//            $('#controls > li:eq(' + workNum + ')' ).click();
//        });
    });
   
})(jQuery);


(function($) {
    $.fn.fade = function (options){
        var defaults = {
            id : false,
            stop : false,
            select : NaN,
            speed : 500
        };
        if (typeof(window.state) == 'undefined') window.state = 0;
        var opts = $.extend(defaults, options);
        var parent = $(this);
        var imageSet = $('img',parent);
        var pause = 14000 / imageSet.length;
        
        imageSet.slice(1).hide();
        imageSet.eq(0).show();
        imageSet.eq(0).addClass('active');
        
        if (!isNaN(opts.select)){
            clearInterval(window.state);
            var next = $("img:eq(" + opts.select + ")",parent);
            $('img',parent).hide();
            next.show();
            $('.controls a',parent.parent()).removeClass('current');
            $('.controls a:eq(' + opts.select+ ')',parent.parent()).addClass('current');
            return;
        }
        
        if (opts.stop == true){
            clearInterval(window.state);
            $('.controls a',parent.parent()).removeClass('current');
            $('.controls a:eq(0)',parent.parent()).addClass('current');
            return;
        }
        //hide all but the first element
        
        window.state = setInterval(function(){
            fadeinout(parent); 
        },pause);
        
        
        var fadeinout = function(parent){
            var prev = $(".active",parent);
            var next = prev.next().length == 0 ? $('img:eq(0)',parent) : prev.next();
            prev.fadeOut(opts.speed,function(){
               prev.removeClass('active');
               next.addClass('active');
               next.fadeIn(opts.speed);
               $('.controls a',parent.parent()).removeClass('current');
               $('.controls a:eq(' + next.index() + ')',parent.parent()).addClass('current');
            });
        }
    }
})(jQuery);



