// FICHIER DE FONCTIONS /////////////////////////////////////////////////////
$(document).ready(function() {
		//init accueil
		init_accueil()
	
		$("#nav-content a").each(
			function() {
				if (window.location.href.indexOf($(this).attr("href")) !== -1) {
					$(this).addClass("actif");
				}
			}
		);
	}
);

/**
*	PAGE ACCUEIL
*/
function init_accueil(){
	if($('#slides').length!=0){
		$("#slides").slides({
			generatePagination: false,
			animationComplete: function(current) {
				// Get the "current" slide number
				var first = parseInt(current)*3-2;
			
				var cpt = 0;
				$('.slide-'+current).find('li').each(function(){
					cpt++;
				})
				var last = (current-1)*3 + cpt/2;
				$('#slides_nav span').html(first+'-'+last);
			}
		});	
		
		$('.change-vid').click(function(e){
			e.preventDefault();
			exploded = this.id.split('_');
			document.getElementById('flash').changeVideo(exploded[1]);
		});
	}
}

function changeTitle(__title,__id){
	$('#zone-info p').html(__title);
	Cufon.refresh();
	
	$('meta[name=ogtitle]').attr('content',__title)
	$('meta[name=ogurl]').attr('content','http://pat.infoassurance.ca/fr/?video='+__id)
	
	if($(location).attr('href').indexOf('/en/')!=-1){
		$('#zone-like').html('<iframe src="http://www.facebook.com/plugins/like.php?locale=en_US&href=' + encodeURIComponent('http://pat.infoassurance.ca/en/?video='+__id) + '&amp;layout=button_count&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:35px;" allowTransparency="true"></iframe>')
		$('#facebooksharer').attr('href', 'http://www.facebook.com/sharer.php?u='+encodeURIComponent('http://pat.infoassurance.ca/en/?video='+__id));
		$('#twittersharer').attr('href','http://twitter.com/home?status='+encodeURIComponent(__title+'http://pat.infoassurance.ca/en/?video='+__id))

	}else{
		$('#zone-like').html('<iframe src="http://www.facebook.com/plugins/like.php?locale=fr_FR&href=' + encodeURIComponent('http://pat.infoassurance.ca/fr/?video='+__id) + '&amp;layout=button_count&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:35px;" allowTransparency="true"></iframe>')
		$('#facebooksharer').attr('href', 'http://www.facebook.com/sharer.php?u='+encodeURIComponent('http://pat.infoassurance.ca/fr/?video='+__id));
		$('#twittersharer').attr('href','http://twitter.com/home?status='+encodeURIComponent(__title+'http://pat.infoassurance.ca/fr/?video='+__id))
	}
}


/**
*	PAGE QUESTIONS ET REPONSES
*/
function handleQuestionsReponses() {
	$("div.collapse").each(
		function () {
			$(this).hide();
			$(this).prev("h3").find("a.toggle").click(function() {
				$(this).toggleClass("opened");
				$(this).parent().next("div.collapse").slideToggle(250);	
				
				return false;
			});
		}
	);
}

/**
*	PAGE CONSEILS À PARTAGER
*/
function handleConseilsPartager() {
	$("fieldset#partager-envoi").hide();
	
	$("#nav-partager a").click(
		function() {
			$("#nav-partager a").removeClass("actif");
			$(this).addClass("actif");
			
			$("form fieldset").hide();
			$("form fieldset#" + $(this).attr("rel")).show();
			
			return false;
		}
	);
	
	$("select#frm-perso").change(
		function() {
			$("#bulle .conseil").hide();
			$("#bulle ." + $(this).val()).show();
		}
	);
	$("select#frm-perso").trigger("change");
	
	$("a.toggle-suivant").click(
		function() {
			$("#nav-partager a.toggle-envoi").trigger("click");
			return false;
		}
	);
	
	$("form").bind("submit", validerFormulaire);
	$("form input").click(
		function() {
			$(this).removeClass("erreur");
		}
	);
}
			
function validerFormulaire() {
	var formulaireValide = true;
	
	$("input#frm-conseil").val(
		encodeHTMLEntities($("select#frm-perso").val().substring($("select#frm-perso").val().indexOf("-") + 1)) + "|" + 
		encodeHTMLEntities($("#bulle ." + $("select#frm-perso").val() + " h5").html()) + "|" + 
		encodeHTMLEntities($("#bulle ." + $("select#frm-perso").val() + " p").html())
	);
	
	$("form input").each(
		function () {
			var value = $(this).val();
			
			if (!value || ($(this).attr("id").indexOf("courriel") != -1 && !verifierCourriel(value))) {
				formulaireValide = false;
				$(this).addClass("erreur");
			} else {
				$(this).removeClass("erreur");
			}
		}
	);
	
	return formulaireValide;
}
