var popupActiveContent;
window.addEvent('domready', function(){

	calculBody();
	$('fondPopup').setProperties({style:'width:'+width_body+'px; height:'+height_body+'px;'});
	new Fx.Style('fondPopup', 'opacity').set(0); 
	/*
		$('popup').setStyles({
					visibility: 'hidden',
					width: 0,
					height: 0
		});
		*/
		
	addProducteursScroll();	
	addScrolltoTop();
	addAccordeon();
	changeImageAccordeon();
	addManifesteSlider();
	openPopup();
});

function addProducteursScroll(){
	 var myTransition = new Fx.Transition(Fx.Transitions.Sine);
	 var scroll = new Fx.Scroll(window, 
	{duration: 600,transition: myTransition.easeOut}
	);	
	$$('#ancres li').each(function(el){
		var lien = el.getFirst();
		lien.addEvent('click', function(e) {
		e = new Event(e);
		removeActiveClass();
		//el.toggleClass('active');
		var element = lien.id.substr(1);
		scroll.toElement(element);
		e.stop();
		 });
	});
	$$('#accordeon li').each(function(el){
		var lien = el.getFirst();
		lien.addEvent('click', function(e) {
		e = new Event(e);
		removeActiveClass();
		var element = lien.id.substr(1);
		var lienAncre = '#'+element;
		$(lienAncre).getParent().toggleClass('active');
		scroll.toElement(element);
		e.stop();
		 });
	});
}	

	function removeActiveClass(){
		$$('#ancres li').each(function(el){
			if(el.hasClass('active') == true) {
				el.removeClass('active');
			}
		});
	}
	function changeImageAccordeon(){
		$$('#ancres li').each(function(el, i){
			var lien = el.getFirst();
			var element = lien.id.substr(1);
			var lienAncre = '_'+element;
			el.addEvent("mouseenter", function(event) {
				$$('#accordeon li').each(function(elm){
					elm.setStyle('width','120px');
				});
				$(lienAncre).getParent().setStyle('width','190px');
				$(lienAncre).getParent().addClass('active');
				$(lienAncre).getFirst().setProperty('src','img/img_bandeau_'+(i+1)+'_on.jpg');	
			});
			el.addEvent("mouseleave", function(event) {
				$$('#accordeon li').each(function(elm){
					elm.setStyle('width','133px');
				});
				$(lienAncre).getParent().removeClass('active');
				$(lienAncre).getFirst().setProperty('src','img/img_bandeau_'+(i+1)+'.jpg');
			});
		});
	}
	
function addScrolltoTop(){
	 	var myTransition = new Fx.Transition(Fx.Transitions.Sine);
	 	var scroll = new Fx.Scroll(window, 
		{duration: 600,transition: myTransition.easeOut}
		);
	$$('#conteneur a.btn','#footer a.openPopup').each(function(el){;
		el.addEvent('click', function(e) {
		e = new Event(e);
		scroll.toTop();
		e.stop();
		 });
	});
}

function addAccordeon(){
	var szNormal = 133, szSmall  = 120, szFull   = 190;
 
	var kwicks = $$("#accordeon li");
	var fx = new Fx.Elements(kwicks, {wait: false, duration: 300, transition: Fx.Transitions.Back.easeOut});
	kwicks.each(function(kwick, i) {
		kwick.addEvent("mouseenter", function(event) {
			var o = {};
			o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
			kwicks.each(function(other, j) {
				if(i != j) {
					var w = other.getStyle("width").toInt();
					if(w != szSmall)  o[j] = {width: [w, szSmall]};
					other.getFirst().getFirst().setProperty('src','img/img_bandeau_'+(j+1)+'.jpg');
					other.removeClass('active');
				}
				else{
					other.addClass('active');
					other.getFirst().getFirst().setProperty('src','img/img_bandeau_'+(j+1)+'_on.jpg');
				}
			});
			fx.start(o);
			
			removeActiveClass();
			
			var lien = kwick.getFirst();
			var element = lien.id.substr(1);
			var lienAncre = '#'+element;
			$(lienAncre).getParent().toggleClass('active');
		});	

	});
	 
	if($("accordeon")){
		$("accordeon").addEvent("mouseleave", function(event) {
			var o = {};
			kwicks.each(function(kwick, i) {
				o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
				kwick.removeClass('active');
				kwick.getFirst().getFirst().setProperty('src','img/img_bandeau_'+(i+1)+'.jpg');
			});
			fx.start(o);	
			removeActiveClass();	
		})
	}
 
}

