var yPhotos = [];
var yPhotosCount = 0;
var yCurrentPhoto = 0;

function changeYachtPhoto( offset ) {
  yCurrentPhoto += offset;
  if ( yCurrentPhoto < 0 )
    yCurrentPhoto += yPhotosCount;
  else if (yCurrentPhoto >= yPhotosCount)
    yCurrentPhoto -= yPhotosCount;
  
  $('.yacht-info .photo').hide();
  $(yPhotos[yCurrentPhoto]).show();
}
function initYachtPhotos() {
  $('.yacht-info .photo').each(function() { yPhotos.push(this); });
  yPhotosCount = yPhotos.length;
  if (yPhotosCount) {
    yCurrentPhoto = 0;
    $(yPhotos[yCurrentPhoto]).show();
    $('.yacht-info .prev').click(function() { changeYachtPhoto(-1); return false; });
    $('.yacht-info .next').click(function() { changeYachtPhoto(+1); return false; });
  }
}
function changeEndPort() {
	var $form = $(this).closest("form");
	var po = parseInt( $form.find("#cart_boat_port_id").val() );
	var bp = parseInt( $form.find("#cart_begin_port_id").val() );
	var ep = parseInt( $form.find("#cart_end_port_id").val() );
	var es = $form.find("#cart_end_port_id option").length;
	var ow = parseFloat( $form.find("#cart_surcharge_for_oneway_price").val() );
	var tp = parseFloat( $form.find("#cart_total_price_without_oneway").val() );
	var ip = parseFloat( $form.find("#cart_instalment_percent").val() );
	var cu = $form.find("#cart_currency").val();
	var price = (es > 1 && po != ep && bp != ep) ? (tp + ow) : tp;
	$form.find("#order_total_price").html( price.format(2," ",",") + " " + cu );
	$form.find("#order_instalment_price").html( (price * ip / 100).format(2," ",",") + " " + cu );
}



function initCharters() {
	initYachtPhotos();
}
$(document).ready( initCharters );