/* ----- ----- ----- Free software Foundation - Affero Licence ----- ----- -----

fade.js - link color effect on mouse over/out
    Copyright (C) 2008  Thibault Garcia (thibault.garcia@revaweb.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

----- ----- ----- Free software Foundation - Affero Licence ----- ----- ----- */
function fade_init(id,attribut,color) {
	var item=document.getElementById(id);
	item.fadeAttribut=attribut;
	
	if(attribut) {
		item.fadeFactor=0.1;
		item.fadeMinStep=1;
		item.fadeDelay=25;
	
		item.fadeRValue=parseInt(color.substring(0,2),16);
		item.fadeGValue=parseInt(color.substring(2,4),16);
		item.fadeBValue=parseInt(color.substring(4,6),16);
	
		eval('item.style.'+item.fadeAttribut+'="rgb('+parseInt(item.fadeRValue)+','+parseInt(item.fadeGValue)+','+parseInt(item.fadeBValue)+')"');
	}
}

function fade_set(id,color) {
	var item=document.getElementById(id);

	if(item.fadeAttribut) {
		if(item.fadeTimeout) clearTimeout(item.fadeTimeout);

		item.fadeRFinal=parseInt(color.substring(0,2),16);
		item.fadeGFinal=parseInt(color.substring(2,4),16);
		item.fadeBFinal=parseInt(color.substring(4,6),16);

		item.fadeRValue=item.fadeRFinal;
		item.fadeGValue=item.fadeGFinal;
		item.fadeBValue=item.fadeBFinal;

		eval('item.style.'+item.fadeAttribut+'="rgb('+parseInt(item.fadeRValue)+','+parseInt(item.fadeGValue)+','+parseInt(item.fadeBValue)+')"');
	}
}

function fade_color(id,color) {
	var item=document.getElementById(id);

	if(item.fadeAttribut) {
		if(item.fadeTimeout) clearTimeout(item.fadeTimeout);

		item.fadeRFinal=parseInt(color.substring(0,2),16);
		item.fadeGFinal=parseInt(color.substring(2,4),16);
		item.fadeBFinal=parseInt(color.substring(4,6),16);

		fade_timer(id);
	}
}

function fade_timer(id) {
	var item=document.getElementById(id);

	if(item.fadeTimeout) clearTimeout(item.fadeTimeout);

	var deltaR=item.fadeFactor*(item.fadeRFinal-item.fadeRValue);
	if(deltaR>0) deltaR=Math.max(item.fadeMinStep,deltaR); else deltaR=Math.min(-item.fadeMinStep,deltaR);

	var deltaG=item.fadeFactor*(item.fadeGFinal-item.fadeGValue);
	if(deltaG>0) deltaG=Math.max(item.fadeMinStep,deltaG); else deltaG=Math.min(-item.fadeMinStep,deltaG);

	var deltaB=item.fadeFactor*(item.fadeBFinal-item.fadeBValue);
	if(deltaB>0) deltaB=Math.max(item.fadeMinStep,deltaB); else deltaB=Math.min(-item.fadeMinStep,deltaB);


	var nb=0;

	if(Math.abs(item.fadeRFinal-item.fadeRValue)>item.fadeMinStep) {
		item.fadeRValue+=deltaR;
	} else {
		item.fadeRValue=item.fadeRFinal;
		nb++;
	}

	if(Math.abs(item.fadeGFinal-item.fadeGValue)>item.fadeMinStep) {
		item.fadeGValue+=deltaG;
	} else {
		item.fadeGValue=item.fadeGFinal;
		nb++;
	}

	if(Math.abs(item.fadeBFinal-item.fadeBValue)>item.fadeMinStep) {
		item.fadeBValue+=deltaB;
	} else {
		item.fadeBValue=item.fadeBFinal;
		nb++;
	}

	eval('item.style.'+item.fadeAttribut+'="rgb('+parseInt(item.fadeRValue)+','+parseInt(item.fadeGValue)+','+parseInt(item.fadeBValue)+')"');

	if(nb<3) {
		item.fadeTimeout=setTimeout('if(fade_timer) fade_timer("'+id+'")',item.fadeDelay);
	}
}
