// JavaScript Document

function ajax(url,data,div)
{

if(div == null){
	div = "page";
}
new_div = document.getElementById(div);
	
var xhr_object = null;

if(window.XMLHttpRequest) // Firefox
   xhr_object = new XMLHttpRequest();
else if(window.ActiveXObject) // Internet Explorer
   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
else { // XMLHttpRequest non supporté par le navigateur
   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
   return;
}

var method   = "POST";
var filename = url;
var data     = data;

xhr_object.open(method, filename, true);

xhr_object.onreadystatechange = function() {
	if(xhr_object.readyState == 4) {
		//new_div.removeChild(document.getElementById(div + '_w'))
		var xmlDoc = xhr_object.responseXML;
		var action = xmlDoc.getElementsByTagName('action');
		for (var iC=0 ; iC < action.length ; iC++){
			var type = action[iC].getElementsByTagName('type')[0].firstChild.nodeValue;
			
			if(type == "html" || type == "add_html"){
				var champs = action[iC].getElementsByTagName('champs')[0].firstChild.nodeValue;
			}
			
			var value = action[iC].getElementsByTagName('value')[0].firstChild.nodeValue;
			if(type == "html"){
				document.getElementById(champs).innerHTML = value;
			}
			if(type == "add_html"){
				text = document.getElementById(champs).innerHTML
				document.getElementById(champs).innerHTML = text + value;
			}
			else if(type == "js"){
				eval(value);
			}
		}
	}
	else if(xhr_object.readyState == 1){
		//var wait=document.createElement("div");
		//new_div.appendChild(wait);
		//wait.id = div + '_w';
		//wait.className = "wait";
	}
}

xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
xhr_object.send(data); 
}


// ---------------------------------------------------------------------------------------
//									EXTRA
// ---------------------------------------------------------------------------------------


function bn_date_jygo(id,action,div){
	ajax("/ajax/dates/jygo.php","id="+id+"&action="+action+"&div="+div,"");
}
function bn_lieu_favoris(id,action,div){
	ajax("/ajax/lieux/favoris.php","id="+id+"&action="+action+"&div="+div,"");
}
function bn_user_amis(id,action,div){
	ajax("/ajax/users/add_amis.php","id="+id+"&action="+action+"&div="+div,"");
}


function infobox(text){
	$('infobox_text').innerHTML = text;
	$('infobox').style.display = "block";
}
// ---------------------------------------------------------------------------------------
//									GESTION DE L'ALBUM PHOTO
// ---------------------------------------------------------------------------------------


var tag_T = 0;
var tag_L = 0;

function unregister(){
  document.onmousemove='';
  //document.getElementById("info_tag").innerHTML='...';
 return false;
}

 function register_position(){
    if(document.images['photo_a_tag'].complete){
	 mouse_x_tag = 0;
	 mouse_y_tag = 0;
	 document.onmousemove = position_tag;
	}
  }
function position_tag(evt_tag){
	
	var ns6=document.getElementById && !document.all
	var mouse_x_tag=(ns6)?evt_tag.pageX : event.clientX+ietruebody().scrollLeft;
	var mouse_y_tag=(ns6)?evt_tag.pageY : event.clientY+ietruebody().scrollTop;

  //if(!evt_tag) evt_tag = window.event;
	//mouse_x_tag = evt_tag.clientX - evt_tag.pageX;
	//mouse_y_tag = evt_tag.clientY - evt_tag.pageY;
	
	photo =document.getElementById('photo_a_tag');

 	oLeft_tag = photo.offsetLeft
	oTop_tag = photo.offsetTop
	while (photo = photo.offsetParent) {
		oLeft_tag += photo.offsetLeft
		oTop_tag += photo.offsetTop
	}
	
	photo =document.getElementById('photo_a_tag');

	 oHeight_tag = photo.offsetHeight;
	 oWidth_tag = photo.offsetWidth;

	tag_T = Math.round(((mouse_y_tag-oTop_tag)*100)/oHeight_tag);
	tag_L = Math.round(((mouse_x_tag-oLeft_tag)*100)/oWidth_tag);

  	//document.getElementById("info_tag").innerHTML = tag_T + '% x ' + tag_L + '%';
} 


