function isBrowserIsGoingToBeClosed(e)
{
	var posy = 0;
	var posx = 0;
	if (typeof(posy1) == "undefined" )
	{
		posy1 = 0;
		posy2 = 0;
		posy3 = 0;
	}
	
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

	// reajuste la position en fonction du scroll de la fenetre:
	posx -= document.documentElement.scrollLeft;
	posy -= document.documentElement.scrollTop;
	browserWindowWidth = top.window.document.body.offsetWidth;

	// cursor touche la zone de fermeture
	if(posy <= 40 && posx >= (browserWindowWidth/1.3))
	{
		posy3 = posy2;
		posy2 = posy1;
		posy1 = posy;
		// test si le cursor est en train de monter 
		if (posy1 < posy2 && posy1 < posy3)
		{
			show_lightbox();
			//window.location.href ='http://www.extrait-gratiz.com/test/stop.php';
		}
	} else {
		posy1 = 0;
		posy2 = 0;
		posy3 = 0;
	}
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function center_lightbox()
{
	var overlay = document.getElementById('overlay');
	var lightbox = document.getElementById('lightbox');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	lightbox.style.top = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - lightbox.style.height) / 2) + 'px';
	lightbox.style.left = ((arrayPageSize[0] - 20 - lightbox.style.width) / 2) + 'px';
	overlay.style.height = arrayPageSize[1] + 'px';
}

function show_lightbox()
{
	var overlay = document.getElementById('overlay');
	var lightbox = document.getElementById('lightbox');

	center_lightbox();
	lightbox.style.display = 'block';
	overlay.style.display = 'block';
	overlay.onclick = function () {hide_lightbox(); return false;}
}

function hide_lightbox()
{
	document.getElementById('lightbox').style.display = 'none';
	document.getElementById('overlay').style.display = 'none';
}

window.onresize = function() {
	center_lightbox();
}

window.onscroll = function() {
	center_lightbox();
}

function init()
{
	// scan la position du curseur	
	if (window.Event){
		document.captureEvents(Event.MOUSEMOVE);
	}
	document.onmousemove = isBrowserIsGoingToBeClosed;

	var num_video = 1;
	var DivListe = document.getElementsByTagName('div').length;
	for (a=0; a<DivListe; a++)
	{
		// ==========================================================================
		// 		MODIFIER TOUS LES FAKES THUMBS
		// ==========================================================================

			if (document.getElementsByTagName('div').item(a).className == 'fake_thumbs')
			{
				var fake_thumbs = document.getElementsByTagName('div').item(a);
				var nb_thumb = fake_thumbs.getElementsByTagName('div').length;

				for (i=0; i<nb_thumb; i++)
				{
					if (fake_thumbs.getElementsByTagName('div').item(i).className == 'thumb')
					{
						var thumb = fake_thumbs.getElementsByTagName('div').item(i);
						
						// gestion de l'url du trade ou de la thumb
						var url_thumb = thumb.firstChild.getAttribute('src');
						var id_thumb = thumb.firstChild.getAttribute('id');

						if(i==0 && thumb.firstChild.getAttribute('longdesc'))
						{
							var url_trade = thumb.firstChild.getAttribute('longdesc');
						} else {
							var url_trade = '../goto.php?tracking=' + id_thumb;
						}
	
						// creation de la balise du lien
						var newThumb = document.createElement('a');
						newThumb.setAttribute('href', url_trade);
						newThumb.setAttribute('target', '_blank');
												
						// ajout de la thumb
						var img = document.createElement('img');
						img.setAttribute('src', url_thumb);
						newThumb.appendChild(img);
						
						// ajout un span  -> a cause du bug ie6 sur les "a:hover img" qui ne marche pas directement... pfff
						var spanPlay = document.createElement('span');
						spanPlay.setAttribute('class', 'play');
						spanPlay.setAttribute('className', 'play');
						newThumb.appendChild(spanPlay);
						
			            // ajout texte sous la thumb
			            var rand_duree = Math.floor((30-9)*Math.random()) + 10;
			            var rand_taille = rand_duree * 11;
			
			            //var linkText = document.createTextNode('Video gratuite n°' + num_video);
			            var linkText = document.createTextNode('Taille : ' + rand_taille + ' Mo - Durée : ' + rand_duree + ' min');
			            newThumb.appendChild(linkText);
						
						// ecrase la thumb par le lien
						thumb.replaceChild(newThumb, thumb.firstChild);

						// gestion du survole
						newThumb.onmouseover = function() {
							minute = this.lastChild.nodeValue;
							this.lastChild.nodeValue = 'Regardez la maintenant !!';
						}

						newThumb.onmouseout = function()	{
							this.lastChild.nodeValue = minute;
						}

					}
					num_video++;
				}
			}
	}

	
	// gestion des sites amis
	var UlListe = document.getElementById("trade_site");
	if(UlListe)
	{
		var sites = UlListe.getElementsByTagName('li');
		
		//alert(sites.length);
		for (i=0; i<sites.length; i++)
		{
				var siteName = sites[i].firstChild.firstChild.nodeValue;
				//alert(siteName);
	
				// creation de la balise du lien
				newLink = document.createElement('a');
				//newLink.setAttribute('href', 'goto.php?tracking=' + noaccent(siteName.replace(/ /g, '-').toLowerCase()));
				newLink.setAttribute('href', '../goto.php?tracking=' + url_rewrite(siteName));
				newLink.setAttribute('target', '_blank');
				newLink.appendChild(document.createTextNode(siteName));
				
				// ecrase la thumb par le lien
				sites[i].replaceChild(newLink, sites[i].firstChild);
		}
	}
}

/*
window.onload = function(){
	fake_thumb();
}
*/

(function(i) {
  var u = navigator.userAgent.toLowerCase();
  var ie = /*@cc_on!@*/false;
  if (/webkit/.test(u)) {
    // safari
    timeout = setTimeout(function(){
			if ( document.readyState == "loaded" || 
				document.readyState == "complete" ) {
				i();
			} else {
			  setTimeout(arguments.callee,10);
			}
		}, 10); 
  } else if ((/mozilla/.test(u) && !/(compatible)/.test(u)) ||
             (/opera/.test(u))) {
    // opera/moz
    document.addEventListener("DOMContentLoaded",i,false);
  } else if (ie) {
    // IE
    (function (){ 
      var tempNode = document.createElement('document:ready'); 
      try {
        tempNode.doScroll('left'); 
        i(); 
        tempNode = null; 
      } catch(e) { 
        setTimeout(arguments.callee, 0); 
      } 
    })();
  } else {
    window.onload = i;
  }
})(init);

function url_rewrite(chaine) {
	temp = chaine.replace(/[àâä]/gi,"a")
	temp = temp.replace(/[éèêë]/gi,"e")
	temp = temp.replace(/[îï]/gi,"i")
	temp = temp.replace(/[ôö]/gi,"o")
	temp = temp.replace(/[ùûü]/gi,"u")
	temp = temp.replace(/(?:^\s+|\s+$)/g, "");
	temp = temp.replace(/ /g, '-')

	return temp.toLowerCase();
	
	// .toLowerCase()
	//return temp
}
