var DEBUG_MODE = 0;
var UNLOAD_ALERT = 0;
var WAIT_SCREEN = 1;
var x = -60;
var y = -60;
var posX = 0;
var posY = 0;
var top_widget = 1;
var loading_s = false;
var ie = (navigator.appName.indexOf("Explorer") > -1 ? true : false);
var x_menu = 1;
var p_menu = '';
var is_logged = false;
var is_visible = true;
var py = -1;
var or_py = new Array();
var obj = false;
var nb_div = 20;
var goto_o = -1;
var n_div = -1;
var nb_max_div = 100;
var start = false;
var is_over_one = false;
var ecart = -1;
var current_request = false;
var current_request2 = false;
var is_started = false;
var waiting = false;
var start_follower = true;

function favoriser(o){
	if(navigator.appName.indexOf("Explorer") > -1){
		o.style.behavior='url(#default#homepage)';o.setHomePage('http://www.plopibox.com');
		return true;
	}
	if(navigator.appName.indexOf("Netscape") > -1){
		alert("Pour mettre Plopi en page de démarrage de votre navigateur, suivez les instructions suivantes:\n1- Menu outils > Options\n2- Onglet Général\n3- Page d'accueil: http://www.plopibox.com'\n4- Validez en cliquant sur 'OK'.");
		return true;
	}
	if(navigator.appName.indexOf("Opera") > -1){
		alert("Pour mettre Plopi en page de démarrage de votre navigateur, suivez les instructions suivantes:\n1- Menu outils > Préférences\n2- Onglet Général\n3- Page d'accueil: http://www.plopibox.com'\n4- Validez en cliquant sur 'OK'.");
		return true;
	}
}

if (o){
    if(navigator.appName.substring(0,3) == "Net"){
    	document.captureEvents(Event.MOUSEMOVE);
    }
	document.onmousemove = Pos_souris;
}

function selectionner(n){
	if(!ie){
		o('B_'+n).setAttribute('class','top_l_sel_bouton');
		o('B_'+n+'b').setAttribute('class','top_bg_sel_bouton');
		o('B_'+n+'c').setAttribute('class','top_r_sel_bouton');
	}
	else{
		o('B_'+n).style.cssText = "width:8px;height:34px;background: transparent url(./images/top_l_sel_bouton.gif) repeat-x  top left;";
		o('B_'+n+'b').style.cssText = "background: transparent url(./images/top_bg_sel_bouton.gif) repeat-x  top left;";
		o('B_'+n+'c').style.cssText = "width:8px;height:34px;background: transparent url(./images/top_r_sel_bouton.gif) repeat-x  top left;";
	}
}

function deselectionner(n){
	if(n != x_menu){
		if(!ie){
			o('B_'+n).setAttribute('class','top_l_bouton');
			o('B_'+n+'b').setAttribute('class','top_bg_bouton');
			o('B_'+n+'c').setAttribute('class','top_r_bouton');
		}
		else{
			o('B_'+n).style.cssText = "width:8px;height:34px;background: transparent url(./images/top_l_bouton.gif) no-repeat  top left;";
			o('B_'+n+'b').style.cssText = "width:auto;height:34px;text-align: center;background: transparent url(./images/top_bg_bouton.gif) repeat-x  top left;";
			o('B_'+n+'c').style.cssText = "width:8px;height:34px;background: transparent url(./images/top_r_bouton.gif) no-repeat  top right;";
		}
	}
}

function select_menu(x){
	if(!ie){
		o('B_'+x_menu).setAttribute('class','top_l_bouton');
		o('B_'+x_menu+'b').setAttribute('class','top_bg_bouton');
		o('B_'+x_menu+'c').setAttribute('class','top_r_bouton');
	}
	else{
		o('B_'+x_menu).style.cssText = "width:8px;height:34px;background: transparent url(./images/top_l_bouton.gif) no-repeat  top left;";
		o('B_'+x_menu+'b').style.cssText = "width:auto;height:34px;text-align: center;background: transparent url(./images/top_bg_bouton.gif) repeat-x  top left;";
		o('B_'+x_menu+'c').style.cssText = "width:8px;height:34px;background: transparent url(./images/top_r_bouton.gif) no-repeat  top right;";
	}
	x_menu = x;
	selectionner(x_menu);
}

