/*to switch based on navigator*/
var nav=(navigator.appName).toLowerCase();

var interval;
var scrollElementID;
var scrollValue;


function repeatScroll(elementID, value, repeatTime){
	scrollElementID=elementID;
	scrollValue=value;
	interval=setInterval("DummyHorizontalScroll()",repeatTime);
}

function stopScroll(){
	if(interval)//test that the variable exists
		clearInterval(interval);
}

function HorizontalScroll(elementID, value) {
	var element=document.getElementById(elementID);
	if (nav =="microsoft internet explorer"){
		element.style.marginLeft = Number(element.style.marginLeft.match(/-?\d+/)[0])-Number(value);
	}else{
		element.style.marginLeft = String(Number(element.style.marginLeft.match(/-?\d+/)[0])-Number(value))+'px';
	}
}

function DummyHorizontalScroll(){
	HorizontalScroll(scrollElementID, scrollValue);
}

/**
*Infobulle script, http://damienalexandre.fr/Info-Bulle-en-Javascript.html
*
*/
function GetId(id){
	return document.getElementById(id);
}
var infoBulle=false; // La variable i nous dit si la bulle est visible ou non
function move(e) {
  if(infoBulle) {  // Si la bulle est visible, on calcule en temps reel sa position ideale
    if (navigator.appName!="Microsoft Internet Explorer") { // Si on est pas sous IE
	GetId("curseur").style.left=e.pageX + 5+"px";
	GetId("curseur").style.top=e.pageY + 10+"px";
	}
	else { // Modif proposé par TeDeum, merci à  lui
		if(document.documentElement.clientWidth>0) {
		GetId("curseur").style.left=20+event.x+document.documentElement.scrollLeft+"px";
		GetId("curseur").style.top=10+event.y+document.documentElement.scrollTop+"px";
		} else {
			GetId("curseur").style.left=20+event.x+document.body.scrollLeft+"px";
			GetId("curseur").style.top=10+event.y+document.body.scrollTop+"px";
		}
  	}
  }
}
function montre(text) {
  if(infoBulle==false) {
	  GetId("curseur").style.visibility="visible"; // Si il est cache (la verif n'est qu'une securité) on le rend visible.
	GetId("curseur").innerHTML = text; // on copie notre texte dans l'élément html
  	infoBulle=true;
  }
}
function cache() {
	if(infoBulle==true) {
		GetId("curseur").style.visibility="hidden"; // Si la bulle est visible on la cache
	infoBulle=false;
	}
}
document.onmousemove=move; // dès que la souris bouge, on appelle la fonction move pour mettre à jour la position de la bulle.
/**
* End of infobulle script
**/


/**
* Adds a photo to the plant currently being created
* plant_id plant being created
* photo_id photo to add the plant being created
**/
function addPhotoToCurrentPlant(plant_id,photo_id){
	new Ajax.Request('/plants/addPhotoToPlant/'+plant_id+'/'+photo_id, {
											method: 'post',
											onComplete:function(transport) {
												new Ajax.Updater('current_selected_photos','/plants/displayCurrentSelectedPhotos/'+plant_id, {
														asynchronous:true, 
														evalScripts:true,
														requestHeaders:['X-Update', 'current_selected_photos']
														});
												}
											});
}

/**
* Removes a photo from the plant currently being created
* plant_id plant being create
* photo_id photo to add the plant being created
**/
function removePhotoFromCurrentPlant(plant_id,photo_id){
	new Ajax.Request('/plants/removePhotoFromPlant/'+plant_id+'/'+photo_id, {
											method: 'post',
											onComplete:function(transport) {
												new Ajax.Updater('current_selected_photos','/plants/displayCurrentSelectedPhotos/'+plant_id, {
														asynchronous:true, 
														evalScripts:true,
														requestHeaders:['X-Update', 'current_selected_photos']
														});
												}
											});
}


/**
* Adds a photo to the plantyforum message currently being created
* attachement_id that will be used to link to the message
* photo_id photo to add the message being created
**/
function addPhotoToCurrentMessage(creation_date, objectTypeId, photo_id, objectId) {
	new Ajax.Request('/attachments/addAttachment/'+photo_id, {
											method: 'post',
											parameters : {'creation_date' : creation_date, 'objectTypeId' : objectTypeId},
											onComplete:function(transport) {
												new Ajax.Updater('current_selected_photos','/attachments/displayCurrentSelectedPhotos/', {
														asynchronous:true, 
														evalScripts:true,
														parameters : {'creation_date' : creation_date, 'objectTypeId' : objectTypeId , 'objectIdEditionMode' : objectId },
														requestHeaders:['X-Update', 'current_selected_photos']
														});
												}
											});
}

/**
* Adds a photo to the plantyforum message currently being created
* This function is used by the ajax upload form
* photo_id photo to add the message being created
**/
function addUploadedPhotoToSelection(creation_date, objectTypeId, photo_id, objectId) {
	addPhotoToCurrentMessage(creation_date, objectTypeId, photo_id, objectId);
	//update current selected photo
	new Ajax.Updater('current_selected_photo','/attachments/displayCurrentSelectedPhotos/', {
			asynchronous:true, 
			evalScripts:true,
			requestHeaders:['X-Update', 'current_selected_photos']
			});
}

/**
* Removes a photo to the plantyforum message currently being created
* attachement_id that will be used to link to the message
* photo_id photo to remove from the message being created
**/
function removeAttachement(creation_date, objectTypeId, photo_id, objectId) {
		new Ajax.Request('/attachments/removeAttachment/'+photo_id, {
											method: 'post',
											parameters : {'creation_date' : creation_date, 'objectTypeId' : objectTypeId, 'objectIdEditionMode' : objectId},
											onComplete:function(transport) {
												new Ajax.Updater('current_selected_photos','/attachments/displayCurrentSelectedPhotos/', {
														asynchronous:true, 
														evalScripts:true,
														parameters : {'creation_date' : creation_date, 'objectTypeId' : objectTypeId, 'objectIdEditionMode' : objectId},
														requestHeaders:['X-Update', 'current_selected_photos']
														});
												}
											});
}
