var search_box;
var order_box;
var view_box;
var popupWin;

var jq = jQuery.noConflict();
function prepare_form() {  
  
  var options = jq('body').data('options');
  document.getElementById('package_search_form').reset();
  document.getElementById('hike_search_form').reset();
  
  if(jq.browser.opera) { // for opera only
    jq(window).resize(function() {                                                                    
      jq('.ellipsis').css('white-space', 'normal');
      jq('.tour_search_result div:eq(0)').css('width', 'auto');
      jq('.tour_search_result div:eq(0)').css('width', jq('.tour_search_result div:eq(0)').width());
      jq('.ellipsis').css('white-space', 'nowrap');
    });
  }

  
  jq(function()  {
    jq("#date_from").datepicker({  dateFormat: 'dd.mm.y', minDate: '+1', showAnim: ''});
    jq("#date_till").datepicker({  dateFormat: 'dd.mm.y', minDate: '+1', showAnim: ''});
    jq("#hike_date_from").datepicker({  dateFormat: 'dd.mm.y', minDate: '+1', showAnim: ''});
    jq("#hike_date_till").datepicker({  dateFormat: 'dd.mm.y', minDate: '+1', showAnim: ''});
  });
  
  if(jq('#hotel_list').length) {
    jq('#itt_hotel_rating_block input').click(function(){
      hotel_refresh();
    });
    jq('#region_list').change(function(){
      hotel_refresh();
    });
  }
  
  region_name = (jq('#region_list').length)?'#region_list':'#region';

  jq('#country').change(function(){
    var hotel_rating_list  = '';
    jq('#itt_hotel_rating_block :checkbox:checked').each(function(index){
      hotel_rating_list  += jq(this).val()+' ';
    });
    jq.getJSON(options.modules_action, { 'action':'get_package_search_filtered_field'
                             , 'event_owner_level' : 1
                             , 'country_id': jq('#country').val()
                             , 'hotel_rating_id': hotel_rating_list
                             , 'tour_kind': jq('#tour_kind').val()
                             , 'tour_type' : jq('#package_search_form :radio:checked').val()
                             }
                             , function(data) {
      jq('#region_list').html(data.region);
      jq('#hotel_list').html(data.hotel);
    });
    return false;
  });
  
  jq('#children').change(function(){            
    jq(':input.age_small[id^="child"]').attr('disabled', 'disabled').hide();
    var child_count = jq('#children').val();        
    switch(child_count) {
      case '0':
        jq('div.child_age-select').hide();        
        break;
      case '3':
        jq('#child3_age').attr('disabled', '').show();
      case '2':
        jq('#child2_age').attr('disabled', '').show();
      case '1':
        jq('#child1_age').attr('disabled', '').show();
        jq('div.child_age-select').show();
        break;
    }
  });
  
  //автопоиск
  search_params = window.location.hash.toString();
  if(search_params == '') {
    search_params = '#' + jq('#module_search_query').val();
  }
  var reg = /action=([a-z_]+)/; 
  var arr = reg.exec(search_params);
  if(arr)
    var action = arr[1];
  else
    var action = null;
    
  if(action) {                      
    
    if(action == 'hike_tour_search')
      display_hike_form();
    else
      display_package_form();
          
    search_params = search_params.substr(1);
    search_params = search_params.split('&');    
    search_params_array = new Array();
    jq.each(search_params, function(index, value){
      param_one = value.split('=');
      var field = param_one[0];
      var value = jq.trim(param_one[1]);
      search_params_array[field] = value;
    });

    if(action == 'package_tour_search') {
      jq('.hotel_rating').attr('checked', '');
      jq('.food_list_input').attr('checked', '');
      if(search_params_array['hotel_rating'] != undefined && search_params_array['hotel_rating'])
        jq.each(search_params_array['hotel_rating'].split('+'), function(i, v) {
          jq('.hotel_rating[value="'+v+'"]').attr('checked', 'checked');
        });
      if(search_params_array['food'] != undefined && search_params_array['food'])
        jq.each(search_params_array['food'].split('+'), function(i, v) {
          jq('.food_list_input[value="'+v+'"]').attr('checked', 'checked');
        });
      if(search_params_array['adults'] != undefined && search_params_array['adults'])
        jq('#adult').val(search_params_array['adults']);
      if(search_params_array['children'] != undefined && search_params_array['children'])
        jq('#children').val(search_params_array['children']).change();
      if(search_params_array['child1_age'] != undefined && search_params_array['child1_age'])
        jq('#child1_age').val(search_params_array['child1_age']);
      if(search_params_array['child2_age'] != undefined && search_params_array['child2_age'])
        jq('#child2_age').val(search_params_array['child2_age']);
      if(search_params_array['child3_age'] != undefined && search_params_array['child3_age'])
        jq('#child3_age').val(search_params_array['child3_age']);
      if(search_params_array['date_from'] != undefined && search_params_array['date_from'])
        jq('#date_from').val(search_params_array['date_from']);
      if(search_params_array['date_till'] != undefined && search_params_array['date_till'])
        jq('#date_till').val(search_params_array['date_till']);
      if(search_params_array['night_from'] != undefined && search_params_array['night_from'])
        jq('#night_from').val(search_params_array['night_from']);
      if(search_params_array['night_till'] != undefined && search_params_array['night_till'])
        jq('#night_till').val(search_params_array['night_till']);
      if(search_params_array['price_from'] != undefined && search_params_array['price_from'])
        jq('#price_from').val(search_params_array['price_from'].replace(/[^\d]/g, ''));
      if(search_params_array['price_till'] != undefined && search_params_array['price_till'])
        jq('#price_till').val(search_params_array['price_till'].replace(/[^\d]/g, ''));
      if(search_params_array['departure_city'] != undefined && search_params_array['departure_city'])
        jq('#departure_city').val(search_params_array['departure_city']);
      if(search_params_array['items_per_page'] != undefined && search_params_array['items_per_page'])
        jq('#items_per_page' + search_params_array['items_per_page']).attr('checked', 'checked');
      if(search_params_array['package_tour_type'] != undefined && search_params_array['package_tour_type'])
        jq('#package_tour_type_'+search_params_array['package_tour_type']).attr('checked', 'checked');
            
      jq.getJSON(options.modules_action, { 'action':'get_package_search_filtered_field'
                                 , 'event_owner_level' : 1
                                 , 'country_id': search_params_array['country'].replace(/\+/g, ' ')
                                 , 'region_id': search_params_array['region'].replace(/\+/g, ' ')
                                 , 'hotel_rating_id': search_params_array['hotel_rating'].replace(/\+/g, ' ')
                                 , 'tour_kind': jq('#tour_kind').val()
                                 , 'tour_type' : jq('#package_search_form :radio:checked').val()
                                 }
                                 , function(data) {
          jq('#region_list').html(data.region);
          jq('#hotel_list').html(data.hotel);
          
          if(search_params_array['country'])
            jq('#country').val(search_params_array['country'].split('+'));
          if(search_params_array['region'] != undefined && search_params_array['region'])
            jq('#region_list').val(search_params_array['region'].split('+'));
          if(search_params_array['hotel'] != undefined && search_params_array['hotel'])
            jq('#hotel_list').val(search_params_array['hotel'].split('+'));
          jq('#package_search_form .btn-search :submit').click();
          jq('#tour_search_module').show();
        });      
      }
      
      if(action == 'hike_tour_search' && search_params_array['country'] != undefined) {                                        
        if(search_params_array['hike_date_from'] != undefined && search_params_array['hike_date_from'])
          jq('#hike_date_from').val(search_params_array['hike_date_from']);
        if(search_params_array['hike_date_till'] != undefined && search_params_array['hike_date_till'])
          jq('#hike_date_till').val(search_params_array['hike_date_till']);
        if(search_params_array['hike_price_till'] != undefined && search_params_array['hike_price_till'])
          jq('#hike_price_till').val(search_params_array['hike_price_till']);
        if(search_params_array['items_per_page'] != undefined && search_params_array['items_per_page'])
          jq('#hike_items_per_page' + search_params_array['items_per_page']).attr('checked', 'checked');
        jq.getJSON(options.modules_action, { 'action':'get_hike_search_filtered_field'
                                 , 'event_owner_level' : 0
                                 , 'country_id': search_params_array['country'].replace(/\+/g, ' ')
                                 , 'transport_id': search_params_array['transport'].replace(/\+/g, ' ')
                                 , 'city_id': search_params_array['city'].replace(/\+/g, ' ')
                                 }
                                 , function(data) {
          jq('#transport_list').html(data.transport);
          jq('#city_list').html(data.city);
          jq('#tour_city_list').html(data.tour_city);
          if(search_params_array['country'])
            jq('#country_list').val(search_params_array['country'].split('+'));
          if(search_params_array['transport'] != undefined && search_params_array['transport'])
            jq('#transport_list').val(search_params_array['transport'].split('+'));
          if(search_params_array['city'] != undefined && search_params_array['city'])
            jq('#city_list').val(search_params_array['city'].split('+'));
          if(search_params_array['tour_city'] != undefined && search_params_array['tour_city'])
            jq('#tour_city_list').val(search_params_array['tour_city'].split('+'));
          jq('#hike_search_form .btn-search :submit').click();
          jq('#tour_search_module').show();
        });                                  
      }
    
    
    
    
    
  } else {
    jq('#tour_search_module').show();
  }                                
  jq('.module_preview_load').hide();
  //---------
    
}

