var animationSpeed = 200;

$(document).ready(function() {
	
	$("#sidebar .login .registerLink a").click(function() {
		if ($(this).parent().hasClass("back")) {
			$("#sidebar .login .register").slideUp(animationSpeed);
			$(this).parent().removeClass("back");
		}
		else {
			$("#sidebar .login .register").slideDown(animationSpeed);
			$(this).parent().addClass("back");
		}
		return false;
	})
	
	searchPanel();
	
	$("a.printVersion").click(function(){
		makePrintVersion();
		
		return false;
	});
	
	resumeForm();
	
	$("#g_map_link").show();
});

function searchPanel()
{
	$("#searchPanel .panel .block").css("display", "none");
	$("#searchPanel .panel .block").eq($("#searchPanel .tabs li").index($("#searchPanel .tabs li.active")[0])).css("display", "block");
	
	$("#searchPanel .tabs li a").click(function() {
		if (!$(this).hasClass("active")) 
		{
			var tabIndex = $("#searchPanel .tabs li a").index(this);
			$("#searchPanel .panel .block").css("display", "none");
			$("#searchPanel .panel .block").eq(tabIndex).css("display", "block");
			$("#searchPanel .tabs li").removeClass("active");
			$(this).parent().addClass("active");
		}
		return false;
	});

	$("#searchPanel .block .sample a").click(function(){
		$("#searchPanel .block:visible .input input").attr("value", $(this).html().replace('\"', ""));
		return false;
	});
	
	$("#searchPanel .readMore a").click(function(){
		if ($(this).parent().hasClass("active"))
		{
			$("#searchPanel .block:visible .more").slideUp(animationSpeed);
			$(this).parent().removeClass("active");
		}
		else 
		{
			$("#searchPanel .block:visible .more").slideDown(animationSpeed);
			$(this).parent().addClass("active");
		}
		return false;
	});
	
	$("#searchPanel .region .current").click(function() {
		if ($("#searchPanel .region .popup").css("display") == "block") {
			$(this).parent().removeClass("clicked");
			$("#searchPanel .region .popup").hide();
		}
		else {
			$(this).parent().addClass("clicked");
			$("#searchPanel .region .popup").show();
		}
		return false;
	});
	$("#searchPanel .region .popup").click(function() {
		$(this).css("display", "none");
		$("#searchPanel .region .selector").removeClass("clicked");
	});
	$("#searchPanel .region .popup a").click(function() {
		$("#searchPanel .region .popup").css("display", "none");
		$("#searchPanel .region .selector").removeClass("clicked");
		$("#searchPanel .region .current").html($(this).html());
		window.location = $("#searchPanel .region .current").attr('href');
	});
}

function makePrintVersion()
{
	$("#wrapper").hide();
	
	var content = $("#content").clone();
	var header = $("#header").clone();
	
	header.attr("class", "print")
	header.appendTo("body");
	content.attr("class", "print");
	content.appendTo("body");
	
	content.contents().find("a.printVersion span").html("Вернуться к нормальной версии");
	content.contents().find("a.printVersion").click(function(){
		backToNormalVersion(content, header);
	});
}
function backToNormalVersion(content, header)
{
	content.remove();
	header.remove();
	
	$("#wrapper").show();
}

function resumeForm()
{
	if ($("#resume_form").size() > 0) {
		
		
		$("#resume_form .value.delete a").click(function() {
			return resumeFormDeleteClick(this);
		});
		
		var workForm = $($("#resume_form .workPlace")[0]).clone(true);
		var eduForm = $($("#resume_form .eduPlace")[0]).clone(true);
		var langForm = $($("#resume_form .value.lang")[0]).clone(true);
		
		$("#resume_form .addWorkPlace a").click(function(){
			wf = workForm.clone(true);
			$(this).parent().before(wf);

			return false;
		});
		
		$("#resume_form .addEduPlace a").click(function(){
			ef = eduForm.clone(true);
			$(this).parent().before(ef);

			return false;
		});
		
		$("#resume_form .addLanguage a").click(function() {
			lf = langForm.clone(true);
			$(this).parent().before(lf);
			
			return false;
		});
	}
}

function resumeFormDeleteClick(sender)
{
	parent = $(sender).parent();
			
	if (parent.hasClass("lang")) {
		parent.remove();
	}
	else if (parent.parent().hasClass("eduPlace") || 
		parent.parent().hasClass("workPlace")) {
		
		parent.parent().remove();		
	}
	
	return false;
}

function gmap_show()
{
	window.map = new s_gmap({
		id: 'gmap',
		info_id: 'g_info',
		on_found: 'window.map.s_set_markers([{x:lat, y:lng, id:"g_marker"}]);'
	});
	window.map.s_find($("#g_marker").html());
}

function gmap_toggle()
{
	var toggle = $("#g_map_frame").css("display");
	if (toggle=="none")
	{
		$("#g_map_frame").css("display","block");
		gmap_show();
		$("#g_map_link").html("Скрыть карту");
	}
	else
	{
		$("#g_map_frame").css("display","none");
		window.map.s_hide();
		$("#g_map_link").html("Показать на карте");
	}
	return false;
}
