// VARIABLES
var to


agt = navigator.userAgent.toLowerCase()
isIE = (agt.indexOf('msie') != -1)? 1:0
isW3C = (document.getElementById && !document.all)? 1:0


function dl(q,u) {
	if (isW3C) { 
		document.getElementById(q).contentDocument.location=u
	} else {
		eval("document."+q+".location="+u)
	}
}

// 

// SEND ALERT -------------------------------------------------------------

function writealertbox(a) {
	document.write('<div id="alertbox">')
	document.write('<div id="alertbox_bg"></div>')
	document.write('<div id="alertbox_txt">ALERT</div>')
	document.write('</div>')
	if (a) opacity('alertbox',0,100,600)
}

function sendalert(m) {
	opacity('alertbox',0,100,600)
	document.getElementById('alertbox_txt').innerHTML = m
}

// NAV CHECK -----------------------------------------------------------------

function isIE6() {
	if (document.all && !window.opera) {
		v = navigator.appVersion.split("MSIE");
		v = v[1] ? parseFloat(v[1]) : null;
		if (v<=6) return true; else return false;
	} else { return false }
}

function writeIE6css() {
	if (isIE6()) {
		document.write('<style type="text/css">@import url(http://www.reporterre.com/css/layout-ie.css);</style>')
	}
}


// Intro -------------------------------------------------------------

intro_on="intro_box1"
function intro(q) {
document.getElementById(intro_on).style.display = "none"
l="intro_box"+q
document.getElementById(l).style.display = "block"
opacity(l,0,100,600)
intro_on=l
}

// USER -----------------------------------------------------------------------

userstory_on = 0
function show_userstory () {
	if (userstory_on) {
		document.getElementById('userstory').style.display = "none"
		document.getElementById('closebox').style.display = "none"
		document.getElementById('bas1').src="/i/bas.gif"
		userstory_on = 0
	} else {
		if (userlb_on) {
			document.getElementById('bas2').src="/i/bas.gif"
			document.getElementById('userlb').style.display = "none"
			document.getElementById('closebox').style.display = "none"
			userlb_on = 0
		}
		document.getElementById('bas1').src="/i/haut.gif"
		document.getElementById('userstory').style.display = "block"
		document.getElementById('closebox').style.display = "block"
		userstory_on = 1
	}
}
userlb_on = 0
function show_userlb () {
	if (userlb_on) {
		document.getElementById('userlb').style.display = "none"
		document.getElementById('closebox').style.display = "none"
		document.getElementById('bas2').src="/i/bas.gif"
		userlb_on = 0
	} else {
		if (userstory_on) {
			document.getElementById('bas1').src="/i/bas.gif"
			document.getElementById('userstory').style.display = "none"
			document.getElementById('closebox').style.display = "none"
			userstory_on = 0
		}
		document.getElementById('bas2').src="/i/haut.gif"
		document.getElementById('userlb').style.display = "block"
		document.getElementById('closebox').style.display = "block"
		userlb_on = 1
	}
}

// Gestion Photos -------------------------------------------------------------

function photos(list,photo_layer1,photo_layer2,caption_layer,id) {
	this.photo = list
	this.nphotos = list.length-1
	this.photo_on = 1
	this.caption_layer = caption_layer
	this.photo_layer1 = photo_layer1
	this.photo_layer2 = photo_layer2
	this.id = id
	this.show()
}

photos.prototype.show = function() {
	if (this.caption_layer) document.getElementById(this.caption_layer).innerHTML = caption[this.photo_on]
	u = "f/"+this.photo[this.photo_on]
	document.getElementById(this.photo_layer1).style.backgroundImage='url('+u+')'
}


photos.prototype.nextphoto = function() {
	if (this.photo_on<this.nphotos) {
		this.photo_on++
		this.show()
	}
}

photos.prototype.previousphoto = function() {
	if (this.photo_on>1) {
		this.photo_on--
		this.show()
	}
}


function buildPortfolio () {
	portfolio = new photos(photolist,'portfolio1',0,'portfolio_legende_txt')
	addEventListener(document, window.opera ? 'keypress' : 'keydown', portfolioKeyHandler)
}

portfolioKeyHandler = function(e) {
	if (!e) e = window.event;
	if (!e.target) e.target = e.srcElement; // ie
	if (typeof e.target.form != 'undefined') return true; // form element has focus
//	alert (e.keyCode)
	
	if (e.keyCode==39 || e.keyCode==40) { portfolio.nextphoto() }
	if (e.keyCode==37 || e.keyCode==38) { portfolio.previousphoto() }
	if (e.keyCode==32 || e.keyCode==8) { captionoo ? captio(0) : captio(1) }
	if (e.keyCode==224 || e.keyCode==16) { captio(1) }
}
// BOITE LEGENDES -----------------------------------------------------------------------