function change_package_tour_type(tour_type) {  	
  var options = jq('body').data('options');
  
  var hotel_rating_list  = '';
  jq('#itt_hotel_rating_block :checkbox:checked').each(function(index){
    hotel_rating_list  += jq(this).val()+' ';
  });
  jq.getJSON(options.modules_action, { 'action':'get_package_search_filtered_field'
                            , 'event_owner_level' : 0
                            , 'hotel_rating_id': hotel_rating_list
                            , 'tour_type': tour_type
                            , 'tour_kind': jq('#tour_kind').val()
                            }
                            , function(data) {  
    jq('#country').html(data.country);
    jq('#region_list').html(data.region);
    jq('#hotel_list').html(data.hotel);
  });
}

function change_package_tour_kind(tour_kind) {  
  
  var options = jq('body').data('options');
  
  var hotel_rating_list  = '';
  jq('#itt_hotel_rating_block :checkbox:checked').each(function(index){
    hotel_rating_list  += jq(this).val()+' ';
  });
    
  jq.getJSON(options.modules_action, { 'action':'get_package_search_filtered_field'
                            , 'event_owner_level' : 0
                            , 'hotel_rating_id': hotel_rating_list
                            , 'tour_type' : jq('#package_search_form :radio:checked').val() 
                            , 'tour_kind': tour_kind
                            }
                            , function(data) {
    jq('#country').html(data.country);
    jq('#region_list').html(data.region);
    jq('#hotel_list').html(data.hotel);
  });
}

