var startInd = 0;
var current_special_offer;	
var flag_show_more = false;
var ctime;	

function start_timer() {
	if(special_offers_count > 1)
		ctime = setTimeout("show_next_special_offer()", timeout);
}
	
$(document).ready(function(){	
	function preload(images) {
		if (typeof document.body == "undefined") return;
		try {
			var div = document.createElement("div");
			var s = div.style;
			s.position = "absolute";
			s.top = s.left = 0;
			s.visibility = "hidden";
			document.body.appendChild(div);
			div.innerHTML = "<img src=\"" + images.join("\" /><img src=\"") + "\" />";
			var lastImg = div.lastChild;
			lastImg.onload = function() { document.body.removeChild(document.body.lastChild); };
		} catch(e) {
			// Error. Do nothing.
		}
	}

	if(special_offers_count > 1)	
		preload(preloadImages);
		
	current_special_offer = $("#0");
	
	$("#area_info").click(function(){
		window.location = $("#largeImgLink").attr("href");
		return false;
	});			
	
	$(".thumbs").mouseover(function(){
		show_big_image($(this));
		clearTimeout(ctime);
		return false;
	});	

	if (special_offers_count > 4)
	{
		$(".thumbnails").hide();
		$(".scrollable").show();
		for (i = 0; i < 4; i++)
		{
			$("#thumbnails_" + i).show();
		}
	}
	else
	{
		$("#item_more_down").hide();
		$("#item_more_up").hide();
		$(".scrollable").show();
	}
	
	$("#item_more_down").click(function(){
		show_more_down();
		return false;
	});	

	$("#item_more_up").click(function(){
		show_more_up();
		return false;
	});	
});
	
function show_next_special_offer() {

	new_id = parseInt(current_special_offer.attr("id")) + 1;
	if(new_id > special_offers_count - 1) 
		new_id = 0;
	if(new_id >= 4 && special_offers_count > 4) 
		flag_show_more = true;
	var obj = $("#" + new_id);
	show_big_image(obj);
	if(flag_show_more) 
		show_more_down();	
	ctime = setTimeout("show_next_special_offer()", timeout);
	return true;
}	

function show_big_image(obj) {

	var objectLink = obj.attr("href");
	var largePath = obj.find('img').attr('src');
	var largeAlt = obj.attr("title");
	
	$("#largeImg").attr({ src: largePath, alt: largeAlt, title: largeAlt });
	$("#largeImgLink").attr({ href: objectLink });
	//$("#largeImgLink").attr({ href: obj.find('div.object_link').attr('innerHTML') });
	
	$("#largeImgTitle").attr({ innerHTML: largeAlt });
	$("#largeImgContent").attr({ innerHTML: obj.find('div.thumb_content').attr('innerHTML') });

	$(".special_offer").removeClass("act");
	obj.find("span").addClass("act");

	current_special_offer = obj;
	
	return true;
}

function redirect_on_object_page(obj) {
	window.location = obj.find('div.object_link').attr('innerHTML');
	return true;
}	

function show_more_down() {

	if (startInd >= special_offers_count - 1)
		startInd = 0;
	else
		startInd++;
	var j = startInd;
	
	$(".thumbnails").hide();
	for (i = 0; i < 4; i++)
	{
		if (i == 0)
		{
			$("#thumbnails_" + j).prependTo(".scrollable");
			var el = $("#thumbnails_" + j);
		}
		else
		{
			$("#thumbnails_" + j).insertAfter(el);
			el = $("#thumbnails_" + j);
		}
		$("#thumbnails_" + j).show();
		j++;
		if (j >= special_offers_count)
			j = 0;
	}
	
	return true;
}

function show_more_up() {

	if (startInd == 0)
		startInd = special_offers_count - 1;
	else
		startInd--;
	var j = startInd;
	
	$(".thumbnails").hide();
	for (i = 0; i < 4; i++)
	{
		if (i == 0)
		{
			$("#thumbnails_" + j).prependTo(".scrollable");
			var el = $("#thumbnails_" + j);
		}
		else
		{
			$("#thumbnails_" + j).insertAfter(el);
			el = $("#thumbnails_" + j);
		}
		$("#thumbnails_" + j).show();
		j++;
		if (j >= special_offers_count)
			j = 0;
	}
	
	return true;
}	

function check_email(email) {
	var reg_mail = /^[a-z0-9\_\+\.\-]+\@([a-z0-9\-]+\.)+[a-z0-9]{2,4}$/i;
	return email == "" || reg_mail.test(email);
}

function check_phone(phone) {
	var reg_phone = /^([\d-+\(\) ]+)$/i;
	return phone == "" || reg_phone.test(phone);
}	

function check_order(form) {
	
	var from = form.elements['from'].value;
	var email = form.elements['email'].value;
	var phone = form.elements['phone'].value;
	var message = form.elements['message'].value;
	var flag = false;
	
	if (from == '') {
		alert('Введите Ваше имя.');
		form.from.focus();
		return false;
	}
	
	if (email == '') {
		alert('Введите свою электронную почту.');
		return false;
	}
	
	if(phone != "" && !check_phone(phone)) {
		alert('Некорректное значение поля: Телефон.');
		form.phone.focus();
		return false;			
	}	
	
	if(email != "" && !check_email(email)) {
		alert('Некорректное значение поля: Электронная почта.');
		form.email.focus();
		return false;			
	}

	for (var i = 0; i <  document.forms[0].service_id.length; i++) {
		if (document.forms[0].service_id[i].checked) flag = true;
	}
	
	if (!flag) {
		alert('Выберите тип услуги.');
		return false;
	}
	
	flag = false;
	for (var i = 0; i <  document.forms[0].order_type_id.length; i++) {
		if (document.forms[0].order_type_id[i].checked) flag = true;
	}
	if (!flag) {
		alert('Выберите тип недвижимости.');
		return false;
	}		

	if (message == '') {
		alert('Введите Ваше сообщение.');
		form.message.focus();
		return false;
	}

	return true;
}	
