﻿/*******************************	DEFAULT CONFIGS		************************ 
	
	 var annonces = loadfairepart({
		annoncesGlobalContainer:'faire-part',		===> 	IDENTIFIANT DU BLOC PRINCIPALE
		annoncesJsonSrc:'json/fp_json0001.json',	===>	URL DU JSON A INTEROGER
                annoncesBase:0,					===>	base
                annoncesLimit:12,				===>	limite
                annoncesCurrentPage:0,				===>	Page en cours
		annoncesNbrItemPerPage:6			===>	Nobre de faire-part pas page
	});
	
							BY MALEK AKA RADOK THE ROOXXOR :D
							
****************************************************************************************/

var loadDetailsAnnonces = function (annoncesObj){

	// INSTANTIATION DE L'OBJET RECU
	// VALEURS PAR DEFAUT
	var defaultConfigs = {
		annoncesGlobalContainer:'',
		annoncesJsonSrc:'',
		annoncesBase:0,
		annoncesLimit:12,
		annoncesCurrentPage:0,
		annoncesNbrItemPerPage:6,
		annoncesPrintFile:'',
		annoncesTypeRubrique:'',
		annoncesDateParution:'',
		annoncesSearchPattern:'',
        annoncesRandomSeed:0.5
	};
	// EXTENSION DE L'OBJET RECU PAR LES VALEUR PAR DEFAUT
	if (!annoncesObj) annoncesObj = {};
	$extend(defaultConfigs, annoncesObj);
	annoncesObj = defaultConfigs;
	
	if (annoncesObj.annoncesGlobalContainer == '') {console.error('annoncesObj.annoncesGlobalContainer : Veuillez indiquer CONTENAIRE PRINCIPAL A CIBLER');return false;}
	if (annoncesObj.annoncesJsonSrc == '') {console.error('annoncesObj.annoncesJsonSrc : Veuillez indiquer le fichier JSON A CIBLER');return false;}


	var annoncesView = annoncesObj;
	annoncesView.animation = false;
	annoncesView.init = function(){
		// CHARGEMENT DU HTML NECCESSAIRE AUX annoncesS
		// CONTEUNEURS + NAV BARRE
		
		$(annoncesView.annoncesGlobalContainer).empty();
		var div = new Element('div',{'id':'listeAnnonces-carnet_conteneur'}); 			
		var ul = new Element('ul',{'id':'listeAnnonces-carnet_conteneur_list','style':'opacity:0;'});
		div.adopt(ul);
		
		var navBar = new Element('div',{'class':'defaultBlockNavBar'});
		var navTools = new Element('p',{'class':'navTools'});
		var aSuiv = new Element('a',{'href':'#','class':'disableLink','html':'pr&eacute;c&eacute;dent','id':'contenair_annoncesDetails_suivant','title':'Annonces précédentes'});
		//var spanCompteur = new Element('span',{'id':'annoncesDetails_conpteur'}); 
		var aPrec = new Element('a',{'href':'#','html':'Suivant','id':'contenair_annoncesDetails_precedent','title':'Annonces suivantes'});
		var aPassAnnonce = new Element('a',{'href':'/passer-annonce/','html':'&raquo; Passer une annonce','title':'Passer une annonce'});
		
		
		$(annoncesView.annoncesGlobalContainer).adopt(div);
		navBar.adopt(navTools);
		navTools.adopt(aSuiv);
		navTools.adopt(aPrec);
		navBar.adopt(aPassAnnonce);
		
		$(annoncesView.annoncesGlobalContainer).adopt(navBar);
		//navTools.adopt(spanCompteur);
		
		
		annoncesView.loadDataJson();
		annoncesView.firstLoad = true;
	}
	annoncesView.loadDataJson = function () {
		// REQUETE JSON
		var reqannoncesUrl = annoncesView.annoncesJsonSrc;
		var annoncesObj;
		var annoncesObjJsonReq = new Request.JSON({
			url:reqannoncesUrl,
			onRequest:function(){
				
			},
			onSuccess:function(responseJSON, responseText) {
				//console.log(responseJSON.annonces.length);
				if (responseJSON.annonces.length == 0) {
					var pMess = new Element('p',{
						'class':'infoFigaro',
						'html':'Aucune annonce ne correspond à votre recherche.<br />Vous pouvez faire une nouvelle recherche sur d\'autres critères.'
					});
					$('annonces-carnet').getChildren('div.defaultBlockNavBar').setStyle('display', 'none');
					$('listeAnnonces-carnet_conteneur').setStyle('display', 'none');
					$('annonces-carnet').adopt(pMess);
					$('annonces-carnet').removeClass('loadData');
					return false;
				} else {
					annoncesView.itemObj = responseJSON.annonces;
					annoncesView.loadHtml();
				}
				
				
			}
        }).send('base='+annoncesView.annoncesBase+
                '&limit='+annoncesView.annoncesLimit+
                '&type_liste=detail'+
                '&type_rubrique='+annoncesView.annoncesTypeRubrique+
                '&date_parution='+annoncesView.annoncesDateParution+
                '&search_pattern='+annoncesView.annoncesSearchPattern);
	}
	annoncesView.loadHtml = function() {
		var lastLi = false;
		var j=0;
		var newElements=new Array();
		var listeDiv = new Array();
		for (i=0;i<2;i++) {
			if (annoncesView.itemObj[j]) {
				var li = new Element ('li');
				var jMax = annoncesView.annoncesNbrItemPerPage*(i+1);
				for (j=j;j<jMax;j++) {
					if (annoncesView.itemObj[j]) {
						var divToggler = new Element('div',{'class':'togglers'});				
						var H3 = new Element ('h3',{'text':annoncesView.itemObj[j].titre}); 
						var aTogglerButton = new Element ('a',{'href':'#','title':'hidden','class':'togglersButton'});
						var pINfos = new Element ('p',{'class':'infos'});
						var spanSrc = new Element('span',{'class':'annonce_src','html':'<a href="#" title="Carnet du jour">Carnet du jour</a>'});
						var spanRub = new Element('span',{'class':'annonce_rubrique','html':annoncesView.itemObj[j].type_evenement_nom});
						var spanDate = new Element('span',{'class':'annonce_date','text':'paru le '+annoncesView.itemObj[j].date});
						
						li.adopt(divToggler);
						divToggler.adopt(H3,aTogglerButton,pINfos);
						pINfos.adopt(spanSrc,spanRub,spanDate);
				
						var divCarnet = new Element('div',{'class':'default_annonce-carnet_bloc'});
						var pContent = new Element('p',{'class':'annonce-content','html':annoncesView.itemObj[j].text});
						var divMinNav = new Element('div',{'class':'defaultBlockNavBar'});
						
						pOr = new Element('p',{'class':'defaultBlockNavBar_pOr'});
						var aLOr = false; 
						if (annoncesView.itemObj[j].livreOr == 'true') {
							var aLOr = new Element('a',{'href':'#','class':'aLOr_left','title':'Livre d\'or','html':'&raquo; Livre d\'or'});
							pOr.adopt(aLOr);
						}			
						li.adopt(divCarnet);
						var pPrint = new Element ('p',{'class':'defaultBlockNavBar_pPrint'});
						var aPrint = new Element('a',{
							'href':'#',
							'title':'Imprimer l\'annonce',
							'class':'imprimer',
							'text':'imprimer'
						});
						pPrint.adopt(aPrint);
						
						divCarnet.adopt(pContent,divMinNav);
						divMinNav.adopt(pOr,pPrint);
					}else lastLi = true;
					
					
				}	
				if (lastLi==false || li.getChildren('dl').lenght != 0 ) {
					$('listeAnnonces-carnet_conteneur_list').adopt(li);
					
					listeDiv.extend(li.getChildren('div.default_annonce-carnet_bloc').getChildren('div.defaultBlockNavBar'));
					//myassociate = 
		
					//getChildren('p.defaultBlockNavBar_pPrint').getChildren('a');
					//console.log(listePrint);
					
					//annoncesView.print(annoncesView.itemObj[j].refAnnonce);
					if (annoncesView.firstLoad == false) {
						newElements[i]=li;
					}
				}
			}
		}
		
		
		listeDiv.each(function(item, key){
			var pElement = item.getChildren('p.defaultBlockNavBar_pPrint');
			pElement[0].getFirst('a').addEvent('click',function(e){
				annoncesView.print(annoncesView.itemObj[key].refAnnonce);
			});
			
		});
		
		/*var listePrintButton = $$('.imprimer');
		listePrintButton.each(function (item, key){
			//item.removeEvent('click');
			item.addEvent('click',function(e){
				e.stop();
				
			});
		});
		*/
		
		// EFFET AU CARGEMENT
		if (annoncesView.firstLoad == true) {
			$(annoncesView.annoncesGlobalContainer).removeClass('loadData');
			var loadingTransition = new Fx.Tween($('listeAnnonces-carnet_conteneur_list'),{duration:800});
			loadingTransition.start('opacity', '0', '1');
		}
		// POUR IE 6/7
		if (Browser.Engine.trident==true /*&& Browser.Engine.version==4*/ ) {
			nbrLi = $(annoncesView.annoncesGlobalContainer).getChildren('div')[0].getChildren('ul')[0].getChildren('li');
			$(annoncesView.annoncesGlobalContainer).getChildren('div')[0].getChildren('ul')[0].setStyle('width',(nbrLi.length)*504+'px');
			nbrLi.each(function(item, key){
				item.setStyle('float','left');
			});
		}
		annoncesView.makeNavigable(newElements);
		
	}
	annoncesView.makeNavigable = function (newElements) {
		
		var defaultSettings = {visibleSlideKey:0};
		settingsObj = defaultSettings;
	   	
		var togglersList = new Array();
		var slidesList = new Array();
		var anchorToDisplayList = new Array();
		
		// APRES PREMIER CHARGEMENT 
		// REATRIBUT LES ACTIONS UNIQUEMENT NOUVEAUX ELEMENTS
		if (newElements != '') {
			newElements.each(function(item, key){
				togglersListKey = item.getChildren('div.togglers');
				anchorToDisplayListKey = item.getChildren('div.togglers').getChildren('a.togglersButton');
				slidesListKey = item.getChildren('div.default_annonce-carnet_bloc');
				togglersList.extend(togglersListKey);
				anchorToDisplayList.extend(anchorToDisplayListKey);
				slidesList.extend(slidesListKey);
			});
		}else {
			togglersList = $$('.togglers');
			anchorToDisplayList = $$('.togglersButton');
			slidesList = $$('.default_annonce-carnet_bloc');
		}
		
		// AFFICHAGE DES ARTICLES AU CHARGEMENT DE LA LISTE
		slidesList.each(function(item, key){
			var myFx = new Fx.Slide(slidesList[key],{
				duration:0,
				onComplete:function(){
					//slidesList[key].setStyle('display','block');
					if (key == settingsObj.visibleSlideKey ) {
						if (newElements=='') slidesList[key].getParent('div').setStyle('height','auto');
					}
				}
			});
			if (annoncesView.firstLoad == true) {
				if (key == settingsObj.visibleSlideKey) {
					myFx.slideIn('vertical');
					togglersList[key].setStyle('border-width','2px 2px 1px 2px');
					item.setStyle('border-width','1px 2px 2px');
					item.setStyle('display','block');
					//item.getParent('div').setStyle('height','auto');
					anchorToDisplayList[key].title = 'visible';
					anchorToDisplayList[key].addClass('visible');
				} else {
					myFx.slideOut('vertical');	
					item.setStyle('margin-bottom','5px');
					togglersList[key].setStyle('padding','11px 11px 8px');
					anchorToDisplayList[key].title = 'hidden';
					anchorToDisplayList[key].removeClass('visible');
					anchorToDisplayList[key].addClass('hidden');
				}
			} else {
				myFx.slideOut('vertical');	
				item.setStyle('margin-bottom','5px');
				/*item.setStyle('display','none');*/
				anchorToDisplayList[key].title = 'hidden';
				anchorToDisplayList[key].removeClass('visible');
				anchorToDisplayList[key].addClass('hidden');
			}
			
		});
		
		var statesObj = {
			'hidden':'visible',
			'visible':'hidden'
		};
		
		// CLIC SUR CHAPEAU
		togglersList.each(function(item, key){
			var myFx2 = new Fx.Slide(slidesList[key],{
				onComplete:function(){
					anchorToDisplayList[key].removeClass(anchorToDisplayList[key].title);
					anchorToDisplayList[key].addClass(statesObj[anchorToDisplayList[key].title]);
					anchorToDisplayList[key].title = statesObj[anchorToDisplayList[key].title];
					if(anchorToDisplayList[key].title == 'hidden') {
						slidesList[key].setStyle('display','none');	
					}
				}
			});
			item.addEvent('click', function(){
				if(anchorToDisplayList[key].title == 'hidden') {
					//slidesList[key].setStyle('display','block');
					slidesList[key].setStyle('display','block');
					myFx2.slideIn('vertical');
					togglersList[key].setStyle('border-width','2px 2px 1px 2px');
					togglersList[key].setStyle('padding','10px 10px 8px');
					//anchorToDisplayList[key].title = 'visible';
					//anchorToDisplayList[key].removeClass('hidden');
					//anchorToDisplayList[key].addClass('visible');
				} else {
					//slidesList[key].setStyle('display','block');
					myFx2.slideOut('vertical');	
					togglersList[key].setStyle('border-width','1px');
					togglersList[key].setStyle('padding','11px 11px 8px');
					//anchorToDisplayList[key].title = 'hidden';
					//anchorToDisplayList[key].removeClass('visible');
					//anchorToDisplayList[key].addClass('hidden');
				}
			}); 
		});
		
		// CLIC SUR boutton
		anchorToDisplayList.each(function(item, key){
			var myFx2 = new Fx.Slide(slidesList[key],{
				onComplete:function(){
					anchorToDisplayList[key].removeClass(anchorToDisplayList[key].title);
					anchorToDisplayList[key].addClass(statesObj[anchorToDisplayList[key].title]);
					anchorToDisplayList[key].title = statesObj[anchorToDisplayList[key].title];
					if(anchorToDisplayList[key].title == 'hidden') {
						slidesList[key].setStyle('display','none');	
					}
				}
			});
			item.addEvent('click',function(e){
				e.stop();
				if(anchorToDisplayList[key].title == 'hidden') {
					//slidesList[key].setStyle('display','block');
					slidesList[key].setStyle('display','block');
					myFx2.slideIn('vertical');
					togglersList[key].setStyle('border-width','2px 2px 1px 2px');
					togglersList[key].setStyle('padding','10px 10px 8px');
					//anchorToDisplayList[key].title = 'visible';
					//anchorToDisplayList[key].removeClass('hidden');
					//anchorToDisplayList[key].addClass('visible');
				} else {
					//slidesList[key].setStyle('display','block');
					myFx2.slideOut('vertical');	
					togglersList[key].setStyle('border-width','1px');
					togglersList[key].setStyle('padding','11px 11px 8px');
					//anchorToDisplayList[key].title = 'hidden';
					//anchorToDisplayList[key].removeClass('visible');
					//anchorToDisplayList[key].addClass('hidden');
				}
			});
		});
		
		annoncesView.togglers = $$('.togglers');
		annoncesView.slides = $$('.default_annonce-carnet_bloc');
		annoncesView.anchorToDisplay =  $$('.togglersButton'); 
		
		/*annoncesView.slides.each(function(item, key){
			item.setStyle('display','block');
		});*/
		
		if (annoncesView.firstLoad == true) {
			
			var listElementViewable = $(annoncesView.annoncesGlobalContainer).getChildren('div')[0].getChildren('ul')[0].getChildren('li');
			if (annoncesView.annoncesCurrentPage+1 == listElementViewable.length ) 	$('contenair_annoncesDetails_precedent').addClass('disableLink');
		
			$('contenair_annoncesDetails_suivant').addEvent('click', function(e){
				annoncesView.scroll(+1);
				e.stop();
			});
			$('contenair_annoncesDetails_precedent').addEvent('click', function(e){
				annoncesView.scroll(-1);
				e.stop();
			});
			annoncesView.firstLoad = false;
		}
		//annoncesView.updatePaginer();
		
		var listeLi = $('listeAnnonces-carnet_conteneur_list').getChildren('li');
		//console.log(listeLi);
		listeLi.each(function(item, key){
			if (key == annoncesView.annoncesCurrentPage) item.setStyle('height','auto');
			else  item.setStyle('height','400px');
		});
	}
	/*annoncesView.updatePaginer = function () {
		var listElementViewable = $(annoncesView.annoncesGlobalContainer).getChildren('div')[0].getChildren('ul')[0].getChildren('li');
		
		$('annoncesDetails_conpteur').empty();
		for (i=1;i<=listElementViewable.length;i++) {
			var a = new Element('a',{'href':'#','text':i,'title':'page '+i});
			if (i == annoncesView.annoncesCurrentPage+1) a.addClass('currentPage');
			$('annoncesDetails_conpteur').adopt(a);
		}
	
	}*/
	annoncesView.scroll = function (direction){
		
		var h2Pos = $('news').getChildren('h2')[0].getPosition();
		var ToolsPos = $('contenair_annoncesDetails_suivant').getPosition();
		
		if (ToolsPos.y-h2Pos.y > 600 ) {
			var myFx = new Fx.Scroll(window, {
				duration: 650,
				wait: false,
				onComplete:function(){annoncesView.close(direction);}
			}).toElement('news');
		}
		else {
			annoncesView.close(direction);
		}
	}
	annoncesView.close = function (direction){
		var nbrItem = annoncesView.togglers.length;
		annoncesView.togglers.each(function(item, key){
			
			var myFx = new Fx.Slide(annoncesView.slides[key], {
				onComplete: function(){
					annoncesView.slides[key].setStyle('display','none');
					annoncesView.togglers[key].setStyle('border-width','1px');
					annoncesView.anchorToDisplay[key].title = 'hidden';
					annoncesView.anchorToDisplay[key].removeClass('visible');
					annoncesView.anchorToDisplay[key].addClass('hidden');
					if (key+1 == annoncesView.togglers.length) annoncesView.moove(direction);
				}
			}).slideOut('vertical');
		});
		
	}
	annoncesView.moove = function (direction) {
		
		var postElement;
		
		if (direction==+1) postElement = annoncesView.annoncesCurrentPage-1;
		else postElement = annoncesView.annoncesCurrentPage+1;
		
		var element = $(annoncesView.annoncesGlobalContainer).getChildren('div')[0].getChildren('ul')[0];
		var listElementViewable = $(annoncesView.annoncesGlobalContainer).getChildren('div')[0].getChildren('ul')[0].getChildren('li');
		var distanceMoove = 500;
		
		var transition = new Fx.Tween(element,{
			onStart:function(){
				annoncesView.animation = true;
			},
			onComplete:function(){
				annoncesView.animation = false;
			}
		});
		if (direction==+1) {
			if (annoncesView.annoncesCurrentPage != 0) {
				if (annoncesView.animation == false ) {
					transition.start('margin-left', element.getStyle('margin-left'), ((element.getStyle('margin-left').toInt())+(distanceMoove))+'px');
					annoncesView.annoncesCurrentPage += -1;
				}
			}
		} else {
			if (annoncesView.annoncesCurrentPage+1 == listElementViewable.length) {
				annoncesView.lastPage = false;
				for (l=0;l<12;l++) {
					if (annoncesView.itemObj[l] && annoncesView.itemObj[l].last == 'true') annoncesView.lastPage = true;
				}
				if (annoncesView.lastPage == false ) {
					// chargemetn de la suite
					if (annoncesView.animation == false ) {
						annoncesView.annoncesBase += (1*annoncesView.annoncesLimit);
						annoncesView.loadDataJson();
						transition.start('margin-left', element.getStyle('margin-left'), ((element.getStyle('margin-left').toInt())-(distanceMoove))+'px');
						
						annoncesView.annoncesCurrentPage += 1;
					}
				}
			} else {
				if (annoncesView.animation == false ) {
					transition.start('margin-left', element.getStyle('margin-left'), ((element.getStyle('margin-left').toInt())-(distanceMoove))+'px');
					
					annoncesView.annoncesCurrentPage += 1;
					
					for (l=0;l<12;l++) {
						if (annoncesView.itemObj[l] && annoncesView.itemObj[l].last == 'true') annoncesView.lastPage = true;
					}
				}
			}
		}
		
		// AJUSTEMETN DE LA HAUTEUR DES BLOCKS
		var listeLi = $('listeAnnonces-carnet_conteneur_list').getChildren('li');
		//console.log(listeLi);
		listeLi.each(function(item, key){
			if (key == annoncesView.annoncesCurrentPage) item.setStyle('height','auto');
			else  item.setStyle('height','400px');
		});
		
		/* STATUE DES BOUTONS */
		if (annoncesView.lastPage == true && annoncesView.annoncesCurrentPage+1 == listElementViewable.length) $('contenair_annoncesDetails_precedent').set('class','disableLink');
		else $('contenair_annoncesDetails_precedent').set('class','');
		
		if (annoncesView.annoncesCurrentPage != 0) $('contenair_annoncesDetails_suivant').set('class','');
		else $('contenair_annoncesDetails_suivant').set('class','disableLink');
	}
	annoncesView.print = function (ref) {
		$('annoncesPrintFrame').set('src','/annonce/print/'+ref);
	}
	annoncesView.init();
}
