

//source http://www.w3schools.com/dom/tryit.asp?filename=try_dom_httprequest_js1
function loadImage(idpage,seq)
{
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{
		//code for Firefox, Opera, IE7, etc.
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		//code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=apply_changes;
		xmlhttp.open("GET","/public/getimage.php?idpage="+idpage+"&seq="+seq,true);
		xmlhttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}
function loadZoom(idpage,src)
{
	hash = src.split('/');
	id = parseInt(hash[hash.length - 1].split('.')[0]);
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{
		//code for Firefox, Opera, IE7, etc.
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		//code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=open_zoom;
		xmlhttp.open("GET","/public/zoom.php?idpage="+idpage+"&id="+id,true);
		xmlhttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}
function loadZoomImage(idpage,seq)
{
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{
		//code for Firefox, Opera, IE7, etc.
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		//code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=apply_zoom_changes;
		xmlhttp.open("GET","/public/getimage.php?idpage="+idpage+"&seq="+seq,true);
		xmlhttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}

function loadNextImage(idpage,seq,zoom)
{
  if(zoom == 0)
  {
	  try
	  {
	    window.clearTimeout(timeoutID);
	    loadImage(idpage,seq);
	  }
	  catch (e)
	  {}
  }
  else
  {
	  loadZoomImage(idpage, seq);
  }
}
function open_zoom()
{
	if (xmlhttp.readyState==4)		// 4 = "loaded"
	{
		if (xmlhttp.status==200)// 200 = "OK"
		{
			var div = document.createElement('div');
			div.id = "background_zoom";
			div.className = "fadezoom";
			x=xmlhttp.responseText;
			x = x.split('::');
			div.innerHTML = x[4];
			document.getElementById('body').appendChild(div);
			var image = document.getElementById('diaporama_image_zoom');
			/*var left = parseFloat(window.innerWidth - 800) / 2 + "px";
			var height = 30 + 52 + x[3];
			var innerHeight = parseFloat((window.innerHeight - height) / 2 );
			if(innerHeight < 0)
			  innerHeight = 0;
			var top620 = innerHeight + parseFloat(x[3] )+ 30 + "px";
			var top = innerHeight + "px";
			document.getElementById('lbCenter').style.left = left;
			document.getElementById('lbCenter').style.top = top;

			document.getElementById('lbBottomContainer').style.top = top620;*/
			var max_width = 0;
			var max_height = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
			  //Non-IE
			  max_width = window.innerWidth;
			  max_height = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			  //IE 6+ in 'standards compliant mode'
			  max_width = document.documentElement.clientWidth;
			  max_height = document.documentElement.clientHeight;
			}
			resizepopup(x[2],x[3],max_width,max_height,"resize");
			//document.getElementById('diaporama_image').get('tween').cancel();
			//loadZoomImage(x[1],x[0]);
			//div.style.display = 'block';
		}
	}
}
function close_zoom()
{
	document.getElementById('body').removeChild(document.getElementById('background_zoom'));
}
function apply_changes()
{
	if (xmlhttp.readyState==4)		// 4 = "loaded"
	{
		if (xmlhttp.status==200)// 200 = "OK"
		{
			x=xmlhttp.responseXML.documentElement.getElementsByTagName('image');
			var image = $('diaporama_image');
			var image_next = document.getElementById('diaporama_image_next');
			var image_container = document.getElementById('diaporama_image_container');
			var titre = document.getElementById('diaporama_titre');
			var precedent = document.getElementById('diaporama_precedent')
			var suivant = document.getElementById('diaporama_suivant');
			var n = document.getElementById('n');
			var max_width = 240;
			var max_height = 240;
			for(i=0;i<x.length;i++)
			{
			    attributes = x[i].attributes;
			    l = attributes.length;
			    for(j=0;j<l;j++)
			    {
			      if(attributes[j].name == 'idpage')
			    	  pos_idpage = j;
			      if(attributes[j].name == 'idimage')
			    	  pos_idimage = j;
			      if(attributes[j].name == 'seq')
			    	  pos_seq = j;
			      if(attributes[j].name == 'type')
			    	  pos_type = j;
			      if(attributes[j].name == 'width')
			    	  pos_width = j;
			      if(attributes[j].name == 'height')
			    	  pos_height = j;
			    }
			}
			var prev_seq = x[0].attributes[pos_seq].value;
			var cur_seq = x[1].attributes[pos_seq].value;
			var next_seq = x[2].attributes[pos_seq].value;
			
			var mytitre = '';
			if(x[0].hasChildNodes())
				mytitre = x[0].firstChild.nodeValue;
			var idpage = x[0].attributes[pos_idpage].value;
			var idimage = x[0].attributes[pos_idimage].value;
			var type = x[0].attributes[pos_type].value;

			var width = x[1].attributes[pos_width].value;
			var height = x[1].attributes[pos_height].value;
			precedent.href="javascript:loadNextImage('"+idpage+"','"+prev_seq+"',0)";
			
			idpage = x[2].attributes[pos_idpage].value;
			idimage = x[2].attributes[pos_idimage].value;
			type = x[2].attributes[pos_type].value;
			suivant.href="javascript:loadNextImage('"+idpage+"','"+next_seq+"',0)";
			image_next.src = "/UserFiles/Media/"+idpage+"/"+idimage+type;
			
			idpage = x[1].attributes[pos_idpage].value;
			idimage = x[1].attributes[pos_idimage].value;
			type = x[1].attributes[pos_type].value;
			
			if(x[1].hasChildNodes())
				mytitre = x[1].firstChild.nodeValue;
			else
				mytitre = '';
			
			var fx = new Fx.Tween(image_container,{duration:2000});
			fx.onComplete = function()
			{
			  image = $('diaporama_image');
			  image.src = "/UserFiles/Media/"+idpage+"/"+idimage+type;
			  n.firstChild.nodeValue = cur_seq;
			  image.addEvent("domready",function(e)
			  {
			    resizepopup(width,height,max_width,max_height,"resize_tiny");			    
			    fxReverse = new Fx.Tween(image_container,{duration:2000});
			    fxReverse.onComplete = function()
			    {
			    	timeoutID = window.setTimeout("loadImage('"+idpage+"','"+next_seq+"')",4000);
			    	
					//precedent.style.display = 'block';
					//suivant.style.display = 'block';
			    }
fxReverse.onStart = function()
{
  titre.firstChild.nodeValue = mytitre;
}
				/*fxReverse.onCancel = function()
				{
				  window.clearTimeout(timeoutID);
				  //precedent.style.display = 'block';
					//suivant.style.display = 'block';
				}*/
			    fxReverse.start('opacity',0,1);
			});
		      } 
		      fx.onStart = function()
		      {
		    	//titre.firstChild.nodeValue = "";
		    	//precedent.style.display = 'none';
			//suivant.style.display = 'none';
		      }
		     /* fx.onCancel = function()
		      {
			window.clearTimeout(timeoutID);
			  //precedent.style.display = 'block';
			  //suivant.style.display = 'block';
		      }*/
		      fx.start('opacity',1,0);
		}
		else
		{
			alert("Problem retrieving data:" + xmlhttp.statusText);
		}
	}
}
function apply_zoom_changes()
{
	if (xmlhttp.readyState==4)		// 4 = "loaded"
	{
		if (xmlhttp.status==200)// 200 = "OK"
		{
			x=xmlhttp.responseXML.documentElement.getElementsByTagName('image');
			var image = document.getElementById('diaporama_image_zoom');
			var image_next = document.getElementById('diaporama_image_next_zoom');
			var titre = document.getElementById('diaporama_titre_zoom');
			var precedent = document.getElementById('diaporama_precedent_zoom');
			var suivant = document.getElementById('diaporama_suivant_zoom');
			var n = document.getElementById('n_zoom');
			for(i=0;i<x.length;i++)
			{
			    attributes = x[i].attributes;
			    l = attributes.length;
			    for(j=0;j<l;j++)
			    {
			      if(attributes[j].name == 'idpage')
			    	  pos_idpage = j;
			      if(attributes[j].name == 'idimage')
			    	  pos_idimage = j;
			      if(attributes[j].name == 'seq')
			    	  pos_seq = j;
			      if(attributes[j].name == 'type')
			    	  pos_type = j;
			      if(attributes[j].name == 'width')
			    	  pos_width = j;
			      if(attributes[j].name == 'height')
			    	  pos_height = j;
			    }
			}
			var prev_seq = x[0].attributes[pos_seq].value;
			var cur_seq = x[1].attributes[pos_seq].value;
			var next_seq = x[2].attributes[pos_seq].value;
			
			var mytitre = '';
			if(x[0].hasChildNodes())
				mytitre = x[0].firstChild.nodeValue;
			var idpage = x[0].attributes[pos_idpage].value;
			var idimage = x[0].attributes[pos_idimage].value;
			var type = x[0].attributes[pos_type].value;

			var width = x[1].attributes[pos_width].value;
			var height = x[1].attributes[pos_height].value;

			precedent.href="javascript:loadNextImage('"+idpage+"','"+prev_seq+"',1)";
			
			idpage = x[2].attributes[pos_idpage].value;
			idimage = x[2].attributes[pos_idimage].value;
			type = x[2].attributes[pos_type].value;
			suivant.href="javascript:loadNextImage('"+idpage+"','"+next_seq+"',1)";
			image_next.src = "/UserFiles/Media/"+idpage+"/"+idimage+type;
			
			idpage = x[1].attributes[pos_idpage].value;
			idimage = x[1].attributes[pos_idimage].value;
			type = x[1].attributes[pos_type].value;
			if(x[1].hasChildNodes())
				mytitre = x[1].firstChild.nodeValue;
			else
				mytitre = '';
			var fx = new Fx.Tween(image,{duration:1000});
			fx.onComplete = function()
			{
			    image.src = "/UserFiles/Media/"+idpage+"/"+idimage+type;
			    n.firstChild.nodeValue = cur_seq;
			    image.addEvent('domready',function()
			     {
			      myWidth = 0;
			      myHeight = 0;
			      if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				myWidth = window.innerWidth;
				myHeight = window.innerHeight;
			      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				//IE 6+ in 'standards compliant mode'
				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
			      }
			      resizepopup(width ,height ,myWidth,myHeight,"resize");
			     });
			    fxReverse = new Fx.Tween(image,{duration:1000});
			    fxReverse.onComplete = function()
			    {
			    	titre.firstChild.nodeValue = mytitre;
			    };
			    fxReverse.onStart = function()
			    {
			    	
			    }
			    fxReverse.start('opacity',0,1);
		    };
		    fx.onStart = function()
		    {
		    	titre.firstChild.nodeValue = "";
		    }
		    fx.start('opacity',1,0);
		    
		    

		}
		else
		{
			alert("Problem retrieving data:" + xmlhttp.statusText);
		}
	}
}
function getRealSize(strSize)
{
	return parseFloat(strSize.substring(0,(strSize.length)-2));
}
function resizepopup(width,height,max_width,max_height,call_function)
{	
	if(!width)
		width = max_width;
	if(!height)
		height = max_height;
	var ratio = width/height;
	var marginTop = 0;
	var marginLeft = 0;
	if(width > max_width)
	{
		width = max_width;
		height = width / ratio;
	}
	if(height > max_height)
	{
		height = max_height;
		width = height * ratio;
	}
	//redimensionne uniquement si le côté le plus long dépasse la taille de la fenetre
	if(width >= height ) //plus large que haute et plus large que la fenetre
	{
	    if(width > max_width)
	    {
	      width = max_width;
	      height = (1/ratio * width);
	    }
	    else
	    {
	      marginLeft = (max_width - width) / 2;
	    }
	    marginTop = (max_height - height) / 2;
	}
	if(width < height)//plus haute que large et plus haute que la fenetre
	{  
	  if(height > max_height)
	  {
	    height = max_height;
	    width = ratio * height;
	  }
	  else
	  {
	    marginTop = (max_height - height) / 2;
	  }
	  marginLeft = (max_width - width) / 2;
	}
	if(marginTop < 0)
	  marginTop = 0;
	if(marginLeft < 0)
	  marginLeft = 0;
	width = parseFloat(width);
	height = parseFloat(height);

	
	  eval(call_function+"(" + width + "," + height + "," + marginTop + "," + marginLeft + ")");
}

function resize(width, height, marginTop, marginLeft)
{
	var lbCenter = document.getElementById('lbCenter');
	var lbImage = document.getElementById('lbImage');
	var lbBottomContainer = document.getElementById('lbBottomContainer');
	var lbBottom = document.getElementById('lbBottom');
	var image = document.getElementById('diaporama_image_zoom');
	var lb_header_center = document.getElementById('lb_header_center');	
	var lbCenterLeftFromValue = 0;
	var lbCenterTopFromValue = 0;
	if(lbCenter.style.left)
	  lbCenterLeftFromValue = getRealSize(lbCenter.style.left);
	if(lbCenter.style.top)
	  lbCenterTopFromValue = getRealSize(lbCenter.style.top);;
	//il faut lancer les animations en meme temps
	var imageWidthFrom = getRealSize(image.style.width);
	var imageWidthTo = width;
	var imageHeightFrom = getRealSize(image.style.height);
	var imageHeightTo = height;
	/*var lbImageWidthFrom = getRealSize(lbImage.style.width);
	var lbImageWidth = width + 20;
	var lbImageHeightFrom = getRealSize(lbImage.style.height);
	var lbImageHeightTo = height + 20;*/
	var lbCenterWidthFrom = getRealSize(lbCenter.style.width);
	var lbCenterWidthTo = (parseFloat(width) + 20);
	var lbCenterHeightFrom = getRealSize(lbCenter.style.height);
	var lbCenterHeightTo = (parseFloat(height) + 30 + 10 + 10 + 40);
	var lbCenterLeftFrom = lbCenterLeftFromValue
	var lbCenterLeftTo = marginLeft - (20/2);
	var lbCenterTopFrom = lbCenterTopFromValue
	var lbCenterTopTo = marginTop - ((30+10+10+40)/2);
	var lbContainerTopFrom = getRealSize(lbBottomContainer.style.top);
	var lbContainerTopTo = height - ((30+10+10+40)/2);;


	var fx = new Fx.Tween(image,{duration:1000});
	  fx.start('width',imageWidthFrom,imageWidthTo);

	fx = new Fx.Tween(image,{duration:1000});
	  fx.start('height',imageHeightFrom,imageHeightTo);

	/*fx = new Fx.Tween(lbImage,{duration:1000});
	  fx.start('width',lbImageWidthFrom,lbImageWidth);

	fx = new Fx.Tween(lbImage,{duration:1000});
	  fx.start('height',lbImageHeightFrom,lbImageHeightTo);*/

	fx = new Fx.Tween(lbCenter,{duration:1000});
	  fx.start('width',lbCenterWidthFrom,lbCenterWidthTo);

	fx = new Fx.Tween(lbCenter,{duration:1000});
	  fx.start('height',lbCenterHeightFrom,lbCenterHeightTo);

	fx = new Fx.Tween(lbCenter,{duration:1000});
	  fx.start('left',lbCenterLeftFrom,lbCenterLeftTo);

	fx = new Fx.Tween(lbCenter,{duration:1000});
	  fx.start('top',lbCenterTopFrom,lbCenterTopTo);
}
function resize_tiny(width,height, top, left)
{
      var image = document.getElementById('diaporama_image');
      image.style.width = width + 'px';
      image.style.height = height + 'px';
      image.style.paddingTop = top + 'px';
      image.style.paddingLeft = left + 'px';
}