function addManifesteSlider(){
	if($('home')){
	
		$('cntSlide').setStyles('overflow:hidden; cursor:n-resize;');
		$('slider').setStyle('display','block');
		
		var scroll2 = new Scroller('cntSlide', {area: 25, velocity: 1});
		$('cntSlide').addEvent('mouseover', scroll2.start.bind(scroll2));
		$('cntSlide').addEvent('mouseout', scroll2.stop.bind(scroll2));
		
		var lastPositionTop;
		scroll2.addEvent('onChange',function(){
			var positionTop = $('cntSlide').getSize().scroll.y;
			var btnTop = $('btn').getStyle('top').toInt();
			if(positionTop < lastPositionTop){
				if(btnTop<=25) $('btn').setStyle('top',10);
				else $('btn').setStyle('top',btnTop-25);
				lastPositionTop = btnTop;
			}
			else{	
				if(btnTop>=110) $('btn').setStyle('top',135);
				else $('btn').setStyle('top',btnTop+25);
				lastPositionTop = btnTop;
			}
		});


	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)
			}
		}
	});
	

	$('cntSlide').addEvents({
		'wheelup': function(e) {
			e = new Event(e).stop();
			var btnTop = $('btn').getStyle('top').toInt();
			var scrollY = this.getSize().scroll.y;
			if(btnTop <= 25){
				$('btn').setStyle('top',10);
				this.scrollTo(scrollY, scrollY-35);
			}
			else{
			   $('btn').setStyle('top',btnTop-25);
			   this.scrollTo(scrollY, scrollY-25);
			}
		}, 
		'wheeldown': function(e) {
			e = new Event(e).stop();
			var btnTop = $('btn').getStyle('top').toInt();
			var scrollY = this.getSize().scroll.y;
			if(btnTop == 110 || btnTop >= 135){
				$('btn').setStyle('top',135);
				this.scrollTo(scrollY, scrollY+35);
			}
			else{
			   $('btn').setStyle('top',btnTop+25);
			   this.scrollTo(scrollY, scrollY+25);
			}
		}
	});
	
		var scrollY = $('cntSlide').getSize().scroll.y;
		var mySlide3 = new Slider($('slider'), $('btn'), {	
		steps: 150,	
		offset:-10,
		mode: 'vertical',	
		onChange: function(step){
			$('cntSlide').scrollTo(scrollY, step+25)
			if(step == 0) 	$('cntSlide').scrollTo(scrollY, step-25);
		}
		}).set(10);
		$('btn').setStyle('top',10);
	    $('cntSlide').scrollTo(scrollY, -10)
	}
}





/* TEMP */
var heightPopupContent;
function getContent(url,replace){
	var ajax = new Ajax(url, {
		method: 'get',
		update: $(replace)
	}).request();
	ajax.addEvent('onComplete',function(){
		closePopup();
		heightPopupContent = $(popupActiveContent).getSize().size.y;
	});
}


function loadAsset(typeFile,file,id){
	if(typeFile == 'css') new Asset.css(''+file+'', {id: ''+id+''});
	else new Asset.javascript(''+file+'', {id: ''+id+''});
}

function resizeElement(elm,width,height){
	$(elm).setStyle('width',''+width+'px');
	$(elm).setStyle('height',''+height+'px');
}

function calculBody(){
	width_body = window.getWidth();
	height_body = window.getScrollHeight();
}

window.onresize = function(){
		calculBody();
		resizeElement('fondPopup',width_body,height_body);
}

function closePopup(){
	var elms = $ES('.closePopup','popup_center');
	elms.each(function(el){
		el.addEvent('click', function(e) {
			e = new Event(e);
						
						var fx2 = $('popup').effects({duration: 600,transition: Fx.Transitions.Quart.easeOut});
						fx2.start({'width':0,'height':0,opacity:0, duration: 200});
							$('popup').empty();
							$('popup').setStyle('visibility','hidden');
						
						var fx = $('fondPopup').effects({duration: 600,transition: Fx.Transitions.Quart.easeOut});						
						fx.start({opacity : 0, duration: 200});						
				
			e.stop();
		});
	});
}


function openPopup(){
	var elms = $ES('.openPopup','conteneur');
	elms.each(function(el){
		el.addEvent('click', function(e) {
			e = new Event(e);
				calculBody();
				$('popup').setStyles('background:#EDEDED; border:1px solid #A0B71F;padding:15px;');
				var content = this.getAttribute('href');
				var position = (content.lastIndexOf("=")+1).toInt();
				var position2 = (content.lastIndexOf("#")).toInt();
				content = content.substring(position,position2);
				popupActiveContent = "popup_"+content+"";
				//loadAsset('css','/fair_friends/css/popup.css','css_popup');	
				//loadAsset('css','/fair_friends/css/'+content+'.css','css_popup2');	
				//loadAsset('js','/fair_friends/js/popup.js','js_popup');	
				//loadAsset('js','/fair_friends/js/'+content+'.js','js_popup2');
				if(content == "credits"){
					var largeur = 460;
					var hauteur = 600;
				}
				else{
					var largeur =  711;
					var hauteur = 400;
					$('popup').setStyles('padding:0; border:0;');
				}
				var fx = $('fondPopup').effects();
				new Fx.Style('popup', 'opacity').set(0);
				fx.start(
				{width: width_body, height: height_body, opacity : .7, duration: 500}
				).chain(function(){
						$('popup').setStyle('visibility','visible');
						
						var fx2 = $('popup').effects({duration: 600,transition: Fx.Transitions.Quart.easeOut});
						fx2.start({'width':largeur,'height':hauteur,opacity:1, duration: 200}).chain(function(){
							getContent('/quinzaine2008/includes/popup/'+content+'.inc.php','popup');
							calculBody();
							resizeElement('fondPopup',width_body,height_body);
							if(content == "visages"){	
								$('popup').setStyle('background','none');
								
								var FO = {
								//9.0.115
									movie:"flv/video.swf", id:"chrono_id", width:"481", height:"385", menu:"false", majorversion:"8", build:"0", xi:"false",wmode:"transparent"
								};
								UFO.create(FO, "videoVisages");
								
							}						
						});
						
				});
				
				
				/*
				var fx = $('fondPopup').effects();									
				fx.start(
				{width: width_body, height: height_body, opacity : .7, duration: 300}
				).chain(function(){
						calculBody();
						resizeElement('fondPopup',width_body,height_body);
						var myEffects = new Fx.Styles('popup', {duration: 300, transition: Fx.Transitions.Quart.easeIn});
 
							myEffects.start({
							    'height': heightPopupContent,
							    'width': 470
							});
							$('popup').setStyle('visibility','visible');
					});
					*/
		
			e.stop();
		});
	});
	
}
