/****************************************************************/
/********************Apply Custom Fonts**************************/
/****************************************************************/
$(document).ready(function() {
	BuildInlineNav();
	CyclePeople();
	ApplyScroll();
	ApplyCufon();
});

function ApplyScroll(){
	$("#main-content").jScrollPane({scrollbarWidth: 5 });
}

function ApplyCufon(){
	Cufon.replace('h2, #header ul li a');
	Cufon.replace('#left ul li a, #left ul li h3', { hover: true });
	Cufon.now();
}

function BuildInlineNav(){
	var before = "";
	$('a[name]').each(function(){
		$('#inline-nav').append(before+'<a href="#'+$(this).attr('name')+'">'+$(this).attr('name')+'</a>');
		before = "&nbsp;/&nbsp;";
	});
}

function CyclePeople(){
	if($('#cycle-people').html() != null){
		$('#cycle-people').html($('#cycle-people').html().replace(/<br>|<br \/>/gi,''));
	}
	var rand = Math.floor(Math.random()*$('#cycle-people').children().size()); 
	$('#cycle-people').cycle({ 
		fx: 'scrollHorz', 
		timeout: 0,
		next:   '#people-right', 
    		prev:   '#people-left',
    		startingSlide: rand,
		before:  onBeforeCyclePeople, 
    		after:   onAfterCyclePeople 
	});
	
	$('.people-box .speech-bubble').hide();
	$('.people-box .speech-pip').hide();
	
	$('.people-box').shuffle();
	
	$('.people-box').append('<div class="speech-pip"></div>');
	
	$('.people-box .person').mouseover(function(){
		$ontop = false;
		$bubble = $(this).find('.speech-bubble');
		$person = $(this).find('.person-image');
		$left = $(this).position().left - ($bubble.outerWidth()/2);
		$top = $(this).position().top - ($bubble.outerHeight()/2) - 5;
		if($top < $('.people-box').position().top){
			$top = $(this).position().top + ($(this).outerHeight() - 50);
			$ontop = true;
		}
		if($left + $(this).outerWidth() > $('.people-box').position().left + $('.people-box').outerWidth()){
			$left = $('.people-box').position().left + $('.people-box').outerWidth() - $(this).outerWidth();
		}
		if($left < $('.people-box').position().left){
			$left = $('.people-box').position().left;
		}
		$bubble.css('left', $left + 'px');
		$bubble.css('top', $top + 'px');
		$bubble.show();
		
		$speechpip = $('.people-box .speech-pip');
		$speechpip.css('left', ($person.position().left + $person.width()/2) + 'px');
		if($ontop){
			$speechpip.css('background-image','url("images/pip-top.png")');
			$speechpip.css('top', $top - 4 + 'px');
		}else{
			$speechpip.css('background-image','url("images/pip.png")');
			$speechpip.css('top', $top + $bubble.height() + 'px');
		}
		$speechpip.show();
	});
	$('.people-box .person').mouseout(function(){
		$(this).find('.speech-bubble').hide();
		$('.people-box .speech-pip').hide();
	});
}

function onBeforeCyclePeople() { 
    	$('#cycle-people .speech-bubble').animate({opacity: 0},500);
} 
function onAfterCyclePeople() { 
	$('#cycle-people .speech-bubble:visible').animate({opacity: 1},500);
}

function Contact(lang){
	var name = $('#txtName').val();
	var email = $('#txtEmail').val();
	var interest = $('#txtInterest').val();
	var comment = $('#txtComment').val();
	
	var pass = true;
	var errorstring = "";
	if(name == ""){ pass = false; errorstring = errorstring + "1"; }
	if(email == ""){ pass = false; errorstring = errorstring + "2"; } 
	if(interest == ""){ pass = false; errorstring = errorstring + "3"; } 
	if(comment == ""){ pass = false; errorstring = errorstring + "4"; } 
	
	ShowErrors(errorstring,lang);
	
	if (pass){
		$.ajax({
			type: "POST",
			url: "functions/contact.php",
			cache: true,
			data: "&name=" + name + "&email=" + email + "&interest=" + interest + "&comment=" + comment,
			success: function(html){
				if(html.indexOf("Error:") != -1){
					ShowErrors(html.replace("Error: ",""),lang);
				}else{
					$("#tblContact").css('display','none');
					$("#divThanks").css('display','block');
				}
			}
		});
	}
}

function ShowErrors(errorstring, lang){
	if(lang=='fr'){
		if(errorstring.indexOf("1") != -1){ $('#txtName').val('Nom pas valide'); $('#txtName').addClass('error'); $('#txtName').focus(function(){ $('#txtName').removeClass('error'); $('#txtName').val(''); $('#txtName').unbind('focus'); }); }
		if(errorstring.indexOf("2") != -1){ $('#txtEmail').val('Email pas valide'); $('#txtEmail').addClass('error'); $('#txtEmail').focus(function(){ $('#txtEmail').removeClass('error'); $('#txtEmail').val(''); $('#txtEmail').unbind('focus'); }); } 
		if(errorstring.indexOf("3") != -1){ $('#txtInterest').val('Secteur d\'intérêt pas valide'); $('#txtInterest').addClass('error'); $('#txtInterest').focus(function(){ $('#txtInterest').removeClass('error'); $('#txtInterest').val(''); $('#txtInterest').unbind('focus'); }); } 
		if(errorstring.indexOf("4") != -1){ $('#txtComment').val('Commentaire pas valide'); $('#txtComment').addClass('error'); $('#txtComment').focus(function(){ $('#txtComment').removeClass('error'); $('#txtComment').val(''); $('#txtComment').unbind('focus'); }); } 
	}else{
		if(errorstring.indexOf("1") != -1){ $('#txtName').val('Invalid name'); $('#txtName').addClass('error'); $('#txtName').focus(function(){ $('#txtName').removeClass('error'); $('#txtName').val(''); $('#txtName').unbind('focus'); }); }
		if(errorstring.indexOf("2") != -1){ $('#txtEmail').val('Invalid email'); $('#txtEmail').addClass('error'); $('#txtEmail').focus(function(){ $('#txtEmail').removeClass('error'); $('#txtEmail').val(''); $('#txtEmail').unbind('focus'); }); } 
		if(errorstring.indexOf("3") != -1){ $('#txtInterest').val('Invalid area of interest'); $('#txtInterest').addClass('error'); $('#txtInterest').focus(function(){ $('#txtInterest').removeClass('error'); $('#txtInterest').val(''); $('#txtInterest').unbind('focus'); }); } 
		if(errorstring.indexOf("4") != -1){ $('#txtComment').val('Invalid comment'); $('#txtComment').addClass('error'); $('#txtComment').focus(function(){ $('#txtComment').removeClass('error'); $('#txtComment').val(''); $('#txtComment').unbind('focus'); }); } 	
	}
}
