window.addEvent('domready', function(){
	calculBody();
	$('fondPopup').setProperties({style:'width:'+window.getWidth()+'px; height:'+height_body+'px;'});
	new Fx.Style('fondPopup', 'opacity').set(0); 
	openPopup();
	closePopup();
	showBubble();
});

window.addEvent('resize',function(){
	calculBody();
	resize("fondPopup",width_body,height_body);
});


function resize(elm,width,height){			
		$(elm).setStyle("width",width);
		$(elm).setStyle("height",height);
}
function calculBody(){
	width_body = window.getWidth();
	height_body = window.getScrollHeight();
}

function runChain(actionsToDo){
   	var runChain = function() {
		actionsToDo.callChain();
		if (actionsToDo.chains.length == 0) { runChain = $clear(timer); }
	}
	var timer = runChain.periodical(500);
}



function openPopup(){
	var elms = $ES('.openPopup','conteneur');
	elms.each(function(el){
		el.addEvent('click', function(e) {
			e = new Event(e);
				var popup_content = "popup_content";
				var actionsToDo = new Chain();
				actionsToDo.chain( function(){	
					$('fondPopup').setStyle('display','block');				
					var fx = $('fondPopup').effects();
					new Fx.Style('popup', 'opacity').set(0);
					fx.start({width: width_body, height: height_body, opacity : .7, duration: 500})			
				});	

				if(el.id != "popup_newsletter_hub"){
					actionsToDo.chain( function(){	
						var fx2 = $('popup').effects({duration: 500,transition: Fx.Transitions.Quart.easeOut});
						fx2.start({'width':961,'height':302,opacity:1, duration: 250})
						$('popup').getFirst().getFirst().getFirst().getFirst().getFirst().setStyle('height',280);
					});	
				}
				
				
				if(el.id == "popup_newsletter_hub"){
					popup_content = "popup_content2";
					actionsToDo.chain( function(){
						//$('fondPopup').setStyle('cursor','pointer');
						// on ferme la popup newsletter au clic du voile
						/*
						$('fondPopup').addEvent('click',function(){
							var actionsToDo = new Chain();
							actionsToDo.chain( function(){
								$(popup_content).empty();
							});
							actionsToDo.chain( function(){	
								var fx = $('fondPopup').effects();
								fx.start({width: width_body, height: height_body, opacity : 0, duration: 500});
							});
							actionsToDo.chain( function(){		
								$('fondPopup').setStyle('cursor','auto');							
								calculBody();	
								resize("fondPopup",width_body,height_body);
							});
							runChain(actionsToDo);
						});
						*/
	
						var myTransition = new Fx.Transition(Fx.Transitions.Sine);
						 var scroll = new Fx.Scroll(window, 
						{duration: 500,transition: myTransition.easeOut});						
						scroll.toElement('liens');
					});
					actionsToDo.chain( function(){
						//$(popup_content).setStyle('top',window.getScrollTop() + window.getHeight()/2 - 19 );
						$(popup_content).setStyle('top', $("popup_newsletter_hub").getPosition().y + 22 );
					});
				}
	
				
				actionsToDo.chain( function(){
					var lang = $E('html').getProperty('lang');
					var url = 'inc/content/ajax/popup/'+el.id+'.php?popupID='+el.id+'&js';
			
					var loadPopup = new Ajax(url, {
						method: 'get',encoding:'iso-8859-15',
						evalScripts:true,onComplete:showBubblePopup,
						update: $(popup_content)
					}).request();				
					
				});	
				if(el.id != "popup_newsletter_hub"){				
					actionsToDo.chain( function(){
						(function(){
							$('popup').getFirst().setStyle('display','block');
						}).delay(500);
					});	
				}
				actionsToDo.chain( function(){
					(function(){
						if(el.id == "popup_locations"){
							$('popup').getFirst().getFirst().getFirst().getFirst().setStyle('background','#0168A2 url(skins/white/img/map.gif) 457px 5px no-repeat');
						}
						else{
							$('popup').getFirst().getFirst().getFirst().getFirst().setStyle('background','none');
						}
						$('popup').getFirst().setStyle('display','block');
					}).delay(500);
				});	
				actionsToDo.chain( function(){				
					calculBody();	
					resize("fondPopup",width_body,height_body);
					//if(window.ie6 ==  true) doFix();
				});
				runChain(actionsToDo);
			
			e.stop();
		});
	});
	
}

