/**
 * Common
 */


$(function() {

	$("#preview a").click(function() {
		if (!$("#photo_container").hasClass("loading")) {
			var sSource = $(this).attr("href");
			$("#photo_container").addClass("loading");
			$("#photo_description").html($(this).next().text());
			$("#photo_container").height($("#photo_container #photo").height());
			$("#photo_container #photo").fadeOut("normal").remove();
			var lImage = new Image();
			$(lImage).load(function() {
				//$("#photo_container").height(lImage.height);
				$("#photo_container").animate({height: lImage.height}, "normal" );
				$(this).hide();
				$("#photo_container").append(this).removeClass("loading");
				$(this).fadeIn("slow");
			});
			$(lImage).attr("src", sSource);
			$(lImage).attr("id", "photo");
			$("#preview a img").removeClass("selected");
			$(this).children("img").addClass("selected");
		}
		return false;
	});


	$(".toggle").click(function() {
		$(this).next().toggle();
	});


	function ShowOrganisations(sId) {
		$("#map_show").load(function() {
			$(this).hide();
			$(this).show();
		});
		$("#map_show").attr("src", "/off-line/map/" + sId + ".gif");
		$(".country li span").removeClass("selected").addClass("clickable").addClass("pseudo_link").addClass("disable");
		$("#" + sId).removeClass("clickable").removeClass("pseudo_link").addClass("selected");
		$(".organisations li a").addClass("disable");
		$("." + sId).removeClass("disable");
		ShowInformer(sId);
	};

	function HideOrganisations() {
		$("#map_show").hide();
		$(".country li span").removeClass("selected").removeClass("disable").addClass("clickable").addClass("pseudo_link");
		$(".organisations li a").removeClass("disable");
		$("#informer").fadeOut("fast");
	};

	function ShowInformer(sId) {
		$("#informer div").html("");
		$("<img src='" + flag[sId] + "' alt='" + $("#" + sId).attr("title") + "' />").appendTo("#informer div");
		$("<h3>" + $("#" + sId).attr("title") + "</h3>").appendTo("#informer div");
		$("<div class='clear'></div>").appendTo("#informer div");
		$("<ul></ul>").appendTo("#informer div");
		$(".organisations li a:not(.disable)").each(function() {
			$("<li><a href=\"" + $(this).attr("href") + "\">" + $(this).html() + "</a></li>").appendTo("#informer div ul");
		});
		$("#informer").fadeIn("fast");
	};


	function ShowCountry() {
		$(".cselector #country").addClass("selected");
		$(".tselector .country").fadeIn("fast");
		isCountryState = true;
	};

	function HideCountry() {
		$(".cselector #country").removeClass("selected");
		$(".tselector .country").fadeOut("fast");
		isCountryState = false;
	};


	$(".cselector #country").click(function() {
		if(isCountryState) {
			HideCountry();
		} else {
			ShowCountry();
		};
	});

	$(".cselector #organisations").toggle(
		function() {
			$(".tselector #world_map").hide();
			$(".tselector .organisations").show();
			$(this).html("Карта мира");
		},
		function() {
			$(".tselector .organisations").hide();
			$(".tselector #world_map").show();
			$(this).html("Организации соотечественников");
		}
	);


	$(".country li span").click(function() {
		ShowOrganisations($(this).attr("id"));
		HideCountry();
		return false;
	});

	$("#informer .close").click(function() {
		HideOrganisations();
	});

	$(".country .close").click(function() {
		HideCountry();
	});


	$(".tselector .country").hide();
	$(".tselector .organisations").hide();
	var isCountryState = false;


	$("area").hover(
		function () {
			//alert("/off-line/map/" + $(this).attr("id") + ".gif");
			$("#map_over").load(function() {
				$(this).hide();
				$(this).show();
			});
			$("#map_over").attr("src", "/off-line/map/" + $(this).attr("class") + ".gif");
			//event.preventDefault();
			//event.stopPropagation();
		},
		function (event) {
			$("#world_map #map_over").hide();
			//alert($(this).attr("title"));
			event.preventDefault();
			event.stopPropagation();
		}
	).click(
		function () {
			ShowOrganisations($(this).attr("class"));
			return false;
		}
	);


});

jQuery.preloadImages = function() {
	jQuery.each (arguments,function (e) {
		jQuery("<img>").attr("src", this);
	});
}
