/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : RCCAQ
//----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
// Inclusion dynamique des fichiers externes javascripts
/*----------------------------------------------------------------------------*/
function importJavascript(src)
{
	document.write("<script type=\"text/javascript\" src=\"" + src + "\"></scr" + "ipt>");
}
function importCss(href, media)
{
	document.write('<link rel="stylesheet" type="text/css" media="' + media + '" href="' + href + '" />');
}

/*----------------------------------------------------------------------------*/
// 	IMPORTATION DE FICHIERS
/*----------------------------------------------------------------------------*/
importJavascript("/includes/js/plugins/abso.caroussel.js");
importJavascript("/includes/js/plugins/scroll.js");

/*----------------------------------------------------------------------------*/
// DÉCLARATIONS JQUERY
/*----------------------------------------------------------------------------*/

var accordionAction = false;

$(document).ready(function() {

    $("hr").replaceWith("<div class=\"hr\">&nbsp;</div>");
	
	// ---------------------------------------------------------------------------
    // ACCORDÉON DE LA FAQ
    // ---------------------------------------------------------------------------
	if($("#Faq").length > 0){
		
		var faqUrl = (window.location.hash).substr(1);	
		$("#Faq").children("dd").css("display", "none");
		
		if(faqUrl != "form"){
			showSelectedQuestion(faqUrl);
		}
		else{
			$.scrollTo("#Form", {offset: {top:-10} });
		}
		
		// Ouverture de l'accordéon au clique
		$("#Faq").children("dt").click(function(){
			if(accordionAction == false){
				accordionAction = true;
				$(this).next("dd").stop().slideToggle(250, function(){
					accordionAction = false;
				});
				$(this).toggleClass("On");
			}
		});	
		
		// État de survol d'un élément de l'accordéon
		$("dt", "#Faq").mouseover(function(){
				$(this).css("cursor", "pointer");
				$(this).css("color", "#9F218B");
		});
		$("dt", "#Faq").mouseout(function(){
				$(this).css("cursor", "default");
				$(this).css("color", "#312d30");
		});
	}

    // ---------------------------------------------------------------------------
    // BOUTONS À COINS RONDS
    // ---------------------------------------------------------------------------
    jQuery("a.LienBouton").css("border-radius", "5px");
    jQuery("a.LienBouton").css("-moz-border-radius", "5px");
    jQuery("a.LienBouton").css("-webkit-border-radius", "5px");

    $("span.Erreur").click(function() {
        $(this).css("visibility", "hidden");
    });

    $("#Temoin").find("a").click(function() {
        var indexPanel = $(this).html();
        AbsoCaroussel.Switch("Slideshow", indexPanel);
        return false;
    });

    $("ul.ListeVideos").find("a").click(function() {
        var url = $(this).attr("href");
        var videoTitle = $(this).parents("li").children(".Titre").text();
        var videoDescription = $(this).parents("li").children(".Description").text();
        url = url.substr(url.indexOf('#') + 1);
        videoSelection(url, videoTitle, videoDescription, true, '1');
        return false;
    });

    if ($("#MenuSecondaire").length > 0) {
        var selectedMenuElement = $("#MenuPrincipal").find("a.On");
        var highlightWidth = $(selectedMenuElement).width() - 1;
        var highlightPosition = $(selectedMenuElement).position();

        if (highlightPosition != null) {
            $("#BlocMenus").append("<div id='MenuHighlight' style='width:" + highlightWidth + "px; left:" + highlightPosition.left + "px;'>&nbsp;</div>");
        }
    }

    if ($("ul.ListeVideos").length > 0) {
        if (window.location.href.indexOf('?v=')) {
            var url = window.location.href;

            if (url.indexOf('?') != -1) {
                loadDefaultVideo(url.substr(url.indexOf('?') + 3));
            }
            else {
                loadDefaultVideo();
            }
        }
    }


    $('input[type="text"], textarea')
        .bind('focus', function() {
            $(this).prev('label').andSelf().addClass('Focus');
        })
        .bind('blur', function() {
            $(this).prev('label').andSelf().removeClass('Focus');
        }
    );

    $('.Enterable').keypress(function(e) {
        if (e.which == 13) {
            $('.Sumbitter').click();
            document.location = $('.Sumbitter').attr('href');
        }
    });
});