function hotel_refresh(){
  
  var options = jq('body').data('options');
  
  var region_list = '';
  jq('#region_list option:selected').each(function(index){
    region_list  += jq(this).val()+' ';
  });
  var hotel_rating_list  = '';
  jq('#itt_hotel_rating_block :checkbox:checked').each(function(index){
    hotel_rating_list  += jq(this).val()+' ';
  });
  
  jq.getJSON(options.modules_action, { 'action':'get_package_search_filtered_field'
                           , 'event_owner_level' : 2
                           , 'country_id': jq('#country').val()
                           , 'region_id': region_list
                           , 'hotel_rating_id': hotel_rating_list
                           , 'tour_kind': jq('#tour_kind').val()
                           , 'tour_type' : jq('#package_search_form :radio:checked').val()
                           }
                           , function(data) {
    jq('#hotel_list').html(data.hotel).val(0);
  });
}

function package_search_form_submit(form_type, show_result_in_popup) {
  
  var options = jq('body').data('options');
  
  jq('#package_search_form div.btn-search :input').attr('disabled', 'disabled');
  var hotel_rating_str  = '';
  jq('#itt_hotel_rating_block :checkbox:checked').each(function(index){
    hotel_rating_str  += jq(this).val()+' ';
  });
  jq('#hotel_rating').val(jq.trim(hotel_rating_str));
  jq('#itt_hotel_rating_block input').attr('disabled', 'disabled');  

  var food_str = '';
  jq('ul.food :checkbox:checked').each(function(index){
    food_str += jq(this).val()+' ';
  });
  jq('#food').val(jq.trim(food_str));
  jq('ul.food input').attr('disabled', 'disabled');

  if(form_type == 'extended') {
    var hotel_list_temp = jq('#hotel_list').val();
    var region_list_temp = jq('#region_list').val();
    var region_list = '';
    jq('#region_list  option:selected').each(function(index){
      if(jq(this).val() != 0) // исключаем все регионы
        region_list  += jq(this).val()+' ';
    });
    jq('#region').val(jq.trim(region_list));
    var hotel_list = '';
    jq('#hotel_list option:selected').each(function(index){
      if(jq(this).val() != 0) // исключаем все отели
        hotel_list  += jq(this).val()+' ';
    });
    jq('#hotel').val(jq.trim(hotel_list));
    jq('#hotel_list').val('');
    jq('#region_list').val('');
  }
  
  var params = (jq('#package_search_form').serialize());
  if(form_type == 'extended') {
    jq('#hotel_list').val(hotel_list_temp);
    jq('#region_list').val(region_list_temp);
  }
  jq('#itt_hotel_rating_block input').attr('disabled', '');
  jq('ul.food input').attr('disabled', '');  
  
  if(show_result_in_popup && options.extended_search_url != ''){
    window.location = options.extended_search_url + '#' + params;
    return false;
  }
  
  if(show_result_in_popup) {
    if(options.modules_popup_type == 'div') {
      if(search_box)
        jq('div.tour_search_result').parents('table.boxy-wrapper').remove();
      search_box = new Boxy('<div class="itt_main_block tour_search_result"><img class="load_center" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>', 
                  { modal: true
                  , title: '&nbsp;'
                  , afterHide: function(event, ui) { jq('body').css('overflow', ''); }
                  , afterShow: function(event, ui) { jq('body').css('overflow', 'hidden'); }
                  });
    } else {
      popupWin = window.open('', '', 'status=no,width=750,height=240,scrollbars=yes,resizable=no,top=10,left='+(screen.width-800)/2);        
      popupWin.document.write('<div class="window_popup" style="width: 100%; text-align: center;"><img  style="margin: 50px 0;" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>');
      popupWin.document.close();
    }
  } else {
    jq('div.tour_search_result').html('');
    jq('.tour_load').show();    
  }  
    
  if(!show_result_in_popup && jq('#module_search_query').val() == '') {
    // change url   
    var loc = window.location.toString();
    if(loc.indexOf('#') == -1)
      window.location = loc + '#' + params;
    else
      window.location = loc.substr(0, loc.indexOf('#')) + '#' + params;
  }
  
  jq.getJSON(options.modules_action, params, function(data) {
    jq('#package_search_form div.btn-search :input').attr('disabled', '');    
    if(show_result_in_popup) {
      if(options.modules_popup_type == 'div') {
        search_box.setContent('<div class="itt_main_block tour_search_result" style="overflow: auto;">'+data.text+'</div>');    
        set_poput_actual_size(search_box);
      } else {
        popupWin.document.write(data.text);
        popupWin.document.close();  
      }
    } else {
      jq('div.tour_search_result').html(data.text);
      jq('.tour_load').hide();
      if(jq.browser.opera) { // for opera only
        jq('.ellipsis').css('white-space', 'normal');
        jq('.tour_search_result div:eq(0)').css('width', jq('.tour_search_result div:eq(0)').width());
        jq('.ellipsis').css('white-space', 'nowrap');
      }
    }    
    return false;
  });
  return false;
  
  jq('#package_search_form').submit();
  jq('#itt_hotel_rating_block input').attr('disabled', '');
  jq('ul.food input').attr('disabled', '');
  return false;
}

