var t_select_open = new Array();



function initpopup2(){

	$('.checkbox_div').click( function () { 
        var _el_check;            
        if ($(this).find('.checkboxActiv:visible').length != 1) {
            $(this).find('.checkboxActiv').fadeIn(400);            
            _el_check = true;
			$(this).parent().find('.checkboxTxt_p').css('color', '#ff4f00');
        }
        else {
            $(this).find('.checkboxActiv').fadeOut(400);
            _el_check = false;
			$(this).parent().find('.checkboxTxt_p').css('color', '#8d795a');
        }
        $(this).find('input.checkbox_ipt').attr("checked", _el_check);
    });
	$('.checkboxTxt_p').click( function () {
        var _el_check;            
        if ($(this).parent().children('.checkbox_div').find('.checkboxActiv:visible').length != 1) {
            $(this).parent().children('.checkbox_div').find('.checkboxActiv').fadeIn(400);            
            _el_check = true;
			$(this).parent().find('.checkboxTxt_p').css('color', '#ff4f00');
        }
        else {
            $(this).parent().children('.checkbox_div').find('.checkboxActiv').fadeOut(400);
            _el_check = false;
			$(this).parent().find('.checkboxTxt_p').css('color', '#8d795a');
        }
        $(this).parent().children('.checkbox_div').find('.checkbox_ipt').attr("checked", _el_check);
    });
}

