<?php echo $header; ?>
<div class="container news product-product">
  <ul id="breadcrumbs-one">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
    <?php } ?>
  </ul>
  <div id="content" class="">
    <h1 class="text-center nomer-h2 h2">
      <?php echo $heading_title; ?>
    </h1>
    <div class="col-xs-12 col-sm-6 col-md-6 nopadding nomer-left-page">
      <div class="nomer-gallery nomer-gallery-page">
          <img class="main_product_image" src="<?php echo $image585; ?>" title="<?php echo $heading_title; ?>" alt="<?php echo $heading_title; ?>">
        <?php if ($images) { ?>
          <div id="slideshow-nomer-thumbnail" style="display: block" class="conteiner-image-additional owl-carousel">
            <?php if($popup && $thumb) { ?>
              <div class="item thumbnail"><a class="pop-me" href="<?php echo $popup; ?>"><img src="<?php echo $thumb; ?>" title="<?php echo $heading_title; ?>" alt="<?php echo $heading_title; ?>"></a></div>
            <?php } ?>
            <?php foreach ($images as $image) { ?>
              <?php if($image['thumb'] != NULL) { ?>
              <div class="item thumbnail"><a class="pop-me" href="<?php echo $image['popup']; ?>"><img src="<?php echo $image['thumb']; ?>" title="<?php echo $heading_title; ?>" alt="<?php echo $heading_title; ?>"></a></div>
              <?php } ?>
            <?php } ?>
          </div>
        <?php } ?>
      </div>
      <div class="descript-nomer nopadding"><?php echo $description; ?></div>
      <div class="reserv-text">
        <hr class="hr-style">
          <?php if($text_entry || $text_departure) { ?>
            <p><span class="nomer-text-first"><?php echo $text_entry; ?></span><?php echo $text_departure; ?></p>
          <?php } ?>
      </div>
      <p><?php echo $text_booking_confirm; ?></p>
    </div>
    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-5 col-md-offset-0 col-lg-offset-1 nomer-right-page">
      <form id="product_form" method="post">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 reserv-left">
          <div class="row">
            
            <div class="col-xs-12 col-sm-12 date-left" id="calendar">
                <?php echo $calendar; ?>
            </div>
            <input class="form-control input-grey date" name="booking_date_start" value="<?php echo date('d-m-Y');?>" type="hidden"/>
            <input class="form-control input-grey date" name="booking_date_end" value="<?php echo date('d-m-Y',strtotime(' +1 day'));?>" type="hidden"/>
            <div class="col-xs-6 col-sm-6 date-left">
             
            </div>
            <div class="col-xs-6 col-sm-6">

            </div>
            <div class="col-sm-12 col-xs-12" id="message_block"></div>
            <?php if($cat_products) { ?>
            <div class="col-xs-12 col-sm-12">
              <select class="form-control input-grey" id="category_products">
              <?php foreach($cat_products as $product) { ?>
                <?php if($product['product_id'] != $product_id) { ?>
                <option value="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></option>
                <?php } else { ?>
                <option value="<?php echo $product['href']; ?>" selected><?php echo $product['name']; ?></option>
                <?php } ?>
              <?php } ?>
              </select>
            </div>
            <?php } ?>
             <div class="col-xs-12 col-sm-12"><hr class="hr-style"></div>
          <div class="col-xs-12 col-sm-12 checkbox-contein" id="option_block">
            <form>
                <?php if ($options) { ?>
                <?php foreach ($options as $option) { ?>
                <?php if ($option['type'] == 'select') { ?>
                <div class="form-header"><?php echo $option['name']; ?></div>
                <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
                  <select name="option[<?php echo $option['product_option_id']; ?>]" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control">
                    <option influence="0" value=""><?php echo $text_select; ?></option>
                    <?php foreach ($option['product_option_value'] as $option_value) { ?>
                    <option influence="<?php echo $option_value['influence']; ?>" value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
                      <?php if ($option_value['price']) { ?>
                      (<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
                      <?php } ?>
                    </option>
                    <?php } ?>
                  </select>
                </div>
                <?php } ?>
                <?php if ($option['type'] == 'radio') { ?>
                <div class="form-header"><?php echo $option['name']; ?></div>
                <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
                  <div id="input-option<?php echo $option['product_option_id']; ?>">
                    <?php foreach ($option['product_option_value'] as $option_value) { ?>
                    <div class="radio">
                      <label>
                        <input type="radio" name="option[<?php echo $option['product_option_id']; ?>]" influence="<?php echo $option_value['influence']; ?>" value="<?php echo $option_value['product_option_value_id']; ?>" />
                        <?php if ($option_value['image']) { ?>
                        <img src="<?php echo $option_value['image']; ?>" alt="<?php echo $option_value['name'] . ($option_value['price'] ? ' ' . $option_value['price_prefix'] . $option_value['price'] : ''); ?>" class="img-thumbnail" />
                        <?php } ?>
                        <?php echo $option_value['name']; ?>
                        <?php if ($option_value['price']) { ?>
                        (<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
                        <?php } ?>
                      </label>
                    </div>
                    <?php } ?>
                  </div>
                </div>
                <?php } ?>
                <?php if ($option['type'] == 'checkbox') { ?>
                <div class="form-header"><?php echo $option['name']; ?></div>
                <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
                  <div id="input-option<?php echo $option['product_option_id']; ?>">
                    <?php foreach ($option['product_option_value'] as $option_value) { ?>
                    <div class="checkbox">
                      <label>
                        <input type="checkbox" name="option[<?php echo $option['product_option_id']; ?>][]" influence="<?php echo $option_value['influence']; ?>" value="<?php echo $option_value['product_option_value_id']; ?>" />
                          <span class="check pull-left"></span>
                        <?php if ($option_value['image']) { ?>
                        <img src="<?php echo $option_value['image']; ?>" alt="<?php echo $option_value['name'] . ($option_value['price'] ? ' ' . $option_value['price_prefix'] . $option_value['price'] : ''); ?>" class="img-thumbnail" />
                        <?php } ?>
                       <span class="pull-left"><?php echo $option_value['name']; ?></span>
                       <span class="pull-right text-left"> <?php if ($option_value['price']) { ?>
                        (<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
                        <?php } ?></span>
                      </label>
                    </div>
                    <?php } ?>
                  </div>
                </div>
                <?php } ?>
                <?php if ($option['type'] == 'text') { ?>
                <div class="form-header"><?php echo $option['name']; ?></div>
                <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
                  <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
                  <input type="text" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option['value']; ?>" placeholder="<?php echo $option['name']; ?>" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control" />
                </div>
                <?php } ?>
                <?php if ($option['type'] == 'textarea') { ?>
                <div class="form-header"><?php echo $option['name']; ?></div>
                <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
                  <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
                  <textarea name="option[<?php echo $option['product_option_id']; ?>]" rows="5" placeholder="<?php echo $option['name']; ?>" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control"><?php echo $option['value']; ?></textarea>
                </div>
                <?php } ?>
                <?php if ($option['type'] == 'file') { ?>
                <div class="form-header"><?php echo $option['name']; ?></div>
                <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
                  <label class="control-label"><?php echo $option['name']; ?></label>
                  <button type="button" id="button-upload<?php echo $option['product_option_id']; ?>" data-loading-text="<?php echo $text_loading; ?>" class="btn btn-default btn-block"><i class="fa fa-upload"></i> <?php echo $button_upload; ?></button>
                  <input type="hidden" name="option[<?php echo $option['product_option_id']; ?>]" value="" id="input-option<?php echo $option['product_option_id']; ?>" />
                </div>
                <?php } ?>
                <?php if ($option['type'] == 'date') { ?>
                <div class="form-header"><?php echo $option['name']; ?></div>
                <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
                  <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
                  <div class="input-group date">
                    <input type="text" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option['value']; ?>" data-date-format="YYYY-MM-DD" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control" />
                    <span class="input-group-btn">
                <button class="btn btn-default" type="button"><i class="fa fa-calendar"></i></button>
                </span></div>
                </div>
                <?php } ?>
                <?php if ($option['type'] == 'datetime') { ?>
                <div class="form-header"><?php echo $option['name']; ?></div>
                <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
                  <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
                  <div class="input-group datetime">
                    <input type="text" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option['value']; ?>" data-date-format="YYYY-MM-DD HH:mm" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control" />
                    <span class="input-group-btn">
                <button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
                </span></div>
                </div>
                <?php } ?>
                <?php if ($option['type'] == 'time') { ?>
                <div class="form-header"><?php echo $option['name']; ?></div>
                <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
                  <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
                  <div class="input-group time">
                    <input type="text" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option['value']; ?>" data-date-format="HH:mm" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control" />
                    <span class="input-group-btn">
                <button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
                </span></div>
                </div>
                <?php } ?>
                <?php } ?>
                <?php } ?>
                <?php if ($recurrings) { ?>
                <hr>
                <h3><?php echo $text_payment_recurring; ?></h3>
                <div class="form-group required">
                  <select name="recurring_id" class="form-control">
                    <option value=""><?php echo $text_select; ?></option>
                    <?php foreach ($recurrings as $recurring) { ?>
                    <option value="<?php echo $recurring['recurring_id']; ?>"><?php echo $recurring['name']; ?></option>
                    <?php } ?>
                  </select>
                  <div class="help-block" id="recurring-description"></div>
                </div>
                <?php } ?>
                <input type="hidden" name="quantity" value="1" id="input-quantity" />
                <input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
              </form>
            </div>
              <div class="button-nomer col-xs-12 col-sm-12 ">
                <a id="button-booking" class="btn booking btn-orange"><?php echo $button_cart; ?></a>
                <a id="button-cart" class="btn booking btn-orange pull-right"><?php echo $button_cart_add; ?></a>
              </div>
              <?php if($attribute_groups) { ?>
              <div class="col-xs-12 col-sm-12 reserv-right">
                  <div class="row">
                      <div class="col-xs-12 col-sm-12 col-md-12 nomer-details">
                          <hr>
                          <b><?php echo $text_comfort; ?></b>
                          <div class="nopadding">
                              <?php foreach ($attribute_groups as $attribute_group) { ?>
                              <?php foreach ($attribute_group['attribute'] as $attribute) { ?>
                              <p><?php echo $attribute['name']; ?></p>
                              <?php } ?>
                              <?php } ?>
                          </div>
                      </div>
                  </div>
              </div>
              <?php } ?>
            </div>
          </div>
        </div>
        <div class="clearfix"></div>
      </form>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$(document).on('change', '#category_products', function() {
    var url = $(this).val();
    window.location.href = url;
})
$(document).on('click', 'a.pop-me', function (event) {
    event.preventDefault();
    var popup = $(this).attr('href');
    $('.main_product_image').attr('src', popup);
    $('.main_product_image').attr('href', popup);
});


$(document).on('change', 'input, select', function () {
    var spots = parseFloat(<?php echo $influence; ?>);
    $.each($('#option_block input:checked, #option_block input:selected, #option_block select option:selected'),function(){
        spots += parseInt($(this).attr('influence'));
    });

    if($('#adults_field').val() > 0) {
        residents = parseInt($('#adults_field').val()) + parseInt($('#childrens_field').val());
    } else {
        residents = 1 + parseInt($('#childrens_field').val());
    }

    amounts = Math.ceil(residents / spots);
    if(amounts > 4) {
        var houses = '<?php echo $text_house_ending; ?>';
    } else {
        var houses = '<?php echo $text_house_end; ?>';
    }
    if(residents > spots) {
        $('#message_block').text('<?php echo $text_house_srart; ?>' + amounts + ' ' + houses);
        $('input[name="quantity"]').val(amounts);
    } else {
        $('#message_block').text('');
        $('input[name="quantity"]').val(1);
    }
    rebuilding();
});

$(document).on('click', '#button-booking', function () {
    startDate = $('input[name=\'booking_date_start\']').val();
    endDate = $('input[name=\'booking_date_end\']').val();
    if(!startDate) {
        $('input[name=\'booking_date_start\']').css({"border" : "1px solid red"});
        alert("<?php echo $text_enter_date; ?>");
    }if(!endDate) {
        $('input[name=\'booking_date_end\']').css({"border" : "1px solid red"});
        alert("<?php echo $text_enter_end; ?>");
    }

    if(startDate && endDate) {
        $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: $('.reserv-left input[type=\'text\'], .reserv-left input[type=\'hidden\'], input[name=\'booking_date_start\'], input[name=\'booking_date_end\'], .reserv-left input[type=\'radio\']:checked, .reserv-left input[type=\'checkbox\']:checked, .reserv-left select, .reserv-left textarea'),
            dataType: 'json',
            success: function(json) {
                $('.alert, .text-danger').remove();
                $('.form-group').removeClass('has-error');

                if (json['error']) {
                    if (json['error']['option']) {
                        for (i in json['error']['option']) {
                            var element = $('#input-option' + i.replace('_', '-'));

                            if (element.parent().hasClass('input-group')) {
                                element.parent().after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
                            } else {
                                element.after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
                            }
                        }
                    }

                    if (json['error']['recurring']) {
                        $('select[name=\'recurring_id\']').after('<div class="text-danger">' + json['error']['recurring'] + '</div>');
                    }

                    // Highlight any found errors
                    $('.text-danger').parent().addClass('has-error');
                }

                if (json['success']) {
                    $('.breadcrumb').after('<div class="alert alert-success">' + json['success'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');

                    $('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');

                    $('html, body').animate({ scrollTop: 0 }, 'slow');

                    $('#cart > ul').load('index.php?route=common/cart/info ul li');
                    window.location = "<?php echo $checkout_link; ?>";
                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    }

});

$('select[name=\'recurring_id\'], input[name="quantity"]').change(function(){
	$.ajax({
		url: 'index.php?route=product/product/getRecurringDescription',
		type: 'post',
		data: $('input[name=\'product_id\'], input[name=\'quantity\'], select[name=\'recurring_id\']'),
		dataType: 'json',
		beforeSend: function() {
			$('#recurring-description').html('');
		},
		success: function(json) {
			$('.alert, .text-danger').remove();

			if (json['success']) {
				$('#recurring-description').html(json['success']);
			}
		}
	});
});
//--></script>
<script type="text/javascript"><!--
$('#button-cart').on('click', function() {
  startDate = $('input[name=\'booking_date_start\']').val();
  endDate = $('input[name=\'booking_date_end\']').val();
  if(!startDate) {
    $('input[name=\'booking_date_start\']').css({"border" : "1px solid red"});
    alert("<?php echo $text_enter_date; ?>");
  }if(!endDate) {
    $('input[name=\'booking_date_end\']').css({"border" : "1px solid red"});
    alert("<?php echo $text_enter_end; ?>");
  }

  if (startDate && endDate) {
          $.ajax({
              url: 'index.php?route=checkout/cart/add',
              type: 'post',
              data: $('.reserv-left input[type=\'text\'], .reserv-left input[type=\'hidden\'], input[name=\'booking_date_start\'], input[name=\'booking_date_end\'], .reserv-left input[type=\'radio\']:checked, .reserv-left input[type=\'checkbox\']:checked, .reserv-left select, .reserv-left textarea'),
              dataType: 'json',
              success: function (json) {
                  $('.alert, .text-danger').remove();
                  $('.form-group').removeClass('has-error');

                  if (json['error']) {
                      if (json['error']['option']) {
                          for (i in json['error']['option']) {
                              var element = $('#input-option' + i.replace('_', '-'));

                              if (element.parent().hasClass('input-group')) {
                                  element.parent().after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
                              } else {
                                  element.after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
                              }
                          }
                      }

                      if (json['error']['recurring']) {
                          $('select[name=\'recurring_id\']').after('<div class="text-danger">' + json['error']['recurring'] + '</div>');
                      }

                      // Highlight any found errors
                      $('.text-danger').parent().addClass('has-error');
                  }

                  if (json['success']) {
                      $('#breadcrumbs-one').after('<div class="alert alert-success">' + json['success'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');

                      $('.number-items').show();
                      $('.number-items amount').html(json['update']);

                      $('html, body').animate({scrollTop: 0}, 'slow');

                      $('#cart > ul').load('index.php?route=common/cart/info ul li');
                  }
              },
              error: function (xhr, ajaxOptions, thrownError) {
                  alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
              }
          });
      }

});
//--></script>
<script type="text/javascript"><!--
$('.datetime').datetimepicker({
	pickDate: true,
	pickTime: true
});

$('.time').datetimepicker({
	pickDate: false
});

  list_day = [];
  control_list_day = '';

  function allowedDates(date) {

    now=new Date();
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);
    now.setMilliseconds(0);

    //Исключаем даты раньше сегодняшнего дня
    if (date.getTime() < now.getDate()) return [false, ''];

    //Исключаем даты из массива
    for (var i = 0; i < jsForbiddenDatesArray.length; i++) {
      if (date.getTime() == jsForbiddenDatesArray[i].getTime())
        return [false, ''];
    }
    //Если нужно исключить выходные дни раскомментируйте строку ниже
    //return jQuery.datepicker.noWeekends(date);
    return [true,''];
  }

$('button[id^=\'button-upload\']').on('click', function() {
	var node = this;

	$('#form-upload').remove();

	$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

	$('#form-upload input[name=\'file\']').trigger('click');

	if (typeof timer != 'undefined') {
    	clearInterval(timer);
	}

	timer = setInterval(function() {
		if ($('#form-upload input[name=\'file\']').val() != '') {
			clearInterval(timer);

			$.ajax({
				url: 'index.php?route=tool/upload',
				type: 'post',
				dataType: 'json',
				data: new FormData($('#form-upload')[0]),
				cache: false,
				contentType: false,
				processData: false,
				beforeSend: function() {
					$(node).button('loading');
				},
				complete: function() {
					$(node).button('reset');
				},
				success: function(json) {
					$('.text-danger').remove();

					if (json['error']) {
						$(node).parent().find('input').after('<div class="text-danger">' + json['error'] + '</div>');
					}

					if (json['success']) {
						alert(json['success']);

						$(node).parent().find('input').val(json['code']);
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		}
	}, 500);
});
//--></script>
<script type="text/javascript"><!--
$('#review').delegate('.pagination a', 'click', function(e) {
    e.preventDefault();

    $('#review').fadeOut('slow');

    $('#review').load(this.href);

    $('#review').fadeIn('slow');
});

$('#review').load('index.php?route=product/product/review&product_id=<?php echo $product_id; ?>');

$('#button-review').on('click', function() {
	$.ajax({
		url: 'index.php?route=product/product/write&product_id=<?php echo $product_id; ?>',
		type: 'post',
		dataType: 'json',
		data: $("#form-review").serialize(),
		beforeSend: function() {
			$('#button-review').button('loading');
		},
		complete: function() {
			$('#button-review').button('reset');
		},
		success: function(json) {
			$('.alert-success, .alert-danger').remove();

			if (json['error']) {
				$('#review').after('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + '</div>');
			}

			if (json['success']) {
				$('#review').after('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '</div>');

				$('input[name=\'name\']').val('');
				$('textarea[name=\'text\']').val('');
				$('input[name=\'rating\']:checked').prop('checked', false);
			}
		}
	});
});

$(document).ready(function() {
	$('.nomer-gallery-page').magnificPopup({
		type:'image',
		delegate: 'a.pop-me',
		gallery: {
			enabled:true
		}
	});

    rebuilding();
});

date = '<?php echo date("d-m-Y"); ?>';
selector = 1;

    function rebuilding() {
        $.ajax({
            url: 'index.php?route=api/order/getdates&customer_group_id=<?php echo $customer_group_id; ?>',
            type: 'post',
            data: 'product_id=<?php echo $product_id; ?>&month=' + date+'&data_start='+ $('input[name="booking_date_start"]').val()+'&data_end='+$('input[name="booking_date_end"]').val() + '&another=1&quantity=' + $('input[name="quantity"]').val(),
            dataType: 'json',
            success: function(json) {
                if(json['calendar']) {
                    $('#calendar').html(json['calendar']);
                }
            }
        });
    }

    function chooseDay(day,next_day) {

        if( $('input[name="booking_date_start"]').val()>day)
            {
                selector = 1;  
            }
        if(selector == 1) {
            $('input[name="booking_date_start"]').val(day);
            $('input[name="booking_date_end"]').val(next_day);
            rebuilding();
        } else {

            $('input[name="booking_date_end"]').val(day);
            rebuilding();
        }
        
        selector = 1 - selector;
    }

//--></script>
<?php echo $footer; ?>
