var prev_tab_id = 1;
var count_elements = 0;
var animate = 0;
var nom_photo = 0;

var direction = 1;
var time = 5000;
var varTimeout;

$(document).ready(function() {
    	if ($('.gallery').length > 0) {
            
		$('.tab a').click (function () {
			//album_change($(this));
			return false;
		});
		
		count_elements = $('#tab_content_'+prev_tab_id+' .one_photo').length;
		if (count_elements > 1) {
			$('.gallery a.prev, .gallery a.next').addClass('sel');
		}
		
		$('.gallery a.prev').click (function () {
                        var ids = $(this).attr('id').split('link_');
			prev_element(ids[1]);
			return false;
		});
		
		$('.gallery a.next').click (function () {
                        var ids = $(this).attr('id').split('link_');
			next_element(ids[1]);
			return false;
		});
		
		//$('.tab_content:not("#tab_content_'+prev_tab_id+'")').hide();
	}
	
	if ($('.object .big_photo').length > 0) {
                init_rates();
		count_elements = $('.object .photos_list .one_photo_object').length;
		if (count_elements > 1) {
			$('.photos_list a.prev, .photos_list a.next').addClass('sel');
		}
		
		$('.photos_list a.prev').click (function () {
			prev_element_in_object();
			return false;
		});
		
		$('.photos_list a.next').click (function () {
			next_element_in_object();
			return false;
		});
	}
});

$(window).load(function () {
    varTimeout = setTimeout('auto_slide()', time);
});

function trim (string) {
	var filter  = /^\s*$/;
	if (filter.test(string)) {
		return '';
	} else {
		return string.replace(/^\s*([^\s].*)$/, '$1').replace(/^(.*[^\s])\s*$/, "$1");
	}
}

function album_change (obj) {
	if (animate == 0) {
		animate = 1;
		var tab_id = obj.parent().attr('id').substring(11);
		if (parseInt(tab_id) > 0) {
			var tab_header_text = $('#tab_header_'+prev_tab_id).html();
			
			if ($('#tab_content_'+tab_id).length > 0) {
				tab_header_change (obj, tab_header_text);
				tab_content_change (tab_id);
				animate = 0;
			} else {
				var params = 'act=load&tab='+tab_id;
				$.ajax({ type: 'POST', url: '/ajax/top_event', data: params, success: 
					function (result) {
						if (result != '') {
							tab_header_change (obj, tab_header_text);
							$('.tabs_content').append(result);
							$('#tab_content_'+tab_id).hide();
							
							var img = document.createElement('img');
							img.onload = function () {
								animate = 0;
								tab_content_change (tab_id);
							};
							$(img).attr('src', $('#tab_content_'+tab_id+' .big_photo img').attr('src'));
						} else {
							alert('Этой вкладки не существует.');
							animate = 0;
						}
					}
				});
			}
			
			clearTimeout(varTimeout);
			varTimeout = setTimeout('next_element()', time);
		}
	}
	return false;
}

function tab_header_change (obj, text) {
	$('#tab_header_'+prev_tab_id).html('<a href="#">'+text+'</a>').removeClass('sel').children('a').click (function () {
		album_change($(this));
		return false;
	});
	
	obj.parent().addClass('sel');
	obj.parent().html(obj.html());
}

function tab_content_change (tab_id) {
	$('#tab_content_'+prev_tab_id).hide();
	$('#tab_content_'+tab_id).show();
	prev_tab_id = tab_id;
	count_elements = $('#tab_content_'+prev_tab_id+' .one_photo').length;
	if (count_elements > 1) {
		$('.gallery a.prev, .gallery a.next').addClass('sel');
	} else {
		$('.gallery a.prev, .gallery a.next').removeClass('sel');
		
	}
}

function auto_slide(){
        if(direction == 1) next_element('all');
        else prev_element('all');
}

