// Fonction générique de récupération d'un élément
function get(id) {
	return document.getElementById(id);
}

function submit() {
	document.forms[0].submit();
}
function submitForm(id) {
	get(id).submit();
}
function submitFormAction(id, action) {
	get('action_value').value = action;
	submitForm(id);
}
function submitFormAction2(id, action) {
	get('action_value2').value = action;
	submitForm(id);
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body && document.body.clientHeight) {
			windowHeight = document.body.clientHeight;
		}
	}
	return windowHeight;
}
function setFooter() {
	if (document.getElementById('footer')) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var pageHeight = document.getElementById('page').offsetHeight;
			var footerElement = document.getElementById('footer');
			footerElement.style.display = 'block';
			var footerHeight  = footerElement.offsetHeight;
			
			var maskElement = document.getElementById('bg_mask');
			var maskHeight  = maskElement.offsetHeight;
			
			//alert('Page = ' + pageHeight + ' / Window = ' + windowHeight);
			
			if (windowHeight >= (pageHeight + footerHeight)) {
				footerElement.style.top = (windowHeight - footerHeight) + 'px';
				maskElement.style.height = windowHeight + 'px';
			} else {
				footerElement.style.top = (pageHeight) + 'px';
				maskElement.style.height = (pageHeight + footerHeight) + 'px';
			}
		}
	}
}

function setBackground() {
	var w = screen.width;
	var h = screen.height;
	
	if (w > 1680) {
		width = 1920;
	} else if (w > 1440) {
		width = 1680;
	} else if (w > 1366) {
		width = 1440;
	} else if (w > 1280) {
		width = 1366;
	} else if (w > 1024) {
		width = 1280;
	} else {
		width = 1024;
	}
	
	document.body.style.backgroundImage = 'url(img/earth_' + width + '.jpg)';
}

window.onload = function() {
	setFooter();
}
window.onresize = function() {
	setBackground();
	setFooter();
}

function bt_over(bt) {
	bt.style.background='url(img/'+bt.id+'_on.png)';
}
function bt_out(bt) {
	bt.style.background='url(img/'+bt.id+'.png)';
}
function resetField(field, default_value) {
	if (field.value == default_value) {
		field.value = '';
	}
}

function showSubmenus_bkp() {
	hide('what');
	show('sub_what');
	hide('browse');
	show('sub_browse');
	hide('post');
	show('sub_post');
	hide('news');
	show('sub_news');
}


function isIE() {
	return (navigator.userAgent.indexOf('MSIE') != -1)
}
function isIE7() {
	return (navigator.userAgent.indexOf('MSIE') != -1 && navigator.appVersion.indexOf('MSIE 7.') !=-1)
}

function showSubmenus() {
	hide('what');
	show('sub_what');
	hide('browse');
	show('sub_browse');
	hide('post');
	show('sub_post');
	hide('news');
	show('sub_news');
	
	if (!isIE() && isFlipOn()) {
		get('flip_on').value = '0';
		flipSubmenu(1);
		setTimeout(function(){ get('flip_on').value = '1'; }, 10000);
	}
}
function isFlipOn() {
	return get('flip_on').value == '1';
}

function flipSubmenu(i) {
	colors = new Array('', '#f76300', '#f98200', '#fcad00', '#cc3366', '#e24f8c', '#ef6ba0', '#357770', '#529992', '#70bab7', '#6d9e26', '#87ad28', '#9ec630');
	if (i <= 12) {
		$('#m_'+i).flip({direction:'tb',color: colors[i],speed:200});
		setTimeout(function(){ flipSubmenu(++i); }, 100);
	}
}

function m_over(menu) {
	menu.style.backgroundColor = '#fff';
	menu.className = 'over';
}
function m_out(menu, i) {
	colors = new Array('', '#f76300', '#f98200', '#fcad00', '#cc3366', '#e24f8c', '#ef6ba0', '#357770', '#529992', '#70bab7', '#6d9e26', '#87ad28', '#9ec630');
	menu.style.backgroundColor = colors[i];
	menu.className = '';
}

function hideSubmenus() {
	hide('sub_what');
	showTableCell('what');
	hide('sub_browse');
	showTableCell('browse');
	hide('sub_post');
	showTableCell('post');
	hide('sub_news');
	showTableCell('news');
}

function c_over(cell) {
	id = cell.id;
	//alert(id);
	for (i = 1; i <= 27; i++) {
		u = i-1;
		if ('c_'+i == id) {
			get('c_'+i).className = 'cell_over';
		} else if ('c_'+u == id) {
			get('c_'+i).className = 'cell_under';
		} else {
			get('c_'+i).className = 'cell';
		}
	}
}

