// JavaScript Document
$(document).ready(function(){

	//main dropdown menu
	$('.topLi').hoverIntent(
		function(){
			$(this).children('.subMenu').stop(true, true).slideDown('fast');
			$('.innerContent, .homeContent').css('z-index','-10');
		},function(){
			$(this).children('.subMenu').stop(true, true).slideUp('fast');
			$('.innerContent, .homeContent').css('z-index','0');
	});
	
	//Connexion block in footer
	$('.connectOpen').click(function(event){
		event.preventDefault();
		$('.connexionBox').fadeToggle();
	})

	//Billboard setup
	if($('.billboard img').length > 1){
		$('.billboard img').each(function(index){
			$('<div class="button alive">' + (index + 1) + '</div>').appendTo('.billboard .controls');
		})
		$('.billboard img:not(:first)').hide();
		$('.billboard img:first').addClass('active');
		$('.billboard .button:first').toggleClass('alive dead');
		$('.billboard .button').click(function(){
			billboardClick($(this), $(this).index());
		});
	}

	//default textfield value
	$('input:text.jqDefault').add('#fieldformBuilderFieldcourriel').each(function(){
		$(this).data('defaultValue', $(this).val());
	}).focus(function(){
		if ($(this).val() == $(this).data('defaultValue')){
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == ''){
			$(this).val($(this).data('defaultValue'));
		}
	})
	
	//newsletter submit
	$('#submitNewsletter').click(function(event){
		event.preventDefault();
		$('#formBuilderFieldenvoyer input:submit').click();
	})
	if($('#fieldformBuilderFieldcourriel').length > 0){
		$('#submitNewsletter').show();
	}

	//comment form slide
	$('.commentAction a:first').click(function(event){
		event.preventDefault();
		$('.formBox').slideToggle();
	})

	//event list slide
	$('.eventContentWrap').hide();
	$('.event h3 a').click(function(event){
		event.preventDefault();
		$(this).find('span').toggleClass('plus minus');
		$(this).parents('.event').find('.eventContentWrap').slideToggle();
	})
	
	//list open close slide
	$('.openCloseBlock .openCloseContent').hide();
	$('.openCloseTitle').click(function(){
		$(this).children('span').toggleClass('minus close')
		$(this).next('.openCloseContent').slideToggle();
	})
	
	//fix share margin on frontpage
	$('#shareCol ul li:last').addClass('last');
	
	//event page test
	eventPage();
	
})

//comment validation
function validateComment(){
	if($('#author').val() == 'Nom (obligatoire)'){
		$('.errorName').slideDown();
		return false;
	}else{
		$('.errorName').slideUp();
	}
	if($('#email').val() == "Adresse courriel (obligatoire, n'apparaîtra pas)"){
		$('.errorEmail').slideDown();
		return false;
	}else{
		$('.errorEmail').slideUp();
	}
	$('#submit').click();
}

//Billboard Functions
var billboardInterval = setInterval('billboardTimer()', 8000);
function billboardTimer(){
	var newButton = $('.billboard .dead').next('.alive').length == 0 ? $('.billboard .alive:first') : $('.billboard .dead').next('.alive');
	var newImg = $('.billboard img.active').parent('a').next('a').length == 0 ? $('.billboard img:first') : $('.billboard img.active').parent('a').next('a').children('img');
	billboardSwap(newButton, newImg);
}
function billboardClick(caller, callerIndex){
	if(caller.hasClass('alive') && $('.billboard img:animated').length == 0){
		clearInterval(billboardInterval);
		var newImg = $('.billboard img').eq(callerIndex);
		billboardSwap(caller, newImg);
	}
}
function billboardSwap(newButton, newImg){
	$('.billboard .dead').add(newButton).toggleClass('alive dead');
	$('.billboard img.active').fadeOut(500, function(){
		newImg.fadeIn(500);
		$('.billboard img.active').add(newImg).toggleClass('active');
	});
}

//URL params to JS variable
function getUrlVars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function eventPage(){
	if($('#eventPage').length > 0){
		var eventid = getUrlVars()["eventid"];
		$('#' + eventid + ' .eventContentWrap').show();
		$('#' + eventid + ' .plus').toggleClass('plus minus');
		if(eventid){
			window.location.hash = '#' + eventid + '';
		}
	}
}
