library.prototype.default_link="/ajax";
$(document).ready(function(){
    function focusTextInput(){
        var inputText = $('input[type=text]'), inputTextValue = '';
        inputText.focus(function(){
            if (this.value == this.lang){
                this.value = '';
            }
        })
        inputText.blur(function(){
            if (this.value == '' || this.value == this.lang){
                this.value = this.lang;
            }
        })
    }
    focusTextInput();

	$('.cSelect').each(function(){
		var self = this;
		$('.options a',this).click(function(){
			changeValue(this);
			if($('#fDate').val() != '' && $('#fLine').val() != ''){
				$.ajj({
					type:"reservation",
					'date':$('#fDate').val(),
					'line':$('#fLine').val()
				},
				function(r){
					$('#formReservation .time:eq(0) .options-list:eq(0) li').remove();
					$(r.time).each(function(i,v){
						$('<li class="options-list-item"><a href="#">'+v+'</a></li>').click(function(){
							changeValue(this); return false;
						}).appendTo('#formReservation .time:eq(0) .options-list:eq(0)');
					});
				});
// 				$.ajax({
// 					url: "/ajax",
// 					type : 'post',
// 					dataType : 'json',
// 					data: {
// 						'date':$('#fDate').val(), 'line':$('#fLine').val(),
// 						type:"reservation"
// 					},
// 					success: function(data){
// 						$('#formReservation .time:eq(0) .options-list:eq(0) li').remove();
// 						$(data).each(function(i,v){
// 							$('<li class="options-list-item"><a href="#">'+v+'</a></li>').click(function(){
// 								changeValue(this); return false;
// 							}).appendTo('#formReservation .time:eq(0) .options-list:eq(0)');
// 						});
// 					}
// 				});
			}
			return false;
		});
	});

	$(document.body).click(function(e){
		if ($(e.target).hasClass('cS-value-container')) $('.cSelect .options').hide();
	});

	cSelect();

	$('#formReservation').bind('submit', function(){
		var err = 0;
		$('#formReservation input').each(function(){
			if($(this).val() == '' || $(this).val() == 'ФИО' || $(this).val() == 'Телефон') err++;
		});
		if(err == 0){
			var request=$(this).serializeArray();
			request["type"]="send_reservation";
			$.ajj({
				line:request[0].value,
				date:request[1].value,
				time:request[2].value,
				name:request[3].value,
				phone:request[4].value,
				type:"send_reservation"
			},
			function(r){
				jQuery.fancybox.hideActivity();
				jQuery.fancybox({'scrolling':'no', 'titleShow':false, 'overlayShow':true, 'showCloseButton':true, 'content':'<div class="mSuccess">'+r.data+'</div>'});
				setTimeout("jQuery.fancybox.close()", 2000);
			});
// 			$.post(location.href, $(this).serializeArray(), function(data){
// 				jQuery.fancybox.hideActivity();
// 				jQuery.fancybox({'scrolling':'no', 'titleShow':false, 'overlayShow':true, 'showCloseButton':true, 'content':'<div class="mSuccess">'+data+'</div>'});
// 				//setTimeout("jQuery.fancybox.close()", 2000);
// 			});
		}else{
			jQuery.fancybox.hideActivity();
			jQuery.fancybox({'scrolling':'no', 'titleShow':false, 'overlayShow':true, 'showCloseButton':false, 'content':'<div class="mSuccess">Вы не заполнили все обязательные поля</div>'});
			setTimeout("jQuery.fancybox.close()", 2000);
		}
		return false;
	});


	$('.cosmetics').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		//timeout: 2000,
		//delay:  -2000,
		random: 0
	});

	$('.look-into').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		//timeout: 2000,
		//delay:  -2000,
		random: 0
	});

// 	$('.ImgBag').fancybox({
// 		"overlayShow" : false,
// 		"zoomSpeedIn" : 600,
// 		"zoomSpeedOut" : 500,
// 		"titlePosition" : "inside",
// 		"hideOnContentClick" : true
// 	});
});


function cSelect(){
	$('.cSelect .options').append('<i class="corners lt"></i><i class="corners rt"></i><i class="corners lb"></i> <i class="corners rb"></i>');
	$('.cSelect').click(function(){
		var self = this;
		$('.cSelect .options').hide();
		$('.cSelect').removeClass('higher');
		this.className += ' higher';
		$('.options',this).slideToggle();
		return false;
	});
}


function changeValue(obj){
	var self = $(obj).parents('.cSelect:eq(0)');
	$('.cS-value',self).text($(obj).text());
	$('.options a', self).removeClass('selected');
	if($('.fDate', self).length<=0){
		$(obj).addClass('selected').parents('.options:eq(0)').find('input:hidden').val($(obj).text());
	}else{
		$(obj).addClass('selected').parents('.options:eq(0)').find('input:hidden').val(($(obj).attr('href')).substr(1));
	}
	$('.options',self).hide();
	$(self).attr('class', $(self).attr('class').replace(/\bhigher\b/gi,''));
}
