var 
  current_search_provider = null,
  SEARCH_SWITCH_SPEED = 350;

search_providers = {
  
  plane_ticket:{
    name:'plane_ticket',
    color:'#FFF196',
    url:'http://flight1.onlinetravel.ch/cgi-bin/flightmore?agentc=stahu&language=hu&agentc=stahu&mode=3&mask=fastbook&flugart=R&klasse=Y&carrc=&child=0&infant=0',
    
    show_search:function() {
      $('#options_for_plane_ticket').fadeIn( SEARCH_SWITCH_SPEED )
      $('#options_for_birthday').hide(  )
      $('#options_for_hotel').hide( )

      
      $('#youth_passangers').change(this.birthday_display)
      $('#student_passangers').change(this.birthday_display)
      
      this.birthday_display() // Show if already set
    },
    
    hide_search:function() {
      $('#options_for_plane_ticket').hide()
      $('#options_for_birthday').hide()
      
      $('#youth_passangers').unbind( 'change', this.birthday_display )
      $('#student_passangers').change( 'change', this.birthday_display )      
      
    },
    
    do_search:function() {
  		var output = {}
  		output.fromt = 'other'
  		output.fromtother = $('#from').val();

  		output.destt = $('#destination').val();			
  		output.hdate1 = $('#hdate1').val();		
  		output.hdate2 = $('#hdate2').val();
  		output.rdate1 = $('#rdate1').val();			
  		output.rdate2 = $('#rdate2').val();
  		output.adult = $('#adult_passangers').val();
  		output.student = $('#student_passangers').val();	
  		output.youth = $('#youth_passangers').val();			

  		output.pday = $('#pday').val();			
  		output.pmonth = $('#pmonth').val();					
  		output.pyear = $('#pyear').val();

      open_url(this.url, output)
    },
    
    
    birthday_display:function(){
        var action_name = 'fadeOut'
        if ( $('#youth_passangers').val() > "0" || $('#student_passangers').val() > "0") 
            action_name = "fadeIn"

        $('#options_for_birthday')[action_name]( SEARCH_SWITCH_SPEED )
    }
    
  },
  
  
  
  plane_ticket_and_hotel: {
    name:'plane_ticket_and_hotel',
    color:'#87D9FF',
    url:'http://montreal.onlinetravel.ch/hotel/session/default/hotel.cgi?Customer=sta-hu',
    show_search:function() {
      $('#options_for_plane_ticket').hide(  )
      $('#options_for_birthday').hide(  )
      $('#options_for_hotel').fadeIn( SEARCH_SWITCH_SPEED)

    },
    
    hide_search:function() {
      $('#options_for_hotel').hide( )
    },
    
    do_search:function() {
      
  		var output = {}
 		
      
  		output.Departure = $('#from').val()
  		output.Destination = utazzolcson.target_city_codes[ $('#destination').val() ]

      
  		output.StartDate = this.pack_date_to_dotted_format( 'h' )
  		output.ReturnDate = this.pack_date_to_dotted_format( 'r' )
      
      output.Room1 = $('roomtype').val()

      
      open_url(this.url, output)   
    },
    
  	// prefix: "h" or "r"
  	pack_date_to_dotted_format: function(prefix) {
  		var day = $('#' + prefix + 'date1').val()
  		var combi_year_and_month = $('#' + prefix + 'date2').val()
      
  		var month = combi_year_and_month.substr(4,2)
  		var year = combi_year_and_month.substr(0,4)
  		return day + '.' + month + '.' + year
  	}
  }
}



function switch_search( target_search_type ) {

    var search_provider = search_providers[ target_search_type ];
    if (search_provider == null) return false;
	if (search_provider == current_search_provider) return false;

    $('#' + target_search_type).addClass('selected');
    

    if (current_search_provider != null) {
      current_search_provider.hide_search();
      $('#' + current_search_provider.name).removeClass('selected');
 
    } else {
      $('#plane_ticket').removeClass('selected');
    }
    
    search_provider.show_search();
    current_search_provider = search_provider;
    
    $('#search_engines').css({
        background: search_provider.color
    })
    
    return false;
}

function prefill_from_data( search_type, from, to, days) {
	$('#from').val( from );
	$('#destination').val( to );
	switch_search(search_type)
}



function do_search() {
  current_search_provider.do_search()
  return false;
}


function open_url(url, hash_to_serialize) {
		//var base_url = '/search_results.html?red='
		//var full_url = url + encodeURIComponent( '&' + $(hash_to_serialize).serialize())
   
    var str = [];
    for(var k in hash_to_serialize) {
      var value = hash_to_serialize[k]
      if (value != null) str.push(k + '=' + value)
    }
		var full_url = url + '&' + str.join('&')

		window.open( full_url , 'utazzolcson_search' )
}


