var EVOLVE = window.EVOLVE || {};

// Quick links show/hide
EVOLVE.QuickLinks = (function() {
  return {
    init: function(el) {
      element = $(el);
      element.children('h2').click(function() {
        if ($(this).hasClass('open')) {
          $(this).removeClass('open');
        } else {
          $(this).addClass('open');
        }
        $(this).siblings('.bd').slideToggle();
      });
    }
  }
})();

// Rotate testimonials
EVOLVE.Testimonials = (function() {
  var element, currentPosition, totalElements, width, maxPosition, goToPosition, autoplay;

  function animation() {
    autoplay = setTimeout(function() {
    goToPosition = currentPosition + 1;
    if (goToPosition > maxPosition) {
      goToPosition = 0;
    }
    currentPosition = goToPosition;
    element.children('ul').animate({'margin-left': '-' + goToPosition * width + 'px'}, 'slow', function() {
      animation();
    });
    // } else {
    //   clearTimeout(autoplay);
    // }
    }, 7000);
  }

  return {
    init: function(el) {
      element = $(el);
      currentPosition = 0;
      totalElements = element.find('li').length;
      width = element.width();
      maxPosition = totalElements - 1;
      if (totalElements > 1) {
        animation();
      }

    }
  }
})();

// Team
EVOLVE.Team = (function() {
  return {
    init: function(el) {
      $('#team .photo').pixastic('desaturate');
      element = $(el);

      // Create an element to store name and bio
      $('<div id="person"/>').appendTo(element);

      element.find('li').hover(
        function() {
          $(this).click(function(e) {
            e.preventDefault();

            $(this).siblings('.active').children('.photo').pixastic('desaturate')
            $(this).siblings().removeClass('active');
            $(this).children('.photo').pixastic('revert');
            $(this).addClass('active');

            $('#person').empty();
            $(this).children('.name, .bio').clone().appendTo('#person');
            $('#person .bio').show();
          });
          $(this).children('.name').show();
        },
        function() {
          $(this).children('.name').hide();
        }
      );
    }
  }
})();

// Set value of form input to act as label
// TODO: Decouple
EVOLVE.FormInputLabel = (function() {
  return {
    init: function(el, label) {
      element = $(el);
      element.val(label);
      element.click(function() {
        if ($(this).val() == label) {
          $(this).val('');
        }
        $(this).blur(function() {
          if ($(this).val() == '') {
            $(this).val(label);
          }
        });
      });
    }
  }
})();

// Google map for the contact page
EVOLVE.Map = (function() {
  // The callback from loading the map api
  window.map = function() {
    var location = new google.maps.LatLng(51.479738, -0.2077915);
    var options = {
      zoom: 15,
      scrollwheel: false,
	  mapTypeControl: false,
      center: new google.maps.LatLng(51.47973919647438, -0.2060),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'), options);
    var marker = new google.maps.Marker({
      position: location,
      map: map,
      
      title: 'Evolve'
    });
  }

  return {
    init: function() {
      if ($('#map-canvas').length) {
        $.getScript('http://maps.googleapis.com/maps/api/js?sensor=false&callback=map');
      }
    }
  }
})();




$(document).ready(function() {
  EVOLVE.FormInputLabel.init('#mailing-list .input input', 'enter email address');
  EVOLVE.FormInputLabel.init('#top-tips .email input', 'enter email address');
  EVOLVE.Team.init('#team');
  EVOLVE.QuickLinks.init('#quick-links');
  EVOLVE.Testimonials.init('#testimonials');
  EVOLVE.Map.init();

  $('#flickr a.group').fancybox();
	
  $('p.otherhear').hide();
  
  $('#hear').change(function(){
  	if($(this).val() == 'Other'){
  		$('p.otherhear').slideDown();
  	}
  });
  
  	
	
  if ($('#s1 img').length > 1) {
    $('#s1').cycle();
  }
});
