$(document).ready(function() {
			
	//	*****  ----------------------------------------------------------------------------
	//	*****  		Footer tabs
	//	*****  ----------------------------------------------------------------------------
			
	$('h3.cf-tab-1').click(function(){
	
		$(this).removeClass('deselected').addClass('selected');
		$('h3.cf-tab-2').removeClass('selected').addClass('deselected');
	
		$('div.cf-tab-2').slideUp(1000, function(){
			$('div.cf-tab-1').slideDown(1000);
		});
	});
	
	$('h3.cf-tab-2').click(function(){
	
		$(this).removeClass('deselected').addClass('selected');
		$('h3.cf-tab-1').removeClass('selected').addClass('deselected');
	
		$('div.cf-tab-1').slideUp(1000, function(){
			$('div.cf-tab-2').slideDown(1000);
		});
	});
	
	$('#cf-box-3 ul li a').hover(function(){
		
		$(this).stop().clearQueue().animate({'background-color':'#ff00ff'},550)
		
	},function(){
	
		$(this).stop().clearQueue().animate({'background-color':'#3FA9F5'},500)
		
	});
	
	
	
	
	//	*****  ----------------------------------------------------------------------------
	//	*****  		Drop down menu
	//	*****  ----------------------------------------------------------------------------
	
	$('#navigation > ul li').hover(function(){
		$(this).find('ul').stop().clearQueue().slideDown(400);
	}, function(){
		
		$(this).find('ul').stop().clearQueue().slideUp(400,function(){ $(this).css('height', 'auto') });
	});
	
	
	
	
	
	
	//	*****  ----------------------------------------------------------------------------
	//	*****  		Social button fade between footer
	//	*****  ----------------------------------------------------------------------------
	
	$('#footer-social').find('a').each(function(){
	
		bk = $(this).css('background-image');
		$(this).append('<div class="fadeIn"></div>');
		$(this).find('.fadeIn').css({'background-image': bk});
		
		$(this).hover(function(){
			$(this).find('.fadeIn').dequeue().fadeTo(500,1);
		}, function(){
			$(this).find('.fadeIn').dequeue().fadeTo(500,0);
		});
			
	});
	
	
	
	
	
	//	*****  ----------------------------------------------------------------------------
	//	*****  		Header form drop down
	//	*****  ----------------------------------------------------------------------------
	
	hf_o_height = $('#enquiry').css('height') ;
	hf_o_targ = '360px';
	hf_o_targ_nopx = '360';
	hf_o_time = 1000 ;
	hf_o_timer = false ;
	hf_f_timer = false ; //First time open timer used to check for @ if autocompleted 
	lockopen = false ;
	
	$('.eb-cb').click(function(){
		$('#jqfix').focus() ; // Used so browser still thinks it's focused
	});
	
	function bindReactions(){
		// Drop on @
		$("#enquiry input[name=email]").keypress(function(event){
			if(event.which == 64){
				$("#enquiry").animate({height: hf_o_targ},1000);
			}
		});
		
		$('#enquiry, #enquiry input').find('input[type=text]').each(function(){
			
			// Store default value in 'rel' for reference.
			$(this).attr('rel', $(this).val());
			
			// Check if field is empty or placeholder changed.
			$(this).focus(function(){
				if($(this).val() == "" || $(this).val() == $(this).attr('rel')){
					$(this).val('') ;
				}
			});
			
			// If unchanged or empty, restore placeholder.
			$(this).blur(function(){
				if($(this).val() == "" || $(this).val() == $(this).attr('rel')){
					$(this).val( $(this).attr('rel') ) ;
				}
				
			});
		});
		
		// Closes it if not being used
		$('#enquiry').focusout(function(){
			
			if(lockopen != true){
			
				hf_o_timer = setTimeout(function(){
					$('#enquiry').animate({height: hf_o_height},hf_o_time).find('input[type=submit]').fadeTo(hf_o_time/2,0,function(){$(this).css('display','none');});
					
				},hf_o_time);
			
			}
			
		});
		
		
		
		// Re-opens to previous state if re-entered
		$('#enquiry').focusin(function(){
			clearTimeout(hf_o_timer)
			if($('#enquiry input[name=email]').val() != "" && $('#enquiry input[name=email]').val() != $('#enquiry input[name=email]').attr('rel')){
				$('#enquiry').animate({height: hf_o_targ},hf_o_time).find('input[type=submit]').fadeTo(hf_o_time*2,1);
			}
		});
		
		// Opens when @ is typed
		$('#enquiry input[name=email]').keypress(function(event){
			if(event.which == 64){
				$('#enquiry').animate({height: hf_o_targ},hf_o_time).find('input[type=submit]').fadeTo(hf_o_time*2,1);
			}
		});
	}
	
	function unbindReactions(){
	
		$('#enquiry').unbind('focusout');
		$('#enquiry').unbind('focusin');
		$('#enquiry input[name=email]').unbind('keypress');
	}
	
	bindReactions();
	
	// Ajax magic
	$('#enquiry-form').submit(function(){
		
		$('#jqfix').focus() ;
		
		$(this).find('input[type=text]').each(function(){
			if($(this).attr('rel') == $(this).val()){
				$(this).val('');
			}
		});
		
		// Action to signify click before AJAX starts
		//unbindReactions();
		lockopen = true ;
		subDOM = $(this).find('input[type=submit]') ;
		subDOM.attr('disabled','disabled');
		subDOM.val('One moment please...');
		
		// 'this' refers to the current submitted form
		var str = $(this).serialize();
		
		$.ajax({
		   type: "POST",
		   url: $('#logo a').attr('href')+"index.php/ajax/enquiry-box/",
		   data: str,
		   success: function(msg){
				
				messagetarget = $('#enquiry-form input[type=submit]');
				
				if(msg=="ok"){
					messagetarget.val('Thank you!');
					unbindReactions() ;
					$('#enquiry').find('p').each(function(){ $(this).slideUp(1000); });
					setTimeout(function(){
						$('#ef-first').text('Your details have been sent successfully. Thank you.').slideDown(1000);
						$('#enquiry').animate({height: hf_o_height},hf_o_time).find('input[type=submit]').fadeTo(hf_o_time/2,0,function(){$(this).css('display','none');});
					}, 1000);
					
				} else {
					
					$('#enquiry-form').find('input[type=text]').each(function(){
						if($(this).val() == ''){
							$(this).val($(this).attr('rel'));
						}
					});
					
					$('#ef-first').text(msg).slideDown(1000, function(){
						newH = parseInt(hf_o_targ_nopx) + parseInt($('#ef-first').outerHeight());
						hf_o_targ = newH + 'px' ;
						$("#enquiry").animate({height: hf_o_targ},500);
					});
					messagetarget.val("Submit");
					subDOM.removeAttr('disabled');
					
					
				}
				
				/*if(msg == '201'){
					// Actions for results
					messagetarget.val('Success!');
					unbindReactions() ;
					$('#ef-first').text('Thank you for signing up to our mailing list.');
					
				} else if(msg == "1") {
					// For all others
					messagetarget.val('The e-mail address supplied was invalid');
				
				} else if(msg == "204") {
					// In Suppression List
					messagetarget.val('The e-mail address has been marked as suppressed. Please contact us.');
				
				} else if(msg == "205") {
					// Is Deleted
					messagetarget.val('The e-mail address supplied was marked as deleted. Please contact us.');
				
				} else if(msg == "206") {
					// Unsubscribed
					messagetarget.val('The e-mail address supplied was previously unsubscribed. Please contact us.');
				
				} else if(msg == "207") {
					messagetarget.val('The e-mail address supplied was has been marked with bounce issues. Please contact us.');
					// Bounced
				
				} else if(msg == "208") {
					// Is unconfirmed
					messagetarget.val('The e-mail address supplied is awaiting confirmation, please confirm or get in contact with us.');
				
				} else {
					// Something unexpected has happened
					messagetarget.val('There was an unexpected issue whilst processing your submission... please get in contact.');
				}*/
				
			}
			
			
		
		});
		
		return false;
	
	});
	
	
	
	
	//	*****  ----------------------------------------------------------------------------
	//	*****  		Book now AJAX
	//	*****  ----------------------------------------------------------------------------
	
	
	
	$('#bn-form').submit(function(){
		
		if($(this).hasClass('bn-form-inline')){
			isInline = true ;
		}
		
		// Action to signify click before AJAX starts
		bnsDOM = $(this).find('input[type=submit]') ;
		bnsDOM.attr('disabled','disabled');
		bnsDOM.val('One moment please...');
		$('p.errors').slideUp(250) ;
		
		// 'this' refers to the current submitted form
		var str = $(this).serialize();
		
		$(this).find('input[type=text]').each(function(){
			if($(this).attr('rel') == $(this).val()){
				$(this).val('');
			}
		});
		
		$.ajax({
		   type: "POST",
		   url: $('#logo a').attr('href')+"index.php/ajax/book-now/",
		   data: str,
		   success: function(msg){
				
				messagetarget = $('#bn-form input[type=submit]');
				
				if(msg=="ok"){
					messagetarget.val('Thank you!');
					tyData = $.get( $('#logo a').attr('href')+"index.php/ajax/book-now-thankyou/", function(data){
						$('#bn-lightbox-panel-content').fadeTo(500,0, function(){
							$(this).html(data).fadeTo(500,1);
						})
						
					});
					
					if(isInline == true){
						$('#inline-area').slideUp(1000);
						$('#form-message').html('Thank you for your interest.<br /><br />A member of the team will be in touch within 48 hours.');
					}
					
				} else {
					
					$('#bn-form').find('input[type=text]').each(function(){
						if($(this).val() == ''){
							$(this).val($(this).attr('rel'));
						}
					});
					
					$('p.errors').html(msg).slideDown(1000) ;
					messagetarget.val("Send");
					bnsDOM.removeAttr('disabled');
					
				}
				
			}
			
			
		
		});
		
		return false;
	
	});
	
	$('.bn-form-inline').find('input[type=text]').each(function(){
			
			// Store default value in 'rel' for reference.
			$(this).attr('rel', $(this).val());
			
			// Check if field is empty or placeholder changed.
			$(this).focus(function(){
				if($(this).val() == "" || $(this).val() == $(this).attr('rel')){
					$(this).val('') ;
				}
			});
			
			// If unchanged or empty, restore placeholder.
			$(this).blur(function(){
				if($(this).val() == "" || $(this).val() == $(this).attr('rel')){
					$(this).val( $(this).attr('rel') ) ;
				}
				
			});
		});
	
	
	//	*****  ----------------------------------------------------------------------------
	//	*****  		People changers / case studies
	//	*****  ----------------------------------------------------------------------------
	
	
	// Team thumb fades and tooltips
	
	var ttt ;
	
	// Get original top co-ords from CSS
	
	tt_deftop = $('.preview-tip').css('top');
	tt_starttop = '-70px' ;
	$('.preview-tip').css('top', tt_starttop).css('opacity','0') ;
	
	$('#sidebar-team a').click(function(){
		
		homeURL = $('#logo a').attr('href');
		
		if($('#ajaxident').attr('rel') == 'people'){
			getURL = homeURL+'index.php/ajax/people-changers/'+$(this).attr('rel');
		} else if($('#ajaxident').attr('rel') == 'clients'){
			getURL = homeURL+'index.php/ajax/our-clients/'+$(this).attr('rel');
		}
		
		
		$('#cat-inner').animate({'margin-left':'-500px','opacity':'0'},1000,function(){
		
			$('#content-actual-team').addClass('loading');
			
			$.get(getURL, function(data) {
			  $('#cat-inner').html(data);
			  $('#content-actual-team').removeClass('loading');
			  $('#cat-inner').css('margin-left','500px')
			  $('#cat-inner').animate({'margin-left':'0','opacity':'1'},1000);
			});
			
		});
		
		
		
		
		return false ;
	});
	
	$('#sidebar-team > div').mouseover(function(){
	
		if($('#ajaxident').attr('rel') == 'people'){
			$(this).find('a').dequeue().fadeTo(500,1)
			.find('span').dequeue().fadeTo(500,0.8) ;
		} else if($('#ajaxident').attr('rel') == 'clients'){
			$(this).find('a').dequeue().animate({'background-color': '#ff00ff !important'})
			.find('span').dequeue().fadeTo(500,0.8) ;
		}
		
		
		tt_target = $(this) ;
		ttt = setTimeout(function(){
			tt_target.find('a').parent().find('.preview-tip').css('display','block').animate({'top': tt_deftop, 'opacity': '1'},300) ;
		},800);
		
	});
	
	
	$('#sidebar-team > div').mouseleave(function(){
		
		clearTimeout(ttt);
		
		if($('#ajaxident').attr('rel') == 'people'){
		
				$(this).find('a').dequeue().fadeTo(500,0.8)
								 .parent().find('.preview-tip').stop().clearQueue().animate({'top': tt_starttop, 'opacity': '0'},100,function(){ $(this).css('display','none') }) ;
				$(this).find('a').find('span').dequeue().fadeTo(500,0.8) ;
		
		} else if($('#ajaxident').attr('rel') == 'clients'){
		
				$(this).find('a').stop().clearQueue().animate({'background-color':'#3FA9F5'},250)
								 .parent().find('.preview-tip').stop().clearQueue().animate({'top': tt_starttop, 'opacity': '0'},100,function(){ $(this).css('display','none') }) ;
				$(this).find('a').find('span').fadeTo(500,0.8) ;
		
		}
	
	});
	
	$('#sidebar-team').mouseleave(function(){
		// Close all tooltips when the mouse leaves the sidebar. This helps smooth over the issue of stuck open tooltips
		$(this).find('.preview-tip').stop().clearQueue().animate({'top': tt_starttop, 'opacity': '0'},100,function(){ $(this).css('display','none') }) ;
	
	});
	
	var sbttimer ;
	
	
	
	
});
