/*		function backToTop()
			http://web-graphics.com/mtarchive/001659.php
			The cool BackToTop Javascript for smooth scrolling back
			to the top of a page was written by David Lindquist.
--------------------------------------------------------------------- */
function btt_install( ) {
	if( !document.getElementsByTagName ) { return; }
	var a, i = 0;
	while(( a = document.getElementsByTagName( 'a' )[i++] )) {
		if( '#top' == a.getAttribute( 'href' )) {
			a.onclick = function() { backToTop(); return false; }
		}
	}
}

function backToTop() {
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = 0;

	if( document.documentElement ) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}

	if( document.body ) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}

	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;

	var x = Math.max( x1,Math.max( x2,x3 ));
	var y = Math.max( y1,Math.max( y2,y3 ));

	window.scrollTo(Math.floor( x/2 ), Math.floor( y/2 ));

	if( x > 0 || y > 0 ) {
		window.setTimeout( 'backToTop()',25 );
	}
	return false;
}


function getElementsByClass( theClass ) {
	var c,i,j;
	var els = new Array( );
	var es  = document.getElementsByTagName( '*' );

	//		getElement by class name
	for( i = 0,j = 0; i < es.length; i++ ) {
		c = ' ' +es[i].className +' ';
		if( c.indexOf( ' ' +theClass +' ' ) != -1 ) { els[j++] = es[i]; }
	}
	return els;
}

function doTab( ) {

	var ee  = this.parentNode;
	var	len = ee.childNodes.length;

	var num = this.id;
	num = num.substring( num.length -1 );
	var t = '';
	var p;
	//		hide
	for( var i = 0; i <= len; i++ ) {
		t = 'pane' +( i +1 );
		p = document.getElementById( t );
		if( null != p ) {
			p.style.display = 'none';
			p.style.visibility = 'hidden';
			t = 'tab' +( i +1 );
			p = document.getElementById( t );
			p.className = 'tab';
		}
	}
	//	show
	t = 'pane' +num;
	p = document.getElementById( t );
	p.style.display    = 'block';
	p.style.visibility = 'visible';
	t = 'tab' +num;
	p = document.getElementById( t );
	p.className += ' selected';
}

function tabs_install( ) {
	var e = getElementsByClass( 'tab' );
	var tabs = e.length;

	if( 0 == tabs ) { return; }
	for( var i = 0; i < tabs; i++ ) {
		e[i].onclick = doTab;
	}
}


/*		if force is 'true' then the designated id is forced hidden
			else this acts as a normal toggle
*/
function toggler( el, force ) {
	var e = document.getElementById( el );

	if( true == force ) {
		e.style.visibility = 'hidden';
		e.style.display = 'none';	
	} else {
		if( 'visible' == e.style.visibility ) {
			e.style.visibility = 'hidden';
			e.style.display = 'none';
		} else {
			e.style.visibility = 'visible';
			e.style.display = 'block';
		}
	}
}

function langSwitcher( v ) {

	var e = document.location.href;
	var s = e.split( '?' );
	e = s[0]+'?lang='+v;
	document.location.href=e;
	return true;
}


function submitForm() {
	//		attempt to hide the submit button to stop multiple clicks...
	var e = document.getElementById( 'submit' );
	e.style.visibility = 'hidden';
	e.parentNode.innerHTML = '<p id="wait">En cours de traitement...</p>';
	//		attempt to kill the hidden list
	e = document.getElementById( 'list' );
	e.innerHTML = '';
	
}

function hide( d ) {

	var e = document.getElementById( d );
	e.style.display = 'none';
	return false;
}



//		GLOBAL VARS FOR COMPARATOR
var msg   = '';
var alertMsg = '';
var compAlert = true;


function add2Comparator( eq,msg2,alertMsg2 ) {
	msg = msg2;
	alertMsg = alertMsg2;

	//		calc url
	var url = location.protocol +'//' +location.hostname +'/remote/';
	//		call server
	XHR_SendData( 'POST',url,'cmd=add&eq='+eq );
	return true;
}


/*		experimental XHR stuff
-----------------------------------------------------------------------------------------	*/