function closePopup(){
	var elms = $ES('.closePopup','popup_center');
	elms.each(function(el){
		el.addEvent('click', function(e) {
			e = new Event(e);
				callClosingPopup();
			e.stop();
		});
	});
}

function callClosingPopup(){
				calculBody();	
				var actionsToDo = new Chain();
					actionsToDo.chain( function(){
						//var fx2 = $('popup_content').effects({duration: 600,transition: Fx.Transitions.Quart.easeOut});
						//fx2.start({'width':0,'height':0,opacity:0, duration: 100});
						$('popup').getFirst().setStyle('display','none');
						if($('popup_errors')) $('popup_errors').setStyle('display','none');
						$('popup_content').empty();
					});
					actionsToDo.chain( function(){
						var fx2 = $('popup').effects({duration: 600,transition: Fx.Transitions.Quart.easeOut});
						//new Fx.Style('popup', 'opacity').set(1);
						fx2.start({'width':0,'height':0,opacity:0, duration: 100});
					});
					actionsToDo.chain( function(){	
							var fx = $('fondPopup').effects();
							fx.start({width: width_body, height: height_body, opacity : 0, duration: 500});
					});
					actionsToDo.chain( function(){				
						calculBody();	
						resize("fondPopup",width_body,height_body);
					});
				runChain(actionsToDo);
}





function showBubble(){
	if($('mybubble'))  $('mybubble').remove();
	var value;
	var Tips2 = new Tips($$('.Tips1'), {
	initialize:function(){
	this.toolTip.setProperty('id','mybubble');
	 this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 100, wait: false}).set(0);
	 },
	 onShow: function(toolTip) {
		value = this.title.getText();
		this.toolTip.setHTML('<span class="bulle"><span class="relative"><span class="gauche"></span><span class="bg"><span class="queue">'+value+'</span></span><span class="droite"></span></span></span>');
		this.fx.start(1);
	 },
	onHide: function(toolTip) {
		this.fx.start(0);
	 }
	 ,fixed:false
	 //,offsets:{'x': 0, 'y': -19}
	,offsets:{'x': -29, 'y': -29}
	});
}


function showBubblePopup(){
	if($('mybubble2'))  $('mybubble2').remove();
	var value;
	var Tips2 = new Tips($$('.Tips2'), {
	initialize:function(){
	this.toolTip.setProperty('id','mybubble2');
	 this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 100, wait: false}).set(0);
	 },
	 onShow: function(toolTip) {
		value = this.title.getText();
		this.toolTip.setHTML('<span class="bulle"><span class="relative"><span class="gauche"></span><span class="bg"><span class="queue">'+value+'</span></span><span class="droite"></span></span></span>');
		this.fx.start(1);
	 },
	onHide: function(toolTip) {
		this.fx.start(0);
	 }
	 ,fixed:false
	 //,offsets:{'x': 0, 'y': -19}
	,offsets:{'x': -29, 'y': -29}
	});
}


var W3CDOM = (document.createElement && document.getElementsByTagName);
function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	var image = '<input /><div class="button"><span class="bulle"><span class="relative"><span class="gauche"></span><span class="bg"><span class="sans_queue"><span>upload</span></span></span><span class="droite"></span></span></span>';

	fakeFileUpload.innerHTML = image ;
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}



