$(document).ready(function(){

//Form submit
$('a.submit').click(function(){
	$(this).parents('form').submit();
	return false;
});


$("input[name=month]").click(function () {
   $(".datepicker[name=date]").datepicker(
		'setDate', $(this).val()+'m'
	).datepicker( 'show' );
});


//Spinner
$(".spinbox#s1").spinner({
	min: 1,
	max: 15,
	timeout: 500,
	change: function () {
	   $num = $(".spinbox").val();
	   if ($num >= 1 & $num <= 15) {
			$("span.price").each(function (i) {
			   $(this).html(parseInt($(this).attr('price')) * $num);
			});
		}
	}
});

//Spinner
$("table.tbl_addons tr td .spinbox").spinner({
	min: 0,
	max: 15,
	timeout: 500,
	change: function () {
	   $global_total = parseInt($('#sub_total').text());
	   $num = $(this).val();//alert($global_total);
	   if ($num >= 0 & $num <= 15) {
		   $total = parseInt($(this).parents('tr').children('td:eq(1)').text()) * $num;
		   $(this).parents('tr').children('td:eq(3)').html($total.toFixed(2));
		   $('.tbl_addons tr').each(function (i) {
		   	if ($(this).children('td:eq(3)').text() != '') $global_total += parseInt($(this).children('td:eq(3)').text());
			});
		   $('#total').text($global_total.toFixed(2))
	   }
	}
});


//Taxi transfer
$("input[name='userinfo[taxi]']").click(function () {
	if ($(this).val() == 1) {
      $('#tbl_taxi').fadeIn('fast');
	}
	else $('#tbl_taxi').fadeOut('');
});


//Payment method switch
$("input[name='userinfo[payment_method]']").click(function () {
	if ($(this).val() == 'paypal') {
      $('table.tbl_total').animate({marginLeft: 0}, 300);
	}
	else $('table.tbl_total').animate({marginLeft: 263}, 300);
});


//Payment method switch
$(".cart input[name='userinfo[payment_method]']").click(function () {
	if ($(this).val() == 'paypal') {
      $('table.tbl_total').animate({marginLeft: 0}, 300);
	}
	else $('table.tbl_total').animate({marginLeft: 263}, 300);
	$('form').hide();
	$('form[name='+ $(this).val() +']').show();
});


//Cols equalize
$cols_height = jQuery.makeArray($('.equalize'));
$cols_height = jQuery.map($cols_height, function(n, i){return n.offsetHeight;});
$('.boats .sec_cor').height($cols_height.max());


//Select onchange relocation
$('select.relocation').change(function(){
	var $relocation = $(this).val();
	if ($relocation) location.href = $relocation;
});


//Form submit
$('a.submit').click(function(){
	$(this).parents('form').submit();
	return false;
});


//Form value switcher /input
$('input.switch').focus(function(){
   var defaultVal = $(this).attr('default_val'),
       currentVal = $(this).val();
   $(this).removeClass('error');
   if (currentVal == defaultVal) $(this).val('');
}).blur(function(){
   var defaultVal = $(this).attr('default_val'),
       currentVal = $(this).val();
   if (currentVal == '') $(this).val(defaultVal);
})


//Xeno
$('#xeno').hover(
	function () {
      $('#xeno .about').animate({marginLeft: -260, width: 266, opacity: 0.9}, 500);
      $('#xeno .button').addClass('hover');
	},
	function () {
      setTimeout(function () {
			$('#xeno .about').animate({marginLeft: 5, width: 0}, 500, function () {$('#xeno .button').removeClass('hover');});
		}, 200);
	}
);


$("#gallery_buttons a").click(function(){

   rotateStop = true;

	$('#gallery_over a').removeAttr('href');

	$('#gallery_bl img:visible').fadeOut(300, function() {
	   if ($('#gallery_bl img:visible').attr('href').length > 0) $('#gallery_over a').attr('href', $('#gallery_bl img:visible').attr('href'));
	});

	if ($(this).hasClass('next')) {
		if($('#gallery_bl img:visible').next().is('img')) $('#gallery_bl img:visible').next().fadeIn(300);
		else $('#gallery_bl img:first').fadeIn(300);
	}
	else {
		if($('#gallery_bl img:visible').prev().is('img')) $('#gallery_bl img:visible').prev().fadeIn(300);
		else $('#gallery_bl img:last').fadeIn(300);
	}

	return false;
});


if ($('#gallery_bl img').length > 1) setTimeout(picRotate,3000);
else {$("#gallery_buttons a").hide();}

//MAIN!
});


var rotateStop = false;

function picRotate () {

	if (rotateStop == true) return false;

	$('#gallery_over a').removeAttr('href');

	$('#gallery_bl img:visible').fadeOut(1500, function() {
	   if ($('#gallery_bl img:visible').attr('href').length > 0) $('#gallery_over a').attr('href', $('#gallery_bl img:visible').attr('href'));
	});
	if($('#gallery_bl img:visible').next().is('img')) $('#gallery_bl img:visible').next().fadeIn(1500);
	else $('#gallery_bl img:first').fadeIn(1500);

	setTimeout(picRotate,5000);
}


Array.prototype.max = function(){
	return Math.max.apply({},this)
}
