$(document).ready(function(){

	initContactForm();
	initEmploisForm();
	initRealisations();

	showTwitterFeed();
		
	if ($("#slideshow-accueil").length > 0) {	
		$('#slideshow-accueil').cycle({
			fx: 'scrollHorz', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			prev:   '#prev-slide', 
			next:   '#next-slide'
		});
	}

	if ($("#presentation-slideshow").length > 0) {	
		$('#presentation-slideshow').cycle({
			fx: 'scrollHorz', 
			prev:   '#prev-slide', 
			next:   '#next-slide', 
			timeout: 0,
			pager:  '#presentation-nav',
			pagerAnchorBuilder: function(idx, slide) { 
				// return selector string for existing anchor 
				return '#presentation-nav li:eq(' + idx + ') a'; 
			} 			
		});
		$('#presentation-slideshow .slide').fadeIn('slow');
	}

	if ($("#equipe-slideshow").length > 0) {	
		$('#equipe-slideshow').cycle({
			fx: 'scrollHorz', 
			prev:   '#prev-slide', 
			next:   '#next-slide',
			timeout: 0,
			pager:  '#presentation-nav',			
			pagerAnchorBuilder: function(idx, slide) { 
				// return selector string for existing anchor 
				return '#presentation-nav li:eq(' + idx + ') a'; 
			},
			rev: 1
		});
	}

	var inputCourriel = $('#input-courriel');
	inputCourriel.data('processed', false);
	autoFill('#input-courriel', 'Votre courriel (restera confidentiel)');

	$("#form-bulletin").submit(function() {
		var courriel = inputCourriel.attr('value');
		
		$.ajax({
			type: "POST",
			url: "http://www.absolu.ca/lib/ajax-subscribe.php",
			data: "courriel="+ courriel,
			dataType: "json",
			success: function(data){
				//alert(data.code + ' ' + data.message);
				inputCourriel.fadeOut('200', function() {
					$(this).attr('value', data.message).fadeIn();
					inputCourriel.data('processed', true);
				});
			},
			error: function(){
				alert("Une erreur s'est produite lors de l'inscription. Merci de réessayer plus tard.");
			}
		});
		return false;
	});

    $(".search-form").submit(function() {
		if ($(".search-text").val() == "Rechercher sur ce site...") {
			return false;
		} else {
			return true;
		}
	});


	if ( $('.author-pagesperso').length ) {
		$('.author-pagesperso h2').append(' <span>'+$(".author-pagesperso li").size()+'</span>');
	}
	if ( $('.author-liens').length ) {
		$('.author-liens h2').append(' <span>'+$(".author-liens li").size()+'</span>');
	}

	$('ul.authors li a').each(function(index) {
		if ($(this).text() == $('h1.author-title').text()) {
			$(this).addClass('current-author');
		}
	});

	if ($("#commentform").length > 0) {
		
		$.validator.addMethod("notEqualTo",function(value, element, params) {
			return this.optional(element) || value != params;
		});
		$("#commentform").validate({
			//debug: true, 
			rules: {
				author: "required",
				email: {
					required: true,
					email: true
				},
				code: {
					required: true,
					remote: $("#commentform").attr("action")+"?action=captcha"
				}
			},
			messages: {
				author: "Ce champ doit être rempli.",
				email: {
					required: "Ce champ doit être rempli.",
					email: "Adresse courriel non valide."
				},
				code: {
					required: "Ce champ doit être rempli.",
					remote: "Le code entré n'est pas valide. Regénérez un nouveau code."
				}
			},
			errorElement: "span",
			wrapper: "",	
			errorPlacement: function(error, element) {
				if (element.attr('type') == 'text' || element.attr('type') == 'textarea'){
					element.rules("add", { notEqualTo: error.html() } );
					if(element.val() == '' || element.val() == error.html()) {
						element.val(error.html());
						element.focus(function () {
							if(element.val() == error.html()) {
								element.val('');	
							}				
						});
					} else {
						element.parent().parent().children("label").append(error);
					}
				}
			},
			focusInvalid: true
		}); 
	}
});