function addSlider(){

	$('offers_btn_top').addEvents({
		'click': function(e) {
			e = new Event(e).stop();
			var scrollY = $('offers_content').getSize().scroll.y;
			$('offers_content').scrollTo(scrollY, scrollY-10);
		},
		'mousedown': function(e) {
			var scrollY = $('offers_content').getSize().scroll.y;
			$('offers_content').scrollTo(scrollY, scrollY-10);
		},
		'mouseenter': function(e) {
			var scrollY = $('offers_content').getSize().scroll.y;
			$('offers_content').scrollTo(scrollY, scrollY-10);
		},
		'mouseover': function(e) {
			var scrollY = $('offers_content').getSize().scroll.y;
			$('offers_content').scrollTo(scrollY, scrollY-10);
		}
	});
	$('offers_btn_bottom').addEvents({
		'click': function(e) {
			e = new Event(e).stop();
			var scrollY = $('offers_content').getSize().scroll.y;
			$('offers_content').scrollTo(scrollY, scrollY+10);
		},
		'mousedown': function(e) {
			var scrollY = $('offers_content').getSize().scroll.y;
			$('offers_content').scrollTo(scrollY, scrollY+10);
		},
		'mouseenter': function(e) {
			var scrollY = $('offers_content').getSize().scroll.y;
			$('offers_content').scrollTo(scrollY, scrollY+10);
		},
		'mouseover': function(e) {
			var scrollY = $('offers_content').getSize().scroll.y;
			$('offers_content').scrollTo(scrollY, scrollY+10);
		}
	});
	
	Element.Events.extend({
		'wheelup': {
			type: Element.Events.mousewheel.type,
			map: function(event){
				event = new Event(event);
				if (event.wheel >= 0) this.fireEvent('wheelup', event)
			}
		},
		'wheeldown': {
			type: Element.Events.mousewheel.type,
			map: function(event){
				event = new Event(event);
				if (event.wheel <= 0) this.fireEvent('wheeldown', event)
			}
		}
	});
	
		
		// move over content method
		var scroll2 = new Scroller('offers_content', {area: 10, velocity: 1});
		$('offers_content').addEvent('mouseover', scroll2.start.bind(scroll2));
		$('offers_content').addEvent('mouseout', scroll2.stop.bind(scroll2));
		
		// wheel over content method
		$('offers_content').addEvents({
			'wheelup': function(e) {
				e = new Event(e).stop();
				var scrollY = $('offers_content').getSize().scroll.y;
				this.scrollTo(scrollY, scrollY-10);
			}, 
			'wheeldown': function(e) {
				e = new Event(e).stop();
				var scrollY = $('offers_content').getSize().scroll.y;
				 this.scrollTo(scrollY, scrollY+10);
			}
		});
}


function dispatchShowHideLocations(){

	var lien = $E('a','paris');
	lien.addEvent('click', function(e) {
		e = new Event(e);
		var content = $('paris_content');

			$$('#popup_locations .active').each(function(activeElm){
				activeElm.removeClass('active');
				activeElm.addClass("none");
			});
			content.removeClass('none');
			content.addClass('active');
					
		e.stop();
	});

	var lien = $E('a','milan');
	lien.addEvent('click', function(e) {
		e = new Event(e);
		var content = $('milan_content');

			$$('#popup_locations .active').each(function(activeElm){
				activeElm.removeClass('active');
				activeElm.addClass("none");
			});
			content.removeClass('none');
			content.addClass('active');
					
		e.stop();
	});

	var ville = 'munich';
	var lien = $E('a',ville);
	lien.addEvent('click', function(e) {
		e = new Event(e);
		var content = $('munich_content');

			$$('#popup_locations .active').each(function(activeElm){
				activeElm.removeClass('active');
				activeElm.addClass("none");
			});
			content.removeClass('none');
			content.addClass('active');
					
		e.stop();
	});


	var lien = $E('a','lisbon');
	lien.addEvent('click', function(e) {
		e = new Event(e);
		var content = $('lisbon_content');

			$$('#popup_locations .active').each(function(activeElm){
				activeElm.removeClass('active');
				activeElm.addClass("none");
			});
			content.removeClass('none');
			content.addClass('active');
					
		e.stop();
	});


	var lien = $E('a','londres');
	lien.addEvent('click', function(e) {
		e = new Event(e);
		var content = $('londres_content');

			$$('#popup_locations .active').each(function(activeElm){
				activeElm.removeClass('active');
				activeElm.addClass("none");
			});
			content.removeClass('none');
			content.addClass('active');
					
		e.stop();
	});	
}



