function addLoadEvent(func) {	
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
    	window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function answer(obj) {
	if (obj.value=="oui") $('ref').style.display = 'block';
	else $('ref').style.display = 'none';
}

function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|s)" + className + "(s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i ];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function getPage(url) {
	var current = url.href;
	var indiceDepart = current.lastIndexOf('/')+1;
	var indiceArrivee = current.length;
	return current.substring(indiceDepart, indiceArrivee);
}


var old_content;
var first = true;


function setOldContent() {
	$('content').innerHTML = old_content;
}

var goIn = false;

function overview_top(ariane1, ariane2) {
	//$('content').innerHTML = '<img style="margin-top:50px;" src="/images/big_loading.gif" alt="" />';
	
	if (first) { 
		old_content = $('content').innerHTML;
		first = false;
	//	goIn = true;
	}

	this.xhttpd = new Ajax();
	this.xhttpd.onsuccess = function(r) {
		//if (r.status==404) $('overview').innerHTML = '<div id="cimage" style="background:#ededed; "></div>';
		//else 
		$('content').innerHTML = r.responseText;
	};	
	this.xhttpd.URLString = 'ariane1='+escape(ariane1)+'&ariane2='+escape(ariane2);
	this.xhttpd.request('/js/ajax/overview.php', {method: 'POST'});
}

var toverview;
var overview_rep;
var overview_obj;
var reel_overview_obj;

function overview(rep, obj) {
	if (obj.className!='selected') {
		obj.className = 'oselected';
		overview_rep = rep;
		overview_obj = obj;
		clearTimeout(toverview);
		toverview = setTimeout("overviewReel();",200);
	}
}

function out_overview(obj) {
	if (obj.className!='selected') {
		obj.className = 'nselected';
		clearTimeout(toverview);
	}
}
//var precedentClick = null;
function overviewReel(rep, obj) {
	
	rep = overview_rep;
	obj = overview_obj;

	var selection = getElementsByClassName('selected','a');
	selection[0].className = '';
	
	var precPage = getPage(selection[0]);
	obj.className = 'selected';

	var currentPage = getPage(obj);

	var page = getPage(document.location);

	if (page!=currentPage) {
		if ($('view')) $('view').style.display = 'none';
		$('overview').style.display = 'block';
	} else {
		if ($('view')) $('view').style.display = 'block';
		$('overview').style.display = 'none';
	}

	if (currentPage!=precPage) {
		$('overview').innerHTML = '<div id="cimage" style="background:#ededed"><img style="margin-top:50px;" src="/images/big_loading.gif" alt="" /></div><div id="ccontent"></div><div id="ccontextual"></div>';
		this.xhttpd = new Ajax();
		this.xhttpd.onsuccess = function(r) {
			if (r.status==404) $('overview').innerHTML = '<div id="cimage" style="background:#ededed; "></div>';
			else {
				$('overview').innerHTML = r.responseText;
				/*
				obj.onclick = function() { };
				if (precedentClick!=null) precedentClick.onclick = function() { return false; }
				precedentClick = obj;
				*/
			}
		};	
		this.xhttpd.URLString = 'header=send';
		this.xhttpd.request('/'+rep+'/overview_'+currentPage, {method: 'POST'});
	}
}

function myAlert(type, title, content) {
	switch (type) {
		case 'error':
			type = 'erreur';
			break;
		case 'alert':
			type = 'alert';
			break;
		case 'actu':
			type = 'actu';
			break;
		default:
			type = 'erreur';
			break;
	}
	smailAlert.alert(content,title, type);
	/*
	window.addEvent('domready', function() {
		var Sexy = new SexyAlertBox({
		  OverlayStyles:{
			'background-color': 'black',
			'opacity': 0.4
		  }
		});
		eval("Sexy."+type+"('<h1>'+title+'</h1><br />'+content);");
	});
	*/

}

function contact() {
	$('fcontact').send.disabled = true;
	this.xhttpd = new Ajax();
	this.xhttpd.onsuccess = function(r) {
		var xml = r.responseXML.documentElement;
		if ((xml) && (xml.nodeName=='error')) myAlert('error','Veuillez saisir correctement le formulaire',xml.firstChild.nodeValue);
		else {
			myAlert('info','Message envoyé','Votre message a été envoyé avec succès');
			$('fcontact').reset();
			$('fcontact').send.disabled = false;
		}
	};
	this.xhttpd.URLString = getDataFromForm($('fcontact'));
	this.xhttpd.request('/js/ajax/contact.php', {method: 'POST'});	
	//alert('ok');
}

function displayNew(id) {
	var xmlhttp = new Ajax();
	xmlhttp.onsuccess = function(r) {
		myAlert('actu',r.responseXML.getElementsByTagName("title")[0].firstChild.nodeValue,r.responseXML.getElementsByTagName("content")[0].firstChild.nodeValue);
	};
	xmlhttp.URLString = 'id='+id;
	xmlhttp.request("/js/ajax/getnews.php", {method: 'POST'});
}

function closeSpeakBox() {
	displayn = false;
	$('content').removeChild($('speakBox'));
}



function initAnim() {
	if ($('pbox')) {
		var img1 = new Image();
		var img2 = new Image();
		var img3 = new Image();
		var img4 = new Image();
		var img5 = new Image();
		img1.src='/images/diapo/diap5_m.jpg';
		img1.alt='Bienvenue chez Up\'Generation - Conseil - Marketing - Innovation';
		img1.onload = function() {
			$('pbox').innerHTML = '';
			$('pbox').appendChild(img1);
			img2.src='/images/diapo/diap1_m.jpg';
			img2.alt='C\'est l\'Histoire d\'une passion pour le Marketing Client';
			img2.onload = function() {
				img3.src='/images/diapo/diap2_m.jpg';
				img3.alt='avec l\'innovation inscrite dans nos Gènes,';
				img3.onload = function() {
					img4.src='/images/diapo/diap3_m.jpg';
					img4.alt='dans le respect de nos engagements Clients,';
					img4.onload = function() {
						img5.src='/images/diapo/diap4_m.jpg';
						img5.alt='Nourris de la créativité et de la mixité de nos Equipes.';
						img5.onload = function() {
							$('pbox').appendChild(img2);
							$('pbox').appendChild(img3);
							$('pbox').appendChild(img4);
							$('pbox').appendChild(img5);
							var V2 = new viewer($$('#pbox img)'),{
								mode: 'alpha',
								interval: 5000
							});
							V2.play(true);
						}
					}
				}
			}

		}

	}
}
addLoadEvent(initAnim);