jQuery(function( $ ){
	/**
	 * Most jQuery.localScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
	 * @see http://flesler.demos.com/jquery/scrollTo/
	 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.LocalScroll.
	 */

	// The default axis is 'y', but in this demo, I want to scroll both
	// You can modify any default like this
	$.localScroll.defaults.axis = 'x';
	/**
	 * NOTE: I use $.localScroll instead of $('#navigation').localScroll() so I
	 * also affect the >> and << links. I want every link in the page to scroll.
	 */
	$.localScroll({
		target: '#content', // could be a selector or a jQuery object too.
		queue:false,
		duration:600,
		hash:false,
		onBefore:function( e, anchor, $target ){
			// The 'this' is the settings object, can be modified
		},
		onAfter:function( anchor, settings ){
			// The 'this' contains the scrolled element (#content)
		}
	});
});

function show_page() {
	$("#loader").fadeOut("200", function() {
		$("BODY").css({background: "#9c2412 url(/files/bg.jpg) repeat-x"});
		$("#page").fadeIn("200");
	});
}

$(function(){
	$("#loading_slider")
		.css( {backgroundPosition: "0 0"} )
		.animate({backgroundPosition:"(190px 0)"}, {duration:1800})
		.animate({backgroundPosition:"(0 0)"}, {duration:1800})
		.animate({backgroundPosition:"(190px 0)"}, {duration:1800})
		.animate({backgroundPosition:"(0 0)"}, {duration:1800})
		.animate({backgroundPosition:"(140px 0)"}, {duration:1800})
		setTimeout(show_page, 9000);
});

$(document).ready(function(){
		$("#loader").click(function(){		show_page();
	});
});

var icons = new Array('hotel.png','room.png','restaurant.png','cam.png','contacts.png','contacts.png');
var n_subs = 5;
var curent_sub = 0;
var last_sub = 0;

function change_icon(curent_sub) {	$("#icon").fadeOut("200", function() {		$(this).css({background: "url(/files/" + icons[curent_sub] + ") no-repeat"});		$(this).fadeIn("400");
	});
}

function show_gallery() {
	$("#frame").html("<iframe id='iframe' width='660' scrolling='no' height='550' frameborder='0' marginheight='0' marginwidth='0' hspace='0' vspace='0' src='/photos.php'></iframe>");
	$("#frame").fadeIn("200");
}

function hide_gallery() {
	$("#frame").html("");
	$("#frame").css({display: "none"});
}

function change_sub(curent_sub) {
	if (curent_sub != last_sub) {		hide_gallery();		change_icon(curent_sub);
		if (curent_sub == 3) {			setTimeout(show_gallery, 800);		}
		last_sub = curent_sub;	}
	$('#content').scrollTo( 'DIV.sub:eq(' + curent_sub + ')', 600 );
}

$(function() {	var width = (n_subs + 1) * 760;
	$(".section").css({width: width + "px"});
	$(".nav_left").click(function(){
			if(curent_sub > 0) { curent_sub = curent_sub - 1; }
			change_sub(curent_sub);
		})
	$(".nav_right").click(function(){
			if(curent_sub < n_subs) { curent_sub = curent_sub + 1; }
			change_sub(curent_sub);
		})
});

$(function() {
	$("#logo").click(function(){
			curent_sub = 0;
			change_sub(curent_sub);
		})
	$(".menu_item").click(function(){
			curent_sub = $(".menu_item").index(this);
			change_sub(curent_sub);
		})
});

$(function(){
	$(".menu_item")
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(0 -10px)"}, {duration:150})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:150})
		});
	$(".nav_left")
		.mouseover(function(){
			$(this).addClass('nav_left_hover')
		})
		.mouseout(function(){
			$(this).removeClass('nav_left_hover')
		});
	$(".nav_right")
		.mouseover(function(){
			$(this).addClass('nav_right_hover')
		})
		.mouseout(function(){
			$(this).removeClass('nav_right_hover')
		});
});

// форма бронирования
function isValidField(field) {	isValid = false;

    if(field == 'date1') { isValid = /^\d{1,2}\.\d{1,2}\.\d{4}$/.test($('#' + field).val()); }
    if(field == 'date2') { isValid = /^\d{1,2}\.\d{1,2}\.\d{4}$/.test($('#' + field).val()); }
    if(field == 'adults') { isValid = $('#' + field).val() != ''; }
    if(field == 'children') { isValid = $('#' + field).val() != ''; }
    if(field == 'name') { isValid = $('#' + field).val().length > 3; }
    if(field == 'address') { isValid = $('#' + field).val().length > 3; }
    if(field == 'phone') { isValid = /^\+?\d{6,}$/.test($('#' + field).val().replace(/ /g, '')); }

    if(!isValid) { $('#' + field).css('color', '#ff4949'); }
    else { $('#' + field).css( 'color', '')
						 .css( 'border', ''); }
    return isValid;
}

function ValidateForm() {
	var ValidateFields = new Array('date1','date2','adults','children','name','address','phone');
	isValid = true;
	for (var i = 0; i < ValidateFields.length; i++) {		if(!isValidField(ValidateFields[i])) {			isValid = false;
			$('#' + ValidateFields[i]).css( 'border', '1px solid #ff4949');		}
	}
	if(isValid) {		$.post("/booking.php", {
			date1: $("#date1").val(),
			date2: $("#date2").val(),
			adults: $("#adults").val(),
			children: $("#children").val(),
			name: $("#name").val(),
			address: $("#address").val(),
			phone: $("#phone").val(),
			email: $("#email").val(),
			additionally: $("#additionally").val(),
			action: "send" });
		$('#booking').fadeOut("200", function () {
			$('DIV.booking').html("<div style='margin: 150px auto; width: 310px;'>Спасибо. Ваша заявка принята. <br>В ближайшее время с Вами свяжется менеджер.</div>");
			$('DIV.booking').fadeIn("200");
		});
	} else { $('#ValidateResult').fadeIn("200"); }
	return false;
}

$(function() {	$("DIV.booking").corner("8px");
	$(".short,.long").focus(function () {
		$(this).css( 'border', '');
		$('#ValidateResult').fadeOut("200");
	});
	$.datepicker.setDefaults( { dateFormat: 'dd.mm.yy',
								firstDay: 1,
								monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
								dayNamesMin: ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'],
								minDate: '0'
	} )
	$("#date1").datepicker();
	$("#date2").datepicker();
});

$(function() {
	$("#uah").click(function(){
		$(".uah,.rur,.usd").hide();
		$(".uah").show();
	})
	$("#rur").click(function(){
		$(".uah,.rur,.usd").hide();
		$(".rur").show();
	})
	$("#usd").click(function(){
		$(".uah,.rur,.usd").hide();
		$(".usd").show();
	})
});
