$(function() {
	// all other banners
	$('.adbanner').each(function() {
		var place = this.getAttribute('pp:adplace');
		var region = this.getAttribute('pp:reg');
		var placeholder = this;

		$.getJSON('/show/?p=' + place + (region ? ('&fl=3&fv=' + region) : ''), function(banner) {
			switch(banner.format) {
				case 'flash':
					var swf = new SWFObject(banner.flash + '?targeturl=' + banner.link, 'adbanner_' + banner.id, banner.width, banner.height, 9, '#000000');
					swf.addParam('wmode', 'transparent');
					swf.addParam('menu', 'false');
					swf.addVariable('targerutl', banner.link);
					swf.write(placeholder.id);
					break;

				default:
				case 'image':
					placeholder.innerHTML = '<a href="' + banner.link + '"><img src="' + banner.image + '" class="png" alt="" /></a>';
					break;
			}
		})
	});

	// flash banner
	$('#rootflash').each(function() {
		var swf = new SWFObject('/swf/promo.swf', this.id, 887, 332, 9, '#000000');
		swf.addParam('wmode', 'transparent');
		swf.addParam('menu', 'false');
		swf.addVariable('xmlpath', ['/show/?p=', this.getAttribute('pp:adplace'), '%26fl=3%26fv=', this.getAttribute('pp:reg')].join('') );
		swf.write(this.id);
	});

})

$(function() {
	$('.pseudoselect').each(function() {
		$(this).click(function() {
			$(this).children('.pseudooption').toggle();
		})

		$('ul li a', this).click(function() {
			var thisValue = $(this).html();
			$(this).closest('.pseudoselect')
				.children('span').html(thisValue).end()
				.children('.pseudooption').hide();

			return false;
		})
	}).hover(
		function () {},
		function() {
			$(this).children('.pseudooption').hide();
		}
	)
})

$(function() {
	$("table#tariffs td .tariff-button").hover(function(){
		$(this).addClass("tariff-button-hovered");
		$(this).parents("td").eq(0).addClass("over-all");

	}, function(){
		$(this).removeClass("tariff-button-hovered");
		$(this).parents("td").eq(0).removeClass("over-all");
	});
})

$(function() {
	$("#content ul.newslist li.news .title a").click(function(e) {
		e.preventDefault();
		$(this).closest("li").addClass("full");
	})
	$("#content ul.newslist a.close").click(function(e) {
		e.preventDefault();
		$(this).closest("li").removeClass("full");
	})
})

$(function(){
	$("input.focusblurinput").each(function(){
		var $t = $(this);
		if($t.val() == "")
			$t.val($t.attr("placeholder"));

		$t.addClass("inactive");

	}).focus(function(){
		var $t = $(this);
		if($t.val() == $t.attr("placeholder"))
			$t.val("");
		$t.removeClass("inactive");

	}).blur(function(){
		var $t = $(this);
		$t.addClass("inactive");

		if($t.val() == "")
			$t.val($t.attr("placeholder"));
	});
});

$(function(){
	$("body.faq, body.decisions, body.optionslist, body.rootshort").each(function(){
		$("dl.faq dt").each(function(){
			$("a", this).click(function(e){
				e.preventDefault();
			});
		}).click(function(e){
			$d = $(this).next("dd").andSelf();
			if(!$d.hasClass("shown")) {
				$d.addClass("shown");
			} else {
				$d.removeClass("shown");
			}
		});
		$("dl.faq dd").click(function(e){
			$(this).prev("dt").trigger("click");
		});
	});

	$("body.faq").each(function(){
		$("a.ask").click(function(e){
			e.preventDefault();
			$(this).next("form").addClass("shown");
						//$(this).parent().find("form").addClass("shown");

			$(this).hide();
		});
	});
});

$(function(){
	$("select#region").change(function(){
		$.getJSON('/regions.action',
			{
				action: 'region',
				json: true,
				region: $(this).val()
			},
			function(json){
				opt = new Array('<option>---</option>');
				$.each(json, function(i){
					opt[i] = ['<option value="', json[i].id, '">', json[i].title, '</option>'].join('');
				});
				$("#city").html(opt.join(''));
		});
	});
});

$(function(){
	// opinion dt
	$("div.opinion a.readfull").click(function(e){
		e.preventDefault();
		$(this).siblings("div.anonce").andSelf().css("display", "none").end()
			.siblings("div.text").css("display", "block");
		$("body").addClass("fixIE6").removeClass("fixIE6");
	});
});


function createMarker(map, i) {
	if(!contacts[i]['coords']) {
		return;
	}
	var contact = contacts[i];
	contact['coords'] = contact['coords'].split(",");

	if(contact['coords'].length < 2) {
		return;
	}

	var placemark = new YMaps.Placemark(new YMaps.GeoPoint(contact['coords'][1], contact['coords'][0]));
	placemark.description = ["<b>", contact['title'],"</b><br />", contact['address']].join(""); 
	map.addOverlay(placemark); 
	YMaps.Events.observe(placemark, placemark.Events.MouseClick, function (obj) {
		placemark.openBalloon();
	});
}

$(function(){
	$("body.contacts").each(function() {
		if(center.length == 0) {
			$("#map_canvas").parent().hide();
		} else {
			var map = new YMaps.Map(document.getElementById("map_canvas"));
			map.setCenter(new YMaps.GeoPoint(center[1], center[0]), 15);
			var zoom = new YMaps.Zoom();
			map.addControl(zoom);
			map.setZoom(parseInt(center[2]) || 15, {});

			if(contacts != null) {
				for(var i in contacts) {
					createMarker(map, i);
				}
			}
		}

		$("#content a.ask").click(function() {
			$(this).hide();
			$(this).next("form").show();
			return false;
		});
	});

	$("body.vacancies").each(function(){
		$("a.send-resume").click(function(e){
			e.preventDefault();
			$(this)
				.parent("div.send-resume").hide()
				.nextAll("form").eq(0).removeClass("vacancy-form-hidden");
		});
	});

	$("body.error a.back").click(function(e){
		e.preventDefault();
		history.back();
	});

	$("div.phonetariff").each(function(){
		$(this).find(".i td.last-row a").click(function(e){
			e.preventDefault();
			var $zone = $(this).closest("table").siblings("div.zones")
			if(!$zone.hasClass("zones-shown")) {
				$zone.addClass("zones-shown");
			} else {
				$zone.removeClass("zones-shown");
			}
		});
	});
});

$(function(){
	$("body.zone").each(function(){
		$("#mosobl").click(function(e){
			e.preventDefault();
			$("div.map-russia").hide();
			$("div.map-mosobl").show();
		});
		$("#russia").click(function(e){
			e.preventDefault();
			$("div.map-mosobl").hide();
			$("div.map-russia").show();
		});
	});
});

