/* Author: Cubic */

jQuery(function() {
  $(document).ready(function() {
    
    $('.emli').each(function() 
    {
      var content = $(this).html();
      var link = content;
      link = link.replace(/ dot /gi, '.');
      link = link.replace(/ at /gi, '@' );
      var a = '<a href="mailto:'+link;
      if($(this).hasClass('candidate')) a += '?subject=Candidate enquiry';
      a += '">'+link+'</a>';
      $(this).after(a);
      $(this).remove();
    });
    
    // Make all of LinkedIn box clickable
    $('footer .linkedin, footer .email').click(function(e)
    {
      var href = $(this).find('a').attr('href');
      window.location.href = href;
    });
    
    // Match columns to the tallest 
    if($('#content > .col1').length > 0)
    {
      matchColumns();
    };
    
    function matchColumns() 
    {
      var col1 = $('#content > .col1');
      var col2 = $('#content > .col2');
      
      if(col1.height() > col2.height())
      {
        col2.css('min-height', col1.height());
      }
      else
      {
        col1.css('min-height', col2.height());
      };
    }
    
    // If case-studies
    if($('#container.case-studies').length > 0)
    {
      var currentCaseStudy;
      
      function initCaseStudies()
      {
        // Get default
        currentCaseStudy = $('#c0');
        
        // Check for hash link..?
        var hash = window.location.hash;
        hash = hash.substring(1);
        if(hash)
        {
          // Show hashed CS
          showCaseStudy(hash);
        };
        
        // Case study click event
        $('.case-studies-col li a').click(function()
        {
          handleCaseStudyClick($(this));
        });
      }
      
      function handleCaseStudyClick(el)
      {
        // Get new id
        var id = el.attr('href');
        id = id.substring(1);
        
        // Show CS
        showCaseStudy(id);
      };
      
      function showCaseStudy(id)
      {
        // Hide old & show new
        if(currentCaseStudy.attr('id') != id)
        {
          $('.case-studies-col li a').removeClass('active');
          $('.case-studies-col li a.'+id).addClass('active');
          currentCaseStudy.fadeOut(function(e)
          {
            currentCaseStudy.removeClass('active');
            matchColumns();
          
            $('#'+id).fadeIn(function(e)
            {
              $(this).addClass('active');
              currentCaseStudy = $(this);
            });
          });
        };
      };
      
      initCaseStudies();
    };
    
    // If slideshow
    if($('#slideshow').length > 0)
    {
      var slides, numSlides, currentSlideIndex;
      var indicators;
      var slideTimer, hoverTimer;
      var isChanging;
      
      function initSlideshow()
      {
        slides = $('#slideshow .slide');
        numSlides = slides.length;
        currentSlideIndex = 1;
        isChanging = false;
        
        // If a slide is clicked
        slides.click(function()
        {
          handleSlideClick($(this));
        });
        
        // Check if slideshow is needed
        if(numSlides > 1) 
        {
          // Set up indicators
          addIndicators();
          
          // Start timer 
          slideTimer = setTimeout(nextSlide, 5000);
        }
      }
      
      function handleSlideClick(el)
      {
        var href = el.find('a').attr('href');
        window.location.href = href;
      }
      
      function addIndicators()
      {
        // Begin html for indicators
        indicators = '<ul class="slideshow-indicators">';
        
        // Add correct number
        for(var i = 0; i < numSlides; i++)
        {
          indicators += '<li';
          if(i == currentSlideIndex-1)
          {
            indicators += ' class="active" ';
          }
          indicators += '>&bull;</li>';
        }
        
        // Close html
        indicators += '</ul>';
        
        // Append to slideshow
        $('#slideshow').append(indicators);
        
        // Get indicators as jq object
        indicators = $('#slideshow .slideshow-indicators li');
      }
      
      function nextSlide()
      {
        clearTimeout(slideTimer);
        
        // Prevent it from changing in the middle of a change
        if(!isChanging)
        {
          isChanging = true;
          
          // Work out next slide index
          var nextSlideIndex = (currentSlideIndex < numSlides) ? currentSlideIndex+1 : 1;
          
          // See if it is neccessary to change
          if(nextSlideIndex != currentSlideIndex)
          {
            // FadeIn new slide
            $(slides[nextSlideIndex-1]).fadeIn('slow', function(e)
            {
              $(this).addClass('active');
            });
            
            // Change indicator
            if(indicators)
            {
              $(indicators[currentSlideIndex-1]).removeClass('active');
              $(indicators[nextSlideIndex-1]).addClass('active');
            }
            
            // FadeOut old slide
            $(slides[currentSlideIndex-1]).fadeOut('slow', function(e)
            {
              $(this).removeClass('active');
              isChanging = false;
            });
        
            // Reset currentSlideIndex
            currentSlideIndex = nextSlideIndex;
          }
          else
          {
            isChanging = false;
          }
          
          // Reset timer
          slideTimer = setTimeout(function()
          {
            nextSlide();
          }, 5000);
        }
      }
      
      initSlideshow();
    }
    
    // If register form
    if($('#register').length > 0)
    {
      var label = $('#register label');
      var input = $('#register input[type="text"]');
      
      input.val('');
      
      input.bind('select, click, focus', function(e)
      {
        label.hide();
      });
      
      input.blur(function(e)
      {
        if($(this).val() == "") label.show();
      });
      
      $('#register').submit(function(e)
      {
        input.val('');
        label.show();
        $('.response p').hide().delay(200).fadeIn().delay(3000).fadeOut();
        return false;
      });
    }
    
    var f3HoverTimer;
    $('.f3').mouseenter(function(e)
    {
      clearTimeout(f3HoverTimer);
      
      var overlay = $(this).children('.overlay');
      $('.f3 .overlay.active').fadeOut();
      f3HoverTimer = setTimeout(function(e)
      {
        overlay.fadeIn('fast');
        overlay.addClass('active');
      }, 200);
    });
    
    $('.f3').mouseleave(function(e)
    {
      var overlay = $(this).children('.overlay');
      overlay.fadeOut('slow');
    });
    
    $('.f3').click(function(e)
    {
      var href = $(this).find('a').attr('href');
      window.location.href = href;
    });
    
    // If contact map
    if($('#image-map').length > 0)
    {
      var image = $('#image-map .image figure img');
      var map   = $('#image-map .map');
      
      $('.show-map a').click(function(e)
      {
        $('.show-map').hide();
        image.hide();
        map.css('visibility', 'visible').css('height', 'auto').show();
        
        return false;
      });
      
      $('.hide-map a').click(function(e)
      {        
        map.hide();
        image.show();
        $('.show-map').show();
        
        return false;
      });
    }
    
  });
});
