
var Vuelos = function (Data, Dictionary, options) {

	// Definimos el array de opciones
	this.options = {
		container: 'results',
		origen: '',
		destino: '',
		pax: 1,
		meses: ['','Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'] 
	};

	this.processJson = function () {

		var ls; var lr; var lb; var le;
		for (var lr_index = 0; lr_index < this.Data[0].lp[0].lt[0].lr.length, lr=this.Data[0].lp[0].lt[0].lr[lr_index]; lr_index++) {
			for (var lb_index=0; lb_index < lr.lb.length, lb=lr.lb[lb_index]; lb_index++) { // ida (0) o vuelta (1). json1.lb[$lb_index] ;)
				for (var le_index = 0; le_index < lb.le.length, le=lb.le[le_index]; le_index++) { // vuelos, cada una de las opciones. Puede tener mas de 1 parada (combinacin salida-llegada)
					this.Data[0].lp[0].lt[0].lr[lr_index].lb[lb_index].le[le_index].fd = this.Dictionary.lb[lb_index].le[le.bei].fd;
					//lastIndexes[lb_index] = count(le.ls);
					for (var ls_index=0; ls_index < le.ls.length, ls=le.ls[ls_index]; ls_index++) { // Cada tramo del viaje, puede ser 1 solo viaje o mas de uno.
						this.Data[0].lp[0].lt[0].lr[lr_index].lb[lb_index].le[le_index].ls[ls_index] = this.Dictionary.lcr[this.Dictionary.lb[lb_index].le[le.bei].ls[ls_index].cai];
					}
				}
			}
		}
	}

	// Definimos el constructor 
  this.init = function() {
		//this.setOptions(options);
		this.Data = Data;
		this.Dictionary = Dictionary;
		this.minPrice=10000;
		this.maxPrice=0;
		this.airlines = Dictionary.lsu;
		this.pax = Dictionary.pax;
		//this.pax = 1;

		// Preproceso el json
		this.processJson();

		this.origen = this.Dictionary.ll[this.Data[0].lp[0].lt[0].lr[0].lb[0].le[0].ls[0].bli].cin;
		//this.destino = this.Dictionary.ll[this.Data[0].lp[0].lt[0].lr[0].lb[0].le[0].ls.getLast().eli].cin;
		this.destino = this.Dictionary.ll[this.Data[0].lp[0].lt[0].lr[0].lb[0].le[0].ls[this.Data[0].lp[0].lt[0].lr[0].lb[0].le[0].ls.length-1].eli].cin;

		this.ida='';
		this.vuelta='';
	}

	// Ejecutamos el constructor
	this.init();
	  
	this.pad = function(i,l,s) {
		var o = i.toString();
		if (!s) { s = '0'; }
		while (o.length < l) {
			o = s + o;
		}
		return o;
	}
	
	this.minutes2hhmm = function (oldtime) {
		var h = Math.floor(oldime/60)*100;
		var m = Math.floor(oldtime%60);
		return h+m;
	}

	this.hhmm2minutes = function(oldtime) {
		var h = Math.floor(oldtime/100)*60;
		var m = (oldtime%100);
		return h+m;
	}

	// recibe YYYYMMDD
	this.getDateWeight = function(time) {
		var myDate = new Date();
		return Date.parse(time);//.getFullYear().getMonth().getDay();
	}

	this.getHourDifference = function(oldtime1, oldtime2, difday) {
		var time1 = this.hhmm2minutes(oldtime1);
		var time2 = this.hhmm2minutes(oldtime2);
		// Si cambia el dia le agrego 60*24
		//if (difday != 0) time1 += (60*24);
		if (time2 > time1) time1 += (60*24);
		var res = time1-time2;
		var h = Math.floor(res/60);
		var m = Math.floor(res%60);
		//return h.toString()+'.'+this.pad(m.toString(), 2, '0');
		return h.toString()+'.'+m.toString();
	}

	this.getPriceOptions = function () {
		return { 'minPrice': this.minPrice, 'maxPrice': this.maxPrice };
	}

	this.getAirlineOptions = function () {
		return this.Dictionary.lsu;
	}
	
	this.setAttributes = function (el, arr) {
		for (var i in arr)
			el.setAttribute(i, arr[i]);
	}

  this.displayResults = function(){
		var cont;
		var html = '';
		var ins = document.getElementById(this.options.container);
		var cont;
		var html = ''; var lr_index; var lr; var le_index; var le; var i; var ls_index; var ls; var cont; var vueltas; var time_ant;
		var getHourDifference = this.getHourDifference;

		for(lr_index = 0; lr_index < this.Data[0].lp[0].lt[0].lr.length, lr = this.Data[0].lp[0].lt[0].lr[lr_index]; ++lr_index) {

			// precio por persona, no total
			//lr.p = Math.round(lr.p/this.pax);

			for(le_index = 0; le_index < lr.lb[0].le.length, le=lr.lb[0].le[le_index]; ++le_index) {

				// almaceno el min y el max price total
				if ((this.minPrice == 0) || (this.minPrice > lr.p)) this.minPrice = lr.p;
				if (this.maxPrice < lr.p) this.maxPrice = lr.p;

				// almaceno el min y el max price por aerolinea
				if ((!this.Dictionary.lsu[le.ls[0].si].minPrice) || (this.Dictionary.lsu[le.ls[0].si].minPrice > lr.p)) 
					this.Dictionary.lsu[le.ls[0].si].minPrice = lr.p;
				this.airlines
				
				if (!(lr.lb[1])) {
					lr.lb[1] = {'le': [{'ls': []}]};
				}
				
				for (var first = true, i = 0, cont = 0, vueltas = lr.lb[1].le[0]; first || (i < lr.lb[1].le.length); first = false, vueltas = lr.lb[1].le[++i]) {
					var stops = (((le.ls.length-1) > (vueltas.ls.length-1))? le.ls.length-1 : vueltas.ls.length-1);
					var selId = lr.ri+'_'+le.er+((vueltas.ls.length > 0)? '_'+vueltas.er : '');
					//html += '<li id="option'+selId+'" recommendation_id_1="'+lr.ri+'" element_id_1_1="'+le.er+'" element_id_1_2="'+((vueltas.ls.length > 0)? vueltas.er : '')+'" stops="'+stops+'" cabin="" currency="ARS" price="'+lr.p+'" departure-time-departure="'+le.ls[0].bdt+'" departure-time-arrival="'+le.ls[le.ls.length-1].edt+'" return-time-departure="'+((vueltas.ls.length > 0)? vueltas.ls[0].bdt : '')+' return-time-arrival="'+((vueltas.ls.length > 0)? vueltas.ls[vueltas.ls.length-1].edt : '')+'" airline="'+this.Dictionary.lsu[le.ls[0].si].c+'">';
					html = '';
					var newLi = document.createElement('li');
					this.setAttributes(newLi, {'id': 'option'+selId, 'recommendation_id_1': lr.ri, 'element_id_1_1': le.er, 'element_id_1_2': ((vueltas.ls.length > 0)? vueltas.er : ''), 'stops': stops, 'cabin': '', 'currency': 'ARS', 'price': lr.p, 'departure-time-departure': le.ls[0].bdt, 'departure-time-arrival': le.ls[le.ls.length-1].edt, 'return-time-departure': ((vueltas.ls.length > 0)? vueltas.ls[0].bdt : ''), 'return-time-arrival': ((vueltas.ls.length > 0)? vueltas.ls[vueltas.ls.length-1].edt : ''), 'airline': this.Dictionary.lsu[le.ls[0].si].c });
/*
					.setAttribute('id', 'option'+selId)
					.setAttribute('recommendation_id_1', lr.ri)
					.setAttribute('element_id_1_1', le.er)
					.setAttribute('element_id_1_2', ((vueltas.ls.length > 0)? vueltas.er : ''))
					.setAttribute('stops', stops)
					.setAttribute('cabin', '')
					.setAttribute('currency', 'ARS')
					.setAttribute('price', lr.p)
					.setAttribute('departure-time-departure', le.ls[0].bdt)
					.setAttribute('departure-time-arrival', le.ls.getLast().edt)
					.setAttribute('return-time-departure', ((vueltas.ls.length > 0)? vueltas.ls[0].bdt : ''))
					.setAttribute('return-time-arrival', ((vueltas.ls.length > 0)? vueltas.ls.getLast().edt : ''))
					.setAttribute('airline', this.Dictionary.lsu[le.ls[0].si].c);
*/
					html += '<div class="show-price"><span class="price"><h1><span class="currency" code="ARS">ARS</span> <span class="amount">'+lr.p+'</span></h1> </span><p>Precio Final por '+this.pax+' pasajero'+((this.pax>1)?'s':'')+'<br />Todos los imp. incluidos</p><p><strong>'+this.Dictionary.lsu[le.ls[0].si].n+'</strong><br />'+((stops > 0)? 'Hasta '+stops+' escalas' : 'Sin escalas')+'</p>&nbsp;<button code="option'+selId+'" class="btn-add-to-cart">Agregar a mi viaje</button>&nbsp;</div>';
					html += '<table cellspacing="0" class="flights"><thead><tr><th class="flight-information"></th><th class="origin">Desde</th><th class="destination">Hasta</th></tr></thead>';

					html += '<tbody class="departure" duration="'+le.fd+'" >';
					for(ls_index=0; ls_index < le.ls.length, ls = le.ls[ls_index]; ls_index++){ 
						html += '<tr airline_code="'+this.Dictionary.lsu[ls.si].c+'" airline_name="'+this.Dictionary.lsu[ls.si].n+'" departure="'+this.Dictionary.ll[ls.bli].loc+'" arrival="'+this.Dictionary.ll[ls.eli].loc+'" departure_date="'+ls.bdd+' '+ls.bdft+':00" arrival_date="'+ls.edd+' '+ls.edft+':00">';
						html += '<th>'+((ls_index == 0)? '<h2 class="pink-arrow">Ida</h2><p style="color: #4AB3DF">'+le.fd+'hs de duraci&oacute;n</p>' : '<p>'+getHourDifference(ls.bdt, time_ant.edt, (ls.edd-time_ant.bdd))+'hs de espera en aeropuerto</p>')+'</th>';
				    html += '<td class="dep"><strong>'+this.Dictionary.ll[ls.bli].cin+' | '+this.Dictionary.ll[ls.bli].loc+'</strong><br />'+ls.bdd.toString().substr(6, 2)+'/'+this.options.meses[(ls.bdd.toString().substr(4, 2)).toInt()].toString().toUpperCase().substr(0, 3)+' '+ls.bdft+'hs</td>';
				    html += '<td class="arr"><strong>'+this.Dictionary.ll[ls.eli].cin+' | '+this.Dictionary.ll[ls.eli].loc+'</strong><br />'+ls.edd.toString().substr(6, 2)+'/'+this.options.meses[(ls.edd.toString().substr(4, 2)).toInt()].toString().toUpperCase().substr(0, 3)+' '+ls.edft+'hs</td>';
						html += '</tr>';
						time_ant = ls;
					}
					html += '</tbody>';

				if (vueltas) {
						html += '<tbody class="return" duration="'+vueltas.fd+'">';
						for (ls_index=0; ls_index < vueltas.ls.length, ls=vueltas.ls[ls_index]; ls_index++) {
							html += '<tr airline_code="'+this.Dictionary.lsu[ls.si].c+'" airline_name="'+this.Dictionary.lsu[ls.si].n+'" departure="'+this.Dictionary.ll[ls.bli].loc+'" arrival="'+this.Dictionary.ll[ls.eli].loc+'" departure_date="'+ls.bdd+' '+ls.bdft+':00" arrival_date="'+ls.edd+' '+ls.edft+':00">';
							html += '<th>'+((ls_index == 0)? '<h2 class="pink-arrow">Vuelta</h2><p style="color: #4AB3DF">'+vueltas.fd+'hs de duraci&oacute;n</p>' : '<p>'+getHourDifference(ls.bdt, time_ant.edt, (ls.edd-time_ant.bdd))+'hs de espera en aeropuerto</p>')+'</th>';
					    html += '<td class="dep"><strong>'+this.Dictionary.ll[ls.bli].cin+' | '+this.Dictionary.ll[ls.bli].loc+'</strong><br />'+ls.bdd.toString().substr(6, 2)+'/'+this.options.meses[(ls.bdd.toString().substr(4, 2)).toInt()].toUpperCase().toString().substr(0, 3)+' '+ls.bdft+'hs</td>';
					    html += '<td class="arr"><strong>'+this.Dictionary.ll[ls.eli].cin+' | '+this.Dictionary.ll[ls.eli].loc+'</strong><br />'+ls.edd.toString().substr(6, 2)+'/'+this.options.meses[(ls.edd.toString().substr(4, 2)).toInt()].toUpperCase().toString().substr(0, 3)+' '+ls.edft+'hs</td>';
							html += '</tr>';
							time_ant = ls;
						}
						html += '</tbody>';
					}

					html +=' </table>';
					newLi.innerHTML  = html;
					ins.appendChild(newLi);
				}
			}
		}
		//ins.innerHTML = html;
  }

};



