function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}

function ajaxLoader() {
	this.deb = 0;
	this.nb = 3;
	this.total = 0;
	this.faderActive = new Array(false,false,false);
	this.linkToDisplay = "";
}

ajaxLoader.prototype.setTotalMedias = function(total) {
	this.total = total;
}

ajaxLoader.prototype.hasAnActiveFader = function() {
	return this.faderActive[0] || this.faderActive[1] || this.faderActive[2];
}

ajaxLoader.prototype.previousMedias = function() {
	this.deb -= 3;
	this.changeMedias();
}

ajaxLoader.prototype.nextMedias = function() {
	this.deb += 3;
	this.changeMedias();
}

ajaxLoader.prototype.changeMedias = function() {
	for(var i=0; i<3; i++) {
		$("#miniature" + i).fadeTo("slow",0,function() {
			var id = (this.id).split("miniature");
			
			if(id[1] == 2) {
				theAjaxLoader.callAjaxDiapos();
			}
		});
	}
}

ajaxLoader.prototype.callAjaxDiapos = function() { 
	var httpObject = getXMLHTTP();
	
	if(httpObject)
	{
    //alert("include/ajaxModules/getDiapos.php?deb=" + this.deb + "&nb=" + this.nb);
		httpObject.open("POST","include/ajaxModules/getDiapos.php?deb=" + this.deb + "&nb=" + this.nb, true);	
	}

	httpObject.onreadystatechange = function()
	{
		/*  When call is finished */
		if (httpObject.readyState==4 && httpObject.responseText)
		{	
			for(var i=0; i<3; i++) {
				$("#miniature" + i).fadeTo("slow",1);
			}
			parent.theAjaxLoader.generateJson(httpObject);		
		}
	};
	
	httpObject.send(null)
}

ajaxLoader.prototype.displayPicture = function(url,titre) {
  var HTML = "<img id='random_image' src='" + url + "' ";
  HTML += " alt='"+titre+"'>";
	if(titre != '') {    
    HTML += "<div style='color:grey;font-style:italic'>"+titre+"</div>";
  }    
  document.getElementById("td_media").innerHTML = HTML;
}

ajaxLoader.prototype.displayVideo = function() {
	document.getElementById("td_media").innerHTML = "<object type='application/x-shockwave-flash' data='flash/player_flv_multi.swf' width='357' height='185'><param name='movie' value='flash/player_flv_multi.swf' /><param name='wmode' value='transparent' /><param name='FlashVars' value='flv=../" + this.linkToDisplay + "&config=flash/config.txt' /></object>";
}

ajaxLoader.prototype.displayYoutube = function() {
	var reg = new RegExp("(width=[0-9]*)", "g");
	this.linkToDisplay = this.linkToDisplay.replace(reg,"width=357");
	var reg = new RegExp("(height=[0-9]*)", "g");
	this.linkToDisplay = this.linkToDisplay.replace(reg,"height=185");
	document.getElementById("td_media").innerHTML = this.linkToDisplay;
}

ajaxLoader.prototype.generateJson = function(httpObject) { 
	var toDisplay = "";
	var JSON = eval('(' + httpObject.responseText + ')'); 

	var cpt = 0;
	
	for(var aMedia in JSON.medias)
	{
		switch(JSON.medias[aMedia].Type) {
			case "Photo":
				if ( JSON.medias[aMedia].Miniature == "")
					 JSON.medias[aMedia].Miniature = JSON.medias[aMedia].Url; //;
				toDisplay = "<img class='miniature_normal' onmouseover='this.className=\"miniature_hover\";' onmouseout='this.className=\"miniature_normal\";' onclick='javascript:theAjaxLoader.displayPicture(\"" + JSON.medias[aMedia].Url + "\",\""+JSON.medias[aMedia].Titre+"\");' src='" + JSON.medias[aMedia].Miniature + "' alt='' width='98' height='74'/>";
				break;
			case "Video":
				this.linkToDisplay = JSON.medias[aMedia].Url;
				toDisplay = "<div class='miniature_normal' onmouseover='this.className=\"miniature_hover\";' onmouseout='this.className=\"miniature_normal\";' onclick='javascript:theAjaxLoader.displayVideo();' style='height:61px;cursor:pointer;background:url(\"" + JSON.medias[aMedia].Miniature + "\") no-repeat'><img style='' src='images/logo_camera.png' alt='' /></div>";
				break;
			case "Video Youtube / Dailymotion":
				this.linkToDisplay = JSON.medias[aMedia].Url;
				toDisplay = "<div class='miniature_normal' onmouseover='this.className=\"miniature_hover\";' onmouseout='this.className=\"miniature_normal\";' onclick='javascript:theAjaxLoader.displayYoutube();' style='height:61px;cursor:pointer;background:url(\"" + JSON.medias[aMedia].Miniature + "\") no-repeat'><img style='' src='images/logo_youtube.png' alt='' /></div>";
				break;
		}
		document.getElementById("miniature" + aMedia).innerHTML = toDisplay;
		
		if(cpt > 0 && cpt%3 == 0) {
			cpt=0;
		}
		else {
			cpt++;
		}
	}
	
	for(var i=2; i>=cpt; i--) {
		document.getElementById("miniature" + i).innerHTML = "";
	}

	if(JSON.previousLink) {
		document.getElementById("diapo_prev").innerHTML = "<img style='cursor:pointer' alt='' src='images/arrow_left.png' onclick='javascript:theAjaxLoader.previousMedias()' />";
	}
	else {
		document.getElementById("diapo_prev").innerHTML = "";
	}
	
	if(JSON.nextLink) {
		document.getElementById("diapo_next").innerHTML = "<img style='cursor:pointer' alt='' src='images/arrow_right.png' onclick='javascript:theAjaxLoader.nextMedias()' />";
	}
	else {
		document.getElementById("diapo_next").innerHTML = "";
	}
}

function trim(myString)
{
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}

function autoClick(element)
{
	try
	{//pour ie
		document.getElementById(element).click();
	}catch(e)
	{//pour ff
		var evt = document.createEvent("MouseEvents"); // créer un évennement souris
		evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);  // intiailser l'évennement déja crée par un click
		var cb = document.getElementById(element); // pointe sur l'élement
		cb.dispatchEvent(evt);  // envoyer l'évennement vers l'élement
	}
}
