/* This one time, Luke Giuliani wrote a switch on an enum that either cured cancer, saved Africa from famine, or achieved world peace.
 * There was user error when it got deployed, so it just wrote this code instead as backup.
 */

var toys = {

	contact : {
		init : function(){
			$('#m1_contact, #mantle_phone, .js_contact').click(function(e){
				e.preventDefault();
				if ($(this).hasClass('jsactive')){
					toys.contact.fadeout();
				} else {
					toys.contact.fadein();
				}
			});
			
			$('.js_close_contact').click(function(e){
				e.preventDefault();
				toys.contact.fadeout();
			});
			
			// also, close if the user hits escape
			$(document).bind('keydown', 'esc', toys.contact.fadeout);
		},
		
		fadein : function(){
			$('#m1_contact').addClass('jsactive');
			$('#contact_strip').slideDown();
			toys.blackout.fadein();
		},
		fadeout : function(){
			$('#m1_contact').removeClass('jsactive');
			$('#contact_strip').slideUp();
			toys.blackout.fadeout();
		}
		
	},
	
	clients: {
		matches: [],
		timeout: null,
		current: null,
		
		init: function() {
		
				
			$('.js_searching').submit(function(e) {
				e.preventDefault();
				if (toys.clients.timeout)
				{
					clearTimeout(toys.clients.timeout);
				}

				toys.clients.live_search();
			});

			$('.js_searching input[type=text]').keyup(function(e) {
				if (toys.clients.timeout)
				{
					clearTimeout(toys.clients.timeout);
				}
				
				toys.clients.timeout = setTimeout('toys.clients.live_search();', 400);
			});
			
			$('.js_sample_search').click(function(e) {
				// e.preventDefault();
				$('.js_searching input[type=text]').val($(e.currentTarget).attr('href').replace('#', ''));
				$('.js_searching').submit();
			});
			
			$('.js_cancel_search').click(function(e) {
				e.preventDefault();
				$('.js_searching input[type=text]').val('');
				$('.js_searching').submit();
			});

			if (window.location.hash)
			{
				$('.js_searching input[type=text]').val(window.location.hash.replace('#', ''));
				$('.js_searching').submit();
			}
			
			// provide optional case (in)sensitivity
			$.extend($.expr[':'], {
			  'containsi': function(elem, i, match, array)
			  {
			    return (elem.textContent || elem.innerText || '').toLowerCase()
			    .indexOf((match[3] || "").toLowerCase()) >= 0;
			  }
			});
			
		},
		
		live_search: function() {
			var q = $('.js_searching input[type=text]').val();
			if (q)
			{
				$('.js_searching').addClass('currently_searching');
				if (toys.clients.current != q)
				{
					toys.clients.current = q;
					$('.listd > div').fadeOut('fast');
					toys.clients.matches = $.makeArray(
						$(".no_case_study > .desc:containsi('" + q + "')").parents('.client') // get the summary text on non-case study
						.add($(".more_desc:containsi('" + q + "')").parents('.client')) // get the body text on case study
						.add($(".case_study > .desc:containsi('" + q + "')").parents('.client')) // make sure we catch the words 'case study'
						.add($("." + q)) // catch tag/class matches
					);

					toys.clients.show_next_match();
				}
			} else {
				$('.listd > div').fadeIn('fast');
				$('.js_searching').removeClass('currently_searching');
			}
		},
		
		show_next_match: function() {
			if (toys.clients.matches.length > 0)
			{
				next = toys.clients.matches.shift();
				$(next).slideDown('fast', toys.clients.show_next_match);
			}
		}
	},
	
	whoareyou: {
		current_dude: null,
		
		init: function() {
			if ($('.js_alldudes').length > 0)
			{
				$('.js_alldudes > div').hide();
			}
		},
		
		clear_choice: function()
		{
			$('.js_' + toys.whoareyou.current_dude).fadeOut('slow');
			$('.js_choose_strip').fadeIn('slow');
			$('#choose').fadeIn('slow');
		},
		
		jump_to: function(who) 
		{
			switch(who)
			{
				case 'busy':
				case 'web':
				case 'visionary':
				case 'product':
				case 'newbie':
				case 'none-of-these':
					if (toys.whoareyou.current_dude)
					{
						$('.js_' + toys.whoareyou.current_dude).fadeOut('slow');
					}
					$('.js_' + who).fadeIn('slow');
					$('.js_choose_strip').fadeOut('slow');
					$('#choose').fadeOut('slow');
					toys.whoareyou.current_dude = who;
					break;
				case '':
				case 'choose':
					toys.whoareyou.clear_choice();
					break;
				default:
					break;
			}
		}
	},
	
	
	whatwedo: {
		current: null,
		
		init: function() {
			$('.js-luke').click(function(e) {
				toys.whatwedo.show('luke');
			});

			$('.js-will').click(function(e) {
				toys.whatwedo.show('will');
			});
			
			toys.whatwedo.reset();

			if (window.location.hash == "#luke")
			{
				toys.whatwedo.show('luke');
			}

			if (window.location.hash == "#will")
			{
				toys.whatwedo.show('will');
			}

		},
		
		show: function(who) {

			if (toys.whatwedo.current)
			{
				toys.whatwedo.reset();
			}
			
			else
			{
				$('.js-bio-container-' + who).fadeIn();
				var whoelse = (who == 'luke') ? 'will' : 'luke';
				$('.js-' + whoelse).fadeOut();
				toys.whatwedo.current = who;			
				$('.the_team').addClass('the_team_active');				
			}

		},
		
		reset: function() {
			$('.js-luke').fadeIn();
			$('.the_team').removeClass('the_team_active');
			$('.js-will').fadeIn();
			$('.js-bio-container-will').fadeOut();
			$('.js-bio-container-luke').fadeOut();
			toys.whatwedo.current = null;
		}
	},
		
	
	blackout: {
	
		init : function(){
			$('#blackout').css('display','none');
			$('#blackout').click(function(){
				toys.blackout.fadeout();
				if ($('#contact_strip').css('display') == "block"){
					toys.contact.fadeout();
				}
			});
		},
		
		fadein : function(){
			toys.max_height('#blackout');
			$('#blackout').fadeIn();
		},
		
		fadeout : function(){
			$('#blackout').fadeOut();
		},
		
		toggle : function(){
			if ($('#blackout').css('display') == 'none'){
				this.fadein();
			} else {
				this.fadeout();
			}
		}
	},

	
	max_height : function (el){
		$(el).css('height', $(document).height());
	},
	
	grid_overlay : {
	
		init : function(){

			$('.js_grid_overlay').click(toys.grid_overlay.toggle);

			$(document).keypress(function(e) {
				// console.log(e.charCode);
			  if (e.charCode == ('103') || e.charCode == ('71')) {
					toys.grid_overlay.toggle();
			  }
			});						

			
		},
	
		toggle: function(){
			toys.max_height('.js_grid_overlay');
			$('.js_grid_overlay').slideToggle();
		}
	},
	
	simple_ass_tabs : {
	
		tab_containers: null,
	
		init: function(){
			toys.simple_ass_tabs.tab_containers = $('.js_tab_these_yo .service');
			if (toys.simple_ass_tabs.tab_containers.length > 0)
			{
				$('.js_tab_yo a').click(function (e) { toys.simple_ass_tabs.show_tab($(e.currentTarget)); } );

				if (window.location.hash.length > 0)
				{
					toys.simple_ass_tabs.show_tab($('.js_tab_yo a[href="' + window.location.hash + '"]'));
				} else
				{
					toys.simple_ass_tabs.show_tab($('.js_tab_yo a').first());
				}

			}
		},
		
		show_tab: function(tab) {
			toys.simple_ass_tabs.tab_containers.hide();
			$('.js_tab_yo a').removeClass('selected');
			$(tab).addClass('selected');
			toys.simple_ass_tabs.tab_containers.filter($(tab).attr('hash')).show();
			// $(window).scrollTop(-10);
		}
	
	},
	

	init: function(){
		
		// seriously. Who uses ie6. You certainly shouldn't. We don't support it anymore anyway. http://ie6funeral.com/
		if ($.browser.msie && ($.browser.version == "6.0"))
		{
			//alert('nojs');
			return;
		}
		
		toys.contact.init();
		toys.clients.init();
		toys.whoareyou.init();
		toys.whatwedo.init();
		toys.blackout.init();
		toys.simple_ass_tabs.init();
//	toys.grid_overlay.init();
//	toys.scribble.init();
		
		/* let them know it's all bout the javascript */
		$('body').addClass('js_enabled');
		
		$('.js_scrollto').click(function(e) {
			e.preventDefault();
			$.scrollTo($($(e.currentTarget).attr('href')), 300);
		});
	
		
		$.history.init(function(hash){
			toys.whoareyou.jump_to(hash);

			var sel = '.js_tab_yo a[href="#' + hash + '"]:visible';
			if ($(sel).length > 0)
			{
				toys.simple_ass_tabs.show_tab($(sel));
			}
		}, { unescape: ",/" });

	}
}


// luke make this better
$(document).ready(toys.init);
