/* ***************************************
 * Fonctions which comes from Plone 2.5
 * -> register_function.js
 * -> cookie_functions.js
 * ***************************************/

// check for ie5 mac
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    &&
    navigator.userAgent.indexOf('Mac') != -1
)

// check for W3CDOM compatibility
var W3CDOM = (!bugRiddenCrashPronePieceOfJunk &&
               typeof document.getElementsByTagName != 'undefined' &&
               typeof document.createElement != 'undefined' );

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+"="+escape(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 unescape(c.substring(nameEQ.length,c.length));
		}
	}
	return null;
}

function setActiveStyleSheet(title, reset) {
	// terminate if we hit a non-compliant DOM implementation
 	if (!W3CDOM){return false};
 	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;
 			}
 		}
 	}
 	if (reset == 1) {
 		createCookie("wstyle", title, 365);
 	}
}

function getActiveStyleSheet() {
	//return $("link[title!=''][disabled='']").attr('title');
	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;
}

/* ***************************************
 * / Ends of "Fonctions which comes from Plone 2.5"
 * ***************************************/

/* ***************************************
 * / Custom functions for T+ T- buttons
 * ***************************************/

function changeTextPlus(){
	var currentStyle = getActiveStyleSheet();

	switch(currentStyle) {
	 case "text normal" : setActiveStyleSheet('text bigger', 1);
	break;
	 case "text smaller" : setActiveStyleSheet('text normal', 1);
	break;

	default : setActiveStyleSheet('text bigger', 1);

	}
}

function changeTextMoins(){
	var currentStyle = getActiveStyleSheet();

	switch(currentStyle) {
	 case "text normal": setActiveStyleSheet('text smaller', 1);
	break;
	 case "text bigger" : setActiveStyleSheet('text normal', 1);
	break;

	default: setActiveStyleSheet('text smaller', 1);
	}
}

/****************************************
 * / User is connecting ?
 ****************************************/

function isAnonymousUser() {
	return readCookie('__ac') == null;
}
function isManager() {
	return readCookie('__ac_is_manager') != null;
}
function getUserInfos() {
	guerir_userinfos = readCookie('guerir_userinfos');
	guerir_userinfos = guerir_userinfos.substring(1, guerir_userinfos.length - 1); // remove extremity "
	var tab = guerir_userinfos.split("<<<>>>");
	var infos = new Array();
	infos['fullname'] = tab[0];
	infos['username'] = tab[1];
	return infos
}

/** JS for portlet_login **/

function portlet_login() {
	if (isAnonymousUser() == true) {$('#bloc_identification').css("display","block");}
	if (isAnonymousUser() == true) {
		$('#bloc_mon_compte').css("display","none");
		$('#bloc_login').css("display","block");
	} else {
		$('#bloc_mon_compte').css("display","block");
		$('#bloc_login').css("display","none");
		infos = getUserInfos();
		$('#user_fullname').html('Mon compte')
		$('#user_account_link').attr('href',$('#user_account_link').attr('href') + '/Members/' + infos['username']);
		$('#user_account_link_2').attr('href',$('#user_account_link_2').attr('href') + '/Members/' + infos['username']);
	}
	if (isManager() == true) {$('#administration-links').css('display', 'block');}
	else {$('#administration-links').css('display', 'none');}
	setActiveStyleSheet(readCookie('wstyle') , 1);
}