function prev_element (curr_id) {
	if ($('.gallery a.prev').hasClass('sel')) {
		if (animate == 0) {
                    if(curr_id == 'all'){
                        prev(1, 'prev');
                     }
                     else{
                         prev(curr_id);
                     }
		}
	}
}

function prev(curr_id, is_prev){
    animate = 1;
                            direction = -1;
                            clearTimeout(varTimeout);

                            $('#tab_content_'+curr_id+' .list_inner .one_photo:last').clone().insertBefore('#tab_content_'+curr_id+' .list_inner .one_photo:first');
                            $('#tab_content_'+curr_id+' .list_inner').css({ marginLeft: '-124px' });
                            $('#tab_content_'+curr_id+' .list_inner .one_photo:last').remove();

                            var src = $('#tab_content_'+curr_id+' .list_inner .one_photo:last').children('.gallery_photo').children('img').attr('src').replace(/small/, 'big');

                            $('#tab_content_'+curr_id+' .list_inner').animate({ marginLeft: '0px' }, 500, 'linear', function () {
                                    var img = document.createElement('img');
                                    img.onload = function () {
                                            $('#tab_content_'+curr_id+' .big_photo .big_photo_text h2').html($('#tab_content_'+curr_id+' .list_inner .one_photo:last').children('.gallery_text').children('h2').html());
                                            $('#tab_content_'+curr_id+' .widget_content').html($('#tab_content_'+curr_id+' .list_inner .one_photo:last').children('.gallery_text').children('.w_content').html());

                                            $('#tab_content_'+curr_id+' .big_photo img.hidden').attr('src', src);
                                            $('#tab_content_'+curr_id+' .big_photo img.hidden').removeClass('hidden').css({ opacity: '1' });

                                            $('#tab_content_'+curr_id+' .big_photo img.visible').animate({ opacity: '0' }, 500, 'linear', function () {
                                                    $('#tab_content_'+curr_id+' .big_photo img.visible').removeClass('visible').addClass('hidden');
                                                    $('#tab_content_'+curr_id+' .big_photo img:not(".hidden")').addClass('visible').css({ opacity: '1' });

                                                    varTimeout = setTimeout('auto_slide()', time);

                                                    animate = 0;
                                                    if(is_prev == 'prev'){
                                                        if(curr_id != 4) prev(++curr_id, 'prev');
                                                    }
                                            });
                                    };
                                    $(img).attr('src', src);
                            });
}

function next_element (curr_id) {
    
	if ($('.gallery a.next').hasClass('sel')) {
		if (animate == 0) {
                    if(curr_id == 'all'){
                        next(1, 'next');
                     }
                     else{
                         next(curr_id,'');
                     }
		}
	}
}

function next(curr_id, is_next){
    animate = 1;
			direction = 1;
			clearTimeout(varTimeout);

			$('#tab_content_'+curr_id+' .list_inner .one_photo:first').clone().insertAfter('#tab_content_'+curr_id+' .list_inner .one_photo:last');
                        $('#tab_content_'+curr_id+' .list_inner').animate({ marginLeft: '-124px' }, 500, 'linear', function () {
			$('#tab_content_'+curr_id+' .list_inner').css({ marginLeft: '0px' }).children('.one_photo:first').remove();
                        
			var src = $('#tab_content_'+curr_id+' .list_inner .one_photo:last').children('.gallery_photo').children('img').attr('src').replace(/small/, 'big');

                        var img = document.createElement('img');
				img.onload = function () {
					$('#tab_content_'+curr_id+' .big_photo .big_photo_text h2').html($('#tab_content_'+curr_id+' .list_inner .one_photo:last').children('.gallery_text').children('h2').html());
                                        var html_widget = $('#tab_content_'+curr_id+' .list_inner .one_photo:last').children('.gallery_text').children('.w_content').html();
                                        $('#tab_content_'+curr_id+' .big_photo+.widget_content').html(html_widget);

					$('#tab_content_'+curr_id+' .big_photo img.hidden').attr('src', src);
					$('#tab_content_'+curr_id+' .big_photo img.hidden').removeClass('hidden').css({ opacity: '1' });
                                        
					$('#tab_content_'+curr_id+' .big_photo img.visible').animate({ opacity: '0' }, 500, 'linear', function () {
                                            $('#tab_content_'+curr_id+' .big_photo img.visible').removeClass('visible').addClass('hidden');
                                            $('#tab_content_'+curr_id+' .big_photo img:not(".hidden")').addClass('visible').css({ opacity: '1' });

                                            varTimeout = setTimeout('auto_slide()', time);
                                            animate = 0;
                                            if(is_next == 'next'){
                                                if(curr_id != 4) next(++curr_id, 'next');
                                            }
					});
				};
				$(img).attr('src', src);
			});
}