function focusLoginField(value) {
	growFields(value);
	get('login_message').style.display = 'none';
}
function growFields(default_login) {
	if (get('login_name').value == default_login) {
		get('login_name').value = '';
	}
	if (get('password').style.width == '0px') {
		growElement('login_name', 120, 180);
		showInline('password');
		growElement('password', 0, 80);
	}
}

function focusPassField(default_pass) {
	if (get('password').value == default_pass) {
		changeInputType(get('password'), 'password', 'pass', 'password');
		get('password').focus();
		//get('password').value = '';
		//get('password').type = 'password';
	}
	if (get('send_pass').style.opacity == 0) {
		setTimeout(function(){ fadeIn('send_pass_sep', 0); fadeIn('send_pass', 0); }, 1000);
	} else if (get('send_pass').style.display == '') {
		setTimeout(function(){ showInline('send_pass_sep'); showInline('send_pass'); }, 1000);
	}
}
function focusPassConfField(id) {
	changeInputType(get(id), 'password', id, id);
	get(id).focus();
}

function changeInputType(oldObject, oType, oName, oId) {
  var newObject = document.createElement('input');
  newObject.type = oType;
  newObject.style.width = oldObject.style.width;
  newObject.name = oName;
  newObject.id = oId;
  oldObject.parentNode.replaceChild(newObject,oldObject);
  newObject.focus();
  return newObject;
}

function fbConnect() {
	window.open('action.php?action=fb_connect', 'Facebook', 'width=600,height=340,resizable=yes');
}

function increaseAmountTwice() {
	get('donation_amount').value++;
	get('donation_amount2').value++;
}
function decreaseAmountTwice() {
	if (get('donation_amount').value > 5) get('donation_amount').value--;
	if (get('donation_amount2').value > 5) get('donation_amount2').value--;
}
function increaseAmount(id, project_ids) {
	get(id).value++;
	computeTotal(project_ids);
}
function decreaseAmount(id, project_ids) {
	if (get(id).value > 5 || (id == 'donation_ecofund' && get(id).value > 0)) {
		get(id).value--;
		computeTotal(project_ids);
	}
}
function computeTotal(project_ids) {
	if (get('total')) {
		sum = 0;
		ids = project_ids.split('-');
		for (i = 0; i < ids.length; i++) {
			if (get('donation_' + ids[i])) {
				sum = parseInt(get('donation_' + ids[i]).value) + sum;
			}
		}
		sum = parseInt(get('donation_ecofund').value) + sum;
		get('total').innerHTML = sum;
	}
}

function sendPass() {
	get('action_value').value = 'send_pass';
	submit();
}
function growElement(id, width, max) {
	if (width < max) {
		width = Math.min(width + 8, max);
		get(id).style.width = width + 'px';
		setTimeout(function(){ growElement(id, width, max); }, 40);
	}
}
function growElementSlow(id, width, max) {
	if (width < max) {
		width += 2;
		get(id).style.width = width + 'px';
		setTimeout(function(){ growElementSlow(id, width, max); }, 30);
	}
}
function growImage(id, width, height, max_width, max_height) {
	if (width < max_width) {
		width += 2;
		get(id).style.width = width + 'px';
		if (height < max_height) {
			height += 2;
			get(id).style.height = height + 'px';
		}
		setTimeout(function(){ growImage(id, width, height, max_width, max_height); }, 20);
	}
}


function growConfirmationField(id) {
	if (get(id).style.width == '0px') {
		growElement(id, 0, 180);
	}
}
function hideError(id) {
	hide(id + '_err');
	//if (id == 'pass') id = 'register_pass';
	get(id).className = '';
}

function light_on(id) {
	get(id+'_mask').style.display = 'none';
}
function light_off(id) {
	get(id+'_mask').style.display = 'block';
}

function showInline(id) {
	get(id).style.display = 'inline';
}
function showInlineBlock(id) {
	get(id).style.display = 'inline-block';
}
function showTableCell(id) {
	if (isIE7()) {
		get(id).style.display = 'block';
	} else {
		get(id).style.display = 'table-cell';
	}
}
function show(id) {
	get(id).style.display = 'block';
}
function hide(id) {
	get(id).style.display = 'none';
}
function showHide(id) {
	if (get(id).style.display == 'block') {
		get(id).style.display = 'none';
	} else {
		get(id).style.display = 'block';
	}
}
function showPopup() {
	show('mask');
	show('popup');
}
function hidePopup() {
	hide('mask');
	hide('popup');
}

