
function setsrcimg( aImg, aSrc ) {
	for ( var j=0; j<document.images.length; j++ )
		  if (document.images[ j ].name == aImg )
		  {
			  	document.images[ j ].src = aSrc ;
				break ;  
		  }
}

var OnLoading = new Array() ;
function OnLoadingAdd(func){
  OnLoading[OnLoading.length] = func;
}

Event.observe(window, 'load', function() {
	var inlinkname ='listmode-';
	var initlisting = false ;
	for (i = 0; i < document.links.length; i++) {
		// Définition de l'événement onclick
		if ( document.links[i].className == 'infopoint' ){
			document.links[i].onclick = function(){
				var linkname = this.name ;
				Help( linkname ) ;
				return false ;
			}
		}

		if ( ( document.links[i].className == 'listmode2' ) || 
			  ( document.links[i].className == 'listmode1' ) ||
			  ( document.links[i].className == 'listmode0' )) {
		 	var linkname = document.links[i].name ; 
			if ( linkname != '' ){
			var myname = linkname.substring( inlinkname.length ) ;
			if ( $( myname ) && !initlisting){
				var selectedclass = $( myname ).className; 
				initlisting = true ;

				if ( selectedclass == 'listing' ){
					setsrcimg( 'list0-'+myname, 'design/default/mod-system/img/list0.png' ) ;
					setsrcimg( 'list1-'+myname, 'design/default/mod-system/img/list1_.png' ) ;
					setsrcimg( 'list2-'+myname, 'design/default/mod-system/img/list2_.png' ) ;
				}
				if ( selectedclass == 'listing0' ){
					setsrcimg( 'list0-'+myname, 'design/default/mod-system/img/list0_.png' ) ;
					setsrcimg( 'list1-'+myname, 'design/default/mod-system/img/list1.png' ) ;
					setsrcimg( 'list2-'+myname, 'design/default/mod-system/img/list2_.png' ) ;
				}
				if ( selectedclass == '' ){
					setsrcimg( 'list0-'+myname, 'design/default/mod-system/img/list0_.png' ) ;
					setsrcimg( 'list1-'+myname, 'design/default/mod-system/img/list1_.png' ) ;
					setsrcimg( 'list2-'+myname, 'design/default/mod-system/img/list2.png' ) ;
				}
			
			}
			document.links[i].onclick = function(){
				var linkname = this.name ;
				linkname = linkname.substring( inlinkname.length ) ;
				var list0 = 'list0-'+linkname ;
				var list1 = 'list1-'+linkname ;
				var list2 = 'list2-'+linkname ;
				var moi = this.className;
				//alert( 'moi['+moi+']' ) ;
				if ( moi == 'listmode0' ){
					setsrcimg( list0, 'design/default/mod-system/img/list0.png' ) ;
					setsrcimg( list1, 'design/default/mod-system/img/list1_.png' ) ;
					setsrcimg( list2, 'design/default/mod-system/img/list2_.png' ) ;
					$( linkname ).className ='listing' ;
				}
				if ( moi == 'listmode1' ){
					setsrcimg( list0, 'design/default/mod-system/img/list0_.png' ) ;
					setsrcimg( list1, 'design/default/mod-system/img/list1.png' ) ;
					setsrcimg( list2, 'design/default/mod-system/img/list2_.png' ) ;
					$( linkname ).className ='listing0' ;
				}
				if ( moi == 'listmode2' ){
					setsrcimg( list0, 'design/default/mod-system/img/list0_.png' ) ;
					setsrcimg( list1, 'design/default/mod-system/img/list1_.png' ) ;
					setsrcimg( list2, 'design/default/mod-system/img/list2.png' ) ;
					$( linkname ).className ='' ;
				}

				var url = 'mod-system/ajax/memolisting.php';
				var className = $( linkname ).className ;
				new Ajax.Request(url, { method: "post", parameters:{listing:linkname, classe:className}});	
				return false ;
			}
		} }// openclose

	}// boucle links

	// onLoad, vérifier qu'on peut lancer une MsgBox
	if (typeof(BoxIntro)!='undefined'){
			Help(BoxIntro) ;
	}
	
	// to have multiple onload events and to be managed by each page itself...
	for(var i = 0;i < OnLoading.length;i++)
		eval(OnLoading[i]);
	 
});

function OpenParameters(a){
	new Effect.toggle(a,'slide'); 
}
function hideIndicator(){
	$('indicator').hide();	
}
function MenuSel( id, sh ){
	var j = 'tool'+id ;
	if ( sh ) {
		if ( $(j) ) $(j).show();
		$( id ).className = 'rolled' ;
	}
	else {
		if ( $(j) ) $(j).hide();
		$( id ).className = '' ;
	}
}