function checkEmail (text) {
	var text = trim(text);
	if (text != '' && text != 'введите ваш e-mail') {
		var filter  = /^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,4}$/;
		if (!filter.test(text)) {
			alert('Недопустимые символы в адресе электронной почты.');
			return false;
		}
	} else {
		alert('Введите адрес электронной почты.');
		return false;
	}
}

function prev_element_in_object () {
	if ($('.photos_list a.prev').hasClass('sel')) {
		if (animate == 0) {
			animate = 1;
			$('.object .photos_list .one_photo_object:last').clone().insertBefore('.object .photos_list .one_photo_object:first');
			$('.object .photos_list .photos_list_inner').css({ marginLeft: '-158px' });
			$('.object .photos_list .one_photo_object:last').remove();
			
			var src = $('.object .photos_list .one_photo_object:first').children('a').children('img').attr('src').replace(/small/, 'big');
			
			$('.object .photos_list .photos_list_inner').animate({ marginLeft: '0px' }, 500, 'linear', function () {
				showTextForObject();
				
				var img = document.createElement('img');
				img.onload = function () {
					$('.object .big_photo img.hidden').attr('src', src);
					$('.object .big_photo img.hidden').removeClass('hidden').css({ opacity: '1' });
					$('.object .big_photo img.visible').animate({ opacity: '0' }, 500, 'linear', function () {
						$('.object .big_photo img.visible').removeClass('visible').addClass('hidden');
						$('.object .big_photo img:not(".hidden")').addClass('visible').css({ opacity: '1' });
						animate = 0;
					});
				};
				$(img).attr('src', src);
			});
		}
	}
}

function next_element_in_object () {
	if ($('.photos_list a.prev').hasClass('sel')) {
		if (animate == 0) {
			animate = 1;
			$('.object .photos_list .one_photo_object:first').clone().insertAfter('.object .photos_list .one_photo_object:last');
			
			$('.object .photos_list .photos_list_inner').animate({ marginLeft: '-158px' }, 500, 'linear', function () {
				$('.object .photos_list .photos_list_inner').css({ marginLeft: '0px' }).children('.one_photo_object:first').remove();
				
				showTextForObject();
				
				var src = $('.object .photos_list .one_photo_object:first').children('a').children('img').attr('src').replace(/small/, 'big');
				var img = document.createElement('img');
				img.onload = function () {
					$('.object .big_photo img.hidden').attr('src', src);
					$('.object .big_photo img.hidden').removeClass('hidden').css({ opacity: '1' });
					$('.object .big_photo img.visible').animate({ opacity: '0' }, 500, 'linear', function () {
						$('.object .big_photo img.visible').removeClass('visible').addClass('hidden');
						$('.object .big_photo img:not(".hidden")').addClass('visible').css({ opacity: '1' });
						animate = 0;
					});
				};
				$(img).attr('src', src);
			});
		}
	}
}

function change_on_this_photo (obj) {
	getPhotoPos (obj);

	if (nom_photo > 0) {
		if (animate == 0) {
			animate = 1;
			moveList(500/nom_photo);
		}
	}
}

function getPhotoPos (obj) {
	if (obj.children('img').attr('src') != $('.object .photos_list .photos_list_inner .one_photo_object:eq('+nom_photo+')').children('a').children('img').attr('src')) {
		nom_photo++;
		getPhotoPos (obj);
	}
}