function old_decreaseAmount(expected, total)
{
	if (get('amount').value > 10) {
		amount = parseInt(get('amount').value);
		amount = Math.max(10, amount-5);
		get('amount').value = amount;
		
		//width = Math.max(1, 140*(amount/expected));
		//get('barre_don').style.width = width+'px';
		
		//get('total').innerHTML = total + amount;
	}
}
function old_increaseAmount(expected, total)
{
	amount = parseInt(get('amount').value);
	amount = amount+5;
	get('amount').value = amount;
	
	//width = Math.max(1, 140*(amount/expected));
	//get('barre_don').style.width = width+'px';
	
	//get('total').innerHTML = total + amount;
}

function resetContent(id) {
	get(id).value = '';
}

function stringRandom(nb_char) {
	var alphabet = "abcdefghijklmn";
	
	var string = '';
	for (i = 0; i < nb_char; i++) {
		string += alphabet[Math.floor(Math.random()*alphabet.length)];
	}
	return string;
}
function testRegister() {
	get('first_name').value = stringRandom(10);
	get('name').value = stringRandom(10);
	//get('login').value = stringRandom(10);
	
	var email = stringRandom(10) + "@" + stringRandom(5) + "." + stringRandom(2);
	get('email').value = email;
	get('email_conf').value = email;
	
	var pass = stringRandom(6);
	get('pass').value = pass;
	get('pass_conf').value = pass;
}

function zoomInProject(id, x, y, total) {
	get('loop_'+id).style.width = '45px';
	show('infos_'+id);
	for (i = 1; i <= total; i++) {
		if (i != id) hide('loop_'+i);
	}
}

function zoomOutProject(id, width, x, y, total) {
	get('loop_'+id).style.width = width+'px';
	hide('infos_'+id);
	for (i = 1; i <= total; i++) {
		if (i != id) showInline('loop_'+i);
	}
}

















function preloadImages() {
	var img = new Array();
	var urls = new Array('item_bg0.gif','item_bg1.gif','Flechesb_1.png','Flechesb_1small.png','Flechesw_1.png','Flechesw_1small.png',
'Flechesb_2.png','Flechesb_2small.png','Flechesw_2.png','Flechesw_2small.png');
	for (var i = 0; i < urls.length; i++) {
		img[img.length] = new Image();
		img[img.length - 1].src = 'img/'+urls[i];
	}
}

function fadeIn(id, opacity) {
	if (opacity < 100) {
		opacity += 2;
		setOpacity(id, opacity);
		setTimeout(function(){ fadeIn(id, opacity); }, 10);
	}
}

function slowFadeIn(id, opacity) {
	if (opacity < 100) {
		opacity += 2;
		setOpacity(id, opacity);
		setTimeout(function(){ slowFadeIn(id, opacity); }, 25);
	}
}

function delayFadeIn(id, opacity) {
	if (opacity < 100) {
		delay = (opacity == 0) ? 1000 : 10;
		opacity += 2;
		setOpacity(id, opacity);
		setTimeout(function(){ slowFadeIn(id, opacity); }, delay);
	}
}

function scan(item) {
	if (item.style.background == '' || item.style.background.substring(0, 4) == 'none') {
		item.style.background = 'no-repeat url(img/item_bg0.gif)';
		moveBackground(item, -40, 120);
		item.style.color = 'white';
	}
}

function moveBackground(item, position, max) {
	if (position < max) {
		position += 14;
		if (position > max) position = max;
		item.style.backgroundPosition = position+'px';
		setTimeout(function(){ moveBackground(item, position, max); }, 20);
	} else {
		if (item.style.color == 'white') {
			item.style.background = '#00ADD6';
			var sub = get(item.id+'_sub').innerHTML;
			if (sub != '') {
				var length = sub.toLowerCase().split('<br>').length - 1;
				item.style.lineHeight = (52/length)+'px';
				item.style.fontSize = '11px';
				item.innerHTML = sub;
			}
			item.style.background = 'no-repeat url(img/item_bg1.gif)';
			moveBackgroundBack(item, position);
		}
	}
}

function moveBackgroundBack(item, position) {
	if (position > 0) {
		position -= 20;
		item.style.backgroundPosition = position+'px';
		setTimeout(function(){ moveBackgroundBack(item, position); }, 20);
	} else {
		//if (item.style.color == 'white') item.style.color = '#85D8EC';
		//item.style.background = 'none';
	}
}

