var guide_data;
$(document).ready(function(){
	$('#btn_booking').fancybox({
		'padding' : 15,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',	
		'titleShow' : false,
		'href' : "/request_cr/",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			$.fancybox.center();
			
			$('input[name*=check_out]:text, input[name*=check_in]:text').attachDatepicker();
			$('#fancybox-content form').submit(function(e){
				e.preventDefault();
			
				$.post('/webforms/ajax_send/', $("#fancybox-content form").serialize(),
					function(_response, _status, _request) {
						_1 = $.parseJSON(_response);
						if (_1.success){
							$.fancybox.close();
							alert(_1.msg);
						} else {
							$('#fancybox-content #error').text(_1.error);
						}
					}
				);
			});
		}
	});
	
	$('#btn_tour_booking').fancybox({
		'padding' : 15,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'scrolling' : 'no',	
		'titleShow' : false,
		'href' : "/request_tour/",
		'ajax' : {
		    type : "POST"
		},
		'onComplete' : function() {
			$.fancybox.center();
			
			$('input[name*=check_in]:text').attachDatepicker();
			$('#fancybox-content form').submit(function(e){
				e.preventDefault();
			
				$.post('/webforms/ajax_send/', $("#fancybox-content form").serialize(),
					function(_response, _status, _request) {
						_1 = $.parseJSON(_response);
						if (_1.success){
							$.fancybox.close();
							alert(_1.msg);
						} else {
							$('#fancybox-content #error').text(_1.error);
						}
					}
				);
			});
		}
	});
});

function checkValue(){
	$('input[name*=child_cnt], input[name*=member_cnt]').attr('maxlength', 3);
	$('input[name*=child_cnt]').val(0);
	$('input[name*=child_cnt], input[name*=member_cnt]').keypress(function(e){ 
		if (e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){
			return false;
		}    
    });
};

function loadTourData(){
	$('select[name*=tour]').parent().parent().hide();
	$.ajax({
		url		: '/webforms/guide_data',
		dataType: 'json',
		data	: 'type=766&value=768',
		success	: function(_data, _status){
			guide_data = _data;
		}
	});
	$('select[name*=type]').change(function(){
		_ind = ~~$('select[name*=type] > option:selected').val();
		_parent = $('select[name*=tour]').parent().parent();
		if (!_ind){
			_parent.hide();
		} else {
			_parent.show();
			$('select[name*=tour] > option').addClass('hidden');
			$('select[name*=tour] > option').each(function(){
				if ($.inArray(~~$(this).val(), guide_data[_ind]) != -1){
					$(this).removeClass('hidden');
				}
			});
			$('select[name*=tour] > option:first').removeClass('hidden');
			$('select[name*=tour] > option:first').attr('selected', true);
		}
	});
};

function loadRequestData(){
	$('select[name*=placement]').parent().parent().hide();
	$.ajax({
		url		: '/webforms/guide_data',
		dataType: 'json',
		data	: 'type=760&value=764',
		success	: function(_data, _status){
			guide_data = _data;
		}
	});
	$('select[name*=type]').change(function(){
		_ind = ~~$('select[name*=type] > option:selected').val();
		_parent = $('select[name*=placement]').parent().parent();
		if (!_ind){
			_parent.hide();
		} else {
			_parent.show();
			$('select[name*=placement] > option').addClass('hidden');
			$('select[name*=placement] > option').each(function(){
				if ($.inArray(~~$(this).val(), guide_data[_ind]) != -1){
					$(this).removeClass('hidden');
				}
			});
			$('select[name*=placement] > option:first').removeClass('hidden');
			$('select[name*=placement] > option:first').attr('selected', true);
		}
	});
};

function sliderBlock(_block, _width, _height, _time, _isStart){
	if (typeof _isStart == 'undefined') _isStart = true;
	$('#' + _block).css({width:_width, height:_height,'border':'1px solid #993366',overflow:'hidden'});
	$('#' + _block + ' img').css({height: _height-5, 'float':'left', 'margin': '3px 5px 3px 3pt','width':_width/4});
	if (_isStart){
		$('#' + _block + ' > div').hide();
		$('#' + _block + ' > div:first').show();
	} else {
		_activeBlock = $('#' + _block + ' > div:visible');
		_activeBlock.hide();
		if (_activeBlock.next().size() == 0){
			$('#' + _block + ' > div:first').show();
		} else {
			_activeBlock.next().show();
		}
	}
	setTimeout(function(){sliderBlock(_block, _width, _height,_time, false)}, _time*1000);
};
