<!-- -
	
/***** Styleswitcher : pour l'agrandissement et la réduction de la taille du texte *****/
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;
		}
    }
  }
  createCookie("style", title, 365);
}

/******************************************************************************/
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() {
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
  setSocialNetworkIcons();
}

/******************************************************************************/
window.onunload = function() {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

/******************************************************************************/
function clearInput() {
   document.getElementById("query").value = "";
}

/******************************************************************************/
function setCriteria( newValue, action, method, query, imgId ) {
  document.getElementById("criteriaImg").src = document.getElementById(imgId).src;
  document.getElementById("criteria").value = newValue;
                    
  document.getElementById('search').action= action;
  document.getElementById('search').method= method;
  document.getElementById('query').name= query;
  document.getElementById('criteriaList').style.display = 'none';
}

/******************************************************************************/                  
function changeBeforeSubmit() {
  if( document.getElementById("query").value == "Chercher" ) {
    document.getElementById("query").value = "";
  } else if ( document.getElementById("criteria").value == "section" ) {
    document.getElementById("query").value = document.getElementById("query").value + " site:www.usherbrooke.ca/" + getHost();
  }
}

/******************************************************************************/
function getHost() {
  var urlString = window.location;
  var urlparts = urlString.toString().split("/");
  var host = "";
  
  for( var i = 0; i < urlparts.length; i++ ) {
    if( urlparts[i].toLowerCase() == "www.usherbrooke.ca" ) {
      if( urlparts[i + 1] ) {
        host = urlparts[i + 1];
      }
      
      i = urlparts.length;
    }
  }
  
  return( host );
}

/******************************************************************************/
function setSocialNetworkIcons() {
  var metaList = document.getElementsByTagName( "meta" ),
      length = metaList.length,
      target, rowString, linkList, metaItem, i, name, content, splitedName, icon, div;

  // meta exists
  if( !length ) {
    return false;
  }

  // get target
  target = document.getElementById( "front_right" ) ||
           document.getElementsByClassName( "content_right" );
  if( !target ) {
    return false;
  }
  if( target.length ) {
    target = target[0];
  }

  // get list
  linkList = [];
  rowString = "<a href='{url}' class='sn_{icon}' target='_blank'> </a>";
  for( i = 0; i < length; ) {
    metaItem = metaList[i++];

    // name exists
    name = metaItem.getAttribute( "name" );
    if( !name ) {
      continue;
    }

    // content exists
    url = metaItem.getAttribute( "content" );
    if( !url ) {
      continue;
    }

    // split name
    splitedName = name.split( /-/ );

    // name with udes-social-[media]
    if( ( splitedName.length != 3 ) ||
        ( splitedName[0] != "udes" ) ||
        ( splitedName[1] != "link" ) ) {
      continue;
    }

    // get social network icon
    icon = splitedName[2];

    // push into list
    linkList.push(
      rowString
        .replace( /{url}/, url )
        .replace( /{icon}/, icon )
    );    
  }

  // append dom
  if( linkList ) {
    div = document.createElement( "div" );
    div.id = "social_networks";
    div.innerHTML = linkList.join( "&nbsp;" );
    target.appendChild( div );
  }
}

// -->

