FLYG.ready(function(){

	/* check for flash and display sidebars accordingly */
	
	document.getElementById(DetectFlashVer(9,0,0) ? 'mapinfo' : 'noflash').style.display='block';

	/* 
	Route Map interface
	create this as a singleton since there's only ever going to be 1 instance at a time
	*/

	function writePixel(outdepdate,retdepdate,outdepair,outarrair)
	{
		// LYNCHPIN TAG
		var img=document.createElement("img");		
		var axel = Math.random()+"";
		var a = axel * 10000000000000;
		var sType = 'search_routemap';
		var sRef = a;
		var sValue = '1.00';
		var sProd = Array();			
		sProd.push('|0|1|'+outdepdate+'|flight|return|'+outdepair+'|'+outarrair+'|'+retdepdate);
		img.src='http://lypn.com/lp/?s=8f8e3c8b37d43826&a='+escape(sType)+'&b='+escape(sRef);
		+'&c='+escape(sValue)+'&d='+escape(sProd.join("!"))+'&z';
		img.style.height='1px';
		img.style.width='1px';
		document.body.appendChild(img);
		// END LYNCHPIN TAG				
	}
	
	FLYG.routemap={
		route:{},															// object to hold route data
		reset:function(){													// reset route data
			this.route={
				outdepair:'',
				outarrair:'',
				outdepdate:'',
				retdepdate:'',
				outdepclass:'E',
				retdepclass:'E',
				outdepprice:0,
				retdepprice:0,
				outdepseason:'',
				retdepseason:'',
				company:'',
				currency:'',
				reverse:false
				};
			this.disable();
			},
		cache:{
			date:{},
			tax:{}
			},
		controls:{
			intro:FLYG.$('intro'),
			options:FLYG.$('tools'),
			outdepname:FLYG.$('map_outdepair'),
			outarrname:FLYG.$('map_outarrair'),
			outdepmonth:new FLYG.FormControls.SelectBox('map_outdepmonth','Departing',true),
			outdepgrid:new FLYG.FormControls.Calendar('map_outdepgrid'),
			outdepinfo:FLYG.$('outdepinfo'),
			retdepmonth:new FLYG.FormControls.SelectBox('map_retdepmonth','Returning'),
			retdepgrid:new FLYG.FormControls.Calendar('map_retdepgrid'),
			retdepinfo:FLYG.$('retdepinfo'),
			total:FLYG.$('summary_totalprice')
			},
		load:function(outdepair,outarrair,reverse){
			this.updateInfo('out',true);
			this.updateInfo('ret',true);
			this.updateTotal();
			this.route.outdepair=outdepair;
			this.route.outarrair=outarrair;
			this.route.reverse=reverse;
			this.controls.outdepname[0].innerHTML=getAirportName(outdepair);
			this.controls.outarrname[0].innerHTML=getAirportName(outarrair);
			//if (outdepair=='DUB') this.disable("Flights departing from Dublin must be purchased from <a href='http://www.globespan.com/ire'>www.globespan.com</a>.");
			//else if (outarrair=='DUB') this.disable("Flights to Dublin must be purchased from <a href='http://www.flyglobespan.ca'>www.flyglobespan.ca</a>.");
			//else 
			this.getTax();
			},
		enable:function(){
			this.controls.intro.hide();
			this.controls.options.show();
			},
		disable:function(e){
			this.updateIntro(e);
			this.controls.intro.show();
			this.controls.options.hide();
			this.updateInfo('out',true);
			this.updateInfo('ret',true);
			},
		getTax:function(){
			var routeKey=this.route.reverse ? this.route.outarrair+this.route.outdepair : this.route.outdepair+this.route.outarrair;
			if (this.cache.tax[routeKey]) this.storeTax();
			else FLYG.Http.script(FLYG.Info.dataStore+"XML/taxes/taxFetch.asp?outdepair="+this.route[this.route.reverse ? 'outarrair' : 'outdepair']+"&outarrair="+this.route[this.route.reverse ? 'outdepair' : 'outarrair']+"&classless=1&format=json&callback=FLYG.routemap.storeTax");
			},
		storeTax:function(taxObj){
			var routeKey=this.route.reverse ? this.route.outarrair+this.route.outdepair : this.route.outdepair+this.route.outarrair;
			if (taxObj) this.cache.tax[routeKey]=taxObj;
			
			if (this.route.reverse){
				if (getAirportProperty(this.route.outdepair,"area")=="EU"){
					this.route.company="P";
					this.route.currency="€";
					}
				else{
					this.route.currency="$";
					this.route.company="X";
					}
				}
			else{
				this.route.company="E";
				this.route.currency="£";
				}
			
			if (this.cache.date[this.route.outdepair+this.route.outarrair]) this.displayDates();
			else FLYG.Http.script(FLYG.Info.rootURL+"XML/flights.asp?outdepair="+this.route.outdepair+"&outarrair="+this.route.outarrair+"&detail=1&callback=FLYG.routemap.displayDates");
			},
		getAvailList:function(startMonth,selectedMonth,leg){
			startMonth=startMonth.split("/");
			var dates=this.cache.date[this.route.outdepair+this.route.outarrair][leg];
			var avail=[], months=[];
			for (var i=0;i<dates.length;i++){
				var s=dates[i].value.split("/");
				if (startMonth[1]==s[1] && startMonth[2]==s[2]) avail.push(dates[i].value);
				if (months.indexOf(s[1]+"/"+s[2])==-1) months.push(s[1]+"/"+s[2]);
				}
			months=months.map(function(x){
				var z=x.split("/");
				return {name:FLYG.Date.months[FLYG.Info.lang || 'EN'][(z[0]*1)-1]+' '+z[1],value:x,selected:(x==selectedMonth)};
				});
			return [avail,months];
			},
		displayDates:function(dateObj){
			if (dateObj){
				if (dateObj.error){
					this.reset();
					this.updateIntro(dateObj.error);
					return;
					}
				else this.cache.date[this.route.outdepair+this.route.outarrair]=dateObj;
				}
			else dateObj=this.cache.date[this.route.outdepair+this.route.outarrair];
			
			this.enable();
			var self=this;
			
			var type=['out','ret'];
			type.forEach(function(leg){
				var m=dateObj[leg][0].value.split("/");
				var monthList=self.updateCalendar(leg,m[1]+"/"+m[2]);
				self.controls[leg+'depmonth'].populate(monthList);
				if (leg=='ret') self.controls.retdepmonth.control[0].selectedIndex=1;
				self.controls[leg+'depmonth'].rename();
				});
			},
		updateCalendar:function(leg,m){
			if (!m){
				m=this.controls[leg+"depmonth"].getValue();
				if (!m){
					if (leg=='out'){
						m=this.controls[leg+"depmonth"].control[0].options[1].value;
						this.controls[leg+"depmonth"].setValue(m);
						}
					else return false;
					}
				}
			var m='01/'+m;
			var availList=this.getAvailList(m,m,leg);
			this.controls[leg+'depgrid'].populate(m,availList[0]);
			return availList[1];
			},
		selectDate:function(leg,d){
			FLYG.$(this.controls[leg+'depgrid'].control[0]).$byTag('td').removeClass('current');
			FLYG.$(d).addClass('current');
			this.route[leg+'depdate']=(d.innerHTML.length==1 ? "0"+d.innerHTML : d.innerHTML)+"/"+this.controls[leg+'depmonth'].getValue();
			this.updateInfo(leg);
			},
		updateInfo:function(leg,clear){
			if (clear){
				this.route[leg+'depclass']='E';
				this.route[leg+'depprice']=0;
				this.controls[leg+'depinfo'].hide();
				}
			else{
				var dt=this.route[leg+'depdate'];
				var dates=this.cache.date[this.route.outdepair+this.route.outarrair][leg];
				for (var i=0;i<dates.length;i++){
					if (dates[i].value==dt){
						flt=dates[i];
						break;
						}
					}
					
				var self=this;
				
				this.route[leg+'depseason']=getSeason(this.route.outdepair,this.route.outarrair,dt);
				var routeKey=this.route.reverse ? this.route.outarrair+this.route.outdepair : this.route.outdepair+this.route.outarrair;
				
				var tax={'E':0,'B':0};
				var taxdepair=this.route.reverse ? this.route.outarrair : this.route.outdepair;
				
				tax.E=this.cache.tax[routeKey][taxdepair+this.route[leg+'depseason']+this.route.company];
				tax.E=leg=='out' ? tax.E.ow : tax.E.re-tax.E.ow;
				if (flt.prices.B){
					tax.B=this.cache.tax[routeKey][getAirportProperty(taxdepair,"business")+this.route[leg+'depseason']+this.route.company];
					tax.B=leg=='out' ? tax.B.ow : tax.B.re-tax.B.ow;
					}
				
				dn=flt.text.split(" ");
				var tbl=FLYG.$(leg+'depinfo').show().children()[0];
				tbl.rows[0].cells[1].innerHTML=[dn[0],dn[1],dn[2].substring(0,3),dn[3]].join(" ");
				tbl.rows[1].cells[1].innerHTML=flt.depart;
				tbl.rows[2].cells[1].innerHTML=flt.arrive;
				if (flt.stops){
					tbl.rows[3].cells[1].innerHTML=flt.stops.map(function(s){
						return getAirportName(s);
						}).join(", ");
					tbl.rows[3].className='tableRow';
					}
				else{
					tbl.rows[3].className='none';
					}
				var prcCells=FLYG.$(tbl.rows[4].cells[0]).children();
				if (flt.prices.B){
					prcCells[0].style.display='none';
					prcCells[1].style.display='block';
					prcCells[1].options[0].value=flt.prices.E ? flt.prices.E[this.route.company]+tax.E : 0;
					prcCells[1].options[1].value=flt.prices.B[this.route.company]+tax.B;
					prcCells[1].onchange=function(){
						var val=this.options[this.selectedIndex].value*1;
						tbl.rows[4].cells[1].innerHTML=val ? "£"+(val).toFixed(2) : 'Sold Out';
						self.route[leg+'depprice']=this.options[this.selectedIndex].value*1;
						self.route[leg+'depclass']=(!this.selectedIndex ? "E" : "B");
						self.updateTotal();
						};
					prcCells[1].selectedIndex=0;
					}
				else{
					prcCells[0].style.display='block';
					prcCells[1].style.display='none';
					}
				if (flt.prices.E){
					tbl.rows[4].cells[1].innerHTML="£"+(flt.prices.E[this.route.company]+tax.E).toFixed(2);
					this.route[leg+'depprice']=flt.prices.E[this.route.company]+tax.E;
					}
				else{
					this.route[leg+'depprice']=0;
					tbl.rows[4].cells[1].innerHTML='Sold Out';
					}
				this.route[leg+'depclass']="E";
				this.updateTotal();
				}
			},
		updateTotal:function(){
			this.controls.total[0].innerHTML=this.route.currency+(this.route.outdepprice+this.route.retdepprice).toFixed(2);
			},
		updateIntro:function(e){
			if (this.controls.intro) this.controls.intro[0].innerHTML=e ? "Sorry, we cannot display flight information for this route due to the following reason: "+e : "Choose a route on the map to view flight times &amp; prices.";
			}
		};

	FLYG.routemap.reset();
	
	FLYG.routemap.controls.outdepmonth.control.change(function(){
		FLYG.routemap.updateCalendar('out');
		FLYG.routemap.updateInfo('out',true);
		FLYG.routemap.controls.outdepmonth.rename();
		});
		
	FLYG.routemap.controls.retdepmonth.control.change(function(){
		var status=!!FLYG.routemap.updateCalendar('ret');
		if (!status){
			FLYG.routemap.route.retdepdate='';
			FLYG.routemap.route.retdepprice=0;
			FLYG.routemap.updateTotal();
			}
		FLYG.routemap.updateInfo('ret',true);
		FLYG.routemap.controls.retdepgrid.control[status ? 'show' : 'hide']();
		FLYG.routemap.controls.retdepmonth.rename();
		});
		
	FLYG.routemap.controls.outdepgrid.control.click(function(e){
		var target=FLYG.Event.target(e);
		if (target.className.match(/avail|current/)) FLYG.routemap.selectDate('out',target);
		});
	FLYG.routemap.controls.retdepgrid.control.click(function(e){
		var target=FLYG.Event.target(e);
		if (target.className.match(/avail|current/)) FLYG.routemap.selectDate('ret',target);
		});
		
	FLYG.$('submitbkg').click(function(e){
		this.blur();
		FLYG.Event.handleBubble(e,false);
		
		var r=FLYG.routemap.route;
		if (!r.outdepdate || !r.outdepprice) return;
		
		var outdepair=r.outdepair, outarrair=r.outarrair, retdepair=r.retdepprice ? r.outarrair : "", retarrair=r.retdepprice ? r.outdepair : "";
			
		if (r.outdepclass=='B'){
			outdepair=getAirportProperty(r.outdepair,"business") || outdepair;
			outarrair=getAirportProperty(r.outarrair,"business") || outarrair;
			}
		if (r.retdepclass=='B'){
			retdepair=getAirportProperty(r.outarrair,"business") || retdepair;
			retarrair=getAirportProperty(r.outdepair,"business") || retarrair;
			}
		
		writePixel(r.outdepdate,r.retdepdate,outdepair,outarrair);
		
		FLYG.$('outdepdata')[0].value=r.outdepdate+"|"+outdepair+"|"+outarrair+"|||||"+r.outdepprice;
		FLYG.$('retdepdata')[0].value=r.retdepdate+"|"+retdepair+"|"+retarrair+"|||||"+r.retdepprice;
		FLYG.$('bkg')[0].submit();
		});
		
	});