function autoFill(id, v){
	$(id)
		.attr({ value: v })
		.focus(function(){
			if($(this).val()==v || $(this).data('processed')== true){
				$(this)
					.val("")
					.data('processed', false);
			}
		})
		.blur(function(){
			if($(this).val()==""){
				$(this)
					.val(v);
			}
		});
}

function initContactForm() {

    $("a#toggle-contact, a#toggle-contact-footer").click(function(e) {
		e.preventDefault();

		$('html, body').animate({scrollTop:0}, 'slow');

        if ($("#emplois-popup:visible").length) {
            $("#emplois-popup").slideUp('slow', function() {
                $("#emplois-popup").unbind('clickoutside');
                $("#emplois-active").hide();
            });
        } 

        if ($("#contact-popup:visible").length) {
            $("#contact-popup").slideUp('slow', function() {
                $("#contact-popup").unbind('clickoutside');
                $("#contact-active").hide();
            });
            return false;
        } else {
            $("#contact-active").show();
            $("#contact-popup").slideDown('slow', function() {
                $(this).bind('clickoutside', function() {
                    $(this).slideUp('slow', function() {
                        $(this).unbind('clickoutside');
                        $("#contact-active").hide();
                    });
                    return false;
                });
            });
        }
        return false;
    });

	var selectBureau = $('#select-bureau');
	selectBureau.data('oldBureau', selectBureau.attr('value'));
	
	selectBureau.change(function() {
		var bureau = selectBureau.attr('value');
		if ($('#contact-'+selectBureau.data('oldBureau')).length) {
			$('#contact-'+selectBureau.data('oldBureau')).slideUp(400, function() {
				$('#contact-'+bureau).delay(200).slideDown();
				selectBureau.data('oldBureau', bureau);
			});
		} else {
			$('#contact-'+bureau).slideDown();
			selectBureau.data('oldBureau', bureau);
		}
	});

}


function initEmploisForm() {

    $("a#toggle-emplois, a#toggle-emplois-footer").click(function(e) {
		e.preventDefault();
		
		$('html, body').animate({scrollTop:0}, 'slow');

        if ($("#contact-popup:visible").length) {
            $("#contact-popup").slideUp('slow', function() {
                $("#contact-popup").unbind('clickoutside');
                $("#contact-active").hide();
            });
		}

        if ($("#emplois-popup:visible").length) {
            $("#emplois-popup").slideUp('slow', function() {
                $("#emplois-popup").unbind('clickoutside');
                $("#emplois-active").hide();
            });
            return false;
        } else {
            $("#emplois-active").show();
            $("#emplois-popup").slideDown('slow', function() {
                $(this).bind('clickoutside', function() {
                    $(this).slideUp('slow', function() {
                        $(this).unbind('clickoutside');
                        $("#emplois-active").hide();
                    });
                    return false;
                });
            });
        }
        return false;
    });
	var selectBureau = $('#select-bureau-emplois');
	selectBureau.data('oldBureau', selectBureau.attr('value'));
	
	selectBureau.change(function() {
		var bureau = selectBureau.attr('value');
		if ($('#emploi-'+selectBureau.data('oldBureau')).length) {
			$('#emploi-'+selectBureau.data('oldBureau')).slideUp(400, function() {
				$('#emploi-'+bureau).delay(200).slideDown();
				selectBureau.data('oldBureau', bureau);
			});
		} else {
			$('#emploi-'+bureau).slideDown();
			selectBureau.data('oldBureau', bureau);
		}
	});	
	
}

