//-----------------------------------------------------------------------------//
// Contenu rotatif
//-----------------------------------------------------------------------------//
var rotateurItemCourant=new Array();
var rotateurDelais=new Array();

document.write("<style type='text/css'>.contenuVisible {font-weight:bold;}</style>");

function InitialiserRotateur(vId, vDelais)
{
    rotateurItemCourant[vId] = 0;
    rotateurDelais[vId]=vDelais;
}

// Rotateur
function SelectionnerCreatif(sender,vIndex)
{
    var rotateur=$find(sender.id);
    var itemsCount = rotateur.get_items().length;
    rotateur.stop();
    window.setTimeout("AfficherCreatifSuivantJusquA(\"" + sender.id + "\"," + vIndex + ")",50);
}

function AfficherCreatifSuivantJusquA(id,vIndex)
{
    var rotateur=$find(id);

    if (rotateurItemCourant[id] != vIndex) {
        rotateur.showNext(Telerik.Web.UI.RotatorScrollDirection.Left);
        window.setTimeout("AfficherCreatifSuivantJusquA(\"" + id + "\"," + vIndex + ")",50);
    } else {
        rotateur.start();
    }
}

function GererChangementContenuRotatif(sender,args)
{
    var id=sender.get_id();
    rotateurItemCourant[id]++;
    var itemsCount = sender.get_items().length;
    if (rotateurItemCourant[id]==itemsCount) {
    	rotateurItemCourant[id]=0;
    	sender._itemsElement.style.left=sender._clipElement.style.width;
    }
    var courant = rotateurItemCourant[id];
    var delais = rotateurDelais[id];
    var delaiCourant=delais[courant];
    sender.set_frameDuration(delaiCourant);
    ReinitialiserBoutons(sender);
    ActiverBoutonAssocie(sender);
}

function ActiverBoutonAssocie(sender)
{
    var id=sender.get_id()
    var idBouton=id.replace("_natif","_s" + rotateurItemCourant[id]);
    var bouton=document.getElementById(idBouton);
    bouton.className="contenuVisible " + bouton.className;
}

function ReinitialiserBoutons(sender)
{
    var id=sender.get_id()
    var idZone=id.replace("_natif","_selecteurs");
    var zone=document.getElementById(idZone);
    var index=0;
    for (index=0;index<zone.children.length;index++)
    {
    	var bouton=zone.children[index];
    	bouton.className=bouton.className.replace("contenuVisible ","");
    }
}

//-----------------------------------------------------------------------------//
// GestionListe 
//-----------------------------------------------------------------------------//
function faqToggleItem(ref)
{	
	// trouver le parent de classe faq-item
	var faqItem = ref.parentNode;
	// changer la classe du parent
	if (faqItem.className == "faqItem visible")
	{
		faqItem.className = "faqItem invisible";
	}
	else if (faqItem.className == "faqItem invisible") 
	{
		faqItem.className = "faqItem visible";
	}
	else {
		// cas par default
		faqItem.className = "faqItem invisible";
	}
}

//-----------------------------------------------------------------------------//
// EvaluerContenu
//-----------------------------------------------------------------------------//
function AfficherConfirmationEvaluerContenu()
{

}

function voteToggleComment(ref, estUtile)
{
	// trouver le parent de classe 'vote' du boutton 
	var vote = ref.parentNode;
	while (!vote.className || vote.className != 'vote')
		vote = vote.parentNode;

	// Recupere les controles
	var commentaire = vote.getElementsByTagName('DIV')[1];
	var divSaisieCommentaire = vote.getElementsByTagName('DIV')[2];
	var divConfirmation = vote.getElementsByTagName('DIV')[5];
	var boutonUtile = vote.getElementsByTagName('Input')[0];
	var boutonInutile = vote.getElementsByTagName('Input')[1];
	var niveauSatisfaction = vote.getElementsByTagName('Input')[2];
	var lblCommentaireUtile = vote.getElementsByTagName('Label')[1];
	var lblCommentaireInutile = vote.getElementsByTagName('Label')[2];

    // Afficher ou cache la zone de saisi des commentaires et converse en memoire le niveau de satisfaction (inutile ou utile)
    if (!commentaire.style.display || commentaire.style.display == 'none' || divConfirmation.style.display == 'block') 
    {
        // Conserve le niveau de satisfaction (Utile ou Inutile) pour le rapport
        niveauSatisfaction.value = ref.title;

        commentaire.style.display = 'block';
        divSaisieCommentaire.style.display = 'block';
    }
    else 
    {
        if (niveauSatisfaction.value == ref.title)
        {
            commentaire.style.display = 'none';
            divSaisieCommentaire.style.display = 'none';

	        boutonUtile.className = 'btnUtile';
	        boutonInutile.className = 'btnInutile';

	        boutonUtile.disabled = false;
	        boutonInutile.disabled = false;
            
            niveauSatisfaction.value = null;
            
            return;
        }
        else
        {
            // Conserve le niveau de satisfaction (Utile ou Inutile) pour le rapport
            niveauSatisfaction.value = ref.title;
        }
    }
    
    // Cache la zone de confirmation si elle est deja affichee.
    divConfirmation.style.display = 'none';

    // Affiche le bon libelle de commentaire
    if (estUtile)
    {
        lblCommentaireUtile.style.display = 'block';
        lblCommentaireInutile.style.display = 'none';

	    boutonUtile.disabled = false;
	    boutonUtile.className = 'btnUtileActif';
	    boutonInutile.disabled = true;
	    boutonInutile.className = 'btnInutileInactif';
    }
    else
    {
        lblCommentaireUtile.style.display = 'none';
        lblCommentaireInutile.style.display = 'block';

	    boutonUtile.disabled = true;
	    boutonUtile.className = 'btnUtileInactif';
	    boutonInutile.disabled = false;
	    boutonInutile.className = 'btnInutileActif';
    }
}

