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

up_down.js - display an iframe on other site contents
    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 ----- ----- ----- */

/* ----- ----- ----- init ----- ----- ----- */
function updown_init(id,corps,open,groupe) {
	var item=document.getElementById(corps);

	var click=document.getElementById(id);

	var updownDiv=document.createElement('div');

	updownDiv.style.filter='alpha(opacity=100)';
	updownDiv.style.opacity=1;
	updownDiv.style.cssFloat='none';
	updownDiv.style.display='inline-block';
	updownDiv.style.width='auto';
	updownDiv.style.height='auto';
	updownDiv.style.margin='0';
	updownDiv.style.padding='0';
	updownDiv.style.border='0';
	updownDiv.style.overflow='hidden';
	updownDiv.style.background='transparent';

	item.updownDiv=updownDiv;
	item.parentNode.replaceChild(updownDiv,item);
	updownDiv.appendChild(item);

	item.updownMax=updownDiv.offsetHeight;

	item.updownFactor=0.2;
	item.updownMinStep=0.8;
	item.updownTime=25;
	item.style.cursor='pointer';

	if(open) {
		item.updownFinal=item.updownMax;
		item.updownValue=item.updownMax;
		updownDiv.style.height=item.updownMax+"px";
	} else {
		item.updownFinal=0;
		item.updownValue=0;
		updownDiv.style.height=0;
	}

	if(groupe) {
		var gitem=document.getElementById(groupe);
		if(gitem.updownGroupe) {
			gitem.updownGroupe.push(corps);
		} else {
			gitem.updownGroupe=new Array(corps);
		}
		item.updownGroupe=groupe;
	}

	eventAddAfter(click,'onclick','updown_click("'+corps+'")');
}

/* ----- ----- ----- Timer ----- ----- ----- */
function updown_timer(id) {
	var item=document.getElementById(id);

	if(item.updownTimer) clearTimeout(item.updownTimer);

	var delta=item.updownFactor*(item.updownFinal-item.updownValue);
	if(delta>0) delta=Math.max(item.updownMinStep,delta); else delta=Math.min(-item.updownMinStep,delta);

	if(Math.abs(item.updownFinal-item.updownValue)>item.updownMinStep) {
		item.updownValue+=delta;
		item.updownDiv.style.height=Math.round(item.updownValue)+"px";
		item.updownTimer=setTimeout('updown_timer("'+id+'")',item.updownTime);
	} else if(item.updownFinal==0) {
			item.updownDiv.style.height=0;
			item.updownValue=0;
	} else {
		item.updownDiv.style.height='auto';
		item.updownValue=item.updownDiv.offsetHeight;
		item.updownMax=item.updownValue;
	}
}

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

	if(item.updownFinal==0) {
		updown_open(id);
	} else {
		updown_close(id);
	}
}

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

	item.updownFinal=item.updownMax;

	/* ***** ***** ***** ***** juliencoudert.com ***** ***** ***** ***** */

	if(item.updownGroupe) {
		var gitem=document.getElementById(item.updownGroupe);
		for(var i=0;i<gitem.updownGroupe.length;i++) {
			if(id!=gitem.updownGroupe[i]) {
				updown_close(gitem.updownGroupe[i]);
			}
		}
	}
/*	if(id!='updown_1')updown_close('updown_1');
	if(id!='updown_2')updown_close('updown_2');
	if(id!='updown_3')updown_close('updown_3');
	if(id!='updown_4')updown_close('updown_4');
	if(id!='updown_5')updown_close('updown_5');
*/
	updown_timer(id);
}

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

	item.updownFinal=0

	updown_timer(id);
}