function ajax_load(url, div_name, show_result_in_popup) {
  var options = jq('body').data('options');
  
  var params = url.substr( (url.indexOf( '?', 0) + 1) );
  params = params.replace(/callback=jsonp[0-9]+&/, '');
  if(show_result_in_popup) {
    if(options.modules_popup_type == 'div')
      search_box.setContent('<div class="itt_main_block tour_search_result"><img class="load_center" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>').center();
    else {
      window.document.write('<div class="window_popup" style="width: 100%; text-align: center;"><img  style="margin: 50px 0;" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>');
      window.document.close();
    }      
  } else {
    jq('.'+div_name).html('');
    jq('.tour_load').show();
  }
  
  jq.getJSON(options.modules_action, params, function(data) {    
    if(show_result_in_popup) {
      if(options.modules_popup_type == 'div') {
        search_box.setContent('<div class="itt_main_block tour_search_result" style="overflow: auto;">'+data.text+'</div>');    
        set_poput_actual_size(search_box);
      } else {
        window.document.write(data.text);
        window.document.close();
      }
    } else {
      jq('.'+div_name).html(data.text);
      jq('.tour_load').hide();
      if(jq.browser.opera) { // for opera only
        jq('.ellipsis').css('white-space', 'normal');
        jq('.tour_search_result div:eq(0)').css('width', jq('.tour_search_result div:eq(0)').width());
        jq('.ellipsis').css('white-space', 'nowrap');
      }
    }
    return false;
  });
  return false;  
}