// ------------------------------------------------------------------------------
// OUVERTURE DE L'ACCORDÉON
// ------------------------------------------------------------------------------
function showSelectedQuestion(faqID){
	if(faqID != "") {
		$("#" + faqID).next("dd").css("display", "block");
		$("#" + faqID).addClass("On");
		$.scrollTo("#" + faqID, 800, {offset: {top:-10} });
		accordionAction = false;
	}
}

// ------------------------------------------------------------------------------
// CAROUSSEL À L'ACCUEIL
// ------------------------------------------------------------------------------
function homeSlideshow(){
	if($("#Slideshow").children("li").length > 1){
		selectedPanel = $("#Slideshow").children("li:first-child").attr("id");
		slideshowTracking(selectedPanel);
		AbsoCaroussel.init({
				id: "Slideshow",
				wrap: true,
				autostart: true,
				transition: "fade",
				preSwitchCallback: function(data){
					selectedPanel = $(data.target.obj).attr("id");
					slideshowTracking(selectedPanel);
				}
		});	
	}
	else{
		$("#Temoin").remove();
	}
}

// ------------------------------------------------------------------------------
// CAROUSSEL DES FOURNISSEURS
// ------------------------------------------------------------------------------
function suppliersSlideshow(){
	AbsoCaroussel.init({
			id: "CarousselFournisseurs",
			wrap: true,
			autostart: true,
			transition: "fade"
	});
}

// ------------------------------------------------------------------------------
// CHANGEMENT DE L'ÉTAT SUR LE TÉMOIN DU PANEL
// ------------------------------------------------------------------------------
function slideshowTracking(id){
	$("#Temoin").find("a").removeClass("On");
	$("#Temoin").find("." + id).addClass("On");
}

// ------------------------------------------------------------------------------
// LECTEUR DE VIDÉOS YOUTUBE
// ------------------------------------------------------------------------------
function videoSelection(url, videoTitle, videoDescription, viewAnchor, autoplay) {
	url = "http://www.youtube.com/v/"+url+"&hl=fr&fs=1&autoplay=" + autoplay + "&rel=0&iv_load_policy=3";
	
	var player = '<object width="360" height="206">';
	player += '<param name="movie" value="'+url+'"></param>';
	player += '<param name="allowFullScreen" value="true"></param>';
	player += '<param name="allowscriptaccess" value="always"></param>';
	player += '<param name="bgcolor" value="#000000"></param>';
	player += '<param name="wmode" value="opaque"></param>';
	player += '<embed src="'+url+'" type="application/x-shockwave-flash" allowscriptaccess="always" bgcolor="#000000" wmode="opaque" allowfullscreen="true" width="360" height="206"></embed>';
	player += '</object>';
	
	$("#VideoPlayer").html(player);
	$("#VideoData").children(".Titre").html(videoTitle);
	$("#VideoData").children(".Description").html(videoDescription);

	if(viewAnchor == true) $.scrollTo( {top:'110px', left:'290px'}, 1000 );
}


// ------------------------------------------------------------------------------
// VIDÉO PAR DÉFAUT
// ------------------------------------------------------------------------------
function loadDefaultVideo(idVideo) {
    var video;
    var url;
    var autoplay = '0';

    if (typeof idVideo == undefined || idVideo == '' || idVideo == null) {
        video = $("ul.ListeVideos:first").children("li:first-child");
        url = $(video).children(".VideoLauncher").attr("href");
        url = url.substr(url.indexOf('#') + 1);
        autoplay = '0';
    }
    else {
        video = $('#' + idVideo);
        url = $(video).children(".VideoLauncher").attr("href");
        url = url.substr(url.indexOf('#') + 1);
        autoplay = '1';
    }
    
    var videoTitle = $(video).children(".Titre").text();
    var videoDescription = $(video).children(".Description").text();
	videoSelection(url, videoTitle, videoDescription, false, autoplay);
}

function MaxLength(obj, mlength) {
    if (obj.getAttribute) {
        if (obj.value.length > mlength) {
            obj.value = obj.value.substring(0, mlength)
        }
    }
}