function reset(item) {
	item.style.color = '#777';
	item.style.background = 'none';
	item.style.lineHeight = '52px';
	item.style.fontSize = '14px';
	item.innerHTML = get(item.id+'_main').innerHTML;
}

function manageArrows(id, content) {
	// 1. MAJ du contenu
	get('content').innerHTML = content;
	
	// 2. Reset des autres fleches
	liste = get('arrows').getElementsByTagName('a');
	len = liste.length;
	for (var i = 0; i < len; i++) {
		fleche = liste.item(i);
		if (fleche.className == 'arrow_blue') fleche.className = 'arrow';
	}
	
	// 3. Fleche bleue
	get(id).className = 'arrow_blue';
}

function manageSmallArrows(id, content) {
	// 1. MAJ du contenu
	get('content').innerHTML = content;
	
	// 2. Reset des autres fleches
	liste = get('arrows').getElementsByTagName('a');
	len = liste.length;
	for (var i = 0; i < len; i++) {
		fleche = liste.item(i);
		if (fleche.className == 'arrow_blue_small') fleche.className = 'arrow_small';
	}
	
	// 3. Fleche bleue
	get(id).className = 'arrow_blue_small';
}

function hideSmallArrows() {
	get('fleche2_1').style.display = 'none';
	get('fleche2_2').style.display = 'none';
	get('fleche2_3').style.display = 'none';
}

function showSmallArrows() {
	get('fleche2_1').className = 'arrow_small';
	get('fleche2_2').className = 'arrow_small';
	get('fleche2_3').className = 'arrow_small';
	get('fleche2_1').style.display = 'block';
	get('fleche2_2').style.display = 'block';
	get('fleche2_3').style.display = 'block';
}

function checkKey(e, form_id) {
	var key;
	if (window.event) {
		key = window.event.keyCode;
	} else {
		key = e.keyCode;
	}
	if (key == 13) {
		get(form_id).submit();
	}
}

function setOpacity(id, opacity) {
	get(id).style.opacity = opacity/100;
	get(id).style.MozOpacity = opacity/100;
	get(id).style.filter = 'Alpha(Opacity='+opacity+')';
	//get(id).filters[0].opacity = opacity;
}

function fadeOutMessage() {
	setTimeout(function(){ fadeOut('message', 100); }, 5000);
}

function fadeOut(id, opacity) {
	if (opacity > 0) {
		opacity -= 5;
		setOpacity(id, opacity);
		setTimeout(function(){ fadeOut(id, opacity); }, 50);
	}
}

function setOpacityIE(id, opacity) {
	get(id).style.opacity = opacity/100;
	get(id).style.MozOpacity = opacity/100;
	get(id).style.filter = 'Alpha(Opacity='+opacity+')';
}

function animate() {
	if (navigator.userAgent.indexOf('MSIE') == -1) {
		dropDown(80);
	}
}

function dropDown(height) {
	if (height < 630) {
		height += 40;
		if (height > 630) height = 630;
		get('page').style.height = height+'px';
		setTimeout(function(){ dropDown(height); }, 30);
	} else {
		fadeInFooter('footer', 0);
	}
}

function fadeInFooter(id, opacity) {
	if (opacity < 100) {
		opacity += 2;
		setOpacity(id, opacity);
		setTimeout(function(){ fadeInFooter(id, opacity); }, 10);
	} else {
		fadeInPicture('picture', 0);
	}
}

function fadeInPicture(id, opacity) {
	if (opacity < 100) {
		opacity += 2;
		setOpacity(id, opacity);
		setTimeout(function(){ fadeInPicture(id, opacity); }, 10);
	} else {
		if (get('arrows') != null) {
			fadeInArrows(1, 0);
		} else {
			fadeInContent('content', 0);
		}
	}
}

function fadeInArrows(id, opacity) {
	if (opacity < 100) {
		opacity += 2;
		setOpacity('fleche'+id, opacity);
		setTimeout(function(){ fadeInArrows(id, opacity); }, 10);
	} else {
		id++;
		if (get('fleche'+id) != null) {
			fadeInArrows(id, 0);
		} else {
			fadeInContent('content', 0);
		}
	}
}

function fadeInArrows_() {
	for (var i = 1; (get('fleche'+i) != null) && i < 5; i++) {
		var id = 'fleche'+i;
		delayFadeIn(id, 0);
	}
}

function fadeInContent(id, opacity) {
	if (opacity < 100) {
		opacity += 2;
		setOpacity(id, opacity);
		setTimeout(function(){ fadeInContent(id, opacity); }, 10);
	} else {
		if (get('email') != null) get('email').focus();
	}
}
