$(document).ready(function(){

	initialize_welink();
	
	$("ul#crisis").liScroll();
	
	$(".jPrompt").click(function(e){ 
		var el = $(this);
		jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {

			if(r){
	
				if(el.hasClass("nyroButton")){
					el.nyroModal().nmCall();
				}else{
					window.location = el.attr("href");
				}

			}
	
		});
		return false;	
	});
	
	// CLICK SUR LE MENU
	var busy = 0;
	$('a.subMenu').click(function(){
		if(!busy){
			busy = 1;
			
			var subMenu = $(this);
			if(navigator.appVersion.indexOf("MSIE 7.")==-1){
				var subWrapper = subMenu.parent().next();
			}else{
				var subWrapper = subMenu.next();	
			}
			
			if(subWrapper.css("display") == "none") subMenu.removeClass('subClose').addClass('subOpen');
			else subMenu.removeClass('subOpen').addClass('subClose');
			
			subWrapper.slideToggle('slow',function(){ busy = 0; });
		}
	});
	
	// HOVER SUR LE MENU
	/*var busy = 0;
	$("ul#nav div.subWrapper").hover(
		function(){
			if(busy==0 && $(this).children('ul').hasClass('active') == false){
				busy = 1;
				$(this).children('li').children('a').removeClass('subClose');
				$(this).children('li').children('a').addClass('subOpen');
				$(this).children('ul.sub').slideDown('slow',function(){
				});
			}
		},
		function(){
			if($(this).children('ul.sub').hasClass('active') == false){
				$(this).children('li').children('a').removeClass('subOpen');
				$(this).children('li').children('a').addClass('subClose');
				$(this).children('ul.sub').slideUp('slow',function(){
					busy = 0;
				});
			}
		}
	);*/
	
	var z_index = 10;
	// SHOPPINGS MAP
	$("div#shoppings div#map a.icon").hover(
		function(){
			if(!$(this).hasClass("selected")){
				var elemId = $(this).attr("id");
				if(elemId!=""){
					$(this).css('z-index', z_index);
					z_index++;
					$("div#shoppings ul#list a#"+elemId).addClass("active");
					var id_shopping = $(this).attr("id_shopping");
					scrollToListMap(id_shopping);
				}
				hoverIn($(this));
			}
		},
		function(){
			if(!$(this).hasClass("selected")){
				var elemId = $(this).attr("id");
				if(elemId!="") $("div#shoppings ul#list a#"+elemId).removeClass("active");
				hoverOut($(this));
			}
		}
	);
	$("div#shoppings ul#list a").hover(
		function(){
			if(!$(this).hasClass("selected")){
				var elemId = $(this).attr("id");
				if(elemId!="") hoverIn($("div#shoppings div#map a#"+elemId));
			}
		},
		function(){
			if(!$(this).hasClass("selected")){
				var elemId = $(this).attr("id");
				if(elemId!="") hoverOut($("div#shoppings div#map a#"+elemId));
			}
		}
	);
	
	// ACCORDION
	var accordionBusy = 0;
	$('ul.accordion > li > a.slide').click(function(){
		if(!accordionBusy){
			accordionBusy = 1;
			if($(this).next().css("display") != "block"){
				$(this).parent().parent().children("li").children("div, ul").slideUp('normal');
				$(this).parent().parent().children("li").children("a").removeClass('active');
				$(this).addClass('active');
				$(this).next().slideToggle('normal',function(){ accordionBusy = 0; });
				return false;
			}else{
				$(this).removeClass('active');
				$(this).next().slideToggle('normal',function(){ accordionBusy = 0; });
				return false;
			}
		}
	});
	
	// HOVER SLIDESHOW
	$("ul#slideshow li a").hover(
		function(){ doSlide($(this)); },
		function(){  }
	);
});

function scrollToListMap(id_shopping){
	$('ul#list').scrollTo('#listMap_shopping_'+id_shopping+'');
}

// MAP - HOVER IN
function hoverIn(object){
	var iniLeft = parseInt(object.css("left"));
	var iniTop = parseInt(object.css("top"));
	
	object.css("left", (iniLeft-8)+"px");
	object.css("top", (iniTop-8)+"px");
	object.children("img").attr("width","32");
	object.children("img").attr("height","32");
	
	var wrapperWidth = 450;
	var textWidth = parseInt(object.children("p").css("width"));
	var objectPos = object.position();
	var objectPosLeft = objectPos.left;
	
	if((objectPosLeft+textWidth) > wrapperWidth){
		object.children("p").css("right","32px");
	}else{
		object.children("p").css("left","32px");
	}
	
	object.children("p").css("display","block");
}
// MAP - HOVER OUT
function hoverOut(object){
	var iniLeft = parseInt(object.css("left"));
	var iniTop = parseInt(object.css("top"));
	
	object.css("left", (iniLeft+8)+"px");
	object.css("top", (iniTop+8)+"px");
	object.children("img").attr("width","16");
	object.children("img").attr("height","16");
	object.children("p").css("display","none");
}


