$(document).ready(function(){
	
	if($('.ngg-galleryoverview').length > 0){
		$('.ngg-galleryoverview').parent().children('h3').hide();
		$('.ngg-galleryoverview').prev('h3').show();
	}
	
	$('.single-entry a').each(function(){
		if($(this).html()=="Vidéo HP"){
			$(this).hide();
		}
	});
	
	if(getCookie('anim_status') != "true"){
		setTimeout(function(){
			hide_about(false);
		}, 4000);	
	}else{
		hide_about(true);
	}
	if($('#imgCaptcha').length > 0){
		template_dir = $('#template_directory').text();
	   	img = document.getElementById('imgCaptcha'); 
   		img.src = template_dir+'/create_image.php?' + Math.floor((new Date()).getTime() / 1000);
   		$('#imgCaptcha').click(function(){
		   	img = document.getElementById('imgCaptcha'); 
	   		img.src = template_dir+'/create_image.php?' + Math.floor((new Date()).getTime() / 1000);  			
   		});		
	}
	$('.thickbox').each(function(){
		if($(this).children('img').length > 0 && (!$(this).hasClass('gal_bas'))){
			$(this).attr('title', $(this).children('img').attr('alt'));
		}
	});
	
	$('#sendbutton').click(function(){
		validate_contact_form();
	});
	
	setTimeout(function(){
		if(!$('#iframed_banner').hasClass('closed')){
			hide_rsr_banner();
		}
	}, 4000);		
	
	$('#banner_rsr_button').click(function(){ 
		if($('#iframed_banner').hasClass('closed')){
			show_rsr_banner();
		}else{
			hide_rsr_banner();
		}
	});
	if(getCookie('banner_status') != 'true'){
		var expdate = new Date ();
		DateExpiration (expdate);
		expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); 
		FixCookie('banner_status', 'true', expdate);	
	}
	
	$('.wp-caption').each(function(){
		//alert($(this).children('p').text());
		caption = $(this).children('a').attr('title');
		$(this).children('p').text(caption);
		html = $(this).html();
		html = html.replace('“]', '');
		$(this).html(html);
	});
	
	if($('.cat-item').length > 5){
		select = '<select onchange="document.location.href=this.options[this.selectedIndex].value;" name="archive-dropdown">';
		select += '<option value="">Choisir une catégorie</option>';
		first = true;
		$('.cat-item').each(function(){
			if(first){
				first = false;
				$(this).parent().addClass('to_delete');
				$(this).parent().parent().addClass('sidebar_cat_div');
			}
			select += '<option value="'+$(this).children('a').attr('href')+'">'+$(this).children('a').text()+'</option>';
			$(this).remove();
		});
		
		select += '</select>';
		$('.to_delete').remove();
		$('.sidebar_cat_div').append(select);
		/*
		html = parent.html();
		html += select;
		parent.html(html);
		*/
	}
	
}); 
function show_rsr_banner(){
	$('#iframed_banner').slideDown('slow', function(){
		$('#iframed_banner').removeClass('closed');
		button_src = $('#banner_rsr_button').children('img').attr('src');
		new_button_src = button_src.replace("close", "open");
		$('#banner_rsr_button').children('img').attr('src', new_button_src);
	});
}
function hide_rsr_banner(){
	$('#iframed_banner').slideUp('slow', function(){
		$('#iframed_banner').addClass('closed');
		button_src = $('#banner_rsr_button').children('img').attr('src');
		new_button_src = button_src.replace("open", "close");
		$('#banner_rsr_button').children('img').attr('src', new_button_src);
	});
}
function validate_contact_form(){
		captcha = trim($('#txtcaptcha').val());
		data_string = "captcha="+captcha;
		template_dir = $('#template_directory').text();
		$.ajax({
		   type: 'POST',
		   url: template_dir+'/validate_captcha.php',
		   data: data_string,
		   success: function(msg){
				error_text = "Veuillez vérifier les champs suivants: \n";
				error = false;
				fname = trim($('#cf_field_1').val());
				if(fname.length == 0){
					error_text += " -Le champs <Prénom> est vide \n";
					error = true;
				}else if(fname.length < 2){
					error_text += " -Le champs <Prénom> doit être composé d'au moins 2 caractères \n";
					error = true;
				}
				lname = trim($('#cf_field_2').val());
				if(lname.length == 0){
					error_text += " -Le champs <Nom> est vide \n";
					error = true;
				}else if(lname.length < 2){
					error_text += " -Le champs <Nom> doit être composé d'au moins 2 caractères \n";
					error = true;
				}
				email = trim($('#cf_field_5').val());
				if(!echeck(email)){
					error_text += " -Le champs <E-mail> est invalide \n";
					error = true;
				}
				mssge = trim($('#cf_field_8').val());
				if(mssge.length == 0){
					error_text += " -Le champs <message> est vide \n";
					error = true;
				}else if(mssge.length < 2){
					error_text += " -Le champs <message> doit être composé d'au moins 2 caractères \n";
					error = true;
				}
				
				if(msg != "ok"){
					error_text += " -Le code de sécurité saisi ne correspond à l'image \n";
					error = true;			
				   	img = document.getElementById('imgCaptcha'); 
			   		img.src = template_dir+'/create_image.php?' + Math.random();
			   		$('#imgCaptcha').click(function(){
					   	img = document.getElementById('imgCaptcha'); 
				   		img.src = template_dir+'/create_image.php?' + Math.random();   			
			   		});	
				}
		
				if(error){
					alert(error_text);
				}else{
					data_string = "fname="+escape(fname)+"&lname="+escape(lname)+"&adress="+escape($('#cf_field_3').val())+"&npa="+escape($('#cf_field_4').val())+"&email="+escape(email)+"&message="+escape(mssge);
					$.ajax({
					   type: 'POST',
					   url: template_dir+'/send_contact.php',
					   data: data_string,
					   success: function(msg){
					   	$('#sendbutton').fadeOut('fast', function(){
					   		$('#success').fadeIn('fast');
					   	});
					   }
					});
				}
		   }
		 }); 	 	
}
function hide_about(status){
	about_div_width = $('#home_box').width();
	$('.focused_divs').width(about_div_width);
	$('.focused_divs').css('float','left');
	//alert($('.focused_divs').width()+" "+about_div_width);
	$('.handle_about').unbind("click");
	if(!status){	
		$('#home_box').children('.idTabs').fadeOut(1000);
		visible_tab = "ninportkoi";
		if($('#a-propos').css('display')=='block'){
			visible_tab = "a-propos";
		}else if($('#contact').css('display')=='block'){
			visible_tab = "contact";
		}	
		//$('#home_box').css('z-index', 100);
		//$("#"+visible_tab).css('z-index', 50);
		if($("#"+visible_tab).length > 0){
			$("#"+visible_tab).hide("drop", { direction: "right" }, 1000, function(){
				setTimeout(function(){
					$("#home_box").animate({"width":"-="+about_div_width+""}, 400);
					$(".focused_divs").animate({"width":"+="+about_div_width+""}, 400);
					setTimeout(function(){
						$('.focus').hide();
						$('.hidden_focus').show();
						/*if($('#show_about').length == 0){
							//$('#evenement').prepend('<div id="show_about">about</div>');
						}else{
							$('#show_about').removeClass("shown");
							$('#show_about').addClass("hidden");
						}*/
						$('.handle_about').bind("click", function(){
							show_about(about_div_width);
						});
					}, 200);	
				}, 1);	
			});	
		}
		var expdate = new Date ();
		DateExpiration (expdate);
		expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); 
		FixCookie('anim_status', 'true', expdate);
	}else{
		$("#home_box").hide();
		$('#home_box').children('.idTabs').hide();
		$(".focused_divs").animate({"width":"+="+about_div_width+""}, 0.1);
		$('.focus').hide();
		$('.hidden_focus').show();	
		/*if($('#show_about').length == 0){
			//$('#evenement').prepend('<div id="show_about">about</div>');
			$('#show_about').addClass("hidden");
		}else{
			$('#show_about').removeClass("shown");
			$('#show_about').addClass("hidden");
		}*/
		$('.handle_about').bind("click", function(){
			show_about(about_div_width);
		});		
	}
}
function show_about(about_div_width){
	$('.handle_about').unbind("click");
	$("#a-propos").hide();
	$("#contact").hide();
	$("#home_box").width(0);
	$("#home_box").show();
	$(".focused_divs").animate({"width":"-="+about_div_width+""}, 400);
	//$('#home_box').css('z-index', 100);
	$("#home_box").animate({"width":"+="+about_div_width+""}, 400, function(){
		$("#a-propos").show("drop", { direction: "right" }, 1000, function(){
			if($('#a-propos').children('h2:eq(0)').children('.handle_about').length == 0){
				$('#a-propos').children('h2:eq(0)').prepend("<span class='handle_about'></span>");
			}
			if($('#contact').children('h2:eq(0)').children('.handle_about').length == 0){
				$('#contact').children('h2:eq(0)').prepend("<span class='handle_about'></span>");
			}
			//$('#show_about').removeClass("hidden");
			//$('#show_about').addClass("shown");
			$('.handle_about').bind("click", function(){
				hide_about(false);
			});			
		});
	});
	setTimeout(function(){
		$('#home_box').children('.idTabs').children('li').children('a:eq(0)').addClass('selected');
		$('#home_box').children('.idTabs').children('li').children('a:eq(1)').removeClass('selected');
		$('#home_box').children('.idTabs').fadeIn(500);
	}, 500);
	setTimeout(function(){
		$('.focus').show();
		$('.hidden_focus').hide();
	}, 200);	

}
function getCookie(nom) {
	deb = document.cookie.indexOf(nom + "=");
	if (deb >= 0) {
		deb += nom.length + 1;
		fin = document.cookie.indexOf(";",deb);
		if (fin < 0) fin = document.cookie.length;
		return unescape(document.cookie.substring(deb,fin));
	}
	return "";
}
function FixCookie(nom,valeur,expire,path,domaine,securise) {
      document.cookie = nom + "=" + escape (valeur) + ((expire) ? "; expires=" + expire.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domaine) ? "; domain=" + domaine : "") + ((securise) ? "; secure" : "");
}
function DateExpiration (DateExp) {

      var aujourdhui = new Date(0);
      var aujourdhuidate = aujourdhui.getTime();
      if (aujourdhuidate > 0)
            DateExp.setTime (DateExp.getTime() - aujourdhuidate);

}
function trim(sString) {
    while (sString.substring(0,1) == ' ' || sString.substring(0,1) == '\t' || sString.substring(0,1) == '\r' || sString.substring(0,1) == '\n')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ' || sString.substring(sString.length-1, sString.length) == '\t' || sString.substring(sString.length-1, sString.length) == '\r' || sString.substring(sString.length-1, sString.length) == '\n')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}
function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	}
	if (str.indexOf(" ")!=-1){
		return false;
	}
	return true;					
}
