function getLocalityContacts (id) {
	var result = ""; 
	$("#locality-choice INS.pseudo-link SPAN").html(localities[id][0]);
	if (localities[id][2] != "") result += "<p class=\"phone\">" + localities[id][2] + "</p>";
	if (id == 28) result += "<p class=\"address\">Главный офис</p>";
	if (localities[id][3] != "") result += "<p class=\"address\">" + localities[id][3] + "</p>";
	var links = "";
	if (localities[id][1] != 0) {
		links += "<li><a href=\"/addresses/" + localities[id][1] + "/\">Офисы</a></li>";
		links += "<li><a href=\"/atm/" + localities[id][1] + "/\">Банкоматы</a></li>";
	}
	if (links != "") result += "<ul>" + links + "</ul>";
	return result;
}

function closeLayer (id) {
	$("#"+id).fadeOut("fast", function(){});
}

$(document).ready(function() {
	if ($("A.colorbox").length) $("A.colorbox").colorbox();
	$('FORM.validate').each(function () {
		this.validator = new FormValidation($(this));
		this.validator.init();
 	});
	$('FORM.ajax-form').each(function () {
		$(this).ajaxForm({ target: $(this) });
	});
	if (isBorderRadiusSupported()) {
		$("BODY").removeClass("no-border-radius");
		$("BODY").addClass("border-radius");
	}
	$(".rates").each( function () { $(this).click( function() {
		$(".rates").removeClass("rates-open");
		$(this).addClass("rates-open");
	} ); } );
	if ($("DIV.vacancy").length > 1) {
		$("DIV.vacancy INS.pseudo-link").hover(
			function () {
				$(this).parents(".vacancy").addClass("vacancy-hover");
			},
			function () {
				if ($(this).parents(".vacancy").find(".description").css("display") == "none") $(this).parents(".vacancy").removeClass("vacancy-hover");
			}
		);
	}
	//Псевдоссылки
	if ($("DIV.description").length == 1)  {
		$("#text INS.pseudo-link").removeClass("pseudo-link");
	}
	$(".pseudo-link").click( function() {
		var obj = $("#" + $(this).attr("oid"));
		if (obj.length) {
			var dub = $("#a" + $(this).attr("oid").substr(1));
			if (obj.css("display") == "none") {
				//obj.css({display:"block"});
				if (dub.length) dub.css({display:"none"});
				$(this).removeClass("pseudo-link");
				$(this).addClass("pseudo-link-hover pseudo-link-open");
				obj.slideDown("fast");
			}
			else {
				obj.css({display:"none"});
				if (dub.length) dub.css({display:"block"});
				$(this).addClass("pseudo-link");
				$(this).removeClass("pseudo-link-hover pseudo-link-open");
			}
		}
		if ($(this).is("A")) return false;
	} );
	$(".pseudo-link").hover(
		function () {
			$(this).removeClass("pseudo-link");
			$(this).addClass("pseudo-link-hover");
		},
		function () {
			var obj = $("#" + $(this).attr("oid"));
			if (!obj.length || $(this).attr("oid") == undefined || obj.css("display") == "none") {
				$(this).removeClass("pseudo-link-hover");
				$(this).addClass("pseudo-link");
			}
		}
	);
	//Раскрывающиеся блоки
	if ($("DIV.description").length > 1) {
		$("DIV.description").css({display:"none"});
		var first = $($("DIV.description")[0]);
		if (first.parents(".qna").length) {
			first.css({display:"block"});
			var link_to_first = $("INS.pseudo-link[oid=" + first.attr("id") + "]");
			link_to_first.removeClass("pseudo-link").addClass("pseudo-link-hover");
		}
	}
	if ($("TABLE.tablesorter").length) $("TABLE.tablesorter").tablesorter({sortList: [[0,0]]});
	if ($("#locality-choice").length) {
		$("#localities .layer-close").click( function () { closeLayer("localities") });
		$("#locality-choice").click( function () {
			this.blur();
			if ($("#localities").css("display") == "none") {
				$("#localities").fadeIn("fast", function(){});
			}
		});
		$("#localities INS.pseudo-link").click( function () {
			closeLayer("localities");
			var id = $(this).attr("id").substr(1);
			var expire = new Date();
			expire = new Date (expire.getYear() + 1901, expire.getMonth(), expire.getDate(), expire.getHours(), expire.getMinutes());
			setCookie("loc", id, expire, "/");
			$("#locality-info").html(getLocalityContacts(id));
		});
		var loc = getCookie("loc");
		if (loc) $("#locality-info").html(getLocalityContacts(loc));
	}
	if ($("#calcs-choice").length) {
		$("#calcs .layer-close").click( function () { closeLayer("calcs") });
		$("#calcs-choice").click( function () {
			this.blur();
			if ($("#calcs").css("display") == "none") {
				$("#calcs").fadeIn("fast", function(){});
			}
		});
	}
	/*
	if ($("DIV.person").length > 1) {
		$("DIV.person").hover(
			function () {
				$(this).addClass("person-open");
			},
			function () {
				var obj = $("#d" + $(this).find("INS.pseudo-link").attr("oid"));
				if (obj.css("display") == "none") {
					$(this).removeClass("person-open");
				}
			}
		);
	}
	*/
	setUpSearchTextField();

});