function initRealisations() {
	
	$(".realisation").hover(function() { //On hover...
		//Animate the image to 0 opacity (fade it out)
		$(this).find(".hover").stop().fadeTo('fast', 1, function() {

		});
	} , function() { //on hover out...
		//Fade the image to full opacity 
		$(this).find(".hover").stop().fadeTo('slow', 0);
	});

	// projet
	$(".projet").hover(function() { //On hover...
		//Animate the image to 0 opacity (fade it out)
		$(this).find(".hover").stop().fadeTo('fast', 1, function() {

		});
	} , function() { //on hover out...
		//Fade the image to full opacity 
		$(this).find(".hover").stop().fadeTo('slow', 0);
	});
	
	if ($('#campagne1').length > 0) {
	
		var $galleryCampagne1 = $("a.campagne1-pieces").colorbox({rel:'campagne1-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne1").click(function(e){
			e.preventDefault();
			$galleryCampagne1.eq(0).click();
		});
		var $galleryCampagne2 = $("a.campagne2-pieces").colorbox({rel:'campagne2-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne2").click(function(e){
			e.preventDefault();
			$galleryCampagne2.eq(0).click();
		});
		var $galleryCampagne3 = $("a.campagne3-pieces").colorbox({rel:'campagne3-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne3").click(function(e){
			e.preventDefault();
			$galleryCampagne3.eq(0).click();
		});
		var $galleryCampagne4 = $("a.campagne4-pieces").colorbox({rel:'campagne4-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne4").click(function(e){
			e.preventDefault();
			$galleryCampagne4.eq(0).click();
		});
		var $galleryCampagne5 = $("a.campagne5-pieces").colorbox({rel:'campagne5-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne5").click(function(e){
			e.preventDefault();
			$galleryCampagne5.eq(0).click();
		});
		var $galleryCampagne6 = $("a.campagne6-pieces").colorbox({rel:'campagne6-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne6").click(function(e){
			e.preventDefault();
			$galleryCampagne6.eq(0).click();
		});
		var $galleryCampagne7 = $("a.campagne7-pieces").colorbox({rel:'campagne7-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne7").click(function(e){
			e.preventDefault();
			$galleryCampagne7.eq(0).click();
		});
		var $galleryCampagne8 = $("a.campagne8-pieces").colorbox({rel:'campagne8-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne8").click(function(e){
			e.preventDefault();
			$galleryCampagne8.eq(0).click();
		});
		var $galleryCampagne9 = $("a.campagne9-pieces").colorbox({rel:'campagne9-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne9").click(function(e){
			e.preventDefault();
			$galleryCampagne9.eq(0).click();
		});
		var $galleryCampagne10 = $("a.campagne10-pieces").colorbox({rel:'campagne10-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne10").click(function(e){
			e.preventDefault();
			$galleryCampagne10.eq(0).click();
		});
		var $galleryCampagne11 = $("a.campagne11-pieces").colorbox({rel:'campagne11-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne11").click(function(e){
			e.preventDefault();
			$galleryCampagne11.eq(0).click();
		});
		var $galleryCampagne12 = $("a.campagne12-pieces").colorbox({rel:'campagne12-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne12").click(function(e){
			e.preventDefault();
			$galleryCampagne12.eq(0).click();
		});
		var $galleryCampagne13 = $("a.campagne13-pieces").colorbox({rel:'campagne13-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne13").click(function(e){
			e.preventDefault();
			$galleryCampagne13.eq(0).click();
		});
		var $galleryCampagne14 = $("a.campagne14-pieces").colorbox({rel:'campagne14-pieces', current:'Image {current} de {total}'});
		$("a.voir-campagne14").click(function(e){
			e.preventDefault();
			$galleryCampagne14.eq(0).click();
		});
	}

	if ($('#marque1').length > 0) {
	
		var $galleryMarque1 = $("a.marque1-pieces").colorbox({rel:'marque1-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque1").click(function(e){
			e.preventDefault();
			$galleryMarque1.eq(0).click();
		});
		var $galleryMarque2 = $("a.marque2-pieces").colorbox({rel:'marque2-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque2").click(function(e){
			e.preventDefault();
			$galleryMarque2.eq(0).click();
		});
		var $galleryMarque3 = $("a.marque3-pieces").colorbox({rel:'marque3-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque3").click(function(e){
			e.preventDefault();
			$galleryMarque3.eq(0).click();
		});
		var $galleryMarque4 = $("a.marque4-pieces").colorbox({rel:'marque4-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque4").click(function(e){
			e.preventDefault();
			$galleryMarque4.eq(0).click();
		});
		var $galleryMarque5 = $("a.marque5-pieces").colorbox({rel:'marque5-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque5").click(function(e){
			e.preventDefault();
			$galleryMarque5.eq(0).click();
		});
		var $galleryMarque6 = $("a.marque6-pieces").colorbox({rel:'marque6-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque6").click(function(e){
			e.preventDefault();
			$galleryMarque6.eq(0).click();
		});
		var $galleryMarque7 = $("a.marque7-pieces").colorbox({rel:'marque7-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque7").click(function(e){
			e.preventDefault();
			$galleryMarque7.eq(0).click();
		});
		var $galleryMarque8 = $("a.marque8-pieces").colorbox({rel:'marque8-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque8").click(function(e){
			e.preventDefault();
			$galleryMarque8.eq(0).click();
		});
		var $galleryMarque9 = $("a.marque9-pieces").colorbox({rel:'marque9-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque9").click(function(e){
			e.preventDefault();
			$galleryMarque9.eq(0).click();
		});
		var $galleryMarque10 = $("a.marque10-pieces").colorbox({rel:'marque10-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque10").click(function(e){
			e.preventDefault();
			$galleryMarque10.eq(0).click();
		});
		var $galleryMarque11 = $("a.marque11-pieces").colorbox({rel:'marque11-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque11").click(function(e){
			e.preventDefault();
			$galleryMarque11.eq(0).click();
		});
		var $galleryMarque12 = $("a.marque12-pieces").colorbox({rel:'marque12-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque12").click(function(e){
			e.preventDefault();
			$galleryMarque12.eq(0).click();
		});
		var $galleryMarque13 = $("a.marque13-pieces").colorbox({rel:'marque13-pieces', current:'Image {current} de {total}'});
		$("a.voir-marque13").click(function(e){
			e.preventDefault();
			$galleryMarque13.eq(0).click();
		});
	}


	if ($('#design1').length > 0) {
	
		var $galleryDesign1 = $("a.design1-pieces").colorbox({rel:'design1-pieces', current:'Image {current} de {total}'});
		$("a.voir-design1").click(function(e){
			e.preventDefault();
			$galleryDesign1.eq(0).click();
		});
		var $galleryDesign2 = $("a.design2-pieces").colorbox({rel:'design2-pieces', current:'Image {current} de {total}'});
		$("a.voir-design2").click(function(e){
			e.preventDefault();
			$galleryDesign2.eq(0).click();
		});
		var $galleryDesign3 = $("a.design3-pieces").colorbox({rel:'design3-pieces', current:'Image {current} de {total}'});
		$("a.voir-design3").click(function(e){
			e.preventDefault();
			$galleryDesign3.eq(0).click();
		});
		var $galleryDesign4 = $("a.design4-pieces").colorbox({rel:'design4-pieces', current:'Image {current} de {total}'});
		$("a.voir-design4").click(function(e){
			e.preventDefault();
			$galleryDesign4.eq(0).click();
		});
		var $galleryDesign5 = $("a.design5-pieces").colorbox({rel:'design5-pieces', current:'Image {current} de {total}'});
		$("a.voir-design5").click(function(e){
			e.preventDefault();
			$galleryDesign5.eq(0).click();
		});
		var $galleryDesign6 = $("a.design6-pieces").colorbox({rel:'design6-pieces', current:'Image {current} de {total}'});
		$("a.voir-design6").click(function(e){
			e.preventDefault();
			$galleryDesign6.eq(0).click();
		});
		var $galleryDesign7 = $("a.design7-pieces").colorbox({rel:'design7-pieces', current:'Image {current} de {total}'});
		$("a.voir-design7").click(function(e){
			e.preventDefault();
			$galleryDesign7.eq(0).click();
		});
		var $galleryDesign8 = $("a.design8-pieces").colorbox({rel:'design8-pieces', current:'Image {current} de {total}'});
		$("a.voir-design8").click(function(e){
			e.preventDefault();
			$galleryDesign8.eq(0).click();
		});
		var $galleryDesign9 = $("a.design9-pieces").colorbox({rel:'design9-pieces', current:'Image {current} de {total}'});
		$("a.voir-design9").click(function(e){
			e.preventDefault();
			$galleryDesign9.eq(0).click();
		});
		var $galleryDesign10 = $("a.design10-pieces").colorbox({rel:'design10-pieces', current:'Image {current} de {total}'});
		$("a.voir-design10").click(function(e){
			e.preventDefault();
			$galleryDesign10.eq(0).click();
		});
		var $galleryDesign11 = $("a.design11-pieces").colorbox({rel:'design11-pieces', current:'Pièce {current} de {total}'});
		$("a.voir-design11").click(function(e){
			e.preventDefault();
			$galleryDesign11.eq(0).click();
		});
		var $galleryDesign12 = $("a.design12-pieces").colorbox({rel:'design12-pieces', current:'Pièce {current} de {total}'});
		$("a.voir-design12").click(function(e){
			e.preventDefault();
			$galleryDesign12.eq(0).click();
		});
		var $galleryDesign13 = $("a.design13-pieces").colorbox({rel:'design13-pieces', current:'Pièce {current} de {total}'});
		$("a.voir-design13").click(function(e){
			e.preventDefault();
			$galleryDesign13.eq(0).click();
		});
		var $galleryDesign13b = $("a.design13-pieces-inline").colorbox({rel:'design13-pieces', current:'Pièce {current} de {total}', inline: true});
	}


	if ($('#radiotele1').length > 0) {
		
		var $galleryRadiotele1 = $("a.radiotele1-pieces").colorbox({rel:'radiotele1-pieces', current:'Pièce {current} de {total}', inline: true});
		$("a.voir-radiotele1").click(function(e){
			e.preventDefault();
			$galleryRadiotele1.eq(0).click();
		});
		var $galleryRadiotele2 = $("a.radiotele2-pieces").colorbox({rel:'radiotele2-pieces', current:'Pièce {current} de {total}', inline: true});
		$("a.voir-radiotele2").click(function(e){
			e.preventDefault();
			$galleryRadiotele2.eq(0).click();
		});
		var $galleryRadiotele3 = $("a.radiotele3-pieces").colorbox({rel:'radiotele3-pieces', current:'Pièce {current} de {total}', inline: true});
		$("a.voir-radiotele3").click(function(e){
			e.preventDefault();
			$galleryRadiotele3.eq(0).click();
		});
		var $galleryRadiotele4 = $("a.radiotele4-pieces").colorbox({rel:'radiotele4-pieces', current:'Pièce {current} de {total}', inline: true});
		$("a.voir-radiotele4").click(function(e){
			e.preventDefault();
			$galleryRadiotele4.eq(0).click();
		});
		var $galleryRadiotele5 = $("a.radiotele5-pieces").colorbox({rel:'radiotele5-pieces', current:'Pièce {current} de {total}', inline: true});
		$("a.voir-radiotele5").click(function(e){
			e.preventDefault();
			$galleryRadiotele5.eq(0).click();
		});
		var $galleryRadiotele6 = $("a.radiotele6-pieces").colorbox({rel:'radiotele6-pieces', current:'Pièce {current} de {total}', inline: true});
		$("a.voir-radiotele6").click(function(e){
			e.preventDefault();
			$galleryRadiotele6.eq(0).click();
		});
		var $galleryRadiotele7 = $("a.radiotele7-pieces").colorbox({rel:'radiotele7-pieces', current:'Pièce {current} de {total}', inline: true});
		$("a.voir-radiotele7").click(function(e){
			e.preventDefault();
			$galleryRadiotele7.eq(0).click();
		});
	}
}

function showTwitterFeed() {
	
	if ($("#twitter-feed").length > 0) {	
		$("#twitter-feed").tweet({
			avatar_size: 48,
			count: 5,
			fetch: 20,
			username: "_Absolu",
			list: "absolu",
			loading_text: "Chargement des tweets...",
			template: function(info) {
				var le_tweet = info['avatar'] + '<div class="tweet_content">' + info['user'] + info['name'] + info['text'] + info['time'] + '</div>';
				return le_tweet;
			}
		}).bind("loaded",function(){$(this).find("a").attr("target","_blank");});
	}
}