function quitterPlopi(){
	//process('logout');
}

function go(p,x){
	/*if(x_menu == x && x != 1){
		return;
	}*/
	select_menu(x);
	p_menu = p;
	//check_login();
	if(!is_logged && p != 'form_rechercher'){
		process('displayLogin');
		return;
	}
	switch(p){
		case 'toolbox':
			o('sub_ariane').innerHTML = 'Ma toolbox';
		break;
		case 'contacts':
			o('sub_ariane').innerHTML = 'Mes contacts';
		break;
		case 'rss':
			o('sub_ariane').innerHTML = 'Mes RSS';
		break;
		case 'emails':
			o('sub_ariane').innerHTML = 'Mes e-mails';
		break;
		case 'favoris':
			o('sub_ariane').innerHTML = 'Mes favoris';
		break;
		case 'form_rechercher':
		default:
			o('sub_ariane').innerHTML = 'Accueil';
		break;
	}
	process(p);
	if(p != 'toolbox'){
		switchArboToolBox(p);
	}
}


function c_email(email){
	var verif = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{1,}[.][a-zA-Z]{2,5}$/
	if (verif.exec(email) == null){
		return false;
	}
	else{
		return true;
	}
}


var current_img = null;
var current_img_titre = null;
var id_current = null;
var id_before = null;
var current_id_arbo = null;

function Pos_souris(e){
   if(obj){
   	if(py == -1 && obj){
			py = !ie ? e.layerY : event.offsetY;
		}
		y = py * -1 + (!ie ? e.pageY : event.clientY + document.documentElement.scrollTop);
		check_div(obj.id);
   	with(obj.style){top=y+'px';}
	}
	else{
		x = !ie ? e.pageX : event.clientX + document.documentElement.scrollLeft;
   	y = !ie ? e.pageY : event.clientY + document.documentElement.scrollTop;
	}
	if(current_img != null){
		with(current_img.style){position='absolute';top=(y+5)+'px';left=(x+5)+'px';}
		with(current_img_titre.style){display='inline';top=(y+25)+'px';left=(x+25)+'px';}
	}
}

function Bouge_Image(){
   if (o){
   		nPosX = posX+(((x-posX)+20)/15);
   		nPosY = posY+(((y-posY)+20)/15);
      posX = nPosX;
	    posY = nPosY;
	    with(o('div_wait').style){left=posX+'px';top=posY+'px';}
  		setTimeout("Bouge_Image()", 1);
	}
}
















function check_div(id){
	var f,t,f2,t2,o_tmp;
	f = parseInt(obj.style.top);
	t = f + parseInt(o(id).style.height);
	for(var i = 0;i<nb_div;i++){
		if(i != n_div){
			o_tmp = o("div_"+i);
			f2 = or_py[i];
			t2 = f2 + parseInt(o_tmp.style.height);
			deb1("("+t+" - "+ecart+") > "+f2+" && ("+f+" + "+ecart+") < "+t2);
			if((t - ecart) > (f2 + ecart) && (f + ecart) < (t2 - ecart)){
				goto_o = i;
				with(o('div_'+goto_o).style){top=or_py[n_div]+'px';}
				is_over_one = true;
			}
			else{
				if(goto_o > -1){
					with(o('div_'+goto_o).style){top=or_py[goto_o]+'px';}
				}
				goto_o = -1;
				is_over_one = false;
			}
			if(is_over_one){
				break;
			}
		}
	}
}

function move(){
	for(var i = 0;i<nb_div;i++){
		or_py[i] = parseInt(o('div_'+i).style.top);
		if(!ie){
			o('div_'+i).setAttribute('class','derriere');
		}
		else{
			o('div_'+i).className = "derriere";
		}
		if(y >= or_py[i] && y <= (or_py[i] + parseInt(o('div_'+i).style.height))){
			n_div = i;
		}
	}
	if(n_div > -1){
		start = true;
		obj = o('div_'+n_div);
		if(!ie){
			obj.setAttribute('class','devant');
		}
		else{
			obj.className = "devant";
		}
		px = -1;
		py = -1;
		is_over_one = false;
		goto_o = -1;
	}
	else{
		deb1('pas trouvé '+n_div+' '+y);
	}
}

