$(document).ready(function(){

	$.fn.extend({
		recherche_ana : function(m,et){
			titre = $(this).find("div").html();
			titre = titre.toLowerCase();
			m = m.toLowerCase();
			sp = m.split(" ");
			if(m=="")
			{
				
					$(this).removeClass("matched");
					$(this).addClass("matched");
			}
					//$(this).append(sp.length);
					//Salert(m);
			if(sp.length!=1  && sp.length !=0)
			{
				match = false;
						continu=true;
				for(i=0;i<sp.length;i++)
				{
					if(continu==true)
					{
						reg = new RegExp("("+sp[i]+")");
						if(titre.match(reg))
						{
							match = true;
							continu=true;
						}
						else
						{
							continu=true;
							if(et==true)
							{
								match = false;
								continu=false;
								
							}
						}
					}
				}
				if(match==true)
				{
					$(this).addClass("matched");
				}
				else
				{
					$(this).removeClass("matched");
				}
			}
			else
			{
			
				var reg1=new RegExp("("+m+")");
				if(titre.match(reg1))
				{
					$(this).addClass("matched");
				}	
				else
				{
					$(this).removeClass("matched");
				}
			}
		}
	});
		calculePages =  function(){ 
					
						nbImages = $("#ana_results a.matched").length;
						nbPages = Math.ceil((nbImages)/21);
						$("#ana_pages").html('');
						for(i=1;i<=nbPages;i++)
						{
							$("#ana_pages").append('<a href="javascript:void(0)" class="ana_page">'+i+'</a>');
						}
						//	$("#ana_pages a.ana_page").eq(0).addClass("selected");
						$("#ana_pages").append('<div style="clear:both;"></div>');
						$("#ana_pages a.ana_page").click(function(){
							num = $(this).html();
							$("a.ana_page.selected").removeClass("selected");
							$(this).addClass("selected");
							$("#ana_results a.newsboxes").css('display','none');
							$("#ana_results a.newsboxes.matched").slice(((num*21)-21),((num*21))).css('display','block');
						});
						$("#ana_results a.newsboxes.matched").each(function(){
							$(this).hover(function(){
								$(this).find("div").css({
									'position':'absolute',
									'left':'207px', 
									'top':'279px',
									'opacity':0.8,
									'font-weight':'bold',
									'text-decoration':'none',
									'background':'none',
									'color':'white',
									//'top':$(this).parent().css('top'),
									'width':'300px',
									'margin-left':'-150px',
									'z-index':'1000000',
									'font-size':'14px',
									'text-align':'left',
									'display':'block'
								});
							},function(){
								$(this).find("div").css({
									'display':'none'
								});
							});
						});
						$("#ana_pages a.ana_page").eq(0).click();
					} 
	$("#archivesNews a").click(function(){
	
		$("#speedArchives").slideUp('normal');
		// Chargement des archives du site ! Nom d'un ptit pgm de bonhomme @_o
		if(!$("#archivesNewsAffichage").is("#archivesNewsAffichage"))
		{
			$("body").append('<div id="archivesNewsAffichage"></div>');
			a=$("#archivesNewsAffichage");
			$(a).css({
				'position':'fixed',
				'left':'50%',
				'top':'50%',
				'width':'880px',
				'height':'400px',
				'margin-left':'-450px',
				'margin-top':'-200px',
				'background':'url(/img/archive_box_eversonic.png)',
				'display':'none',
				'z-index':'999999'
			});
			
			$(a).append('<div class="fallnew"></div>');
			$(a).find('div.fallnew').css({
				position : 'absolute',
				left : '723px',
				top : '5px',
				width : '139px',
				height : '394px',
				'z-index':'10000',
				background : 'url(/img/archive_box_eversonic_chute.png)'
			});
			
			$(a).append('<div class="cols1"></div>');
			$(a).find('div.cols1').css({
				position : 'absolute',
				left : '116px',
				top : '252px',
				width : '53px',
				height : '71px',
				'z-index':'1000010',
				background : 'url(/img/archive_box_eversonic_barre.png)'
			});
			$(a).append('<div class="cols2"></div>');
			$(a).find('div.cols2').css({
				position : 'absolute',
				left : '75px',
				top : '202px',
				width : '32px',
				height : '190px',
				'z-index':'1000010',
				background : 'url(/img/archive_box_eversonic_colonne.png)'
			});
			$(a).append('<div class="chao"></div>');
			$(a).find('div.chao').css({
				position : 'absolute',
				left : 0,
				top : '77px',
				width : '140px',
				height : '242px',
				'z-index':'1000010',
				background : 'url(/img/archive_box_eversonic_chao.png) 0 0 no-repeat'
			});
			$(a).append('<a class="close" href="javascript:void(0)" style="width:150px;height:32px;position:absolute;opacity:0.4;margin-top:8px;display:block;left:630px;"></a>');
		// 	$(a).append('<div class="searching"><input type="text" class="match" /><input type="button" value="Rechercher" class="button" /><br />Avanc&eacute; : <a href="javascript:void(0)" class="rech_ou rech_selected">Recherche OU</a>- <a href="javascript:void(0)" class="rech_et">Recherche ET</a></div>');
			$(a).find("div.searching").css({
				'position':'absolute',
				'top':'2px',
				'right':'80px'
			})
			$(a).find("div.searching a.rech_ou").click(function(){
				$(a).find("div.searching a.rech_et").removeClass("rech_selected");
				$(this).addClass("rech_selected");
			});
			$(a).find("div.searching a.rech_et").click(function(){
				$(a).find("div.searching a.rech_ou").removeClass("rech_selected");
				$(this).addClass("rech_selected");
			});
			$(a).find("div.searching input.button").click(function(){
				$(a).find("a.newsboxes").each(function(){
					if($(a).find("div.searching a.rech_ou").is(".rech_selected"))
					{
						et = false;
					}
					if($(a).find("div.searching a.rech_et").is(".rech_selected"))
					{
						et = true;
					}
					$(this).recherche_ana($(a).find("div.searching input.match").val(),et);
					
				});
				calculePages();
			});
			aClose = $(a).find('a.close');
			$(aClose).css({
				'position':'absolute',
				'top':'2px',
				'right':'2px',
				'text-decoration':'none',
				'color':'navy',
				'font-weight':'bold'
			}).hover(function(){
				$(this).css('border-bottom','1px dotted navy');
			},function(){
				$(this).css('border-bottom','none');
			});
			$(aClose).click(function(){
				// On click sur close
				$("#archivesNewsAffichage").slideUp('normal');
			});
			$("#archivesNewsAffichage").append('<h2 style="margin-left:20px;display:block;height:15px"></h2>');
			$("#archivesNewsAffichage").append('<div id="ana_pages"></div>');
			
					$("#ana_pages").css({
						'width':'700px',
						'margin':'5px auto'
					});
			$("#archivesNewsAffichage").append('<div id="ana_results"></div>');
			
					$("#ana_results").css({
						'width':'650px',
						'margin-left':'140px'
					});
			$("#ana_results").html('<img src="/img/loading.gif" style="width:32px;position:absolute;left:50%;margin-left:-16px;top:50%;margin-top:-16px;" border="0" />');
			$.ajax({
				type:"post",
				url:'/FinalVersion/ajax_archivesNews.php5',
				data:'',
				success:function(result)
				{
					$("#ana_results").html(result);
					
					calculePages();
					
									chargeAllPages();
									onClickLoadAjaxLoadIt();
									removeHashFromLinks();
									
					
					$("a.newsboxes").click(function(){
						//$(this).append('toto');
						document.location.hash = $(this).attr('href');
					    chargePage(this,$(this).attr('href'),false,false,function(){  
							$(a).slideUp('normal');  
							$(a).find('.loadNewArch').remove(); });
						$(a).append('<div class="loadNewArch" style="position:absolute;left:32%;text-align:center;padding:30px;top:42%;font-weight:bold;width:30%;background:white;border:1px solid black;font-weight:bold;">Chargement de l\'actualit&eacute; s&eacute;l&eacute;ctionn&eacute;e<img src="http://opengraphicdesign.com/wp-content/uploads/2009/01/ring32.gif" style="width:32px;margin-left:0 auto;" border="0" /></div>');
						if(!$("#speedArchives").is("#speedArchives"))
						{
							$("body").append('<div id="speedArchives"></div>');
							$("#speedArchives").css({
								'left':'5px',
								'top':'5px',
								'position':'fixed',
								'width':'100px',
								'background':'white',
								'border':'1px solid navy',
								'display':'none'
							
							});
							$("#speedArchives").append('<h2 style="text-align:center;margin-bottom:20px;">Archives</h2>');
							$("#speedArchives").append('<a href="javascript:void(0)" class="reopen" style="position:absolute;left:15px;bottom:5px;color:navy;font-weight:bold;">Acc&egrave;s rapide</a>');
							$("#speedArchives a.reopen").click(function(){
								a=$("#archivesNewsAffichage");
								$(a).slideDown('normal');
								$(this).parent().slideUp('normal');
							});
							$("#speedArchives").slideDown('normal');
						}
						else
						{
							$("#speedArchives").slideDown('normal');
						}
								//a=$("#archivesNewsAffichage");
					});
				},
				error:function()
				{ 
					$("#archivesNewsAffichage").append('erreur lors du chargement  de /FinalVersion/ajax/archivesNews.php');
				}
			});
			
			$(a).slideDown('normal');
		}
		else
		{
			
			a=$("#archivesNewsAffichage");
			$(a).slideDown('normal');
		}
	});
});