function display_hike_form() {
  jq('div.tour_search_result').html('');
  jq('div.extended_package_search_form').hide();
  jq('div.extended_hike_search_form').show();  
  jq('#form_type_hike').val(2);
  return false;
}

function display_package_form() {
  jq('div.tour_search_result').html('');
  jq('div.extended_hike_search_form').hide();
  jq('div.extended_package_search_form').show();
  jq('#form_type_package').val(1);
  return false;
}

function hike_search_form_submit(form_type, show_result_in_popup) {                  

  var options = jq('body').data('options');  

  if(jq('div.btn-search :input').parents("form").attr('target') != '_blank')
    jq('#hike_search_form div.btn-search :input').attr('disabled', 'disabled');    

  var country_list_temp = jq('#country_list').val();
  var transport_list_temp = jq('#transport_list').val();
  var city_list_temp = jq('#city_list').val();
  var tour_city_list_temp = jq('#tour_city_list').val();
  
  var country_list = '';
  jq('#country_list option:selected').each(function(index){
    country_list  += jq(this).val()+' ';
  });
  jq('#hike_country').val(jq.trim(country_list));
  var transport_list = '';
  jq('#transport_list option'+((jq('#transport_list').val() && jq('#transport_list').val() != 900)?':selected':'') ).each(function(index){
    if(jq(this).val() != 0)
      transport_list  += jq(this).val()+' ';
  });
  jq('#transport').val(jq.trim(transport_list));
  var city_list = '';
  jq('#city_list option'+((jq('#city_list').val() && jq('#city_list').val() != 900)?':selected':'') ).each(function(index){
    if(jq(this).val() != 0)
      city_list  += jq(this).val()+' ';
  });
  jq('#city').val(jq.trim(city_list));
  var tour_city_list = '';
  jq('#tour_city_list option'+((jq('#tour_city_list').val() && jq('#tour_city_list').val() != 900)?':selected':'') ).each(function(index){
    if(jq(this).val() != 0)
      tour_city_list  += jq(this).val()+' ';
  });
  jq('#tour_city').val(jq.trim(tour_city_list));

  jq('#country_list').val('');
  jq('#transport_list').val('');
  jq('#city_list').val('');
  jq('#tour_city_list').val('');

  var params = (jq('#hike_search_form').serializeArray());
  var params_str = (jq('#hike_search_form').serialize());
  jq('#country_list').val(country_list_temp);
  jq('#transport_list').val(transport_list_temp);
  jq('#city_list').val(city_list_temp);
  jq('#tour_city_list').val(tour_city_list_temp);
  
  if(show_result_in_popup && options.extended_search_url != ''){
    window.location = options.extended_search_url + '#' + params_str;
    return false;
  }
  
  if(show_result_in_popup) {
    if(options.modules_popup_type == 'div') {
      if(search_box)
        jq('div.tour_search_result').parents('table.boxy-wrapper').remove();
      search_box = new Boxy('<div class="itt_main_block tour_search_result"><img class="load_center" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>', 
                  { modal: true
                  , title: '&nbsp;'
                  , afterHide: function(event, ui) { jq('body').css('overflow', ''); }
                  , afterShow: function(event, ui) { jq('body').css('overflow', 'hidden'); }
                  });
    } else {
      popupWin = window.open('', '', 'status=no,width=750,height=240,scrollbars=yes,resizable=no,top=10,left='+(screen.width-800)/2);        
      popupWin.document.write('<div class="window_popup" style="width: 100%; text-align: center;"><img  style="margin: 50px 0;" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>');
      popupWin.document.close();
    }
  } else {
    jq('div.tour_search_result').html('');
    jq('.tour_load').show();    
  }
  
  if(!show_result_in_popup && jq('#module_search_query').val() == '') {
    var loc = window.location.toString();
    if(loc.indexOf('#') == -1)
      window.location = loc + '#' + params_str;
    else
      window.location = loc.substr(0, loc.indexOf('#')) + '#' + params_str;
  }
  
  jq.getJSON(options.modules_action, params, function(data) {
    jq('#hike_search_form div.btn-search :input').attr('disabled', '');    
    if(show_result_in_popup) {      
      if(options.modules_popup_type == 'div') {
        search_box.setContent('<div class="itt_main_block tour_search_result" style="overflow: auto;">'+data.text+'</div>');    
        set_poput_actual_size(search_box);
      } else {
        popupWin.document.write(data.text);
        popupWin.document.close(); 
      }
    } else {
      jq('div.tour_search_result').html(data.text);
      jq('.tour_load').hide();
      if(jq.browser.opera) { // for opera only
        jq('.ellipsis').css('white-space', 'normal');
        jq('.tour_search_result div:eq(0)').css('width', jq('.tour_search_result div:eq(0)').width());
        jq('.ellipsis').css('white-space', 'nowrap');
      }
    }    
    return false;
  });
  return false;
}