// SLIDESHOW - DO SLIDE
var slideBusy = 0;
var slideTime = 500;
var easeType = "linear";
var slideInterval = setInterval('autoSlide();',5000);
function doSlide(slide){
	if(!slide.parent().hasClass("active")){
		slideBusy = 1;
		clearInterval(slideInterval);
		$('ul#slideshow li.active').stop();
		$('ul#slideshow li.active a img').stop();
		closeSlide();
		openSlide(slide);
	}
}
// SLIDESHOW - AUTO SLIDE
function autoSlide(){
	var activeSlide = $('ul#slideshow li.active');
	var nextSlide = activeSlide.next();
	if(nextSlide.length == 0) nextSlide = activeSlide.parent().children(":first");
	closeSlide();
	openSlide(nextSlide.children("a"));
}
// SLIDESHOW - OPEN SLIDE
function openSlide(slide){
	var newSlide = slide.parent();
	movePicture("show",slide);
	newSlide.addClass("active");
	//alert(newSlide.html());
	newSlide.animate({
		width:'394px'
		//width:'400px'
	},
	{
		queue:false,
		duration:slideTime,
		easing:easeType,
		complete:function(){
			//newSlide.children('span.width').text(newSlide.width());
			slideBusy = 0;
			clearInterval(slideInterval);
			slideInterval = setInterval('autoSlide();',5000);
		}
	});
}
// SLIDESHOW - CLOSE SLIDE
function closeSlide(){
	var activeSlide = $('ul#slideshow li.active');
	movePicture("hide",activeSlide);
	activeSlide.removeClass("active");
	activeSlide.animate({
		width:'59px'
	},
	{
		queue:false,
		duration:slideTime,
		easing:easeType,
		complete:function(){
			//activeSlide.children('span.width').text(activeSlide.width());
		}
	});
} 
// SLIDESHOW - MOVE PICTURE
function movePicture(type,slide){
	if(type=="show"){
		slide.children("img").animate({
			left:'0px'
		},
		{
			queue:false,
			duration:slideTime,
			easing:easeType,
			complete:function(){
				//var position = slide.position();
				//slide.children('span.pos').text(position.left);
			}
		});
	}else{
		var leftPos = slide.children("a").children("img").attr("left");
		slide.children("a").children("img").animate({
			left:leftPos+'px'
		},
		{
			queue:false,
			duration:slideTime,
			easing:easeType,
			complete:function(){ 
				//var position = slide.position();
				//slide.children('span.pos').text(position.left);
			}
		});
	}
}


function initialize_welink(){
	$(".defaultFocus").focus( function(){ 
		$(this).attr('default_focus', $(this).val());
		$(this).addClass('active');
		$(this).val('');
	});
	
	$(".defaultFocus").blur( function() { 
		if($(this).val() == ""){
			$(this).val($(this).attr('default_focus'));
			if($(this).val() == $(this).attr('default')){
				$(this).removeClass('active');
			}
		}
	} );

	$("form.checking").each(function(){
		$(this).submit(function(e){ 
			return checkForm(e,$(this).attr("id"));
		});
	});
	
	$(".datePicker").datepicker({dateFormat:"dd/mm/yy"});
	
	$(".btnQuery").button();
	
	$(".tabs").tabs();
	
	$('.nyroModal').nyroModal(); 
	
	$(".jPrompt").click(function(e){ 
		var el = $(this);
		jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {
			if(r){
				if(el.hasClass("nyroButton")){
						el.nyroModal().nmCall();
				}else{
					window.location = el.attr("href");
				}
			}
		});
		return false;	
	});
}



function checkForm(e,id){
	var error=0;
	$("label.required").next().removeClass("errorField"); 
	
	$("form#"+id+" label.required").each(function(){
	
	
		if($(this).next().prop('nodeName') == "INPUT"){
			// IL SAGIT DUN INPUT
			if($(this).next().attr('type') == "checkbox"){
				// IL S'AGIT D'UNE CHECKBOX QUI DOIT ETRE CHECKED
				if(!$(this).next().is(':checked')){
					error=1;
					$(this).next().addClass("errorField");
				}
			}else if($(this).next().attr('type') == "text"){
				// IL S'AGIT D'UN INPUT TEXT
				var value = $(this).next().val();
			
				if($(this).next().hasClass("phone")){
					// LA VALUE DOIT CORRESPONDRE A UN NUMERO DE TEL
					if(value=='' || value=='+32'){
						error=1;
						$(this).next().addClass("errorField");
					}
				}else if($(this).next().hasClass("email")){
					// LA VALUE DOIT CORRESPONDRE A UN EMAIL
					var reg_mail = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
					if(!(reg_mail.exec(value)!=null)){
						error = 2;
						$(this).next().addClass("errorField");
					}
				}else{
					// LA VALUE DOIT ETRE DIFFERENT DE VIDE
					if(value=='' || value == $(this).next().attr("default_value")){
						error=1;
						$(this).next().addClass("errorField");
					}
				}
				
			}
		
		}else if($(this).next().prop('nodeName') == "TEXTAREA"){
			
			if($(this).next().val() == ''){
				error=1;
				$(this).next().addClass("errorField");
			}
			
		}
		
	});
	
	
	
	if($("form#"+id+" label.exception").next().val() != '' && $("form#"+id+" label.exception").next().val() != null){
		error = 0;
		$("label.required").next().removeClass("errorField"); 
	}
	
	
	if(error>0){
		switch(error){
			case 1: jAlert("Un ou plusieurs champs sont incomplets !","Échec lors de la validation du formulaire"); break;
			case 2: jAlert("Un ou plusieurs champs sont incorrects !","Échec lors de la validation du formulaire"); break;
			default: jAlert("Veuillez vérifier le formulaire.","Échec lors de la validation du formulaire"); break;
		}
		
		return false;
	}else{ 
		
		if ($('form#'+id).hasClass("nyroForm")){
			e.preventDefault();
			//alert('nyro !');
			$("form#"+id).nyroModal().nmCall();
			//$("form#"+id).nyroModal();
			return false;
		}else{

			return true; 
		}
	}
}

