$(function(){
	var imgCnt = $(".banner-rotator #img").size();
	var curImage = 1;
	var btns = '';
	var tid = 0;
	var cnttbl = 1;
	$(".banner-rotator").css("width", imgCnt*1024+"px");
	for (i=1;i<=imgCnt;i++) {
		btns = btns+'<a id="'+i+'" class="btn" href="#"><img src="/img/btn.png"></a> ';
	}
	
	//tid = setInterval(Slide(curImage), 1000);

	$("#btns").html(btns);
	
	$(".btn").click(function() {
		var id = $(this).attr("id");
		var l = (1024-(id*1024));
		//alert(l);
		//alert(id);
		$(".banner-rotator").animate({'left':l+'px'}, 500);
		clearTimeout(tid);
		return false;
	});
	
	$('.table-text').text($('#portal').val());
	
	if (document.URL.indexOf('portal') > 0) {
		cnttbl = 1;
		$('.table-head').html('Порталы<span style="color: #aeaeae;"> — Промо и визитки — Интернет магазины</span>');
		$('.table-text').text($('#portal').val());
		$('.scroll-table').animate({left: 0+'px'}, 1);
	}
	if (document.URL.indexOf('promo') > 0) {
		cnttbl = 2;
		$('.table-head').html('<span style="color: #aeaeae;">Порталы — </span>Промо и визитки<span style="color: #aeaeae;"> — Интернет магазины</span>');
		$('.table-text').text($('#promo').val());
		$('.scroll-table').animate({left: -720+'px'}, 1);
	}
	if (document.URL.indexOf('market') > 0) {
		cnttbl = 3;
		$('.table-head').html('<span style="color: #aeaeae;">Порталы — Промо и визитки — </span>Интернет магазины');
		$('.table-text').text($('#market').val());
		$('.scroll-table').animate({left: -1440+'px'}, 1);
	}
	
	$('.table-link').click(function(){
		$('.table-desc').slideToggle('slow', function(){
			$('.table-det').toggleClass('upped');
		});
		return false;
	});
	
	if (cnttbl==1) {
		$('.table-left').addClass('passive-left');
	} else {
		$('.table-left').removeClass('passive-left');
	}
	
	$('.table-left').click(function(){
		if (cnttbl > 1) {
			$('.scroll-table').animate({left: $('.scroll-table').position().left+720+'px'}, 500);
			$('.table-head').fadeOut(function(){
				if (cnttbl == 2) {
					$('.table-head').html('<span style="color: #aeaeae;">Порталы — </span>Промо и визитки<span style="color: #aeaeae;"> — Интернет магазины</span>');
				} else {
					$('.table-head').html('Порталы<span style="color: #aeaeae;"> — Промо и визитки — Интернет магазины</span>');
				}
				$('.table-head').fadeIn();
			});
			$('.table-text').fadeOut(function(){
				if (cnttbl == 2) {
					$('.table-text').text($('#promo').val());
				} else {
					$('.table-text').text($('#portal').val());
				}
				$('.table-text').fadeIn();
			});
			cnttbl--;
			if (cnttbl == 1) {
				$('.table-left').addClass('passive-left');
			}
		}
		return false;
	});
	
	$('.table-right').click(function(){
		if (cnttbl == 3) {
			$('.scroll-table').animate({left: 0+'px'}, 500);
			$('.table-left').addClass('passive-left');
			$('.table-head').fadeOut(function(){
				$('.table-head').html('Виды и типы сайтов: порталы<span style="color: #aeaeae;"> — иромо и визитки — интернет магазины</span>');
				$('.table-head').fadeIn();
			});
			$('.table-text').fadeOut(function(){
				$('.table-text').text($('#portal').val());
				$('.table-text').fadeIn();
			});
			cnttbl = 1;
		} else {
			$('.scroll-table').animate({left: $('.scroll-table').position().left-720+'px'}, 500);
			$('.table-left').removeClass('passive-left');
			$('.table-head').fadeOut(function(){
				if (cnttbl == 2) {
					$('.table-head').html('Виды и типы сайтов: <span style="color: #aeaeae;">порталы — </span>промо и визитки<span style="color: #aeaeae;"> — интернет магазины</span>');
				} else {
					$('.table-head').html('Виды и типы сайтов: <span style="color: #aeaeae;">порталы — промо и визитки — </span>интернет магазины');
				}
				$('.table-head').fadeIn();
			});
			$('.table-text').fadeOut(function(){
				if (cnttbl == 2) {
					$('.table-text').text($('#promo').val());
				} else {
					$('.table-text').text($('#market').val());
				}
				$('.table-text').fadeIn();
			});
			cnttbl++;
		}
		return false;
	});
});

function Slide(cnt) {
	$(".banner-rotator").animate({'left':1024-(cnt*1024)+'px'}, 500);
}

function getLink(phone) {
	var ph = phone.replace("+", "");
	/*if (ph.test(/[^\d]/g)) {
		alert('Неправильный ввод номера. Используйте только цифры');
		return false;
	}*/
	if ($('#phone-country').val().length==0 || $('#phone-code').val().length==0 || $('#phone-number').val().length==0) {
		alert('Введите номер');
		return false;
	} else {
		$.ajax({
			url: "/getlink.php?phone="+ph,
			success: function(data){
				$('#link-container').html(data);
				$('#link-label').removeClass('none');
			}
		});
	}
}

function checkCountry() {
	$('#phone-country').val($('#phone-country').val().replace(/[^\d+]/g, ''));
	if ($('#phone-country').val().length>=3) {
		$('#phone-code').focus().select();
	}
}

function checkCode() {
	$('#phone-code').val($('#phone-code').val().replace(/[^\d]/g, ''));
	if ($('#phone-code').val().length>=3) {
		$('#phone-number').focus().select();
	}
}

function checkNumber() {
	$('#phone-number').val($('#phone-number').val().replace(/[^\d]/g, ''));
	if ($('#phone-number').val().length>=7) {
		$('#getlink-button').focus();
	}
}

function clearInput(id) {
	$('#'+id).attr('value', '');
}

function dolinaScroll(i) {
	$(".src-full").animate({'left':720-(i*720)+'px'}, 75, 'linear');
}

function stopAnim() {
	$(".src-full").stop();
}

