var  Cinsurance = new Class({
	startdate: null,
	enddate: null,
	layer_input: null,
	errorlayer: null,
	nightcount: 0,
	popcount: null,
	initialize: function(){
		this.layer_input = new CmooLayer_insurance();
		this.errorlayer = new CmooErrorLayer();
		$('mmt_insurance_submit').addEvent('click', (this.submitClick).bind(this));
		this.startdate = new Cnaptar('startdate',$('mmt_insurance_date_from_ym'),$('mmt_insurance_date_from_d'),$('mmt_startdatecalendar'), datum.shiftDate(_currentdate,3), datum.shiftDate(_currentdate,367), this.startdateChange.bind(this));
		this.enddate = new Cnaptar('enddate',$('mmt_insurance_date_to_ym'),$('mmt_insurance_date_to_d'),$('mmt_enddatecalendar'), datum.shiftDate(_currentdate,3), datum.shiftDate(_currentdate,367), this.enddateChange.bind(this));
		for (var i=1; i<=10; i++) new Element('option',{ 'html': i, 'value': i }).inject($('mmt_insurance_count'));
		$each($$('A[class=insurance_info_ikon]'), function(link){
			var tipbox = $(link.get('rel'))
			tipbox.setStyle('display', 'block');
			tipbox.setStyle('top', ($(link).getCoordinates().top-tipbox.getCoordinates().height+215)+'px');
			tipbox.setStyle('left', ($(link).getCoordinates().left+20)+'px');
			tipbox.setStyle('display', 'none');
			link.addEvent('mouseover', function(){
				$(link.get('rel')).setStyle('display','block');
			}.bind(this));
			link.addEvent('mouseout', function(){
				$(link.get('rel')).setStyle('display','none');
			}.bind(this));
		}.bind(this));
	},
	startdateChange: function(){
		this.enddate.setNewRange(datum.shiftDate($A(this.startdate.getDate()),0),this.nightcount);
	},
	enddateChange: function(){
		this.nightcount = datum.daysBetweenDates(this.startdate.getDate(),this.enddate.getDate());
	},
	zeropad: function(value){
		return (((""+value).length == 1)?"0"+value:""+value)
	},
	insData: {
		count: null,
		date_from: null,
		date_to: null,
		region: null,
		type: null,
		totalprice: null,
		passengers: new Array()
	},
	submitClick: function(){
		this.insData.passengers=new Array();
		this.insData.totalprice=null;
		this.insData.count=$('mmt_insurance_count').get('value');
		this.insData.date_from=$('mmt_insurance_date_from_ym').get('value')+this.zeropad($('mmt_insurance_date_from_d').get('value'));
		this.insData.date_to=$('mmt_insurance_date_to_ym').get('value')+this.zeropad($('mmt_insurance_date_to_d').get('value'));
		this.insData.region=$('mmt_insurance_region').get('value');
		var cb1=$('biz_mod1').get('checked');
		var cb2=$('biz_mod2').get('checked');
		var cb3=$('biz_mod3').get('checked');
		if ((cb1===false)&&(cb2===false)&&(cb3===false)){
			this.errorlayer.pop('Kérem válasszon egy biztosítási típust!');
		}else{
			if (cb1 === true) this.insData.type="CLASSIC";
			if (cb2 === true) this.insData.type="PREMIUM";
			if (cb3 === true) this.insData.type="PRIVILEG";
			this.popcount = 0;
			this.popOne();
		}
	},
	popOne: function(){
		this.popcount++;
		this.layer_input.pop(this.popcount+'. utas adatai: ', this.onPopClose.bind(this));
	},
	onPopClose: function(){
		var firstname=$('mmt_insurance_firstname').get('value');
		var lastname=$('mmt_insurance_lastname').get('value');
		var sel = $('mmt_cmoolayer_insurance_naptar').getElements('SELECT');
		var birthdate = sel[0].get('value')+'.'+this.zeropad(sel[1].get('value'))+'.'+this.zeropad(sel[2].get('value'));
		this.addPass(this.popcount,firstname,lastname,birthdate);
		// ha van meg utas, bekerjuk, ha nincs elkuldjuk az arajanlat kerest
		if (this.popcount<this.insData.count) {
			this.popOne();
		}else{
			var priceRes = this.ajax_sendPriceReq(this.insData);
			//itt is van a valasz
			if  (priceRes["error"] != null){
				this.errorlayer.pop(priceRes['error']['message']);
			}else{
				window.location = '/vasarlas';
				this.insData.totalprice = priceRes["totalPrice"];
			}
		}
	},
	// arajanlat keres kuldese
	ajax_sendPriceReq: function(insData){
		var ret = null;
		var myJSONRemote = new Request.JSON({
			url:'/extrak/ajax/insurance_priceRequest.php',
			method:'post',
			async: false,
			data: {
				reqData: insData
			},
			onComplete: function(obj,str){
				ret=obj;
				if (ret==null) {
					alert(str);
					ret=str;
				}
			}
		}).send();
		return ret;
	},
	// egy utas hozzaadasa az utasok koze
	addPass: function(id,firstname,lastname,birthdate){
		// az id 1-el, az utasok 0-val kezdodik
		this.insData.passengers[id-1]= { firstname: firstname, lastname: lastname, birthdate: birthdate }
	}
});

window.addEvent('domready',function(){
	new Cinsurance();
});