function moveList (time) {
	if (nom_photo > 0) {
		$('.object .photos_list .photos_list_inner .one_photo_object:first').clone().insertAfter('.object .photos_list .photos_list_inner .one_photo_object:last');
		$('.object .photos_list .photos_list_inner').animate({ marginLeft: '-158px' }, time, 'linear', function () {
			$('.object .photos_list .photos_list_inner').css({ marginLeft: '0px' }).children('.one_photo_object:first').remove();
			nom_photo--;
			moveList(time);
		});
	} else {
		showTextForObject();
		
		var src = $('.object .photos_list .photos_list_inner .one_photo_object:first').children('a').children('img').attr('src').replace(/small/, 'big');
		var img = document.createElement('img');
		img.onload = function () {
			$('.object .big_photo img.hidden').attr('src', src);
			$('.object .big_photo img.hidden').removeClass('hidden').css({ opacity: '1' });
			$('.object .big_photo img.visible').animate({ opacity: '0' }, 500, 'linear', function () {
				$('.object .big_photo img.visible').removeClass('visible').addClass('hidden');
				$('.object .big_photo img:not(".hidden")').addClass('visible').css({ opacity: '1' });
				animate = 0;
			});
		};
		$(img).attr('src', src);
	}
}

function showTextForObject () {
	var content = $('.object .photos_list .one_photo_object:first').children('.photo_text').html();
	if (content == '') {
		if ($('.object .big_photo .big_photo_text_object:visible').length == 1) {
			$('.object .big_photo .shadow_object').css('display', 'none');
			$('.object .big_photo .big_photo_text_object').css('display', 'none');
		}
	} else {
		if ($('.object .big_photo .big_photo_text_object:visible').length == 0) {
			$('.object .big_photo .shadow_object').css('display', 'block');
			$('.object .big_photo .big_photo_text_object').css('display', 'block');
		}
	}
	$('.object .big_photo .big_photo_text_object').html(content);
}

function submitReservation () {
	if(!document.form1.agree.checked){
		alert("Пожалуйста, прочитайте условия и подтвердите гарантию оплаты!");
		return false;
	}
	if(document.form1.name.value.length<3){
		alert("слишком короткое имя!");
		return false;
	}
	if(document.form1.contacts.value.length==0){
		alert("Вы не указали контакты!");
		return false;
	}
	if(document.form1.baza2.value.length==0){
		alert("Вы не указали альтернативный вариант!");
		return false;
	}
	if(document.form1.gorod.value.length==0){
		alert("Вы не указали название агенства и город!");
		return false;
	}
	if(document.form1.d_from.value.length==0){
		alert("Не указана дата заезда!");
		return false;
	}
	if(document.form1.d_to.value.length==0){
		alert("Не указана дата выезда!");
		return false;
	}
	if(document.form1.nomer.value.length==0){
		alert("Вы не указали тип размещения!");
		return false;
	}
	if(document.form1.lechenie.value.length==0){
		alert("Вы не указали лечение!");
		return false;
	}
	if(document.form1.trans.value.length==0){
		alert("Не указан трансфер!");
		return false;
	}
	if(document.form1.sendname.value.length==0){
		alert("Не указано имя, кто подал заявку!");
		return false;
	}
	if(document.form1.contacts.value.length==0){
		alert("Вы не указали свою контактную информацию!");
		return false;
	}
	if(document.form1.strahov.value.length==0){
		alert("Вы не указали сведения о страховании!");
		return false;
	}
	if(document.form1.total_td.value.length==0){
		alert("Вы не указали количество туродней!");
		return false;
	}
	return true;
}

function checkPrice(obj1, obj2, obj3) {
	if (obj1.val() == 'поиск') {
		obj1.val('');
	}
	obj2.val(obj2.val().replace(/[^0-9]/g, ''));
	obj3.val(obj3.val().replace(/[^0-9]/g, ''));
}