var box = {};

window.addEvent('domready',function(){
	init_ajaxlinks();
	init_teaser();
	init_team();
	init_tabs();
	init_smoothscroll();
	init_accordion();
	init_graphs();
	init_bibliocategories();
	init_multibox();
	//init_allsmooth();
	init_overtext();
	//console.log(location);
});

function init_overtext() {
	$$('.overtext').each(function(el) {
		new OverText(el);
	});
}

function init_allsmooth() {
	new Fx.SmoothScroll({ duration:300 }, window);
}

function init_ajaxlinks() {
	$$('.ajaxlink').each(function(el) {
		var ajaxref = el.get('href')+"ajax";
		el.set('href', ajaxref);
	});
}

function init_multibox() {
	box = new MultiBox('mb', {descClassName: 'multiBoxDesc', useOverlay: true});
	
	/* 	l'overlay de la multibox créé un bug d'affichage lorsqu'il est utilisé avec le système de tabulation
		il faut recalculer la hauteur de l'overlay à chaque changement de tab */
	
	$$('.tabs .menu a').addEvent('click', function() {
		refresh_overlay.delay(1);
	});
}

function refresh_overlay() {
		var newHeight = $('wrap').getSize().y;
		$('OverlayContainer').setStyle('display', 'none');
		$('OverlayContainer').setStyle('height', newHeight);
		$('OverlayContainer').setStyle('display', 'block');
}

function init_team() {
	
	
	
	var mores = new Array;
	var moresHeight = new Array;
	var moresFx = new Array;
	
	$$('.profiles .more').each(function(el,k) {
		mores[k] = el;
		moresHeight[k] = el.getSize().y;
		moresFx[k] = new Fx.Tween(el, {duration:"short", link:"cancel"});
		moresFx[k].set('height', 0);
	});
	
	
	$$('.profiles .btn-more').each(function(el, k) {
		el.addEvents({
		    'click': function(e){
		    	
		    	var more = mores[k];
		    	var moreHeight = moresHeight[k];
		    	var moreFx = moresFx[k];
				//console.log(more, moreHeight);
				
		    	if(more.getSize().y > 0) {
		    		moreFx.start('height', 0);
		    	} else {
			    	moreFx.start('height', moreHeight);
		    	}
		    	
		    	return false;
		    }
		});
	});

		
	//console.log("Team Initialized");
}


function init_bibliocategories() {

	// transforms a list of links in a dropdown box
	var dropbox = new Element('select', {
		'class' : 'biblio-dropdown',
		'events': {
	        'change': function(){
	            location.href=this.value;
	        }
	    }
	});
	
	$$('.biblio-categories li a').each(function(el) {
		var elOption = new Element('option', {'value' : el.href, 'text' : el.text});
		
		if(el.hasClass('selected')) {
			elOption.set('selected', 'selected');
		}
		
		elOption.inject(dropbox);
	});

	$$('.biblio-categories').each(function(el) {
		dropbox.inject(el, 'after');
		el.destroy();
	});

	//console.log('bibliocategories initialized');
}

function init_accordion() {
	//pour l'accordeon R&D
	var myAccordion = new Fx.Accordion($$('.toggler'), $$('.element'), {
	    display: 1,
	    alwaysHide: true,
	    duration: 'short'
	});
	//console.log('accordions initialized');
}

function init_graphs() {
	//get each graph
	$$('.graph').each(function(el) {
	
		//console.log('graph');
		// request points
		var jsonRequest = new Request.JSON({
			url: "/societe/json-objectifs/"+el.get('id').replace(/graph-/, ""),
			onSuccess: function(el2){
				//success
				
				
				
				var total_days = el2.total_days;
				var total_points = el2.total_points;
				
				if(total_days == "") {
					total_days = 17;
				}
				
				if(total_points == "") {
					total_points = 30;
				}
				
				//draw the graph
				var d1 = el2.points;
		    	var d2 = [[0, total_points], [total_days, 0]];
		    	var f = Flotr.draw(el.get('id'), [ d1, d2 ]);
			}
		}).get();
		

	});
	
	//console.log('graphs initialized');
}