/*		fonction callback de traitement des
			données reçues après la requête
------------------------------------------------- */
function XHR_HandleReply( reply ) {

	var r = reply.responseText;	
	var rr = r.substr( 3,3 );

	if( 'add' == rr ) {

		var album = document.getElementById( 'album' );
		var parAlbum = album.parentNode;
		parAlbum.setAttribute( 'id','album' );
		parAlbum.innerHTML = msg;

		rr = r.replace( /(\s)*\[add\]/i,'' );

		if( rr == 'TRUE' ) {
			var a = document.getElementById( 'alert' );
			a.innerHTML = '<div onClick="hide(\'alert\');">' +alertMsg +'</div>';
			a.style.display = 'block';
	
		}

	} else if( 'sup' == rr ) {

		rr = r.replace( /\[sup\]/i,'' );

		var CF = document.getElementById( 'fComparator' );
		CF.innerHTML = rr;

	}

	return true;

}

/*		fonction qui crée l'objet requête
			et qui installe la fonction callback
			de traitement de la réponse.
------------------------------------------------- */
function XHR_GetHTTPObject( ) {
	var xmlhttp = false;

	if( !xmlhttp && typeof XMLHttpRequest != 'undefined' ) {
		try { xmlhttp = new XMLHttpRequest(); }
		catch( e ) { xmlhttp = false; }
	 }

	if( xmlhttp ) {
		xmlhttp.onreadystatechange = function( ) {
			if( xmlhttp.readyState == 4 ) { // 4 : état "complete"
				if( xmlhttp.status == 200 ) { // 200 : code HTTP pour OK
					XHR_HandleReply( xmlhttp );
				}
			}
		}
	}
	return xmlhttp;
}

/*		fonction qui envoie une requête XmlHttpRequest
			@param string methode d'envoi ['GET'|'POST']
			@param string url du script php à invoquer
			@param string données à envoyer sous la forme
			var1=value1&var2=value2...
------------------------------------------------- */
function XHR_SendData( method,url,data ) {
	var xmlhttp = XHR_GetHTTPObject( );

	if( !xmlhttp ) { return false; }

	if( method == 'GET' ) {
		if( data == 'null' ) {
			xmlhttp.open( 'GET',url,true );
		} else {
			xmlhttp.open( 'GET',url +'?' +data,true );
		}
		xmlhttp.send( null );
	} else if( method == 'POST' ) {
		xmlhttp.open( 'POST',url,true );
		xmlhttp.setRequestHeader( 'Content-Type','application/x-www-form-urlencoded' );
		xmlhttp.send( data );
	}
	return true;
}





/*		COOKIES
----------------------------------------------------------------------------------------	*/
function Cookie_Set( cookieName,cookieValue,expireDays ) {
	//		checks
	if( null == cookieName ) {
		return false; 
	}

	var exdate = new Date();
	exdate.setDate( exdate.getDate() +expireDays );
	document.cookie = cookieName +'=' +escape( cookieValue )+ (( null == expireDays )
		? ''
		: ';expires=' +exdate.toGMTString()) ;
}


function Cookie_Get( cookieName ) {
	//		checks
	if( null == cookieName ) {
		return false;
	}

	if( document.cookie.length > 0 ) {
		c_start = document.cookie.indexOf( cookieName +'=' );
		if( -1 != c_start ) { 
			c_start = c_start +cookieName.length +1;
			c_end   = document.cookie.indexOf( ';',c_start );
			if( -1 == c_end ) { 
				c_end = document.cookie.length;
			}
    return unescape( document.cookie.substring( c_start,c_end ));
    } 
  }
	return '';
}


/*		COMPARATOR
----------------------------------------------------------------------------------------	*/
function CF_SelectCheck( cmd ) {
	//		get the form...
	var i;
	var CF = document.getElementById( 'fComparator' );
	var CF_len = CF.length;
	for( i = 1; i < CF_len;i++ ) {
		CF[i].checked = '';
		if( 'all' == cmd ) {
			CF[i].checked = 'true';
		}
	}
}

function CF_SelectAction( v ) {
	if( '' != v ) {
		var i,lst = '';
		var sep = '';
		var CF = document.getElementById( 'fComparator' );
		var CF_len = CF.length;
		for( i = 1; i < CF_len;i++ ) {
			if( CF[i].checked != '' ) {
				lst += sep +CF[i].value;
				sep = ',';
			}
		}
		//		calc url
		var url = location.protocol +'//' +location.hostname +'/remote/';
		//		call server
		XHR_SendData( 'POST',url,'cmd=remove&eq='+lst );
	}
	return true;
}


