/**
 * @version $Id: produits.js,v 1.11 2011-07-28 14:47:07 cg Exp $
 * @package cdl-2010
 * @copyright Copyright (C) 2010 BROCELIA. All rights reserved.
 */

window.addEvent('load', function () {

	// fourchette de prix
	try{
		var tick = new Ticker({
			min:PRIX_MIN,
			max:PRIX_MAX,
			step:9,
			draggerLeft:'dragger_left_prix',
			draggerRight:'dragger_right_prix',
			bar:'bar_prix',
			onFinish:changePrix,
			zonePrice:null
		});
	} catch(e) {
		if(console) console.log(e);
//		var fourchette = $('fourchette');
//		fourchette.getPrevious().destroy();
//		fourchette.destroy();
	}


	// prix min/max et/ou page demandés
	if(location.hash.length>0 && location.hash.substring(0,1)=='#') {
		var minMax = location.hash.substring(1).split('-');
		if(minMax[0] || minMax[1]) initChangePrix(minMax[0].toInt(), minMax[1].toInt());
		if(minMax.length>2 && minMax[2].toInt()>1) {
			changePage(minMax[2].toInt());
		}
	}

	// pagination dynamique
	$$('.pagination li').each(function (li) {
		var a = li.getElement('a');
		a.addEvent('click', function (event) {
			var p;
			if(li.hasClass('previous')) {
				p = PAGE - 1;
			} else if(li.hasClass('next')) {
				p = PAGE + 1;
			} else {
				p = a.getText().toInt();
			}
			if(p!=PAGE && p>0 && p<=NB_PAGES) {
				changePage(p);
			}
			/*
			if(window.scroll) {
				window.scroll(0,200);
			} else if(window.scrollTo) {
				window.scrollTo(0,200);
			}
			*/
			event.stop();
		});
    });
});

function changePage(page) {
//	if(console) console.log('changePage('+page+')');
	var start = PRODUITS_LIMIT * (page - 1);
	var end = start + PRODUITS_LIMIT;
	var nb = 0;
	// affichage/masquage des produits
	PRODUITS.each(function(produit, i) {
		if(produit.prix>=PRIX_MIN_SELECTED && produit.prix<=PRIX_MAX_SELECTED) {
			if(nb>=start && nb<end) {
				showProduit(produit, i);
			} else {
				hideProduit(produit);
			}
			nb++;
		} else {
			hideProduit(produit);
		}
	});
	setPage(page);
}

function setPage(page) {

	var start = page<3 ? 1 : page-2;
	if(start + 4 < NB_PAGES) {
		end = start + 4;
	} else {
		end = NB_PAGES;
		if(end>5 && start>end-5) {
			start = end - 4;
		}
	}

	$$('ul.pagination').each(function (ul, i) {
		var j = start;
		ul.getElements('li').each(function (li, k) {
			if(li.hasClass('previous')) {
				if(page>1) {
					li.removeClass('active');
				} else {
					li.addClass('active');
				}
			} else if(li.hasClass('next')) {
				// construction des numéro après
				if(page<NB_PAGES) {
					li.addClass('active');
				} else {
					li.removeClass('active');
				}
			} else {
				li.getElement('a').setText(j);
				li.setStyle('display', j>end?'none':'');
				if(j == page) {
					li.addClass('active');
				} else {
					li.removeClass('active');
				}
				j++;
			}
		});
	});
	PAGE = page;
}

function initChangePrix(min, max) {
	// positionner les curseurs sur la reglette :
	if(!min) min = PRIX_MIN;
	if(!max) max = PRIX_MAX;
	setminmax(min, max);
    // mettre à jour la liste de produits :
    changePrix(min, max);
}

function setminmax(min, max) {
//	if(console) console.log('setminmax('+min+', '+max+') TODO !!!!');
}

function changePrix(min, max) {
//	if(console) console.log('changePrix('+min+', '+max+')');
	PRIX_MIN_SELECTED = min;
	PRIX_MAX_SELECTED = max;
	var nb = 0;
	// affichage/masquage des produits
	PRODUITS.each(function(produit, i) {
		if(produit.prix>=min && produit.prix<=max) {
			if(nb<PRODUITS_LIMIT) {
				showProduit(produit, i);
			} else {
				hideProduit(produit);
			}
			nb++;
		} else {
			hideProduit(produit);
		}
	});
	// nb résultats
	if(nb>1) {
		$('produits-negation').set('html', '');
		$('produits-nb').set('html', nb+' '+libelle_pluriel);
	} else if(nb>0) {
		$('produits-negation').set('html', '');
		$('produits-nb').set('html', '1 '+libelle_singulier);
	} else {
		$('produits-negation').set('html', "n'");
		$('produits-nb').set('html', 'aucun produit');
	}
	// pagination
	NB_PAGES = Math.ceil(nb / PRODUITS_LIMIT);
	setPage(1);
}

function showProduit(produit, i) {
	if(produit.exists) {
		var div = $('produit-'+produit.id);
		if(div) {
			div.setStyle('display', '');
		}
	} else {
		var prev = null;
		var div = new Element(MODE=='liste'?'tr':'div');
		div.id = 'produit-'+produit.id;
		//div.set('html', 'Produit #'+produit.id);
		while(i>0) {
			if(PRODUITS[i-1].exists) {
				prev = $('produit-'+PRODUITS[i-1].id);
				break;
			}
			i--;
		}
		if(prev) {
			div.inject(prev, 'after');
		} else {
			div.inject($('produits'));
		}
		produit.exists = true;
		var r = new Request({method:'get', url:'index.php', onSuccess:produitOnSuccess});
		r.produitId = produit.id;
		r.send('option=com_cdl&view=blocproduit&format=raw&layout='+(MODE=='liste'?'ligne':'default')+'&id='+produit.id+'&typeId='+typeId);
	}
}

function hideProduit(produit) {
	if(produit.exists) {
		var div = $('produit-'+produit.id);
		if(div) {
			div.setStyle('display', 'none');
		}
	}
}

function produitOnSuccess(responseText, responseXML) {
	var div = $('produit-'+this.produitId);
	if(div) {
		div.set('html', responseText);
	}
}