function init_smoothscroll() {

	var mySmoothScroll = new Fx.SmoothScroll({
	    links: '.smooth',
	    wheelStops: false
	});
	
	if(!$defined($('gotop'))) {
		return false;
	}
	
	var gotop = $('gotop');

	var startOpacity = 30;
	var stopOpacity = 100;
	
	gotop.setOpacity(0);
	
	/* recupere la valeur du scroll en cours */
	window.addEvent('scroll', function() {
		var pos = window.getScroll().y;
		
		if(pos < startOpacity) {
			gotop.setOpacity(0);
		} else {
			if(pos < stopOpacity) {
				//evolution
				var myOpacity = Math.round(((pos - startOpacity) / (stopOpacity - startOpacity)) * 10) / 10;
				
				gotop.setOpacity(myOpacity);
				
			} else {
				//1
				gotop.setOpacity(1);
			}
		}
		
		var maxBottom = ($('wrap').getSize().y - ($('foot').getSize().y + $('submenu').getSize().y + 150));
		
		//console.log(maxBottom);
		
		if(maxBottom < 0) {
//			$('submenu').setStyle('position', 'relative');
//			$('submenu').setStyle('visibility', 'visible');
		} else {
			if(pos > maxBottom) {
//				$('submenu').setStyle('visibility', 'hidden');
				$('submenu').setStyle('opacity', 0.5);
			} else {
				//console.log('not over');
//				$('submenu').setStyle('visibility', 'visible');
				$('submenu').setStyle('opacity', 1);
			}
		}
		
		//console.log('pos : '+pos);
		//console.log('body : '+$('wrap').getSize().y);
		//console.log('submenu : '+$('submenu').getSize().y);
		//console.log('foot : '+$('foot').getSize().y);
		//console.log('-------------');
	});
	//console.log('smoothscroll initialized');
}

function init_teaser() {

	//SAMPLE 5 (mode: vertical, using "onWalk" )
	
	if($defined($('info-teaser'))) {
		
		var info5 = $('info-teaser').set('opacity',0.5);
		var sampleObjectItems =[
			{title:'Bleu', autor:'Lorem', date:'5 Jun 2007', link:'http://www.link1.com'},
			{title:'Vert', autor:'Ipsum', date:'6 Dic 2007', link:'http://www.link2.com'}
		];
		var nS5 = new noobSlide({
			mode: 'horizontal',
			box: $('box-teaser'),
			size: 700,
			items: sampleObjectItems,
			addButtons: {
				previous: $('prev'),
				next: $('next')
			},
			onWalk: function(currentItem){
				info5.empty();
				new Element('h4').set('html','<a href="'+currentItem.link+'">link</a>'+currentItem.title).inject(info5);
				new Element('p').set('html','<b>Autor</b>: '+currentItem.autor+' &nbsp; &nbsp; <b>Date</b>: '+currentItem.date).inject(info5);
			}
		});
	}
	//console.log('teasers initialized');
}

function init_tabs() {
		
	var k = 0;
	if(location.hash) {
		var found = false;
		$$('.tabs .menu li a').each(function(el) {

			if(el.href.indexOf(location.hash) > 0) {
				found = true;
			}
			/*if(found == false) {
				k++;
			}*/
		});
	} else {
		//console.log('pas dandre a verifier');
		//console.log(location);
	}

	var sections = $$('.tabs .tab-panel') ;
	
	var swapper = new TabSwapper({
	  selectedClass: 'on',
	  deselectedClass: 'off',
	  tabs: $$('.tabs .menu li'),
	  clickers: $$('.tabs .menu li'),
	  sections: sections,
	  initPanel: k,
	  /*remember what the last tab the user clicked was*/
	  
//	  cookieName: 'tabSetExample',
	  cookieName: null,
	  
	  /*use transitions to fade across*/
	  smooth: true,
		smoothSize: false
	});	
	//console.log('tabs initialized');	
	if(location.hash){
		//console.log(k);
		//console.log(location.hash.replace('#',"")) ;
		var target = $(location.hash.replace('#',"")) ;
		//console.log(target) ;
		if(target){
			var tab = target.getParent('div.tab-panel') ;
			//console.log(tab) ;
			if(tab){
				//console.log("show") ;
				swapper.show( sections.indexOf(tab) ) ;
			}
		}
	}
}

function init_gmap() {

	var myLatlng = new google.maps.LatLng(48.894938,2.256752);
	var myOptions = {
	  zoom: 13,
	  center: myLatlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	var contentString = '<div id="map-content">'+
	    '<h4>Groupe XPERTeam</h4>'+
	    '&raquo; <a target="_blank" href="http://maps.google.fr/maps?hl=fr&source=hp&q=14-16+rue+Moli%C3%A8re+92400+Courbevoie&ie=UTF8&t=h&z=16&iwloc=A">Itinéraire Google Map</a><br />'+
	    '</div>';
	    
	var infowindow = new google.maps.InfoWindow({
	    content: contentString
	});
	
	var marker = new google.maps.Marker({
	    position: myLatlng,
	    map: map,
	    title: 'Symetrix E-learning'
	});
	
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map, marker);
	});
	//console.log('gmap initialized');
	
}