function aff_photo_date(photo){
	//hs.marginLeft = 230;
	//hs.htmlExpand(document.getElementById('bar_haut'), { contentId: 'galerie_photo', align: 'center'} );
	ajax('/ajax/reports/miniature.php','photo='+photo,'photo_miniature');
}
function aff_galerie_date(date){
	//hs.marginLeft = 230;
	//hs.htmlExpand(document.getElementById('bar_haut'), { contentId: 'galerie_photo', align: 'center'} );
	ajax('/ajax/reports/miniature.php','id='+date,'photo_miniature');
}

// --------------------------------------------------------------------------------------------------------------
//                                   VIEW_PHOTO
// --------------------------------------------------------------------------------------------------------------
	
function view_photo(photo,tag,com,auto,secrettag)
{
	
if((tag != "0_0") && (tag != "") && (tag != "undefined") && (tag != "-1_-1")){
	if(secrettag >= "0"){
		if(confirm('Es-tu sur de vouloir ajouter un [Secret Tag] sur cette photo ?\n\nUn [Secret Tag] permet d\'indiquer a une personne que tu es fan d\'elle sans dire qui tu es.') == false){
			return false;	
		}
	}
	else{
		if(confirm('Es-tu sur de vouloir mettre ton tag sur la photo ?') == false){
			return false;	
		}
	}
}

if((com != "") && (com != "undefined") && (com != null)){
	com = "&com=" + com;	
}
else{
	com = ""	
}

if(auto == "auto")
	auto = "&auto=1";
else
	auto = "";

if(secrettag >= "0")
	secrettag = "&secrettag=" + secrettag;
else
	secrettag = "";


var filename = "/ajax/reports/view_photo.php";
var data = "photo=" + photo + "&tag=" + tag + com + auto + secrettag;

ajax(filename,data,'view_photo');

}

// --------------------------------------------------------------------------------------------------------------
//                                   VIEW_PHOTO_user
// --------------------------------------------------------------------------------------------------------------
	
function view_photo_user(photo,tag,com,auto)
{
	
if((tag != "0_0") && (tag != "") && (tag != "undefined") && (tag != "-1_-1")){
	if(confirm('Es-tu sur de vouloir mettre ton tag sur la photo ?') == false){
		return false;	
	}
}

if((com != "") && (com != "undefined") && (com != null)){
	com = "&com=" + com;	
}
else{
	com = ""	
}

if(auto == "auto")
	auto = "&auto=1";
else
	auto = "";

var filename = "/ajax/users/album/view_photo.php";
var data = "photo=" + photo + "&tag=" + tag + com + auto;

ajax(filename,data,'view_photo');

}


// --------------------------------------------------------------------------------------------------------------
//                                   VIEW_PHOTO_TAG_CLUBBING
// --------------------------------------------------------------------------------------------------------------
	
function view_photo_tag_club(photo,user,tag,com,auto)
{
	
if((tag != "0_0") && (tag != "") && (tag != "undefined") && (tag != "-1_-1")){
	if(confirm('Es-tu sur de vouloir mettre ton tag sur la photo ?') == false){
		return false;	
	}
}

if((com != "") && (com != "undefined") && (com != null)){
	com = "&com=" + com;	
}
else{
	com = ""	
}

if(auto == "auto")
	auto = "&auto=1";
else
	auto = "";

var filename = "/ajax/users/tag_club/view_photo.php";
var data = "photo=" + photo + "&user=" + user + "&tag=" + tag + com + auto;

ajax(filename,data,'view_photo');

}

// --------------------------------------------------------------------------------------------------------------
//                                   BB CODE
// --------------------------------------------------------------------------------------------------------------




	function insert_text(form, id, open, close)
	{
		msgfield = document.forms[form].elements[id];

		// IE support
		if (document.selection && document.selection.createRange)
		{
			msgfield.focus();
			sel = document.selection.createRange();
			sel.text = open + sel.text + close;
			msgfield.focus();
		}

		// Moz support
		else if (msgfield.selectionStart || msgfield.selectionStart == '0')
		{
			var startPos = msgfield.selectionStart;
			var endPos = msgfield.selectionEnd;

			msgfield.value = msgfield.value.substring(0, startPos) + open + msgfield.value.substring(startPos, endPos) + close + msgfield.value.substring(endPos, msgfield.value.length);
			msgfield.selectionStart = msgfield.selectionEnd = endPos + open.length + close.length;
			msgfield.focus();
		}

		// Fallback support for other browsers
		else
		{
			msgfield.value += open + close;
			msgfield.focus();
		}

		return;
	}