function country_list_click(){ 

  var options = jq('body').data('options');  

  var country_list = '';
  jq('#country_list option:selected').each(function(index){
    if(jq(this).val() != 0)
      country_list  += jq(this).val()+' ';
  });
  jq.getJSON(options.modules_action, { 'action':'get_hike_search_filtered_field', 'event_owner_level' : 1, 'country_id':  jq.trim(country_list) }, function(data) {
    jq('#transport_list').html(data.transport);
    jq('#city_list').html(data.city);
    jq('#tour_city_list').html(data.tour_city);    
  });
}

function transport_list_click(){    
  
  var options = jq('body').data('options');

  var country_list = '';
  jq('#country_list option:selected').each(function(index){
    if(jq(this).val() != 0)
      country_list  += jq(this).val()+' ';
  });
  var transport_list = '';
  jq('#transport_list option:selected').each(function(index){
    if(jq(this).val() != 0)
      transport_list  += jq(this).val()+' ';
  });
  jq.getJSON(options.modules_action, { 'action':'get_hike_search_filtered_field', 'event_owner_level' : 2, 'country_id':  jq.trim(country_list), 'transport_id':  jq.trim(transport_list) }, function(data) {
    jq('#city_list').html(data.city);
    jq('#tour_city_list').html(data.tour_city);    
  });
}

function city_list_click(){
  
  var options = jq('body').data('options');    
 
  var country_list = '';
  jq('#country_list option:selected').each(function(index){
    if(jq(this).val() != 0)
      country_list  += jq(this).val()+' ';
  });
  var transport_list = '';
  jq('#transport_list option:selected').each(function(index){
    if(jq(this).val() != 0)
      transport_list  += jq(this).val()+' ';
  });
  var city_list = '';
  jq('#city_list option:selected').each(function(index){
    if(jq(this).val() != 0)
      city_list  += jq(this).val()+' ';
  });  
  jq.getJSON(options.modules_action, { 'action':'get_hike_search_filtered_field', 'event_owner_level' : 3, 'country_id':  jq.trim(country_list), 'transport_id':  jq.trim(transport_list), 'city_id':  jq.trim(city_list) }, function(data) {
    jq('#tour_city_list').html(data.tour_city);    
  });
}