captionoo = 0
function captio(q) {
	if (q) {
		if (captionoo==0) {
			caption_object = document.getElementById("portfolio_legende").style
			caption_object.display = 'block'
			opacity('portfolio_legende',0,100,600)
			captionoo = 1;
		}
		if (captionoo==-1) { captionoo=1 }
	} else {
		captionoo=-1
		setTimeout("hidecaption()",100)
	}
}

function hidecaption() {
	if (captionoo==-1) {	
		caption_object = document.getElementById("portfolio_legende").style
		opacity('portfolio_legende',100,0,600)
		captionoo=0
	}
}

// EVENTS -------------------------------------------------------------

addEventListener = function (el, event, func) {
	try {
		el.addEventListener(event, func, false);
	} catch (e) {
		try {
			el.detachEvent('on'+ event, func);
			el.attachEvent('on'+ event, func);
		} catch (e) {
			el['on'+ event] = func;
		}
	} 
},

removeEventListener = function (el, event, func) {
	try {
		el.removeEventListener(event, func, false);
	} catch (e) {
		try {
			el.detachEvent('on'+ event, func);
		} catch (e) {
			el['on'+ event] = null;
		}
	}
}

// GESTION REPORTAGE -------------------------------------------------------------

function reportage(q,n0,n1,n2) {
	this.rep = q
	this.photo_on = 1
	this.sphoto_on = 1
	this.page_on = 1
	this.visu_on = 3  // 1=planche 2=pellicule 3=synops
	this.nsphoto = n0
	this.nphoto = n1
	this.nplanche = n2
}


reportage.prototype.pellicule = function(q) {
	document.getElementById("reportages_fl1").style.display = 'inline'
	document.getElementById("reportages_fl2").style.display = 'inline'
	document.getElementById("reportages_count").style.display = 'inline'
	document.getElementById("reportages_count2").style.display = 'inline'
	if (q) this.photo_on = q
	this.visu_on = 2
	u="pellicule.php?r="+this.rep+"&f="+this.photo_on
	myhtml = loadxhr(u,'reportages_conteneur')
	this.affichepage("reportages_count")
	this.affichepage("reportages_count2")
}

reportage.prototype.showphoto = function(q) {
	document.getElementById("reportages_fl1").style.display = 'inline'
	document.getElementById("reportages_fl2").style.display = 'inline'
	document.getElementById("reportages_count").style.display = 'inline'
	document.getElementById("reportages_count2").style.display = 'inline'
	if (q) this.photo_on = q
	this.visu_on = 2
	u="photo.php?r="+this.rep+"&f="+this.photo_on
	myhtml = loadxhr(u,'reportages_page')
	this.affichepage("reportages_count")
	this.affichepage("reportages_count2")
}

reportage.prototype.showsphoto = function(q) {
	if (q) this.sphoto_on = q
	u="synops.php?r="+this.rep+"&f="+this.sphoto_on
	myhtml = loadxhr(u,'reportages_conteneur')
}

reportage.prototype.planche = function() {
	document.getElementById("reportages_fl1").style.display = 'inline'
	document.getElementById("reportages_fl2").style.display = 'inline'
	document.getElementById("reportages_count").style.display = 'inline'
	document.getElementById("reportages_count2").style.display = 'inline'
	this.visu_on = 1
	u="planche.php?r="+this.rep+"&p="+this.page_on
	myhtml = loadxhr(u,'reportages_conteneur')
	this.affichepage("reportages_count")
	this.affichepage("reportages_count2")
}

reportage.prototype.info = function() {
	document.getElementById("reportages_fl1").style.display = 'none'
	document.getElementById("reportages_fl2").style.display = 'none'
	document.getElementById("reportages_count").style.display = 'none'
	document.getElementById("reportages_count2").style.display = 'none'
	this.sphoto_on = 1
	this.visu_on = 3
	u="synops.php?r="+this.rep
	myhtml = loadxhr(u,'reportages_conteneur')
}

reportage.prototype.next = function() {
	if (this.visu_on==1) {
		if (this.page_on<this.nplanche) this.planche(++this.page_on)
	} else if (this.visu_on==2) {
		if (this.photo_on<this.nphoto) this.showphoto(++this.photo_on)
	} else if (this.visu_on==3) {
		if (this.sphoto_on<this.nsphoto) this.showsphoto(++this.sphoto_on)
	}
}

reportage.prototype.previous = function() {
	if (this.visu_on==1) {
		if (this.page_on>1) this.planche(--this.page_on)
	} else if (this.visu_on==2) {
		if (this.photo_on>1) this.showphoto(--this.photo_on)
	} else if (this.visu_on==3) {
		if (this.sphoto_on>1) this.showsphoto(--this.sphoto_on)
	}
	
}

