$(document).ready(function(){
	    $("button").button();
	    $("#tabs").tabs();
	    
	    $("#dialog").dialog({
	    	autoOpen: false,
			resizable: false,
			height:140,
			modal: true,
			close: function() {
				$('#tabs').tabs('select' , 4);
			},
			buttons: {
				'Ja': function() {
					$(this).dialog('close');
					document.location.href = "index.php?s=backpage&bp=flug";
				},
				'Nein': function() {
					$(this).dialog('close');
					$('#tabs').tabs('select' , 4);
				}
			}
		});

	    $('#tabs').bind('tabsselect', function(event, ui){
		    switch(ui.tab.id){
			    case 'PackageList':
			    	url.setRequest('zts/xml/GetPackageDestinationsWithoutA.aspx.xml');
			    	url.setIFrame('http://id-reisewelt.zts.net/Deeplink/PackageList.aspx');
			    	url.setType('P');
			    	$("#activity").attr("disabled", true);
			    	$(".activity").css("display", "none");
				    break;
			    case 'FlightList':
			    	$('#dialog').dialog('open');
			    	//url.setRequest('zts/xml/GetFlightDestinations.aspx.xml');
			    	//url.setIFrame('http://id-reisewelt.zts.net/Deeplink/FlightList.aspx');
				    break;
			    case 'HotelList':
			    	url.setRequest('zts/xml/GetHotelDestinations.aspx.xml');
			    	url.setIFrame('http://id-reisewelt.zts.net/Deeplink/HotelList.aspx');
			    	url.setType('H,FH,FW');
			    	$("#activity").attr("disabled", true);
			    	$(".activity").css("display", "none");
				    break;
			    case 'CruiseList':
			    	url.setRequest('zts/xml/GetCruiseDestinations.aspx.xml');
			    	url.setIFrame('http://id-reisewelt.zts.net/Deeplink/CruiseList.aspx');
			    	url.setType('KA');
			    	$("#activity").attr("disabled", true);
			    	$(".activity").css("display", "none");
				    break;
			    case 'TourList':
			    	url.setRequest('zts/xml/GetTourDestinations.aspx.xml');
			    	url.setIFrame('http://id-reisewelt.zts.net/Deeplink/TourList.aspx');
			    	url.setType('TA');
			    	$("#activity").attr("disabled", true);
			    	$(".activity").css("display", "none");
				    break;
			    case 'RoundtripList':
			    	url.setRequest('zts/xml/GetRoundTripDestinations.aspx.xml');
			    	url.setIFrame('http://id-reisewelt.zts.net/Deeplink/RoundtripList.aspx');
			    	url.setType('R');
			    	$("#activity").attr("disabled", true);
			    	$(".activity").css("display", "none");
				    break;
			    case 'ActivityList':
			    	//url.setRequest('zts/xml/GetActivityDestinations.aspx.xml');
			    	url.setRequest('zts/xml/GetPackageDestinationsWithA.aspx.xml');
			    	//url.setIFrame('http://id-reisewelt.zts.net/Deeplink/ActivityList.aspx');
			    	url.setIFrame('http://id-reisewelt.zts.net/Deeplink/packagelist.aspx');
			    	//url.setType('WEL,SKP,TAK,FIT,GOK');
			    	url.setType('H,FH,FW');
			    	$("#activity").attr("disabled", false);
			    	$(".activity").css("display", "");
				    break;
			    default:
			    	url.setRequest('zts/xml/GetHotelDestinations.aspx.xml');
			    	url.setIFrame('http://id-reisewelt.zts.net/Deeplink/HotelList.aspx');
			    	url.setType('H,FH,FW');
			    	$("#activity").attr("disabled", true);
			    	$(".activity").css("display", "none");
		    }
		    
		    if(!$("#activity").attr("disabled")){
		      refresh_act(0);
	      }else{
	        $('#region').find('option').each(function(){
            $(this).remove();
          });
          $("#activity").append("<option value=''>ALLE</option>");
          
	        refresh(0);
	      }
		    getValidDate(url.getType());
		 });

	    $('#tabs').tabs("select" , 2);
	    $('#tabs').tabs("select" , 0);
	    
	    $('#datepicker').datepicker({
	    showOn: "button",
      buttonImage: "images/calendar.gif",
      buttonImageOnly: true,
      
			changeMonth: true,
			changeYear: true,
			dateFormat: 'dd.mm.yy',
			showAnim: 'fold',
			
			closeText: 'schließen',
			prevText: '&#x3c;zurück',
			nextText: 'Vor&#x3e;',
			currentText: 'heute',
			monthNames: ['Januar','Februar','März','April','Mai','Juni',
			'Juli','August','September','Oktober','November','Dezember'],
			monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
			'Jul','Aug','Sep','Okt','Nov','Dez'],
			dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
			dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			weekHeader: 'Wo',
			dateFormat: 'dd.mm.yy',
			firstDay: 1,
			isRTL: false,
			showMonthAfterYear: true,
			yearSuffix: ''
		});
	    
	    $('#datepicker2').datepicker({
	    showOn: "button",
      buttonImage: "images/calendar.gif",
      buttonImageOnly: true,

			changeMonth: true,
			changeYear: true,
			dateFormat: 'dd.mm.yy',
			showAnim: 'fold',
			
			closeText: 'schließen',
			prevText: '&#x3c;zurück',
			nextText: 'Vor&#x3e;',
			currentText: 'heute',
			monthNames: ['Januar','Februar','März','April','Mai','Juni',
			'Juli','August','September','Oktober','November','Dezember'],
			monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
			'Jul','Aug','Sep','Okt','Nov','Dez'],
			dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
			dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			weekHeader: 'Wo',
			dateFormat: 'dd.mm.yy',
			firstDay: 1,
			isRTL: false,
			showMonthAfterYear: true,
			yearSuffix: ''
		});
	    
	});


  $(function(){
    $("select#activity").change(function(){
      if(!$("#activity").attr("disabled")){
          refresh_act(2);
        }else{
          refresh(2);
        }
      $('#country option:first').attr('selected', 'selected');
      $('#region option:first').attr('selected', 'selected');
      $('#destination option:first').attr('selected', 'selected');
    });
  });
  
	$(function(){
		$("select#country").change(function(){
			if(!$("#activity").attr("disabled")){
          refresh_act(3);
        }else{
          refresh(3);
        }
			$('#region option:first').attr('selected', 'selected');
			$('#destination option:first').attr('selected', 'selected');
		});
	});
	  
	$(function(){
		$("select#region").change(function(){
			if(!$("#activity").attr("disabled")){
          refresh_act(4);
        }else{
          refresh(4);
        }
			$('#destination option:first').attr('selected', 'selected');
		});
	});
	
	
	function getValidDate(type){
		$.ajax({
			type: 'POST',
	    	url: 'include/ibe/getValidDates.php',
	    	data: 'type=' + type,
	    	dataType: 'xml',
    	  	success: function(data){
				date = $(data).find('Date');
				dateFrom =  new Date(date.attr('YearFrom'), date.attr('MonthFrom'), date.attr('DayFrom'));
				dateFrom2 =  new Date(date.attr('YearFrom2'), date.attr('MonthFrom2'), date.attr('DayFrom2'));
				dateTo =  new Date(date.attr('YearTo'), date.attr('MonthTo'), date.attr('DayTo'));
				
				if(date.attr('validated') == "false"){
					$("#datepicker").datepicker( "option", "disabled", true );
					$("#datepicker").attr("disabled", true);
					
					$("#datepicker").val('');
					
					$("#datepicker2").datepicker( "option", "disabled", true );
					$("#datepicker2").attr("disabled", true);
					
					$("#datepicker2").val('');
				}else{
					$("#datepicker").datepicker( "option", "disabled", false );
					$("#datepicker").attr("disabled", false);

					$("#datepicker").datepicker("option", "minDate", dateFrom);
	    			$("#datepicker").datepicker("option", "maxDate", dateTo);
	    			$("#datepicker").val($.datepicker.formatDate('dd.mm.yy', $("#datepicker").datepicker("option", "minDate")));
	    			
	    			$("#datepicker2").datepicker( "option", "disabled", false );
					$("#datepicker2").attr("disabled", false);

					$("#datepicker2").datepicker("option", "minDate", dateFrom);
	    			$("#datepicker2").datepicker("option", "maxDate", dateTo);
	    			$("#datepicker2").val($.datepicker.formatDate('dd.mm.yy', $("#datepicker2").datepicker("option", "minDate")));
	    			$("#datepicker2").val($.datepicker.formatDate('dd.mm.yy', dateFrom2));
	    			//console.log($.datepicker.formatDate('dd.mm.yy', $("#datepicker2").datepicker("option", "minDate")));
				}
	    	}
	    });
	}
	
	
	
	 function refresh(level, co_selected){
    $.ajax({
        type: "GET",
      url: url.getRequest(),
      dataType: "xml",
      success: function(xml){
        var country = $('#country');
        var region = $('#region');
        var destination = $('#destination');
        
        if(level == 0){
          $('#country').find('option').each(function(){
            $(this).remove();
          });
          $('#region').find('option').each(function(){
            $(this).remove();
          });
          $('#destination').find('option').each(function(){
            $(this).remove();
          });
        }
        
        // Country
        if(level <= 1){
          country.append("<option value=''>ALLE</option>");
          region.append("<option value=''>ALLE</option>");
          destination.append("<option value=''>ALLE</option>");
        }
        $(xml).find('Country').each(function(){
          var co_id = $(this).attr('COID');
          var co_value = $(this).attr('Title');
          if(co_id == co_selected){
            co_sel = 'selected="selected"';
          }else{
            co_sel = '';
          }
          if(level <= 2){
            country.append("<option value='"+ co_id +"' " + co_sel + ">" + co_value + "</option>");
          }
          
          // Region
          if(co_id == country[0].value){
            if(level <= 3){
              $('#region').find('option').each(function(){
                $(this).remove();
              });
              region.append("<option value=''>ALLE</option>");
            }
            
            $(this).find('Region').each(function(){
              var rg_id = $(this).attr('RGID');
              var rg_value = $(this).attr('Title');
              if(level <= 3){
                region.append("<option value='"+ rg_id +"'>" + rg_value + "</option>");
              }
              
              // Destination
              if(rg_id == region[0].value){
                $('#destination').find('option').each(function(){
                  $(this).remove();
                });
                destination.append("<option value=''>ALLE</option>");
                $(this).find('Destination').each(function(){
                  var de_id = $(this).attr('DEID');
                  var de_value = $(this).attr('Title');
                  destination.append("<option value='"+ de_id +"'>" + de_value + "</option>");
                });
              }
            });
          }
        });
      }
    });
  }
	
	
	 function refresh_act(level, co_selected){
    $.ajax({
      type: "GET",
      url: url.getRequest(),
      dataType: "xml",
      success: function(xml){
        var activity = $('#activity');
        var country = $('#country');
        var region = $('#region');
        var destination = $('#destination');
        
        if(level == 0){
          $('#activity').find('option').each(function(){
            $(this).remove();
          });
          $('#country').find('option').each(function(){
            $(this).remove();
          });
          $('#region').find('option').each(function(){
            $(this).remove();
          });
          $('#destination').find('option').each(function(){
            $(this).remove();
          });
        }
        
        // Activity
        if(level <= 1){
          activity.append("<option value=''>ALLE</option>");
          country.append("<option value=''>ALLE</option>");
          region.append("<option value=''>ALLE</option>");
          destination.append("<option value=''>ALLE</option>");
        }
        $(xml).find('ActivityType').each(function(){
          var act_id = $(this).attr('ACT');
          var act_value = $(this).attr('Title');
          if(level <= 1){
            activity.append("<option value='"+ act_id +"'>" + act_value + "</option>");
          }
          
          
          // Country
          if(act_id == activity[0].value){
            if(level <= 2){
              $('#country').find('option').each(function(){
                $(this).remove();
              });
              country.append("<option value=''>ALLE</option>");
            }
            
            $(this).find('Country').each(function(){
              var co_id = $(this).attr('COID');
              var co_value = $(this).attr('Title');
              if(level <= 2){
                country.append("<option value='"+ co_id +"'>" + co_value + "</option>");
              }
              // Region
              if(co_id == country[0].value){
                if(level <= 3){
                  $('#region').find('option').each(function(){
                    $(this).remove();
                  });
                  region.append("<option value=''>ALLE</option>");
                }
                
                $(this).find('Region').each(function(){
                  var rg_id = $(this).attr('RGID');
                  var rg_value = $(this).attr('Title');
                  if(level <= 3){
                    region.append("<option value='"+ rg_id +"'>" + rg_value + "</option>");
                  }
                  
                  // Destination
                  if(rg_id == region[0].value){
                    $('#destination').find('option').each(function(){
                      $(this).remove();
                    });
                    destination.append("<option value=''>ALLE</option>");
                    $(this).find('Destination').each(function(){
                      var de_id = $(this).attr('DEID');
                      var de_value = $(this).attr('Title');
                      destination.append("<option value='"+ de_id +"'>" + de_value + "</option>");
                    });
                  }
                });
              }
            });
          }
        });
      }
    });
  }

	function showList(){
	  var activity = $('#activity');
		var country = $('#country');
		var region = $('#region');
		var destination = $('#destination');
		
		if($('#datepicker')[0].value !=''){
			var date = $.datepicker.formatDate('yymmdd', $.datepicker.parseDate('dd.mm.yy', $('#datepicker')[0].value));
		}else{
			var date = '';
		}
		var nights = ($.datepicker.parseDate('dd.mm.yy', $('#datepicker2')[0].value) - $.datepicker.parseDate('dd.mm.yy', $('#datepicker')[0].value)) / 86400000;

    if(!$("#activity").attr("disabled")){
      var act = '&ACT=' + activity[0].value;
    }else{
      var act = '';
    }
    
    if(1){
      var persons = '&Adults=2&Child1=8&Child2=&Child3=';
    }else{
      var persons = '';
    }

		if(country[0].value == ''){
			var ifUrl = url.getIFrame() + '?ALL=1' + '&DATE=' + date + '&NIGHTS=' + nights + act;
		}else{
			var ifUrl = url.getIFrame() + '?COID=' + country[0].value + '&RGID=' + region[0].value + '&DEID=' + destination[0].value + '&DATE=' + date + '&NIGHTS=' + nights + act;
		}
		$('#_iframe').attr('src', ifUrl);
	}
	
	var url = {
		iframe: 'http://id-reisewelt.zts.net/Deeplink/HotelList.aspx',
		req: 'zts/xml/GetHotelDestinations.aspx.xml',
		type: '',

		setIFrame: function(v) { this.iframe = v; },
		setRequest: function(v) { this.req = v; },
		setType: function(v) { this.type = v; },

		getIFrame: function() { return this.iframe; },
		getRequest: function() { return this.req; },
		getType: function() { return this.type; }
	}