function package_tour_order(tour_id) {
  var options = jq('body').data('options');
  if(options.modules_popup_type == 'div') {
    if(order_box)
      jq('div.tour_order').parents('table.boxy-wrapper').remove();    
    order_box = new Boxy('<div class="itt_main_block tour_order"><img class="load_center" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>', 
                  { modal: true
                  , title: '&nbsp;'
                  , afterHide: function(event, ui) { jq('body').css('overflow', ''); }
                  , afterShow: function(event, ui) { jq('body').css('overflow', 'hidden'); }
                  });
  } else {
    orderWin = window.open('', '', 'status=no,width=750,height=240,scrollbars=yes,resizable=no,top=10,left='+(screen.width-800)/2);        
    orderWin.document.write('<div class="window_popup" style="width: 100%; text-align: center;"><img  style="margin: 50px 0;" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>');
    orderWin.document.close();
  }
  jq.getJSON(options.modules_action, { 'action':'get_package_tour_order_form', 'tour_id' : tour_id}, function(data) {
    if(options.modules_popup_type == 'div') {
      order_box.setContent('<div class="itt_main_block tour_order" style="overflow: auto;">'+data.text+'</div>');    
      set_poput_actual_size(order_box);
    } else {
      orderWin.document.write(data.text);
      orderWin.document.close();  
    }    
    return;
  });
}

function package_tour_order_submit(){
  var options = jq('body').data('options');
  var params = (jq('#package_order_form').serialize());
  jq('.package_order_load').html('<img src="'+options.modules_url+'images/ajax_loader.gif" />');
  jq('#package_order_form :submit').hide();
  jq.getJSON(options.modules_action, params, function(data) {  
    jq('.package_order_load').html('');
    jq('#package_order_form :submit').show();
    if(data.success == false) {
      jq('.error_form').html(data.error).show();
      if(options.modules_popup_type == 'div')
        set_poput_actual_size(order_box);
    } else {
      if(options.modules_popup_type == 'div') {      
        order_box.setContent(jq('#tour_order_success_message').html());    
        set_poput_actual_size(order_box);
      } else {
        jq('.tour_order').html(jq('#tour_order_success_message').html());
      }
    }
    return false;
  });
  return false;  
}

function hike_tour_order(tour_id) {                                                                      
  var options = jq('body').data('options');
  if(options.modules_popup_type == 'div') {
    if(order_box)
      jq('div.tour_order').parents('table.boxy-wrapper').remove();
    order_box = new Boxy('<div class="itt_main_block tour_order"><img class="load_center" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>', 
                  { modal: true
                  , title: '&nbsp;'
                  , afterHide: function(event, ui) { jq('body').css('overflow', ''); }
                  , afterShow: function(event, ui) { jq('body').css('overflow', 'hidden'); }
                  });
  } else {
    orderWin = window.open('', '', 'status=no,width=750,height=240,scrollbars=yes,resizable=no,top=10,left='+(screen.width-800)/2);        
    orderWin.document.write('<div class="window_popup" style="width: 100%; text-align: center;"><img  style="margin: 50px 0;" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>');
    orderWin.document.close();
  }
  jq.getJSON(options.modules_action, { 'action':'get_hike_tour_order_form', 'tour_id' : tour_id }, function(data) {            
    if(options.modules_popup_type == 'div') {
      order_box.setContent('<div class="itt_main_block tour_order" style="overflow: auto;">'+data.text+'</div>');            
      reload_price();
    } else {
      orderWin.document.write(data.text.replace('/*%onload_custom_func%*/', 'reload_price();'));
      orderWin.document.close();
    }        
  });
}

