var images_zoom = new Array();
var img_cpt=0;;

function ajout_evenement(elem, type_even, fct){
	if(elem.addEventListener){ elem.addEventListener(type_even, fct, false); return true;}
	else if(elem.attachEvent){return elem.attachEvent('on'+type_even,fct);}
	else{el['on'+type_elem]=fct;}
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getWinXY() {
  var 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;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

function getdocXY()
{
	var x=0, y=0;
	x = document.getElementsByTagName('body')[0].offsetWidth;
	y = document.getElementById('cadre1').offsetHeight+20;
	if(y < getWinXY()[1] )
		y = getWinXY()[1];
	return [x,y];
}

function cacher_image(e)
{
	document.getElementById('photo_zoom_box').style.display='none';
}

function afficher_image(e)
{
	var docL;
	var docH;
	var decalage_vert;
	var img;
	
	var box = document.getElementById('photo_zoom_box');
	if(e && e.preventDefault) e.preventDefault();
	else if(window.event && window.event.returnValue) window.event.returnValue = false;
	
	if(window.event) lnk = e.srcElement;
	else if(e) lnk = e.target;
	if(lnk != null ){while( lnk && (lnk.nodeType == 1) && (lnk.tagName.toLowerCase() != 'a')) lnk = lnk.parentNode;}
	
	box.innerHTML = '<div><img src="'+lnk.href+'" alt="Cliquez pour fermer"/></div>';
	box.style.position = 'absolute';
	box.style.left = '0px';
	box.style.top = '0px';
	box.style.width = getdocXY()[0]+'px';
	box.style.height = getdocXY()[1]+'px';
	box.style.background = 'url(../../scripts/bg.png)';
	box.style.cursor='pointer';
	
	img = new Image();
	img.src = lnk.href;
	decalage_vert = getScrollXY()[1] + (getWinXY()[1]-(img.height+30))/2;
	if(decalage_vert<0)decalage_vert=5;
	
	var box2 = box.getElementsByTagName('div')[0];
	box2.style.background = '#fff';
	box2.style.width = img.width+20+'px';
	box2.style.margin = decalage_vert+'px auto 0 auto';
	box2.style.border = '1px solid #000';
	box2.style.overflow = 'hidden';
	
	var box3 = box2.getElementsByTagName('img')[0];
	box3.style.margin='11px auto';
	box.style.display = 'block';
	
	ajout_evenement(box,'click',cacher_image);
	
	return false;
}

function init_photo_zoom()
{
	var img_cpt=0;
	for(i=0; (a = document.getElementsByTagName("a")[i]); i++){
		if(a.className == "imglink" ){
			images_zoom[img_cpt] = new Image();
			images_zoom[img_cpt].src = a.href;
			ajout_evenement(a,'click',afficher_image);
			img_cpt++;
		}
	}
	var bg = new Image();
	bg.src = '../../scripts/bg.png';
}

document.write('<div id="photo_zoom_box"></div>');
init_photo_zoom();
