/* Author: 
	Marc-Antoine Noreau-Marois
*/
$(document).ready(function() {
	details(700);
	carousels();
	menuPrincipal();
	maillist();
	accueil();
	creaPref();
	facebook();
	$("a.fancybox").fancybox({
		'speedIn'		:	200, 
		'speedOut'		:	200, 
		'overlayColor'	:	''
	});
	var ww = $(window).width();
	var fswf_widht = $(window).height()*1.2
	var fswf_height = fswf_widht*0.7
	$("a.fancyswf").fancybox({
		'speedIn'		:	200, 
		'speedOut'		:	200, 
		'width'			:   fswf_widht,
		'height'		:   fswf_height
	});
});

function facebook(){
	$('.partagerFacebook').click(function(e){
		e.preventDefault();
		window.open($(this).attr('href'), 'Partager', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=400');
	})
}

function menuPrincipal() {
	$("nav.sub-navigation").hide();
	$("nav.sub-navigation div.item").hide();
	$("nav.main-navigation a.item").click(function() {
		if ($(this).parent().hasClass("musee")) {
			$("nav.sub-navigation").show();
			$("nav.sub-navigation div.item").hide();
			$("nav.sub-navigation div.musee").show();
		} else if ($(this).parent().hasClass("expositions")) {
			$("nav.sub-navigation").show();
			$("nav.sub-navigation div.item").hide();
			$("nav.sub-navigation div.expositions").show();
		} else if ($(this).parent().hasClass("histoire")) {
			$("nav.sub-navigation").show();
			$("nav.sub-navigation div.item").hide();
			$("nav.sub-navigation div.histoire").show();
		} else if ($(this).parent().hasClass("activites")) {
			$("nav.sub-navigation").show();
			$("nav.sub-navigation div.item").hide();
			$("nav.sub-navigation div.activites").show();
		}
		return false;
	});
	
	if ($('html').hasClass('url-musee-de-charlevoix')) {
		$("nav.sub-navigation").show();
		$("nav.sub-navigation div.musee").show();
	}
	
	if ($('html').hasClass('url-expositions')) {
		$("nav.sub-navigation").show();
		$("nav.sub-navigation div.expositions").show();
	}
	
	if ($('html').hasClass('url-histoire-patrimoine')) {
		$("nav.sub-navigation").show();
		$("nav.sub-navigation div.histoire").show();
	}
	
	if ($('html').hasClass('url-education-activites')) {
		$("nav.sub-navigation").show();
		$("nav.sub-navigation div.activites").show();
	}
}

function carousels() {
	$('#carousel-coeur').bxSlider({
		displaySlideQty: 5,
		moveSlideQty: 1
	});
	
	$('#carousel-expositions').bxSlider({
		displaySlideQty: 3,
		moveSlideQty: 1
	});
}

function fermerDetails(vitesse) {
	$("div.bloc div.details").each(function(i, e) {
		if ($(this).hasClass("ouvert")) {
			$(this).animate({
				left: '+=366'
			}, vitesse, function() {
				$(this).removeClass("ouvert");
			});
		}
	});
}

function details(vitesse) {
	$("div.bloc .clickable").click(function() {
		fermerDetails(vitesse);
		$("div.bloc .clickable").show();
		$(this).hide();
		$(this).parent().find('div.details').show().animate({
			left: '-=366'
		}, vitesse, function() {
			$(this).addClass("ouvert");
		});
		return false;
	});
	
	$("div.bloc div.details p.fermer a").click(function() {
		$("div.bloc .clickable").show();
		$(this).parent().parent().animate({
			left: '+=366'
		}, vitesse, function() {
			$(this).removeClass("ouvert");
		});
		return false;
	});
}

// ############
//
// maillist
//
// ############
function maillist(){
	if($('html').attr('lang') == 'fr') {
		var my_url = '/fr/mail/enregistrer/';
		var valide = 'Inscription réussi!';
		var erreur = 'Courriel invalide';
	}
	else {
		var my_url = '/en/mail/enregistrer/';
		var valide = 'Sign it!';
		var erreur = 'Invalid e-mail';
	}
	var email = '';
	
	$('.infolettre input#courriel').focus(function(){
		if($('.infolettre input#courriel').val() == valide) {
			$('.infolettre input#courriel').val('');
			$('.infolettre input#courriel').css('color', 'black');
		}
		
		if($('.infolettre input#courriel').val() == erreur) {
			$('.infolettre input#courriel').val(email);
			$('.infolettre input#courriel').css('color', 'black');
		}
	})
	
	$('.infolettre input').keypress(function(e){
		if(e.which == 13) {
			$('.infolettre a').click();
		}
	})
	
	$('.infolettre a').click(function(e){
		e.preventDefault();
		if($('.infolettre input#courriel').val() != '' && (/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test($('.infolettre input#courriel').val()))) {
			my_url += $('.infolettre input#courriel').val();
			if($('.infolettre input#prenom').val() == '') {
				my_url += '/null';
			}
			else {
				my_url += '/'+$('.infolettre input#prenom').val();
			}
			
			if($('.infolettre input#nom').val() == '') {
				my_url += '/null';
			}
			else {
				my_url += '/'+$('.infolettre input#nom').val();
			}
			
			if($('.infolettre input[type=radio]:checked').length) {
				my_url += '/'+$('.infolettre input[type=radio]:checked').val();
			}
			else {
				my_url += '/null';
			}
			$.ajax({
			   url: my_url
			});
			$('.infolettre input[type=text]').val('');
			$('.infolettre input[type=radio]:checked').attr('checked', '');
			$('.infolettre input#courriel').val(valide);
			$('.infolettre input#courriel').css('color', 'green');
		}
		else {
			email = $('.infolettre input#courriel').val();
			$('.infolettre input#courriel').val(erreur);
			$('.infolettre input#courriel').css('color', 'red');
		}
	})
}

function accueil(){
	$('nav .mont').hide();
	$('nav .desc').parent().find('ul').css('position', 'relative');
	$('nav .desc').parent().find('ul').css('top', '0px');
	
	$('nav .desc').each(function(){
		if(parseInt($(this).parent().attr('max')) >= $(this).parent().find('ul li').length) {
			$(this).hide();
		}
	})
	
	$('.desc').click(function(e){
		e.preventDefault();
		var pos = $(this).parent().find('ul').css('top');
		pos = parseInt(pos.substr(0, pos.length-2));
		
		distance = pos-$(this).parent().find('ul li').height();
				
		if($(this).parent().find('ul li').length >= parseInt($(this).parent().attr('last'))+1) {
			$(this).parent().attr('last', parseInt($(this).parent().attr('last'))+1);
			$(this).parent().find('ul').css('top', distance+'px');
			$('nav .mont').show();
			if($(this).parent().find('ul li').length == parseInt($(this).parent().attr('last'))) {
				$('nav .desc').hide();
			}
		}
		
	})
	
	$('.mont').click(function(e){
		e.preventDefault();
		var pos = $(this).parent().find('ul').css('top');
		pos = parseInt(pos.substr(0, pos.length-2));
		
		distance = pos+$(this).parent().find('ul li').height();
				
		if(parseInt($(this).parent().attr('max')) <= parseInt($(this).parent().attr('last'))-1) {
			$(this).parent().attr('last', parseInt($(this).parent().attr('last'))-1);
			$(this).parent().find('ul').css('top', distance+'px');
			$('nav .desc').show();
			
			if(parseInt($(this).parent().attr('max')) == parseInt($(this).parent().attr('last'))){
				$('nav .mont').hide();
			}
		}
	})
}


function creaPref(){
	$('.crea-pref').click(function(e){
		e.preventDefault();
		var listPref=getCookie("listPref");
		if (listPref==null || listPref=="") {
			setCookie('listPref', '');
			listPref = '';
		}
  			listPref = listPref.split('<||>');
  			if($.inArray($(this).attr('href'), listPref) < 0){
  				$(this).parent().addClass('dmcdc');
  				$(this).find('.aimez-moi').attr('src', '/img/bouton-aimez-moi-pu.png');
  				$.ajax({
				  url: '/fr/objets/vote/1',
				  type: "POST",
	  			  data: {src : $(this).attr('href')},
				  success: function(e) {
				  }
				});
  				listPref.push($(this).attr('href'));
  				if($('#crea-list-items').length) {
  					var className = 'odd';
  					
  					if($('#crea-list-items img:last').hasClass('odd')) {
  						className = '';
  					}
  					
  					$('<img class="crea-cpc '+className+'" refWidth="'+$(this).attr('refWidth')+'" refHeight="'+$(this).attr('refHeight')+'" refSrc="'+$(this).attr('href')+'" src="'+$(this).find('img.crea-objet').attr('src')+'"/>').appendTo($('#crea-list-items'));
  					$( "#crea-list-items img" ).draggable({ appendTo: "body", helper: "clone" });
  				}	
  			}
  			else {
  				$(this).parent().removeClass('dmcdc');
  				$(this).find('.aimez-moi').attr('src', '/img/bouton-aimez-moi.png');
  				$.ajax({
				  url: '/fr/objets/vote/0',
				  type: "POST",
	  			  data: {src : $(this).attr('href')},
				  success: function(e) {
				  }
				});
  				listPref.splice($.inArray($(this).attr('href'), listPref), 1);
  				if($('#crea-list-items').length) {
  					$('#crea-list-items img[refSrc="'+$(this).attr('href')+'"]').remove();
  					$( "#crea-list-items img" ).removeClass('odd');
  					$( "#crea-list-items img:odd" ).addClass('odd');
  				}
  			}
  			listPref = listPref.join('<||>');
  			setCookie('listPref', listPref);		
	})
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+(exdate.toUTCString())) +"; path=/";
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}
