/**
 * 
 */
var allJSSelectors = {
		dialog					: '.tech-dialog',
		moreFeautersLink 		: '.more-features a',
		bannerListLink			: '.banner-list a',
		subscribeForm			: '#subscribeForm'
}
jQuery(function () {
	var $ = jQuery;
	jQuery('.free_call').live('click',function () {
		var phone = prompt('Введите ваш телефонный номер');
		if (phone) {
			$.post('/request-free-call.php',{
				phone : phone
			},function () {
				alert('В ближайшее время Вам перезвонят');
			});
		}
		return false;
	});
	// Should i display dialog?
	var displayDialog = readCookie('dtd'); // display technical dialog
	displayDialog = readCookie('more_features_visible') == 1;
	
	// Display sample dialog
	$(allJSSelectors.dialog).dialog({
		position 		: ['left','bottom'],
		draggable 		: false,
		resizable		: false,
		width			: 510,
		height			: 300,
		show			: 'slide',
		autoOpen		: displayDialog,
		title			: 'Дополнительные возможности',
		beforeClose     : function () {
			
			window.clearInterval(intervalId);
			setCookie('more_features_visible',0);
		}
	});
	 $("#tabs").tabs();
	
	var currentPos = 0;
	var maxItem = $('.hotlist .hot').length;
	var elementWidth = 480;
	
	var intervalFunc = function () {
		
		currentPos = parseInt(currentPos) + 1;
		if (maxItem <= currentPos) {
			currentPos = 0;
		}
		
		$('.hotlist').animate({
				marginLeft :  -1 * currentPos * elementWidth 
		},500);
		
	}
	var intervalId = window.setInterval(intervalFunc,2000);

	$(allJSSelectors.moreFeautersLink).click(function () {
		$(allJSSelectors.dialog).dialog("close");
		$(allJSSelectors.dialog).dialog("open");
		setCookie('more_features_visible',1);
		return false;
	});
	$(allJSSelectors.bannerListLink).click(function () {
		if (intervalId) {
			window.clearInterval(intervalId);
			intervalId = 0;
		}
		var position = $(this).parent().prevAll().length;
		// Чтобы установилась нужная позиция отнимаем 1 
		currentPos = position - 1;
		intervalFunc();
		return false;
	});
})
jQuery(function () {
	var $ = jQuery;
	$(allJSSelectors.subscribeForm).submit(function () {
		var form = $(this);
		var email = form.find('input[name=email]').val();
		if (!email.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/i)) {
			alert('Вы не ввели E-mail');
			return false;
		}
		$.post('/subscribe.php',{
			subscribe	: 1,
			email		: email,
			ajax		: 1
		},function (code) {
			var data = eval('(' + code + ')');
			if (data.error) {
				alert(data.error);
			}
			form.slideUp().after('<h2>Форма отослана</h2>');
			window.setTimeout(function () {
				window.location = '/subscribed/';
			},3000);
		});
		return false;
	});
});
/**
 * В данном скрипте мы автоматом загружаем аяксом блок туров, первые 16 выводим, остальные скрываем, только нажатие кнопки отобразит все
 */
jQuery(function () {
	var $ = jQuery;
	var visible = false;
	var block = $('.all_tours .loading') 
	if (block.hasClass('loaded'))  { return ;}
	if (block.length > 0) {
		// Ajax-request
		$.get('./?getTours=1',{},function (html) {
			$('p.intro',block).remove();
			$('p',block).removeClass('hidden');
			block.append(html);
			block.append('<hr/>');
			$('tr:gt(16)').hide();
		})
	}
	$('.show-more',block).click(function () {
		if (!visible) {
			
			$('table tr:gt(16)',block).show();
			window.setTimeout(function () {
				$('table',block).effect('highlight');
				visible = true;
			},100);
			
		} else {
			$('table tr:gt(16)',block).hide();
			visible = false;
		}
		return false;
	});
});
function readCookie(name){var nameEQ=name+"=";var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==' ')c=c.substring(1,c.length);if(c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);}return null;}
function setCookie(name,value) {
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000)); //365 дней
	document.cookie = name + '=' + value + '; expires=' + date.toGMTString() + '; path=/';
}