quotes = {
    current_quote:0,
    quotes:[],
    quote_interval:null,
    
    init:function() {
        this.scan_quotes();
        this.current_quote = 0;
        this.quote_interval = setInterval(this.next_quote, 6000)        
    },
    
    scan_quotes:function() {
        $("#testimonials .testimonial").each(function(idx,el){
            var new_id = "quote_" + Math.round(Math.random() * 10000)
            el.id = new_id
            quotes.quotes.push(el.id)
        });
    },
    
    next_quote:function() {

        $("#" + quotes.quotes[quotes.current_quote] ).fadeOut( 200, function(){
            quotes.current_quote += Math.round(Math.random() * quotes.quotes.length) + 1
            if (quotes.current_quote >= quotes.quotes.length)
                quotes.current_quote = quotes.current_quote % quotes.quotes.length;
                
            $("#" + quotes.quotes[quotes.current_quote] ).fadeIn();
        });
    }
    

}


examples = {
    current_quote:0,
    quotes:[],
    quote_interval:null,
    
    init:function() {
        this.scan_quotes();
        this.current_quote = 0;
        this.quote_interval = setInterval(this.next_quote, 3000)        
    },
    
    scan_quotes:function() {
        $("#daily_hit a").each(function(idx,el){
            var new_id = "quote_" + Math.round(Math.random() * 10000)
            el.id = new_id
            examples.quotes.push(el.id)
        });
    },
    
    next_quote:function() {
        
        $("#" + examples.quotes[examples.current_quote] ).fadeOut( 200, function(){
            examples.current_quote += Math.round(Math.random() * quotes.quotes.length) + 1
            if (examples.current_quote >= examples.quotes.length)
                examples.current_quote = examples.current_quote % examples.quotes.length;
                
            $("#" + examples.quotes[examples.current_quote] ).fadeIn();
        });
    }
    

}


utazzolcson={}
utazzolcson.target_city_codes = {
    'Alicante':'ALC',
    'Amsterdam':'AMS',
    'Athen':'ATH',
    'Bangkok':'BKK',
    'Barcelona':'BCN',
    'Berlin':'BER',
    'Bilbao':'BIO',
    'Boston':'BOS',
    'Brindisi':'BDS',
    'Brüsszel':'BRU',
    'Budapest':'BUD',
    'Buenos Aires':'BUE',
    'Casablanca':'CAS',
    'Catania':'CTA',
    'Chicago':'CHI',
    'Delhi':'DEL',
    'Denpasar':'DPS',
    'Dubai':'DXB',
    'Dublin':'DUB',
    'Düsseldorf':'DUS',
    'Edinburgh':'EDI',
    'Faro':'FAO',
    'Firenze':'FLR',
    'Fortaleza':'FOR',
    'Genova':'GOA',
    'Hamburg':'HAM',
    'Hannover':'HAJ',
    'Havanna':'HAV',
    'Helsinki':'HEL',
    'Hongkong':'HKG',
    'Honolulu':'HNL',
    'Ibiza':'IBZ',
    'Isztambul':'IST',
    'Jakarta':'CGK',
    'Jerez':'XRY',
    'Cairo':'CAI',
    'Fokváros':'CPT',
    'Kho Samui':'USM',
    'Köln':'CGN',
    'Koppenhága':'CPH',
    'Kuala Lumpur':'KUL',
    'Lamezia Terme':'SUF',
    'Las Vegas':'LAS',
    'Lima':'LIM',
    'Lisszabon':'LIS',
    'London':'LON',
    'Los Angeles':'LAX',
    'Luxembourg':'LUX',
    'Madrid':'MAD',
    'Milano':'MIL',
    'Malaga':'AGP',
    'Málta':'MLA',
    'Manchester':'MAN',
    'Manila':'MNL',
    'Marseille':'MRS',
    'Mexico':'MEX',
    'Miami':'MIA',
    'Montreal':'YMX',
    'Mumbai':'BOM',
    'München':'MUC',
    'Murcia':'MJV',
    'Nápoly':'NAP',
    'New York':'NYC',
    'Nizza':'NCE',
    'Nürnberg':'NUE',
    'Orlando':'ORL',
    'Osaka':'KIX',
    'Palermo':'PMO',
    'Palma de Mallorca':'PMI',
    'Párizs':'PAR',
    'Peking':'PEK',
    'Perth':'PER',
    'Phuket':'HKT',
    'Porto':'OPO',
    'Prága':'PRG',
    'Recife':'REC',
    'Reus':'REU',
    'Rio de Janeiro':'GIG',
    'Róma':'ROM',
    'Saigon':'SGN',
    'Salzburg':'SZG',
    'San Diego':'SAN',
    'San Francisco':'SFO',
    'San Jose':'SJO',
    'Sao Paulo':'SAO',
    'Seattle':'SEA',
    'Sevilla':'SQV',
    'Shanghai':'PVG',
    'Szingapúr':'SIN',
    'Stockholm':'STO',
    'Sydney':'SYD',
    'Tel Aviv':'TLV',
    'Tokyo':'TYO',
    'Toronto':'YYZ',
    'Tunis':'TUN',
    'Valencia':'VLC',
    'Vancouver':'YVR',
    'Velence':'VCE',
    'Verona':'VRN',
    'Washington':'WAS',
    'Bécs':'VIE',
    'Frankfurt':'FRA',
    'Funchal':'FNC',
    'Glasgow':'GLA',
    'Göteborg':'GOT',
    'Granada':'GRX',
    'Oslo':'OSL',
    'Zürich':'ZRH',
    'Genf':'GVA',
    'Bázel':'BSL',
    'Varsó':'WAW',
    'Krakkó':'KRK',
    'Tallinn':'TLL',
    'Riga':'RIX',
    'Vilnius':'VNO',
    'Larnaca':'LCA',
    'Bukarest':'BUH',
    'Szófia':'SOF',
    'Kijev':'KBP',
    'Moszkva':'MOW',
    'Dubrovnik':'DBV',
    'Szarajevó':'SJJ',
    'Thessaloniki':'SKG',
    'Bejrút':'BEY',
    'Damaszkusz':'DAM',
    'Varna':'VAR',
    'Tirana':'TIA',
    'Split':'SPU',
    'Jekatyerinburg':'SVX',
    'Ogyessza':'ODS',
    'Kolozsvár':'CLJ',
    'Temesvár':'TSR',
    'Marosvásárhely':'TGM',
    'Szkopje':'SKP',
    'Malmö':'MMX',
    'Korfu':'CFU',
    'Heraklion':'HER',
    'Rhodosz':'RHO',
    'Porto':'OPO'
}
    
