var $j = jQuery.noConflict();
var calEvents 		= null;
var listDateEvents 	= '';
var urlEvents 		= {};	

$j(document).ready(function(){
	
	/**
	 * MENU PRINCIPAL
	 */
	$j('ul.sf-menu').superfish({ 
    	delay:       600,                           
    	animation:   {opacity:'show',height:'show',width:'show'},
		speed:       'fast',
		autoArrows:  false,
		dropShadows: false
    });
	
	/**
	 * AGENDA
	 */
	 var calOptions = {
		isYearEditable:true
		
	};
	calEvents = new dhtmlxCalendarObject('dhtmlxCalendar', true, calOptions);
	calEvents.setSensitive(listDateEvents);
	calEvents.attachEvent("onClick",openEvent);
	calEvents.loadUserLanguage('fr');
	calEvents.draw();
	
	/**
	 * ZOOM
	 */
	$j('.lightBox').lightBox({fixedNavigation:true});	
	
	/**
	 * VIDAGE AUTOMATIQUE DES CHAMPS
	 */
	$j('.deleteDefaultValueOnFocus').deleteDefaultValueOnFocus();
	
	/**
	 * roll over des images
	 */
	$j('.rollOver').rollOver();
	
	/**
	 * Init du slider de banniere
	 */
	$j('.container_banner_cycle').cycle({
		delay: 400
	});
	
	/**
	 * Init du slider de banniere
	 */
	$j('.container_advert_col_right').cycle({
		delay: 10000
	});
	
	/**
	 * Init slider fiche article
	 */
	initSliderSlugArticle();
	
	/**
	 * Init des formulaire de commentaire
	 */
	initFormCommentSlug();
	
	/**
	 * Accordeons
	 */
	 initAccordion('.accordionContainer', '.toggle', 0);
	
	
	/**
	 * Init formulaire thematique guide pratiques
	 */
	$j('#select_thematic_pratical_guide').change(function(){
		
		hideAllSelectSubThematic();
		if($j('#select_thematic_pratical_guide').val() != 0){
			showSelectSubThematic($j('#select_thematic_pratical_guide').val());
		}
		
		$j.ajax({
			url: '/consultation/pratical-guide-ajax',
			dataType: 'html',
			data: {'thematic_id' : $j('#select_thematic_pratical_guide').val()},
			contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
			beforeSend: function(){
				$j('#container_list_pratical_guide').html('<div class="ajax_loading"></div>');
			},
			success: function (data, textStatus, XMLHttpRequest){
				$j('#container_list_pratical_guide').html(data);
			}
		});
	});
		
	$j('.field_sous_themes_guides_pratiques').change(function(){
		
		$j.ajax({
			url: '/consultation/pratical-guide-ajax',
			dataType: 'html',
			data: {	'thematic_id' : $j('#select_thematic_pratical_guide').val(),
					'sub_thematic_id' : $j('#select_sub_thematic_pratical_guide').val() },
			contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
			beforeSend: function(){
				$j('#container_list_pratical_guide').html('<div class="ajax_loading"></div>');
			},
			success: function (data, textStatus, XMLHttpRequest){
				$j('#container_list_pratical_guide').html(data);
			}
		});
	});
	
	/**
	*Webcame aeroport
	*/
	$j(function(){
		if(document.getElementById('webcam1')){
			changeImage('webcam1','/images/upload/aerodrome-webcam-1.jpg');	
		}
		if(document.getElementById('webcam2')){
			changeImage('webcam2','/images/upload/aerodrome-webcam-2.jpg');	
		}
	});
   
});


/**
 * Affiche un événement
 */
function openEvent(date){
	var day 	= calEvents.getFormatedDate("%d", date);
	var month 	= calEvents.getFormatedDate("%m", date);
	var year 	= calEvents.getFormatedDate("%Y", date);
	var eventId = year + '_' + month + '_' + day;
	if(urlEvents[eventId]){
		document.location.href = urlEvents[eventId];
	}
}

/**
 * Creation d'un accordeon
 */
function initAccordion(accordionClass, headerClass, activeClass){
	$j( accordionClass ).accordion({
		header: headerClass, 
		fillSpace: false, 
		autoheight: false, 
		active: activeClass
	});
}

/**
 * Initialise le composant slider de la fiche d'un article
 */
function initSliderSlugArticle(){
	
	if($j('.contener_photo_diapo')){
		
		/* Slider */
		$j('.contener_photo_diapo').cycle({
			fx:     'scrollHorz',
			next:   '#btn_diapo_next',
			prev:   '#btn_diapo_prev'
		});
	}
}

/**
 * Initialise le formulaire de commentaire
 */
function initFormCommentSlug(){
	if($j('#f_comment')){
		$j('#f_comment').submit(function(){
			$j.ajax({
				type: 'POST',
				url: '/comment/submit',
				data: $j("#f_comment").serialize(),			  
				dataType: 'json',
				beforeSend: function(){
					$j('#f_comment .contener_f_comment_error').hide();
					$j('#f_comment .contener_f_comment_loading').show();
					$j('#f_comment input').removeClass('field_error');
					$j('#f_comment textarea').removeClass('field_error');
				},
				success: function (data, textStatus, XMLHttpRequest){
					$j('#f_comment .contener_f_comment_loading').hide();
					if(!data.formSuccess){
						$j('#f_comment .contener_f_comment_error').show();
						for(var i=0;i<data.errorFields.length;i++){
							$j('#'+data.errorFields[i]).addClass('field_error');
						}
					}else{
						$j('#f_comment').hide();
						$j('#container_form_comment_success').show('slow');
					}
				}
			});
		});
	}
}