reportage.prototype.affichepage = function(c) {
	if (this.visu_on==1) {
		t = this.page_on+"/"+this.nplanche
	} else if (this.visu_on==2) {
		t = this.photo_on+"/"+this.nphoto
	}
	document.getElementById(c).innerHTML = t
}
StoryKeyNav = function() {
	addEventListener(document, window.opera ? 'keypress' : 'keydown', storyKeyHandler)
}
storyKeyHandler = function(e) {
	if (!e) e = window.event;
	if (!e.target) e.target = e.srcElement; // ie
	if (typeof e.target.form != 'undefined') return true; // form element has focus
	if (story.visu_on==2 || story.visu_on==3) {
		if (e.keyCode==39) { story.next() }
		if (e.keyCode==37) { story.previous() }
	}
}

// FONCTIONS PS -------------------------------------------------------------

keyword_oo = 0
function keywords() {
	if (keyword_oo) {
		document.getElementById("img_keyword").style.display = 'none'
		keyword_oo = 0
	} else {
		document.getElementById("img_keyword").style.display = 'block'
		keyword_oo = 1
	}
}

search_oo = 0
function searchoption() {
	if (search_oo) {
		document.getElementById("search_2").style.display = 'none'
		document.getElementById("search_1").style.display = 'block'
		search_oo = 0
	} else {
		document.getElementById("search_1").style.display = 'none'
		document.getElementById("search_2").style.display = 'block'
		search_oo = 1
	}
}



// CHARGEMENT DYNAMIQUE -------------------------------------------------------------

function loadxhr(q,conteneur) {
	var xhr; 
	try { 
		xhr = new ActiveXObject('Msxml2.XMLHTTP')
	} catch (e) {
		try {
			xhr = new ActiveXObject('Microsoft.XMLHTTP')
		} catch (e2) {
          try {  
          	xhr = new XMLHttpRequest()
          } catch (e3) {
          	xhr = false
          }
		}
	}
	
	xhr.onreadystatechange = function() { 
		if (xhr.readyState==4) {
			if (xhr.status==200) {
				document.getElementById(conteneur).innerHTML = xhr.responseText
			} else { 
				document.getElementById(conteneur).innerHTML =  "Error code " + xhr.status
			}
		}
	}
	
	xhr.open("GET",q,true); 
	xhr.send(null); 
}


// MENU DEROULANT -------------------------------------------------------------


function menu(q) {
	this.close_height = "6px"
	this.menu_open = 0
	this.object = document.getElementById(q)
	this.firstrun=1
}

menu.prototype.show = function() {
	if (this.firstrun) {
		this.open_height=this.object.scrollHeight+"px"
		this.firstrun=0
	}
	if (!this.menu_open) {
		this.object.style.height = this.open_height
		this.menu_open = 1
	} else {
		this.object.style.height = this.close_height
		this.menu_open = 0
	}	
}

// NAV COLONNE -------------------------------------------------------------

var lastopen = 'colonneb'

function recherche() {
	if (lastopen=='colonneb') lastopen=0
	coloo(lastopen,'recherche',500)
	lastopen = 'recherche'
}

function compte() {
	if (lastopen=='colonneb') lastopen=0
	coloo(lastopen,'compte',500)
	lastopen = 'compte'
}

function contact() {
	if (lastopen=='colonneb') lastopen=0
	coloo(lastopen,'contact',500)
	lastopen = 'contact'
}

function close() {
	coloo(lastopen,0,500)
	lastopen = 'colonneb'
}

function coloo(id1,id2,millisec) { 

    var speed = Math.round(millisec / 100); 
    var timer = 0;
 	if (id1) {
		for(i=100;i>0;i=i-2) { 
			setTimeout("changeOpac(" + i + ",'" + id1 + "')",(timer * speed)); 
			timer++; 
		}
		setTimeout("document.getElementById('"+id1+"').style.display = 'none'",(timer*speed))
	} else {
		document.getElementById('colonne').style.display = 'block'
	}
	if (id2) {
		setTimeout("document.getElementById('"+id2+"').style.display = 'block'",(timer*speed))
		for(j=0;j<=100;j=j+2) { 
			setTimeout("changeOpac(" + j + ",'" + id2 + "')",(timer * speed)); 
			timer++; 
		}
	} else {
		setTimeout("document.getElementById('colonne').style.display = 'none'",(timer * speed))
	}
} 

// OPACITY

function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style;
	object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")";
}

function opacity(id,start,end,millisec) { 
    var speed = Math.round(millisec / 100); 
    var timer = 0;
    if (start>end) {
		for(i=start;i>=end;i=i-2) { 
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
		}
	} else {
		for(j=start;j<=end;j=j+2) { 
			setTimeout("changeOpac(" + j + ",'" + id + "')",(timer * speed)); 
			timer++; 
		}
	}
}

function opacity2(id,start,end,millisec) { 
    var speed = Math.round(millisec / 100); 
    var timer = 0;
		for(i=start;i>=end;i=i-2) { 
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
		}
		for(j=end;j<=start;j=j+2) { 
			setTimeout("changeOpac(" + j + ",'" + id + "')",(timer * speed)); 
			timer++; 
		}
}