function verifyMaxLengthOnKey(ref) 
{
    var SelText;
    var Range;
    
    if (ref.MaxLength != null) 
    {
        Range = window.document.selection.createRange();
        SelText = Range.text;
        
        if (SelText != null && SelText != '' && SelText != undefined) 
        {
            return true;
        }
        if (ref.value.length >= ref.MaxLength) 
        {
            return false;
        }
        
        return true;
    }
}

function verifyMaxLengthOnPaste(ref) 
{
    var Data = window.clipboardData.getData('Text');
    var SelText;
    var Range; 

    if (ref.MaxLength != null) 
    {
        Range = window.document.selection.createRange();
        SelText = Range.text; 
        
        if (SelText != null && SelText != '' && SelText != undefined) 
        {
            if ((ref.value.length - SelText.length) + Data.length < ref.MaxLength) 
            {
                return true; 
            }
        }
        if (ref.value.length + Data.length >= ref.MaxLength) 
        {
            window.clipboardData.setData('Text',Data.substring(0,ref.MaxLength-ref.value.length));
        }
        
        return true;
    }
}

function verifyMaxLengthOnDragDrop(ref) 
{
    var data;

    if (ref.MaxLength != null) 
    {
        data = window.event.dataTransfer.getData('Text');
        
        if (ref.value.length + data.length > ref.MaxLength) 
        {
            if (ref.MaxLength-ref.value.length > 0) 
            {
                ref.value = ref.value + data.substring(0,ref.MaxLength-ref.value.length); 
            }
            return false;
        }
        return true;
    }
}

//-----------------------------------------------------------------------------//
// InfoBulleGlossaire
//-----------------------------------------------------------------------------//
function findPos(obj) 
{
	var left = 0;
	var top = 0;

	if (obj.offsetParent)
	{
		left = obj.offsetLeft;
		top = obj.offsetTop;

		while (obj = obj.offsetParent)
		{
			left += obj.offsetLeft;
			top += obj.offsetTop;
		}
	}
	return [left, top];
}

function getComputedWidth(id)
{
	var width = 0;
        if(document.defaultView)
	{
                var elm = document.getElementById(id);
                width = document.defaultView.getComputedStyle(elm, "").getPropertyValue("width");
                width = width.split('px');
                width = width[0];
        }
        else
	{
               width = document.getElementById(id).offsetWidth;
        }
        return width;
}

function obtenirDivParentId(target)
{
	var parentElm = target.parentNode;

	if (parentElm.tagName != "DIV")
		return obtenirDivParentId(parentElm);
	else
		return parentElm.id;
}

function montrerBulle(target, reference)
{
	var targetPos = findPos(target);
	var targetWidth = target.offsetWidth;
	
	var parentId = obtenirDivParentId(target);
	var zoneTexteWidth = getComputedWidth(parentId);
	
	var isLeftAlign = zoneTexteWidth/2 > targetPos[0];
	var bulleGlossaire = document.getElementById('bulleGlossaire');
	var bulleGlossaireTete = document.getElementById('bulleGlossaireTete');
	var bulleGlossaireWidth = getComputedWidth('bulleGlossaire');
	document.getElementById('bulleGlossaireTexte').innerHTML = document.getElementById(reference).innerHTML;
	
	bulleGlossaireTete.className = (zoneTexteWidth/2 > targetPos[0]) ? 'bulleLeft' : 'bulleRight';
	bulleGlossaire.style.top = targetPos[1] + 15 + 'px';
	bulleGlossaire.style.left = (isLeftAlign) ? targetPos[0] - 50 + 'px' : targetPos[0] + targetWidth - bulleGlossaireWidth + 55+ 'px';

}

function cacherBulle()
{
	document.getElementById('bulleGlossaire').style.top = '-9999px';
}
//-----------------------------------------------------------------------------//
// SelecteurCss
//
// Avec le code du Style Switcher de Paul Sowden, http://www.alistapart.com/articles/alternate/
//-----------------------------------------------------------------------------//
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
//-----------------------------------------------------------------------------//
// BoiteRecherche
//-----------------------------------------------------------------------------//
function cliquerBouton(id, e)
{

    var Touche;

    if(window.event) // IE
    {
        Touche = window.event.keyCode;
    }
    else if(e.which) // Netscape/Firefox/Opera/etc.
    {
        Touche = e.which;
    }

    if (Touche == 13)
    {
        var bouton = document.getElementById(id);
        if (bouton)
        {
            //le .focus contourne le probleme du contenu non-reconnu lorsque obtenu par auto-complete
            bouton.focus(); 
            bouton.click();
        }
        
        return false;
    }
    else
        return true;

}

function cliquerAnchor(id, e)
{

    var Touche;

    if(window.event) // IE
    {
        Touche = window.event.keyCode;
    }
    else if(e.which) // Netscape/Firefox/Opera/etc.
    {
        Touche = e.which;
    }

    if (Touche == 13)
    {
		
    	var href = $("#"+id).attr("href");
		if (href)
		{
			window.location = href;
		}

        return false;
    }
    else
        return true;

}
//-----------------------------------------------------------------------------//
// FiltreListe
//-----------------------------------------------------------------------------//
function appliquerFiltre(idClientDropDownList) 
{ 
    var dropDownList = document.getElementById(idClientDropDownList);
    location.href = dropDownList[dropDownList.selectedIndex].value;
}

