﻿//é

$(document).ready(function(){
	navigation();
//	finishDesign();
//	enhanceBehaviour();
});

$(window).load(function(){
	initPrettyPhoto();
	initCycle();
	finishDesign();
	handleAjax();
});


/* This function finishes design */
function finishDesign(){
	$('#mapBlk').html('<iframe width="559" height="460" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.fr/maps?f=q&amp;source=s_q&amp;hl=fr&amp;geocode=&amp;q=Boston+Scientific+International+33+Rue+des+Vanesses,+Villepinte&amp;sll=48.97284,2.505213&amp;sspn=0.012071,0.033023&amp;ie=UTF8&amp;hq=Boston+Scientific+International&amp;hnear=33+Rue+des+Vanesses,+93420+Villepinte,+Seine-Saint-Denis,+Ile-de-France&amp;ll=48.972837,2.50514&amp;spn=0.025917,0.047894&amp;z=13&amp;iwloc=&amp;output=embed"></iframe>');

// Egalisation des hauteurs
	var hautMax=0;
	$('.gen #centerBlk .col:not(".home")').each(function(){
		hautMax=$(this).height() > hautMax ? $(this).height() : hautMax;
	});
	$('.gen #centerBlk .col:not(".home")').height(hautMax);

// Mise en place des accordéons
	if(typeof $.fn.accordion != "undefined"){
		$("#accordion").accordion({
			header:		"h3"
			,autoHeight:	false
		});
	}

// Egalisation colonnes cours
	$('ul.listeCours ul li .dates').each(function(){
		$(this).height($(this).prev().height());
	});

// Ajout légende astérisque
	$('.agrement').attr('title', $('p#legende').text());

// Egalisation des hauteurs documents ressources
	var hautMax=0;
	$('ul.ressources li.doc').each(function(){
		hautMax=$(this).height() > hautMax ? $(this).height() : hautMax;
	});
	$('ul.ressources li.doc').height(hautMax);

/*
	if($.browser.msie==true && $.browser.version==6){
		initIE6();
	}
*/
	return true;
}

/*
	This function handles the navigation bar and the subMenus
*/
function navigation(){
// création des sous-menus
	$('nav .snav').each(function(){
		$(this).hide();
	});
// Elément pour le masquage
	$('nav')
		.prepend('<div class="hiders" id="hide1" />');
	$('.hiders')
		.each(function(){
			$(this)
				.mouseover(function(){
					$('.snav', 'nav')
						.hide('normal');
					$('.hiders')
						.hide();
				});
		});
// Réactivité
	$('nav li.rub').each(function(){
		var monLi=$(this)
		,timer;
		function manipulateSNav(){
			if($('.snav', monLi).length > 0){
				$('nav .snav').hide();
				$('.snav', monLi).show();
// Autorisation à suivre les liens du sous-menu
				$('.snav a', monLi).click(function(event){
					event.stopPropagation();
					return true;
				});
				$('.hiders', 'nav').show();
				return false;
			}
// Si pas de sous-menu
			else{
				return true;
			}
		}
		$(monLi)
// Best of both worlds...
			.click(manipulateSNav)
			.hover(function(){
				if(timer){
					clearTimeout(timer);
					timer=null;
				}
				timer=setTimeout(manipulateSNav, 300);
			}, function(){
				clearTimeout(timer);
				timer=null;
			});
	});
}

function initPrettyPhoto(){
	if(typeof $.prettyPhoto != "undefined"){
		$(".gallery a[rel^='prettyPhoto']")
			.prettyPhoto(
				{
					theme:			'dark_rounded'
					,opacity:		0.6
					,overlay_gallery:	false
					,callback:		function(){
									return true;
								}
				}
			);
	}
}


function initCycle(){
	if(typeof $.fn.cycle != "undefined"){
		var nbNews=$('#actus ul li').length;
		$('#actus ul')
			.before('<div id="actusPager" class="nb' + nbNews + '" />')
			.cycle({
				fx:		'turnUp'
				,speed:		1500
				,timeout:	5000
				,pause:		1
				,pager:		'#actusPager'
				,pagerAnchorBuilder:
						function(index, DOMelement){
							return '<a href="#" class="it' + (index+1) + '">'+ (index+1) + '</a>';
						}
				,activePagerClass:
						'current'
				,before:	function(){
							$('#actus').css('overflow', 'visible');
						}
			});
	}
}


// Fonction de gestion des appels ajax
function handleAjax(){
	$('form.discreet').submit(function(){
		var monForm=$(this).attr('id')
		,actionFile=$(this).attr('action')
		,fields=$(this).serialize();
		$.ajax({
			type:		"POST"
			,url:		actionFile
			,data:		fields+'&ajax=1'
			,dataType:	"json"
			,success:	function(data){
						if(data.etat=='null'){
							$('#' + monForm + ' .null').show('fast', function(){
								setTimeout(function(){
									$('#' + monForm + ' .null').fadeOut('slow', function(){
										for(var i in data.url){
											if(data.url[i] == 0){
												$('#'+i).val('').focus();
											}
										}
									});
								}, 3000);
							});
						}
						else if(data.etat=='HS'){
							$('#' + monForm + ' .hs').show('fast', function(){
								setTimeout(function(){
									$('#' + monForm + ' .hs').fadeOut('slow');
								}, 3000);
							});
						}
						else if(data.etat=='err'){
							$('#' + monForm + ' .err').show('fast', function(){
								setTimeout(function(){
									$('#' + monForm + ' .err').fadeOut('slow');
								}, 3000);
							});
						}
// Sortie positive
						else if(data.etat == 'ok'){
							if(data.url == 31){
								location.href=data.infoAdmin;
							}
							else{
								$('#' + monForm + ' .ok').show('fast', function(){
									$(this).siblings('fieldset').hide();
								})
							}
						}
					}
			,error:		function(data){
						if(data.status == '404'){
							afficheMessage('', 'Error', messagesAdmin['a404']);
						}
						else{
							$('#' + monForm + ' .errAjax').show('fast', function(){
								setTimeout(function(){
									$('#' + monForm + ' .errAjax').fadeOut('slow');
								}, 5000);
							});
						}
					}
		});
		return false;
	});
}

/* This function intercepts natural behaviour to enhance user experience */
function enhanceBehaviour(){
	return true;
}

function initIE6(){
	return true;
}