function hike_tour_view(tour_id) {
  
  var options = jq('body').data('options');
                                                                        
  if(view_box)
    jq('div.tour_view').parents('table.boxy-wrapper').remove();
  view_box = new Boxy('<div class="itt_main_block tour_order"><img class="load_center" src="'+options.modules_url+'images/ajax_loader_circle.gif" /></div>', 
                { modal: true
                , title: '&nbsp;'
                , afterHide: function(event, ui) { jq('body').css('overflow', ''); }
                , afterShow: function(event, ui) { jq('body').css('overflow', 'hidden'); }
                });
  jq.getJSON(options.modules_action, { 'action':'get_hike_tour_view', 'tour_id' : tour_id }, function(data) {        
    view_box.setContent('<div class="itt_main_block tour_order" style="overflow: auto;">'+data.text+'</div>');    
    set_poput_actual_size(view_box);
  });
}

function hike_tour_order_submit(){
  
  var options = jq('body').data('options');

  var params = (jq('#hike_order_form').serialize());
  jq('.hike_order_load').html('<img src="'+options.modules_url+'images/ajax_loader.gif" />');
  jq('#hike_order_form :submit').hide();
  jq.getJSON(options.modules_action, params, function(data) {
    jq('.hike_order_load').html('');
    jq('#hike_order_form :submit').show();
    if(data.success == false) {
      jq('.error_form').html(data.error).show();
      if(options.modules_popup_type == 'div')
        set_poput_actual_size(order_box);
    } else {
      if(options.modules_popup_type == 'div') {      
        order_box.setContent(jq('#tour_order_success_message').html());    
        set_poput_actual_size(order_box);
      } else {
        jq('.tour_order').html(jq('#tour_order_success_message').html());
      }
    }
    return false;
  });
  return false;  
}


//hike order

function reload_price() {                            
  var departure_date = jq('#departure_date').val();
  if(!departure_date)
    return false;
  var departure_timestamp = Date.UTC(departure_date.substr(0, 4), departure_date.substr(5, 2)-1, departure_date.substr(8, 2), 0, 0, 0) / 1000;
  var now_timestamp = Number(new Date())/1000;
  var accomodation = jq('#accomodation').val();
  
  var price = offer_prices[departure_date][accomodation];  
  
  var offer_early_prices_str = '';
  if(offer_early_prices[departure_date]) {
    var early_price = offer_early_prices[departure_date][accomodation];    
    jq.each(early_price, function(key, value) {
      if(now_timestamp < value['early_timestamp']) {
        var price_selected = '';//(price_select == value['price'])?'checked="checked"':'';
        offer_early_prices_str += '<input type="radio" name="price" ' + price_selected + ' value="' + value['price'] + '" />' + jq('#for_count_weeks').html().replace('$count', value['week_num']) + ': ' + value['price'] + ' ' + currency_symbol + '<br />';
      }
    });
  }
  var price_selected = 'checked="checked"';
  offer_early_prices_str += '<input type="radio" name="price" ' + price_selected + ' value="' + price + '" />' + jq('#without_prepay_message').html() + ': ' + price + ' ' + currency_symbol;
  jq('#price').html(offer_early_prices_str);
  set_poput_actual_size(order_box);
}

function show_hide_tour_info(){
  if(jq('#hike_tour_info').css('display') == 'none') {
    jq('.hike_tour_add_info_link').html( jq('#hide_add_info_message').html() );
    jq('#hike_tour_info').show(); 
  } else {    
    jq('.hike_tour_add_info_link').html( jq('#show_add_info_message').html() ); 
    jq('#hike_tour_info').hide();
  }
  return false;
}

function show_hike_description(hike_id){
  jq('div[id^="hike_description_"]').hide();
  jq('#hike_description_'+hike_id).show();
  return false;
}

function set_poput_actual_size(box) {
  box.resize(730, ''); // or 'auto'
  var size = box.getContentSize();
  h = ((jq(window).height()-80) < (size[1]))?(jq(window).height()-80):(size[1]);      
  box.resize(730, h).center();  
}

function set_window_popup_actual_size(box){
  var height = jq(box.document).find('.window_popup').height();
  height = ((screen.height-170) < height)?(screen.height-170):(height);      
  box.resizeTo(760, height+80);
//  h = ((jq(window).height()-80) < (size[1]))?(jq(window).height()-80):(size[1]);      
}