function showErrors(error1,error2,error3){
	//if($('error_all'))
	// testing if newsletter popup
	if($('error_all')) $('error_all').removeClass('none');
	if($('error_email')) $('error_email').removeClass('none');
	if($('error_email_already')) $('error_email_already').removeClass('none');
	if($('error_sep')) $('error_sep').addClass('none');
	
	if(error2 == true && error1 == true) {
		if($('error_all')) {
			$('error_sep').removeClass('none');
			$('error_email_already').addClass('none');
		}
	}
	else if(error1 == true) {
	$('error_email').addClass('none');
	$('error_email_already').addClass('none');
	}
	else if(error2 == true) {
	$('error_all').addClass('none');
	$('error_email_already').addClass('none');
	}
	else if(error3 == true){
	$('error_all').addClass('none');
	$('error_email').addClass('none');
	}
	
	if(error1 == true || error2 == true || error3 == true) displayErrors();
	
	function displayErrors(){
		$('popup_errors').setStyle('display','block');
		if($('error_all')) $('popup_errors').getFirst().setOpacity(0.9);
		if($('error_all')) $('popup_errors').getFirst().getNext().setOpacity(0.2);
	}
	
	if($('error_all')) $('popup_errors').getFirst().getNext().removeClass('background3');
	if($('error_all')) $('popup_errors').getFirst().getNext().addClass('background2');
	$('error').removeClass('none');
	if($('error_all')) $('success').addClass('none');
}



function showValid(){
	$('popup_errors').setStyle('display','block');
	if($('error_all')) $('popup_errors').getFirst().setOpacity(0.9);
	if($('error_all')) $('popup_errors').getFirst().getNext().setOpacity(0.2);
	if($('error_all')) $('popup_errors').getFirst().getNext().removeClass('background2');
	if($('error_all')) $('popup_errors').getFirst().getNext().addClass('background3');
	$('error').addClass('none');
	$('success').removeClass('none');
	
	if($('closeError')){
		$('closeError').removeEvent('click');
			$('closeError').addEvent('click', function(e) {
				e = new Event(e);
					callClosingPopup();
				e.stop();
		});
	}

}




function checkForm(){
	el = $('btn_submit');
	el.addEvent('click', function(e) {
		e = new Event(e);
//		$('form').send({evalResponse:true,method: 'post',headers: {'Content-type': 'application/x-www-form-urlencoded; charset=utf-8'}});
		
		var formObject = document.getElementById('form'); 


var handleSuccess = function(o){ 
	if (o.responseText=="A")  showErrors(true,false,false);
	if (o.responseText=="B")  showErrors(false,true,false);
	if (o.responseText=="AB") showErrors(true,true,false);
	if (o.responseText=="D") showErrors(false,false,true);
	if (o.responseText=="C") showValid();
}

	var mycallback = 
	{ 
	  upload: handleSuccess, 
	  argument: ['foo','bar'] 
	};


//the second argument is true to indicate file upload. 
//arnaud
YAHOO.util.Connect.setForm(formObject, true); 
var cObj = YAHOO.util.Connect.asyncRequest('POST', 'inc/content/ajax/popup/popup_join_hub.submit.php', mycallback); 

	function closeErrors(){
		$('popup_errors').setStyle('display','none');
	}
		if($('closeError')){
			$('closeError').addEvent('click', function(e) {
				e = new Event(e);
					closeErrors();
				e.stop();
			});
			//(function(){closeErrors(); }).delay(3000);
		}
		e.stop();
	});
}