//Установить Cookie
function setCookie (name, value, expire, path) {
	document.cookie = name + '=' + escape (value) + ((expire == null)? '' : ('; expires=' + expire.toGMTString())) + ((path == null)? '' : ('; path=' + path));
	//alert(name + '=' + escape (value) + ((expire == null)? '' : ('; expires=' + expire.toGMTString())) + ((path == null)? '' : ('; path=' + path)));
}

//Прочитать Cookie
function getCookie (name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) end = cookie.length;
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

//Инициализация формы поиска
function setUpSearchTextField () {
	if ($("#search-text").length) {
		var placeHolder = $("#search-text").attr('placeholder');
		if ($("#search-text").val() == "" && placeHolder) {
			$("#search-text").val(placeHolder);
			$("#search-text").focus( function() { if ($(this).val() == $(this).attr('placeholder')) $(this).val(""); } );
			$("#search-text").blur( function() { if ($(this).val() == "") $(this).val($(this).attr('placeholder')) } );
		}
	}
}

//Проверка поддержки браузером свойства borderRadius
function isBorderRadiusSupported () {
	var s = document.documentElement.style;
	return typeof s.borderRadius === "string" || typeof s.WebkitBorderRadius === "string" || typeof s.KhtmlBorderRadius === "string" || typeof s.MozBorderRadius === "string";
}


function winOpen(href, target, width, height, resizable, toolbar) {
	if (resizable != "yes" && resizable != "no") resizable = "yes";
	(resizable == "no") ? scrollbars = "no" : scrollbars = "yes";
	(toolbar == "yes") ? toolbar = "yes" : toolbar = "no";
	window.open(href, target, 'width=' + width + ', height=' + height + ', location=no, toolbar=' + toolbar + ', menubar=no, status=no, scrollbars=' + scrollbars + ', resizable=' + resizable + ', ');
}

//Открытие изображения в отдельном окне
function picOpen (pic, width, height) {
	var x = (screen.availWidth - width ) / 2;
	var y = (screen.availHeight - height ) / 2;
	var pWidth = parseInt(width) + 40;
	var pHeight = parseInt(height) + 40;
	var sFeatures = "width=" + pWidth + ", height=" + pHeight + ", toolbar=no, status=no, scrollbars=0, resizable=no, left=" + x + ", top=" + y;
	var page=window.open('', '', sFeatures);
	page.document.write('<html><head><title>Просмотр изображения</title>');
	page.document.write('<style type="text/css">');
	page.document.write('* { margin: 0; padding: 0; }');
	page.document.write('BODY { color: #000; padding: 20px 0; background: #FFF; text-align: center; }');
	page.document.write('IMG { border: none; }');
	page.document.write('</style>');
	page.document.write('</head><body>');
	page.document.write('<a href="javascript:self.close()" title="закрыть окно"><img width="'+width+'" height="'+height+'" src="'+pic+'" /></a></p>');
	page.document.write('</body></html>');
	page.document.close();
	if (window.focus) page.focus();
}
