﻿/*******************************	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 loadAnnonces = function (annoncesObj){

	// INSTANTIATION DE L'OBJET RECU
	// VALEURS PAR DEFAUT
	var defaultConfigs = {
		annoncesGlobalContainer:'',
		annoncesJsonSrc:'',
		annoncesBase:0,
		annoncesLimit:12,
		annoncesCurrentPage:0,
		annoncesNbrItemPerPage:6,
		annoncesDateParution:'',
		annoncesTypeRubrique:'',
        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(){
		//$(annoncesView.annoncesGlobalContainer).addClass('loadData');
		// CHARGEMENT DU HTML NECCESSAIRE AUX annoncesS
		// CONTEUNEURS
				
		var div = new Element('div',{'id':'contenair_annonces'});
		var ul = new Element('ul');
		div.adopt(ul);
		annoncesView.contenairElement = div;
		/// BARRE DE NAVIGATION
		var navBar = new Element('div',{'class':'defaultBlockNavBar'});
		var p = new Element('p',{'class':'navTools'});
		var suivant = new Element('a',{'id':'contenair_annonces_suivant','html':'pr&eacute;c&eacute;dent'});
		//var span = new Element('span',{'text':'|'});
		var precedent = new Element('a',{'id':'contenair_annonces_precedente','html':'suivant'});
		var creerFp = new Element('a',{'href':'/passer-annonce/','title':'Passer une annonce', 'html':'&raquo; Passer une annonce'});
		
		navBar.adopt(p);
		p.adopt(suivant);
		//p.adopt(span);
		p.adopt(precedent);
		navBar.adopt(creerFp);
		annoncesView.navBar = navBar;
		$(annoncesView.annoncesGlobalContainer).empty();
		
		annoncesView.firstLoad = true;
		annoncesView.loadDataJson();
	}
	annoncesView.loadDataJson = function () {
		// REQUETE JSON
		var reqannoncesUrl = annoncesView.annoncesJsonSrc;
		var annoncesObj;
		var annoncesObjJsonReq = new Request.JSON({
			url:reqannoncesUrl,
			onRequest:function(){
				
			},
			onSuccess:function(responseJSON, responseText) {
				annoncesView.itemObj = responseJSON.annonces;
				annoncesView.loadHtml();
			}
        }).send('base='+annoncesView.annoncesBase+
                '&limit='+annoncesView.annoncesLimit+
                '&type_liste=home' + 
                '&type_rubrique='+annoncesView.annoncesTypeRubrique+
                '&random_seed='+annoncesView.annoncesRandomSeed+
                '&date_parution='+annoncesView.annoncesDateParution);
	}
	annoncesView.loadHtml = function() {
		var lastLi = false;
		var fp_descriptionText;
		var j=0;
		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 dl = new Element('dl',{'class':annoncesView.itemObj[j].theme});
						li.adopt(dl);
						var dt = new Element('dt',{
							'html':annoncesView.itemObj[j].type_evenement_nom
						});
						dl.adopt(dt);
						
						// TRONQUAGE
						fp_descriptionText = annoncesView.itemObj[j].text;
						//alert(fp_descriptionText.lenght);
						if (fp_descriptionText.length > 55 ) fp_descriptionText = fp_descriptionText.substr(0, 55)+'[…]';
						var dd = new Element('dd',{
							'html':'</p><p class="fp_description"><a href="/' + annoncesView.itemObj[j].type_rubrique_cle + '/annonces/'+annoncesView.itemObj[j].ref+'/">'+fp_descriptionText+'</a></p>'
						});
						dl.adopt(dd);
					}else lastLi = true;
				}
				if (lastLi==false || li.getChildren('dl').lenght != 0 ) annoncesView.contenairElement.getChildren('ul').adopt(li);
			}
		}
		if (annoncesView.firstLoad == true) {
			$(annoncesView.annoncesGlobalContainer).setStyle('height','auto');
			$(annoncesView.annoncesGlobalContainer).removeClass('loadData');	

			/*if (Browser.Engine.trident!=true || Browser.Engine.version>4 ) {
				
			}*/
			
			annoncesView.contenairElement.setStyle('opacity', '0');
			var loadingTransition = new Fx.Tween(annoncesView.contenairElement,{duration:800});
			loadingTransition.start('opacity', '0', '1');
			
			$(annoncesView.annoncesGlobalContainer).adopt(annoncesView.contenairElement)
			$(annoncesView.annoncesGlobalContainer).adopt(annoncesView.navBar);
			$('contenair_annonces_suivant').addClass('disableLink');
			annoncesView.firstLoad = false;
		}
		// POUR IE 6
		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');
				//item.setStyle('margin-right','4px');
			});
		}
		
		annoncesView.makeNavigable();
	}
	annoncesView.makeNavigable = function () {
		
		//console.log(annoncesView.annoncesCurrentPage);
		var listElementViewable = $(annoncesView.annoncesGlobalContainer).getChildren('div')[0].getChildren('ul')[0].getChildren('li');
		if (annoncesView.annoncesCurrentPage+1 == listElementViewable.length ) 	$('contenair_annonces_precedente').addClass('disableLink');
		
		$('contenair_annonces_suivant').addEvent('click', function(){
			annoncesView.moove(+1);
		});
		$('contenair_annonces_precedente').addEvent('click', function(){
			annoncesView.moove(-1);
		});
		
	}
	annoncesView.moove = function (direction) {
		
		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;
					if (annoncesView.annoncesCurrentPage==0) {
						$('contenair_annonces_suivant').addClass('disableLink');
						$('contenair_annonces_precedente').removeClass('disableLink');
					}else if (annoncesView.annoncesCurrentPage+1 != listElementViewable.length) $('contenair_annonces_precedente').removeClass('disableLink');
				}
			}else {
				
				if (annoncesView.annoncesCurrentPage+1 == listElementViewable.length) {
					$('contenair_annonces_precedente').addClass('disableLink');
				}else $('contenair_annonces_precedente').removeClass('disableLink');
			}
		} else {
			//console.log('lalala');
			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;
						$('contenair_annonces_suivant').removeClass('disableLink');
						$('contenair_annonces_precedente').removeClass('disableLink');
					}
				}else{
					$('contenair_annonces_precedente').addClass('disableLink');
				}
			} 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;
					}
					//if (annoncesView.lastPage == true) $('contenair_annonces_precedente').addClass('disableLink');
				}
			}
			if (annoncesView.annoncesCurrentPage != 0) $('contenair_annonces_suivant').removeClass('disableLink');
		}
	}
	annoncesView.init();
}