/**
 * Participation sondage
 */
function doPostPollReply(){
	
	if($j("input[name=poll_participation_poll_reply_id]:checked").length>0){
		$j.ajax({
			type: 'POST',
			url: '/poll/submit',
			data: $j("#f_poll").serialize(),			  
			dataType: 'json',
			beforeSend: function(){
				$j('#f_poll .contener_f_poll_error').hide();
				$j('#f_poll .contener_f_poll_loading').show();
			},
			success: function (data, textStatus, XMLHttpRequest){
				$j('#f_poll .contener_f_poll_loading').hide();
				if(!data.formSuccess){
					$j('#f_poll .contener_f_poll_error').show();
				}else{
					$j('#f_poll').hide();
					/* Chargement des résultats */
					$j.ajax({
						type: 'GET',
						url: '/poll/result',
						dataType: 'html',
						data:{'poll_participation_poll_reply_id':$j("input[name=poll_participation_poll_reply_id]:checked").val()},
						beforeSend: function(){							
							$j('#container_f_poll_success').show('slow');
							$j('#container_f_poll_success').html('<div class="ajax_loading"></div>');
						},
						success: function (data, textStatus, XMLHttpRequest){
							$j('#container_f_poll_success').html(data);
						}
					});
				}
			}
		});
	}else{
		$j('#f_poll .contener_f_poll_error').show();
	}
}

/**
 * DESCINSRIPTION NEWSLETTER
 */
function doActionNewsletter(){
	
		if($j('#f_newsletter input[name=email]').val() != '' && $j('#f_newsletter input[name=email]').val() != 'Votre email ICI'){
			
			$j.ajax({
				type: 'POST',
				url:'/cgi-bin2/weblist_ml_subscribe.pl',
				data: $j("#f_newsletter").serialize(),			  
				dataType: 'html',
				success: function (data, textStatus, XMLHttpRequest){

					/* Selection du type de dialog a afficher*/
					var dialogBoxId = '';
					if($j('#f_newsletter input[name=ac]:checked').val() == 'unsubscribe'){
						dialogBoxId = 'dialog_newsletter_unsub';
					}else{
						dialogBoxId = 'dialog_newsletter_sub';
					}
					/* Affichage de la confirmation */
					$j('#'+dialogBoxId).dialog({
						resizable:false,
						draggable:false,
						title:$j('#'+dialogBoxId+' .title').html(),
						modal:true,
						buttons:{
							"Ok": function() { $j(this).dialog("close"); }
						}
					});
				}
			});
			
		}else{
			
			/* Affichage d'un dialog d'erreur */
			$j('#dialog_newsletter_error').dialog({
				resizable:false,
				draggable:false,
				title:$j('#dialog_newsletter_error .title').html(),
				modal:true,
				buttons:{
					"Ok": function() { $j(this).dialog("close"); }
				}
			});
		}
		
}

function displayNewBac(){
	document.getElementById('block_new_bac').style.visibility = 'visible';	
	document.getElementById('block_new_bac').style.display 	  = 'inline';
	document.getElementById('block_repair_bac').style.visibility = 'hidden';	
	document.getElementById('block_repair_bac').style.display 	 = 'none';	
}

function displayRepairBac(){
	document.getElementById('block_repair_bac').style.visibility = 'visible';	
	document.getElementById('block_repair_bac').style.display 	 = 'inline';
	document.getElementById('block_new_bac').style.visibility = 'hidden';	
	document.getElementById('block_new_bac').style.display 	  = 'none';	
}

function hiddeAllBac(){
	document.getElementById('block_new_bac').style.visibility = 'hidden';	
	document.getElementById('block_new_bac').style.display 	  = 'none';
	document.getElementById('block_repair_bac').style.visibility = 'hidden';	
	document.getElementById('block_repair_bac').style.display 	 = 'none';
}

function displayBlockBac(val){
	if(val == 'nouveau_bac'){
		displayNewBac();
	}
	else if(val == 'reparation_bac'){
		displayRepairBac();
	}
	else{
		hiddeAllBac();
	}
}

/**
* Images Webcam
*/

function changeImage(imgId, imgName){
	document.getElementById(imgId).src = imgName;
	window.setTimeout("changeImage('" + imgId+ "','" + imgName + "')",6000);
}

/**
*	Affiche/masque les listes déroulantes de sous-thématiques des publications
*/
function hideAllSelectSubThematic(){
	document.getElementById('select_sub_thematic_pratical_guide').value = '';
	var tmp = document.getElementById('listIdThematic').value.split('__');
	var i = 0;
	while (i < tmp.length){
		if(tmp[i]!=''){
			if(document.getElementById('listSubThematic_' + tmp[i])){
				document.getElementById('listSubThematic_' + tmp[i]).value ='';
				document.getElementById('listSubThematic_' + tmp[i]).style.visibility ='hidden';
				document.getElementById('listSubThematic_' + tmp[i]).style.display ='none';
			}
		}
		i=i+1;	
	}
}

function showSelectSubThematic(num){

	if(document.getElementById('listSubThematic_' + num)){
		document.getElementById('listSubThematic_' + num).style.visibility	='visible';
		document.getElementById('listSubThematic_' + num).style.display	='inline';
	}
}