$(function() {
	
	//--------------------------------------------------------------	   
	// CHAMP INPUT ANIMATION
	//--------------------------------------------------------------
	$.fn.formLabels({
    	//labelParent : '#connexion'
	});	
		   
	// BLOC LOGIN
	$('#open_login').click(function () {
		
		if($(this).parent().css('top') == '-286px') {
			$(this).parent().animate({top: '-110px'}, {queue: false, duration: 950, easing: 'easeInOutBack'});
		}else{
			$(this).parent().animate({top: '-286px'}, {queue: false, duration: 600, easing: 'easeInOutBack'});
		}
		
	});
	//--------------------------------------------------------------
	//--------------------------------------------------------------
	
	
	//--------------------------------------------------------------
	// ONGLETS RECHERCHE
	//--------------------------------------------------------------
	$('#recherche .onglet').click(function () {
		var this_el = $(this);
		$('#recherche .onglet').animate({opacity: '0.5'}, {queue: false, duration: 200, complete: function () {
				this_el.animate({opacity: '1'}, {queue: false, duration: 550});																							 
			}		
		});	
	});
	//--------------------------------------------------------------
	//--------------------------------------------------------------
	
	
	//--------------------------------------------------------------
	// LISTE DE COURSES
	//--------------------------------------------------------------
	$('#tpl_listeCourses').css('display', 'none');
	$('#tpl_listeCourses_open').click(function () {
		if($('#tpl_listeCourses').css('display') == 'none') {
			$(this).css('background-position', '0 -66px');
			$('#tpl_listeCourses').slideDown(1200, 'easeInOutCirc');
		}else{			
			$('#tpl_listeCourses').slideUp({duration:1200, easing: 'easeInOutCirc', complete: function () {
					$('#tpl_listeCourses_open').css('background-position', '0 0px');															
				}
			});
		}
		return false;
	});
	//--------------------------------------------------------------
	//--------------------------------------------------------------
	
	
	//--------------------------------------------------------------
	// SLIDER HOME
	//--------------------------------------------------------------
	$('#slider').galleryView({
		panel_width: 666,
		panel_height: 320,
		transition_speed: 1500,
		transition_interval: 5000,
		border: 'none',
		pause_on_hover: false,
		fade_panels: true
	});
	//--------------------------------------------------------------
	//--------------------------------------------------------------

//  Index > Home Slider	
//gli 27/1/2012 duplication du code de bcc.alpha
	
	 $('#slider_home').nivoSlider({ 
		effect: 'fade',
	 	pauseTime:5000, 
		pauseOnHover:true,
       directionNav: false,
       directionNavHide: false,
		controlNav:true,
	 	controlNavThumbsFromRel:false
	});
	
	//--------------------------------------------------------------
	// SLIDE BAR
	//--------------------------------------------------------------
	$("#slider_distance, #slider_temps, #slider_commercants").slider({
		range: true,
		min: 0,
		max: 6,
		step: 1,
		values: [0, 6]
	});	
	$("#slider_vehicule, #slider_bio").slider({
		range: 'min',
		value: 3,
		min: 0,
		max: 3,
		step: 1
	});
	//--------------------------------------------------------------
	//--------------------------------------------------------------
	
	
	//--------------------------------------------------------------
	// CHECKBOX
	//--------------------------------------------------------------
	$('.checkbox').click( function () { 
        var _el_check;            
        if ($(this).find('.checkboxActiv:visible').length != 1) {
            $(this).find('.checkboxActiv').fadeIn(650);            
            _el_check = true;
			$(this).parent().find('.checkboxTxt').css('color', '#ff4f00');
        }
        else {
            $(this).find('.checkboxActiv').fadeOut(650);
            _el_check = false;
			$(this).parent().find('.checkboxTxt').css('color', '#8d795a');
        }
        $(this).find('.checkbox').attr("checked", _el_check);
        
        // validate the enclosing form
        $(this).closest('form').validate().element($(this).find('input.checkbox'));
    });
	$('.checkboxTxt').click( function () {
        var _el_check;            
        if ($(this).parent().children('.checkbox').find('.checkboxActiv:visible').length != 1) {
            $(this).parent().children('.checkbox').find('.checkboxActiv').fadeIn(650);            
            _el_check = true;
			$(this).parent().find('.checkboxTxt').css('color', '#ff4f00');
        }
        else {
            $(this).parent().children('.checkbox').find('.checkboxActiv').fadeOut(650);
            _el_check = false;
			$(this).parent().find('.checkboxTxt').css('color', '#8d795a');
        }
        $(this).parent().children('.checkbox').find('.checkbox').attr("checked", _el_check);
        
        // validate the enclosing form
        $(this).closest('form').validate().element($(this).parent().find('input.checkbox'));
    });
	//--------------------------------------------------------------
	//--------------------------------------------------------------	
	
	
	//--------------------------------------------------------------
	// RADIO
	//--------------------------------------------------------------
	$('div.radio').click( function () {
        var _el_check = $(this).find('input').attr('name');
		/* R�initialiste tous les radio du m�me nom */
		$('div.radio > input[name="'+_el_check+'"]').attr('checked', false);
		$('div.radio > input[name="'+_el_check+'"]').parent().find('.radioActiv').css('display', 'none');
		$('div.radio > input[name="'+_el_check+'"]').parent().next('.radioTxt').css('color', '#8d795a');
		
		/* Active le champ radio selectionn� */
		$(this).find('.radioActiv').fadeIn(650);
		$(this).next('.radioTxt').css('color', '#ff4f00');
		$(this).find('input[name="'+_el_check+'"]').attr('checked', true);
        
        // validate the enclosing form
        $(this).closest('form').validate().element($(this).find('input.radio'));
    });
	
	$('.radioTxt').click( function () {
        var _el_check = $(this).prev('div.radio').find('input').attr('name');
		/* R�initialiste tous les radio du m�me nom */
		$('div.radio > input[name="'+_el_check+'"]').attr('checked', false);
		$('div.radio > input[name="'+_el_check+'"]').parent().find('.radioActiv').css('display', 'none');
		$('div.radio > input[name="'+_el_check+'"]').parent().next('.radioTxt').css('color', '#8d795a');
		
		/* Active le champ radio selectionn� */
		$(this).prev('div.radio').find('.radioActiv').fadeIn(650);
		$(this).css('color', '#ff4f00');
		$(this).prev('div.radio').find('input[name="'+_el_check+'"]').attr('checked', true);
        
        // validate the enclosing form
        $(this).closest('form').validate().element($(this).parent().find('input.radio'));
    });
    //--------------------------------------------------------------
	//--------------------------------------------------------------
	
	
	//--------------------------------------------------------------
	// SELECT CUSTOM
	//--------------------------------------------------------------
	/*$('body').click(function () {
		$('select').hide();
	});*/
	
	 /*&& $.browser.version.substring(0, 1) == '7' */
	if(!($.browser.msie)) {
		$('.select_bg').click(function(){ 
			//ajout l id select dans un tableau 
			
			selectCustom( $(this).parent().attr('id'), $(this)); 
		});
		$('.select_bg').each(function(){ choiceSelectCustom($(this).parent().attr('id')); });
	}
	
	//-----------------------
	//  Sp�cial IE7
	//-----------------------
	else {
		//  Correction liste
		$('.select_bg').each( function() {
			$(this).parent().find('select').css('background-color', '#ffffff');
			$(this).parent().removeAttr('class');
			$(this).remove();    
		});
	}
	
	
	
	
	$('.select_container').each(function(){ 
		t_select_open.push($(this).attr("id"))
	});
	
	
	$(document).bind("click",function(event) {
		
		
		var id_target = $(event.target).parent().attr("id");
		
		var t_temp = [];
		for(var i = 0 ; i < t_select_open.length ; i++){
			
			if(t_select_open[i] != id_target){
				$('#'+t_select_open[i]+' > select').hide();
				
			}
		}
		//t_select_open = t_temp;
		
	});
	
	
	
	function selectCustom(id, el)
	{
		$('.select_container').css('z-index', '1');
		el.parent().css('z-index', '11');
		$('#'+id+' > select > option').click(function(){
			choiceSelectCustom(id)
			$('#'+id+' > select').hide();
			
		});
		var optionNumber = $('#'+id+' > select > option').length;
		if( $('#'+id+' > select:hidden').length > 0){
			if( optionNumber == 1 ){
				/*$('#'+id+' > select').attr('size', 1).show();*/
			}else if( optionNumber < 12 ){
				$('#'+id+' > select').attr('size', optionNumber).show();
			}else{
				$('#'+id+' > select').attr('size', 12).show();
			}
			$('#'+id+' > select').focus();
		}else{
			$('#'+id+' > select').hide();
		}
	}
	
	function choiceSelectCustom(id)
	{
		var selectValue = $('#'+id+' > select > option:selected').text();
		$('#'+id+' .select_bg').text(selectValue);
		//$('.'+className+' > select').hide();
	}
	//--------------------------------------------------------------
	//--------------------------------------------------------------	
	
	
	//--------------------------------------------------------------
	// SOUS-MENU
	//--------------------------------------------------------------
	$('#menu .subMenuOpen').hover(function(){		
		var padding_a_top = parseInt($(this).find('.subMenu a').css('padding-top').replace("px", ""));
		var padding_a_bottom = parseInt($(this).find('.subMenu a').css('padding-bottom').replace("px", ""));
		var height_a = $(this).find('.subMenu a').height()+padding_a_top+padding_a_bottom;
		var _height = $(this).find('.subMenu > *').length * height_a;
		//var _width = $(this).find('.menu').width();
		//$('.subMenu').css('width', _width+'px');
		$(this).find('.subMenu').stop().animate({height: _height+'px'}, {queue:false, duration:600});
	}, function(){
		$(this).find('.subMenu').stop().animate({height: '0px'}, {queue:false, duration:270});
	});
	
	$('.subMenuOpen .subMenu a').hover(function(){
		$(this).stop().animate({paddingLeft: '20px'}, {queue:false, duration:150});
	}, function(){
		$(this).stop().animate({paddingLeft: '10px'}, {queue:false, duration:450});
	});
	//--------------------------------------------------------------
	//--------------------------------------------------------------
	
	
	//--------------------------------------------------------------
	// MENU CONTACT FOOTER
	//--------------------------------------------------------------
	/*$('#footer .contact').click(function () {
	
		if($(this).find('.contact_select').length) {
			$(this).find('.contact_select').fadeIn(250);
			setTimeout(function close_contact() {
				$('#footer .contact').find('.contact_select').fadeOut(500);
			},6000);
		}else{
			$(this).find('.contact_select').fadeOut(500);
		}
		
	});*/
	
	/*var _timer;
	$('#capsuleContent').hover(function(){;
		clearTimeout(_timer);
		$('#capsuleOver').stop().animate({height: '36px', paddingTop: '15px'}, {queue: false, duration: 400});
    },
    function(){		
		_timer = setTimeout("$('#capsuleOver').stop().animate({height: '0px', paddingTop: '0px'}, {queue: false, duration: 250});",500);	
    });*/
	
	var _timer;
	$('#footer .contact').hover(function(){	
		clearTimeout(_timer);		
		$(this).find('.contact_select').fadeIn(250);				
	}, function(){
		_timer = setTimeout(function showContactFooter () {
			$('#footer .contact').find('.contact_select').fadeOut(500);
		},700);
	});
	//--------------------------------------------------------------
	//--------------------------------------------------------------	
	
});