function AfterALLSortable(){
	var options = {	  
		constraint: false, 
		handle:'moduleHeader',
		markDropZone:true,
		containment: ['col1', 'col2', 'col3'],
		dropOnEmpty: true, onUpdate: function(list) {
		  var methodStart = list.down('li') ? 'remove' : 'add';
		  list[methodStart + 'ClassName']('empty');
		}
	};
	if ($('col1')) Sortable.create('col1', options);
	if ($('col2')) Sortable.create('col2', options);
	if ($('col3')) Sortable.create('col3', options);
	
}

function updateOrder( list ){
	var url = 'mod-system/ajax/memolisting.php';
	new Ajax.Request(url, { method: "post", parameters:{orderlisting:list.id, order:Sortable.serialize(list)}});	
}

var Globalurl = '' ;
function SetVideo(){
	document.getElementById("leplayer").src = "mod-system/player.php?url=" + encodeURI(Globalurl);	
	Element.show("leplayerconteneur");
}
function playIt( url ){
	Globalurl = url ;
	Element.setStyle("lenoir", {opacity: 0.01});
	Element.show("yoyo");
	Element.show("lenoir");
	new Effect.Opacity("lenoir", {duration:2, from:0.01, to:0.7, afterFinish:SetVideo})	
}
function HideVideo(){
	Element.hide("yoyo");
}
function stopVideo(){
	new Effect.Opacity("lenoir", {duration:1.5, from:0.7, to:0.01, afterFinish:HideVideo})	
}
function EraseVideo(){
	Element.hide("leplayerconteneur") ;
	document.getElementById("leplayer").src = "mod-system/player.php?url=stop";
}


function getWindowWidth( who) {
	return ($(who).innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0);
}
function getWindowHeight( who ){
	return ($(who).innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0);
}
function getDocumentWidth(who){
	return Math.min(document.body.scrollWidth,getWindowWidth(who));
}
function getDocumentHeight(who){
	return Math.max(document.body.scrollHeight,getWindowHeight(who));
}

function Center( who ){
	var dimensions = $(who).getDimensions();
	Position.prepare();
	var offset_left = (Position.deltaX + Math.floor((getWindowWidth(who) - dimensions.width) / 2));
	var offset_top = (Position.deltaY + ((getWindowHeight(who) > dimensions.height) ? Math.floor((getWindowHeight(who) - dimensions.height) / 2) : 0));
	$(who).setStyle({
		top: ((dimensions.height <= getDocumentHeight(who)) ? ((offset_top != null && offset_top > 0) ? offset_top : '10') + 'px' : 10),
		left: ((dimensions.width <= getDocumentWidth(who)) ? ((offset_left != null && offset_left > 0) ? offset_left : '10') + 'px' : 10)
	});
}

function WriteCookie(nom, valeur, jours, chemin, domaine, secure)
{
	var expire = new Date ();
	expire.setTime (expire.getTime() + (jours * 24 * 60 * 60 * 1000));

	document.cookie = nom + "=" + escape(valeur) + ( (expire) ? ";expires=" + expire.toGMTString() : "") +
	( (chemin) ? ";path=" + chemin : "") + ( (domaine) ? ";domain=" + domaine : "") +
	( (secure) ? ";secure" : "");
}


function isNumeric(sText){
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++){
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1){
            IsNumber = false;
        }
    }
    return IsNumber;
}
function isPhoneNumber(sText){
	return ( isNumeric(sText) && (sText.length>=8) && (sText.charAt(0)!="0") ) ;
}

function ValidateDeviceCode(){
	$('error').hide();
	new Ajax.Updater( 'error',
				'mod-user/ajax/edit_device.php',
				{
					onComplete: hideIndicator,
					evalScripts:true,
					method:"post",
					encoding:'ISO 8859-1',
					parameters:$('form_settings').serialize(true)
				}
	);
}

function DownloadPlayer(){
	ShowIndicator();
	new Ajax.Updater( '_INFODIV',
						   'mod-user/ajax/downloadplayer.php', { 
								onComplete:ShowInfoPos, 
								method: "post", 
								evalScripts:true
								}
						  );		
}

function ShowListOfDevices(){
	HideIndicator();	
	$('_LIST_OF_DEVICES').show();
}

function GetListDevices( cons ){
	themanufacturer = document.form_devices.manufacturer.options[ document.form_devices.manufacturer.selectedIndex ].value ;
	if ( themanufacturer != '--' ){
		ShowIndicator();
		new Ajax.Updater(
			'_LIST_OF_DEVICES',
			'mod-user/ajax/devicemodel.php',
			{
				onComplete:ShowListOfDevices,
				evalScripts : true,
				method:"post",
				parameters:{constructeur:themanufacturer, inBox:1}
			}
		);
	}
}