
worldRoller = {

	obj : null,

	started : false,

	direction : 0,

	arriveCountry : null,

	departCountry : null,

	arriveAirport : null,

	departAirport : null,

	arriveAirportCode : null,

	departAirportCode : null,

	reset : function(){

        self.formframe.location.href  = "/vg1/timetable/maplowcostform.action";
        this.hideObject(get("formframe"))
        this.hideObject(get('selection'))
        this.hideSelect(get('arrive'))
        this.hideSelect(get('depart'))
        this.started = false
        this.direction = 0
        this.arriveAirport = this.departAirport = this.arriveAirportCode = this.departAirportCode = this.departCountry = this.arriveCountry = null
        this.updateHelper()
        get('arrive_content').innerHTML = ""
        get('depart_content').innerHTML = ""
        this.hideObject(get('foot'))


    },

	setObject : function(anObj) {
		worldRoller.obj = anObj
	},

	departCountryWasSelectedFromSelect : function(value){
		worldRoller.obj.departCountryWasSelected(value)
	},

	arriveCountryWasSelectedFromSelect : function(value){
		worldRoller.obj.arriveCountryWasSelected(value)
	},

	startWithDepart : function(array){
		this.showObject(get('selection'))
		this.showObject(get('foot'))
		this.started = true
		this.direction = 1
		this.updateHelper()
	},

	startWithArrive : function(array){
		this.showObject(get('selection'))
		this.showObject(get('foot'))
		this.started = true
		this.direction = -1
		this.updateHelper()
	},

	arriveCityWasSelected : function(name, code){
		this.hideObject(get('depart'))
		this.hideObject(get('arrive'))
		this.arriveAirport = name
		this.arriveAirportCode = code
		this.updateHelper()
		get('arrive_content').innerHTML = name
	},

	departCityWasSelected : function(name, code){
        this.hideObject(get('depart'))
        this.hideObject(get('arrive'))
        this.departAirport = name
        this.departAirportCode = code
        get('depart_content').innerHTML = name
        this.updateHelper()
    },

	arriveCityWasDeselected : function(direct){
		if (direct){
			this.showObject(get('arrive'))
			this.hideObject(get('depart'))
		}else{
			this.hideObject(get('arrive'))
		}
		this.arriveAirportCode = null
		this.arriveAirport = null
		this.updateHelper()
		get('arrive_content').innerHTML = ""

	},

	departCityWasDeselected : function(direct){
		if (direct){
			this.showObject(get('depart'))
			this.hideObject(get('arrive'))
		}else{
			this.hideObject(get('depart'))
		}
		this.departAirport = null
		this.departAirportCode = null
		this.updateHelper()
		get('depart_content').innerHTML = ""

	},

	arriveCountryWasSelected : function(code){
		this.updateSelect(get('arrive'), code)
		this.arriveCountry = code
		this.updateHelper()
	},

	departCountryWasSelected : function(code){
		this.updateSelect(get('depart'), code)
		this.departCountry = code
		this.updateHelper()
	},

	updateHelper : function(){
		//inizio
    		if ( !this.started && !this.arriveAirport && !this.arriveCountry && !this.departAirport && !this.departCountry){
			get('title').innerHTML = eval("title_init_"+this.getLang());
			get('foot').innerHTML = eval("foot_init_"+this.getLang());
		}
		if ( this.started && this.direction == 1 && !this.arriveAirport && !this.arriveCountry && !this.departAirport && !this.departCountry){
			get('title').innerHTML = eval("title_dep1_"+this.getLang());
			get('foot').innerHTML = eval("foot_init_"+this.getLang());
		}
		if ( this.started && this.direction == -1 && !this.arriveAirport && !this.arriveCountry && !this.departAirport && !this.departCountry){
			get('title').innerHTML = eval("title_arr1_"+this.getLang());
			get('foot').innerHTML = eval("foot_init_"+this.getLang());
		}
		//Selezionata nazione partenza
		if (!this.arriveAirport && !this.arriveCountry && !this.departAirport && this.departCountry){
			get('title').innerHTML = eval("title_dep2_"+this.getLang());
			get('foot').innerHTML = eval("foot_init_"+this.getLang());
		}
		//Selezionata nazione arrivo
		if (!this.arriveAirport && this.arriveCountry && !this.departAirport && !this.departCountry){
			get('title').innerHTML = eval("title_arr2_"+this.getLang());
			get('foot').innerHTML = eval("foot_init_"+this.getLang());
		}
		//Selezionato un aeroporto partenza, non ancora selezionata nazione arrivo
		if (!this.arriveAirport && !this.arriveCountry && this.departAirport && this.departCountry){
			get('title').innerHTML = eval("title_dep3_"+this.getLang());
			get('foot').innerHTML = eval("foot_sel_"+this.getLang()) +" "+ this.colorize(this.departAirport) +" "+ eval("foot_dep_"+this.getLang()) +"<br />" +  eval("foot_desel_"+this.getLang()) +" <img src='/airports/airports_map/cancel.png' alt='cancel' />"
		}
		//Selezionato un aeroporto arrivo, non ancora selezionata nazione partenza
		if (this.arriveAirport && this.arriveCountry && !this.departAirport && !this.departCountry){
			get('title').innerHTML =eval("title_arr3_"+this.getLang());
			get('foot').innerHTML = eval("foot_sel_"+this.getLang()) +" "+ this.colorize(this.arriveAirport) +" "+ eval("foot_arr_"+this.getLang()) +"<br />" +  eval("foot_desel_"+this.getLang()) +" <img src='/airports/airports_map/cancel.png' alt='cancel' />"
		}
		//Selezionato un aeroporto partenza, selezionata nazione arrivo, non ancora selezionato aeroporto arrivo
		if (!this.arriveAirport && this.arriveCountry && this.departAirport && this.departCountry){
			get('title').innerHTML = eval("title_dep4_"+this.getLang());
			get('foot').innerHTML = eval("foot_sel_"+this.getLang()) +" "+ this.colorize(this.departAirport) +" "+ eval("foot_dep_"+this.getLang()) +"<br />" +  eval("foot_desel_"+this.getLang()) +" <img src='/airports/airports_map/cancel.png' alt='cancel' />"
		}
		//Selezionato un aeroporto arrivo, selezionata nazione partenza, non ancora selezionato aeroporto partenza
		if (this.arriveAirport && this.arriveCountry && !this.departAirport && this.departCountry){
			get('title').innerHTML = eval("title_arr4_"+this.getLang());
			get('foot').innerHTML = eval("foot_sel_"+this.getLang()) +" "+ this.colorize(this.arriveAirport) +" "+ eval("foot_arr_"+this.getLang()) +"<br />" +  eval("foot_desel_"+this.getLang()) +" <img src='/airports/airports_map/cancel.png' alt='cancel' />"
		}
		//tutto ok
		if (this.arriveAirport && this.departAirport){
			get('title').innerHTML = eval("title_end_"+this.getLang());
			get('foot').innerHTML = eval("foot_sel_"+this.getLang()) +" "+ this.colorize(this.departAirport) +" "+ eval("foot_dep_"+this.getLang()) +" " + this.colorize(this.arriveAirport) +" "+ eval("foot_arr_"+this.getLang()) +"<br />"+  eval("foot_desel_"+this.getLang()) +" <img src='/airports/airports_map/cancel.png' alt='cancel' />"
			this.showButton()
		}else{
			this.hideButton()
		}
	},

	colorize : function(str, color){
		return "<span  class='vg_text6'>" + str + "</span>"
	},

	updateAirports : function (sel, array){
		if (array){
			sel.options.length = 0
			sel.options[0] = new Option(eval("option_"+this.getLang()), "none")
			for (i = 0; i < array.length; i = i + 2) {
				sel.options[sel.options.length] = new Option(array[i],array[i+1])
			}
		}
		this.showSelect(sel)
	},

	updateSelect : function(sel, code) {
		var index = this.searchInSelectForValue(sel, code.toUpperCase())
		if (index)
			sel.selectedIndex = index
	},

	searchInSelectForValue : function(sel,code){
		for (var i = 0; i< sel.options.length; i++){
			if (sel.options[i].value == code){
				return i
			}
		}
	},

	fillDepartAirports : function(array){
		this.updateAirports(get("depart"), array)
	},

	fillArriveAirports : function(array){
		this.updateAirports(get("arrive"), array)
	},

	showSelect : function(sel){
		this.showObject(sel)
	},

	hideSelect : function(sel){
		this.hideObject(sel)
	},

	getWebPath : function(){
		return $F('wr_path');
	},
	//se calendario in un'altra pagina
	getCalPath : function(){
		return $F('wr_cal');
	},

	getZone : function(){
		return $F('wr_zone');
	},

	getLang : function(){
		return $F('wr_lang');
	},

	getAction : function(){
		return $F('wr_action');
	},

  getStrings : function(){
    return $F('wr_strings');
 },

	hideObject : function(obj) {
		obj.style.display = "none"
	},

	showObject : function(obj){
		obj.style.display = "block"
	},

	showButton : function() {
		//self.formframe.location.href  = "/vg1/timetable/maplowcost.action?searching=frame"
		this.showObject(get("formframe"))
		self.formframe.document.searchform.departureAirport.value  = worldRoller.departAirportCode
		self.formframe.document.searchform.arrivalAirport.value  = worldRoller.arriveAirportCode


	},

	hideButton : function() {
		this.hideObject(get("okbutton"))
		this.hideObject(get("formframe"))
		self.formframe.location.href = "/vg1/timetable/maplowcostform.action"
	},

	placeAppObject : function(src, width, height){
		if (AC_FL_RunContent == 0) {
			throw("This page requires AC_RunActiveContent.js.")
		} else {
			AC_FL_RunContent(
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
				'width', width,
				'height', height,
				'src', src,
				'quality', 'high',
				'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
				'align', 'middle',
				'play', 'true',
				'loop', 'true',
				'scale', 'showall',
				'devicefont', 'false',
				'id', 'appObject',
				'bgcolor', '#ffffff',
				'name', 'appObject',
				'menu', 'false',
				'allowFullScreen', 'false',
				'allowScriptAccess','sameDomain',
				'movie', src,
				'salign', ''
				)
		}
	},
	//se calendario nella stessa pagina
      gotoTimePage:function() {
    		if (this.arriveAirportCode && this.departAirportCode){
    			redrawCalDA(this.departAirportCode,this.arriveAirportCode)
    		 }
		}
}

//se calendario in un'altra pagina
function gotoTimePage(){
	if (worldRoller.arriveAirportCode && worldRoller.departAirportCode)
		window.location = worldRoller.getCalPath()+"?departureAirport="+worldRoller.departAirportCode+"&arrivalAirport="+worldRoller.arriveAirportCode;
}


function stopAndPrevent(event){
	if (event.preventDefault) {
    	event.stopPropagation();
		event.preventDefault();
    } else {
      event.returnValue = false;
      event.cancelBubble = true;
    }
}

function observe(obj, func, caption){
	caption = caption || false
	if (obj.addEventListener) {
      window.addEventListener("DOMMouseScroll", func, caption)
    } else if (obj.attachEvent) {
      obj.attachEvent('onmousewheel', func, caption);
    }
}

function worldAppInit(){
	worldRoller.obj = get("appObject")
	observe(worldRoller.obj, stopAndPrevent)
	observe(get('arrive'), stopAndPrevent)
	observe(get('depart'), stopAndPrevent)
}

DuckSoup.page.addOnLoadTask(worldAppInit)