utazzolcson.target_cities = function(){
    var o = []
    for( var k in utazzolcson.target_city_codes) {
        o.push(k)
    }
    return o
}()

function findValue(li) {
	if( li == null ) return alert("No match!");
	else var sValue = li.selectValue;
}

function selectItem(li) {
	findValue(li);
}


function lookupLocal(){
	var oSuggest = $("#destination")[0].autocompleter;
	oSuggest.findValue();
	return false;
}



$(document).ready(function(){
    quotes.init()
    examples.init()
    $("#destination").autocompleteArray( utazzolcson.target_cities,
        {
            delay:10,
            minChars:1,
            matchSubset:1,
            onItemSelect:selectItem,
            onFindValue:findValue,
            autoFill:false,
            maxItemsToShow:10
        } )

    $('#destination').focus().select();
    // $('#head').click(function(){
    //   $('#head').toggleClass('zoo');
    // })
    // 
    $('#search_form').submit( do_search )
    current_search_provider = search_providers.plane_ticket
	quotes.next_quote()
	examples.next_quote()
	switch_search('plane_ticket_and_hotel')
	
	var start_date = new Date();
	var end_date = new Date();
	
	start_date.setFullYear( start_date.getFullYear(), start_date.getMonth(), start_date.getDate() + 5)
	end_date.setFullYear( start_date.getFullYear(), start_date.getMonth(), start_date.getDate() + 5)
	
	// alert( '' + start_date.getFullYear() + '' + leadingZero(start_date.getMonth() + 1))
	
	$('#hdate1').val( start_date.getDate());
	$('#hdate2').val( '' + start_date.getFullYear() + '' + leadingZero(start_date.getMonth() + 1));
	
	$('#rdate1').val( end_date.getDate());
	$('#rdate2').val( '' + end_date.getFullYear() + '' + leadingZero(end_date.getMonth() + 1));
})

function leadingZero(num) {
	if (num > 9) return '' + num
	return '0' + num
}


var insurance_is_america = false

var insurance_is_europe = false
var insurance_shorter_then_three_weeks = false
 
function redirect_insurance(type)
{
	var url = ''
    if (type == 'CareMed') {
        url = 'http://statravel.hu/hu/offertypes/show/51'
    }
    if (type == 'caremed-am') {
        url = 'http://statravel.hu/hu/offertypes/show/52'
    }
	if (type == 'Euromondial') {
		url = 'http://statravel.hu/hu/offertypes/show/31'
	}

    window.open(url, 'utazzolcson_insurance')
}
 
function getContinent()
{
    // alert($('#search_continent').val())    
  insurance_is_europe = (($('#search_continent').val() == "Európa") || ($('#search_continent').val() == "Europe"))
  insurance_is_america = (($('#search_continent').val() == "Észak-Amerika") || ($('#search_continent').val() == "North America"))
}
 
function longer_then_two_weeks()
{
      getContinent()
      if (insurance_is_america) {
        redirect_insurance('caremed-am')  
      } else {
    redirect_insurance('CareMed')
    }
}
 
function shorter_then_two_weeks()
{
  getContinent()
  if (insurance_is_america) {
    redirect_insurance('caremed-am')  
  } else if (!insurance_is_europe) {
    redirect_insurance('CareMed')
  }
  else
  {
    redirect_insurance('Euromondial')
  }
}

/* insurances */


