// 
//  plans.js
//  Page Comparateur de forfait ("Votre forfait sur mesure").
//  
//  Created by Ned Baldessin on 2008-09-17.
//  Copyright 2008 Ned Baldessin. All rights reserved.
// 

// 
// We need at least 2 options selected to
// launch a search.
function load_offers() {
	if ($$('#plansFrame input').findAll(function(n){return n.checked}).length < 2) {
		$('default_message').show();
		$('load_offers').innerHTML = '';
	} else {
		$('default_message').hide();
		new Ajax.Updater('load_offers', $('user-options').getAttribute('action'), {parameters: $('user-options').serialize(true)});
	}
}

document.observe('dom:loaded', function(){
	$$('div.colonne div.box').each(function(el){
		var boxId = el.id;
		el.style.background='url(/images/imgsite/offreSurMesure/bg-'+boxId+'.png) bottom left no-repeat';
		var boxChild = $(el).firstDescendant();
		boxChild.observe('click', function(e){
			
			// On change la classe pour passer le texte en rouge.
			$(boxChild).up().up().up().select('div').each(function(an_el){$(an_el).removeClassName('selected');});
			$(boxChild).addClassName('selected');
			
			var boxFamily = $(el).id.substring(0,4);
			for(i=1;i<6;i++){
				var boxFamilyChild = boxFamily+i;
				if($(boxFamilyChild) && boxFamilyChild!=boxId)
					$(boxFamilyChild).style.background='url(/images/imgsite/offreSurMesure/bg-'+boxFamilyChild+'.png) bottom left no-repeat';
			}
			el.style.background='url(/images/imgsite/offreSurMesure/bg-'+boxId+'-active.png) bottom left no-repeat';
			$(el).select('input')[0].checked = true;
			if (Event.element(e).tagName == 'DIV') {
				load_offers();
			}
			
		});
	});
	
	// Fetch the result via XHR and display them in "Nos Meilleures Offres".
	$$('#plansFrame input').each(function(el){
		$(el).observe('click', function(){
			load_offers();
		})
		
	});
});