function release(){
	if(start){
		start = false;
		if(goto_o < 0){
			goto_o = n_div;
		}
		with(o('div_'+goto_o).style){top=or_py[n_div]+'px';}
		with(obj.style){top=or_py[goto_o]+'px';}
		if(n_div != goto_o){
			moveFav(n_div,goto_o);
		}
		obj = false;
		n_div = -1;
	}
}

function deb1(s){
	//o('deb1').innerHTML = s;
}
function deb2(s){
	alert(s);
	//o('deb2').innerHTML = s;
}

function prepare(){
	for(var i = 0;i<nb_max_div;i++){
		if(o('div_'+i)){
			o('div_'+i).onmousedown = move;
			o('div_'+i).onmouseup = release;
			//o('div_'+i).onmouseout = release;
		}
		else{
			break;
		}
	}
	nb_div = i;
	if(nb_div < 2){
		nb_div = 0;
	}
	else{
		if(ecart < 0){
			ecart = 20;//parseInt(o('div_1').style.top) - (parseInt(o('div_0').style.top) + parseInt(o('div_0').style.height));
		}
	}
	move(nb_div-1);
	release();
}





































function getRequest(){
	if(!current_request){
    current_request = false;
        try {
            current_request = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2) {
            try {
                current_request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (err3) {
				try {
					current_request = new XMLHttpRequest();
				}
				catch (err1) 
				{
					current_request = false;
				}
            }
        }
    }
    return current_request;
}

function getRequest2(){
	if(!current_request){
    current_request2 = false;
        try {
            current_request2 = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2) {
            try {
                current_request2 = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (err3) {
				try {
					current_request2 = new XMLHttpRequest();
				}
				catch (err1) 
				{
					current_request2 = false;
				}
            }
        }
    }
    return current_request2;
}

function isStarted(){
	return is_started;
}

if(UNLOAD_ALERT){
	
	window.onunload = function(){
													alert('Attention, vous allez quitter votre travail en cours !');
													window.open('http://www.google.fr');
												};
}

function message(msg){
	msg = '<div align="right" style="position:absolute;left:'+(x+20)+'px;top:'+(y+20)+'px;"><input type="button" onclick="cacher_message()" value="Ok" /></div>'+msg;
	o('message').innerHTML = msg;
	o('message').style.display = 'inline';
}

function cacher_message(){
	o('message').style.display = 'none';
}

function debug(t){
	if(DEBUG_MODE){
		alert(t);
	}
}

function wait(){
	if(WAIT_SCREEN){
		with(o('div_wait').style){display='inline';}
		waiting = true;
	}
}

function stop_wait(){
	if(WAIT_SCREEN){
		with(o('div_wait').style){display='none';}
		waiting = false;
	}
}

function o(id){
	if(!document.getElementById(id)){
		debug("L'objet "+id+" n'existe pas !");
		return false;
	}
	return document.getElementById(id);
}

function is_waiting(){
	return waiting;
}

function i(data){ // place le contenu dans le cadre principal du framework
	o('main_content').innerHTML = data;
	wait();
}

function sendForm(id){
	var data = '';
	for(var i=0;i<o(id).elements.length;i++){
		if(o(id).elements[i].name != ''){
			data += o(id).elements[i].name+'='+o(id).elements[i].value+'&';
		}
	}
	return data;
}

var currentData;
var ie = ((document.all) ? true : false);
var ns4 = (document.layers)? true:false;
var ns6 = (document.getElementById)? true:false;

function process(action,vars){
	var synchro = !arguments[3] ? true : arguments[3];
	is_started = true;
	wait();
	var msg = arguments[2];
	vars = 'action='+action+'&'+vars;
	var request = !isStarted ? getRequest() : getRequest2();
	try {
	request.open("POST", "index.php" , synchro);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	request.send(vars);
	request.onreadystatechange = function(){
															         if(request.readyState == 4){
															              if(request.status == 200){
															                applyData(action,request.responseText,msg);
															              }
															         }
															    };
	}
	catch(err){
		alert("Erreur , veuillez recommencer.");
	}
}