var isAncestor = function(parent,child) {
	var walker = child;
	while (walker != null && walker != document.body) {
		walker = walker.parentNode;
		if (parent == walker) return true;
	}
	return false;
};

function setHeaderConnectedModule (username) 
{
	$('div#header div#login').empty();
	$('div#header div#login').append('<div id="head_connexion_co"><span id="username"></span><a id="disconnect" href="#"><span class="head_btn" title="Se déconnecter">Déconnexion</span></a></div>');

	$('div#header div#login div#head_connexion_co span#username').append(username);
	
	$("div#header div#login div#head_connexion_co a#disconnect").click(function() {
		$.ajax({
			type: "GET",
			url: "ws/deconnexion.php",
			success: function(retour){
				eval(retour);
			},
			error: function (data, status, e){
				alert(status+e);
			}
		});
		return false;
	});
};
function setHeaderLoginModule () 
{
	$('div#header div#login').empty();
	$('div#header div#login').append('<span id="head_connexion" title="Se connecter / S\'inscrire">Connexion &amp; inscription</span>');
	$("div#header div#login #head_connexion").fancybox({
		'height': 340,
		'href':'connexion.php'
	});
};

$(document).ready(function(){  
	
	$("div#header div#login #head_connexion").fancybox({
		'height': 340,
		'href':'connexion.php'
	});

	$("div#header div#login div#head_connexion_co a#disconnect").click(function() {
		$.ajax({
			type: "GET",
			url: "ws/deconnexion.php",
			success: function(retour){
				eval(retour);
			},
			error: function (data, status, e){
				alert(status+e);
			}
		});
		return false;
	});
	$('#fancyboxNoActivation').fancybox({
		'height':340,
		'width':350,
		'href': 'messageNonActive.php'
	});
	$('#fancyboxFPasswd').fancybox({
		'height':340,
		'width':350,
		'href': 'formForgotPasswd.php'
	});
	$("div#header ul#menu a.orange").click(function() {
		$.ajax({
			type: "GET",
			url: "ws/private_access.php",
			success: function(retour){
				eval(retour);
			},
			error: function (data, status, e){
				alert(status+e);
			}
		});
		return false;
	});
});
