
//var proxyBaseUrl = '/frontoffice/ajax/';

var SearchModule = Class.create({
	initialize: function(formId, destiSelId, monthSelId, compaSelId){
		this.formId = formId;
		this.destiSelId = destiSelId;
		this.monthSelId = monthSelId;
		this.compaSelId = compaSelId;		
		
		
		//console.log('month:'+$(this.monthSelId).selectedIndex)
		//console.log('compa:'+$(this.compaSelId).selectedIndex)
		if($(this.monthSelId).selectedIndex == 0 && $(this.monthSelId).length == 1){	
			$(this.monthSelId).disabled = 'disabled';
		}
		if($(this.compaSelId).selectedIndex == 0 && $(this.monthSelId).selectedIndex == 0){	
			$(this.compaSelId).disabled = 'disabled';
		}
		
		Event.observe(this.destiSelId, 'change', this.onDestiChange.bind(this));
		Event.observe(this.monthSelId, 'change', this.onMonthChange.bind(this));
		
	},
	
	updateList: function(elt, title, data){
		
		var oldOptions = elt.descendants();
		for(var i=0; i<oldOptions.length; i++){
			oldOptions[i].remove();
		}
		
		if(data != null && data.length > 0){						
						
			var title = new Element('option', { 'value': '', 'class': 'title' }).update(title);
			elt.insert(title);
			
			data.each(function(item, index){								
				var option = new Element('option', { 'value': item.value }).update(item.label);
				elt.insert(option); 
				
			});
			elt.disabled = '';
		}
		else{
			elt.selectedIndex = 0;
		  	elt.disabled = 'disabled';
		}
	},
	
	updateMonths: function(xhr){
		if(xhr.responseJSON.months != null)
			this.updateList($('month'), 'Tous les mois', xhr.responseJSON.months);
		//$('searchLoader').hide();
	},
	
	updateCompanies: function(xhr){
		if(xhr.responseJSON.companies != null)
			this.updateList($('compid'), 'Toutes les compagnies', xhr.responseJSON.companies);
		//$('searchLoader').hide();
	},
	
	onDestiChange: function(evt){		
		
		
		
		//$('searchLoader').style.display = 'block';
		//console.log('dest:'+evt.target.selectedIndex);
		//console.log('month:'+$(this.monthSelId).selectedIndex)
		//console.log('compa:'+$(this.compaSelId).selectedIndex)
		
		$(this.monthSelId).selectedIndex = 0;
		$(this.monthSelId).disabled = 'disabled';
		$(this.compaSelId).selectedIndex = 0;
		$(this.compaSelId).disabled = 'disabled';
		
		//console.log(evt.target[evt.target.selectedIndex].className);
		
		if(evt.target.selectedIndex != 0 && evt.target[evt.target.selectedIndex].className != 'title'){
			
			new Ajax.Request(proxyBaseUrl+'getmonths',{
				parameters: 'destid='+evt.target.value,
				method: 'get', 
				evalJSON: true,
				onSuccess: this.updateMonths.bind(this)
			});				
		}
		else{
			$(this.monthSelId).selectedIndex = 0;
			$(this.monthSelId).disabled = 'disabled';
			$(this.compaSelId).selectedIndex = 0;
			$(this.compaSelId).disabled = 'disabled';
		}
	}, 
	
	onMonthChange: function(evt){
		
		//$('searchLoader').show();
		
		//if(evt.target.selectedIndex != 0){
			
			new Ajax.Request(proxyBaseUrl+'getcompanies',{
				parameters: 'destid='+$(this.destiSelId).value+'&month='+evt.target.value,
				method: 'get', 
				evalJSON: true,
				onSuccess: this.updateCompanies.bind(this)
			});				
		//}
	}
});
if($('cruise_search') != null){
	Event.observe(window, 'load', function(evt){
		var SM = new SearchModule('cruise_search', 'destid', 'month', 'compid');
	});
}

if($('illus_txt') != null){

  var txt = $('illus_txt').innerHTML;
  
  var dotIdx = txt.indexOf('. ', 300);
  
  if( dotIdx > 0 ){
    $('illus_txt').innerHTML = txt.substring(0, dotIdx)+'... '+'&nbsp;&nbsp;<a id="illus_more" href="#">plus d\'infos</a>';
  }

  $('illus_more').observe('click', function(){
      $('illus_txt').innerHTML = txt;
  });
}

if($$('#pointofview .content') != null && $$('#pointofview .content').length > 0){

  var txt = $$('#pointofview .content')[0].innerHTML;
  
  var dotIdx = txt.indexOf('. ', 300);
  
  if( dotIdx > 0 ){
    $$('#pointofview .content')[0].innerHTML = txt.substring(0, dotIdx)+'... '+'&nbsp;&nbsp;<a id="prog_more" href="#">plus d\'infos</a>';
  }

  $('prog_more').observe('click', function(){
      $$('#pointofview .content')[0].innerHTML = txt;
  });
}

