var IndxScrollSteps = 0;
$(document).ready(function() {
	
	//left menu	
	
	$("UL.l-menu > LI > A").hover(function() {
		if( !$(this).parent().is('.active')) {
			$(this).parent().addClass('a');
			$(this).after('<i class="tl"></i><i class="tr"></i><i class="br"></i><i class="bl"></i>');
		}
	}, function() {
		if( !$(this).parent().is('.active')) {
			$(this).parent().removeClass('a');
			$("I", $(this).parent()).remove();
		}
	})
	
	
	$("#scrollhead .arr").click(function() {
		$(".scrollhead-hover2").hide();
		$(this).fadeOut('fast');
		$("#scrollhead").scrollTo("+=1062px", 500, {easing:'swing', axis:'x', onAfter:function() {
			$(".scrollhead-hover2").show();
			$("#scrollhead .arr").fadeIn('fast');
			if( IndxScrollSteps++ == 4 ) {
				IndxScrollSteps = 0;
				$("#scrollhead").scrollTo("0px", {axis:'x'});
			}
		}});
	});
	
	InitGeoMap();
	InitGraph();
	InitPhotoGallery();
	InitEnts();
	InitArchive();
	
	if($(".CaptchaError").is('STRONG')) {
		$(window).scrollTo($(".CaptchaError"));
	}
});


var FixedReg = null;

function InitGeoMap() {

	$("#GeoMap AREA").mouseover(function() {
		var Name =$(this).attr('for_region');
		$("#"+Name).fadeIn('fast');
	}).mouseout(function() {
		var Name =$(this).attr('for_region');
		if( Name != FixedReg ) {
			$("#GeoMap DIV:animated").stop(true, true);
			$("#"+Name).fadeOut('fast');
		}
	}).click(function() {
		var Name =$(this).attr('for_region');
		$(".Ents").hide();
		if(Name=="w1") {
			$("#GeoMap DIV:animated").stop(true, true);
			if(FixedReg) {
				$("#"+FixedReg).fadeOut('fast');
				$("#"+FixedReg+"_tty").hide();
				$(".Yaz").css('filter', 'none');
				
				FixedReg = null;
			}
			$("#GeoMap .ToolTip").hide();
			$("#"+Name).hide();
			$("#world").hide();
			$("#world_map").hide();
			$("#russia").show();
			$("#RegionsHL").show();
			$("#russia_map").show();
			$("#world_small").show();
		} else {
			if(FixedReg!=Name) {
				if(FixedReg) {
					$("#"+FixedReg).fadeOut('fast');
					$("#"+FixedReg+"_tty").hide();
					$(".Yaz").css('filter', 'none');
				}
				FixedReg = Name;
				$("#GeoMap .ToolTip").hide();
				if( $("#"+Name+"_tt").is('DIV') ) {
					$("#"+Name+"_tt").fadeIn().fadeTo('fast', 0.9);
					$("#"+Name+"_tty").show().css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+sHtmlRoot+"_i/geomap/yaz.png', sizingMethod='crop')");
				}
			}
		}
		return false;
	});
	
	
	
	$("#world_small").click(function () {
		$(".Ents").hide();
		if(FixedReg) {
			$("#"+FixedReg).fadeOut('fast');
			$("#"+FixedReg+"_tty").hide();
			$(".Yaz").css('filter', 'none');
			FixedReg = null;
		}
		$("#GeoMap .ToolTip").hide();
		$("#world").show();
		$("#world_map").show();
		$("#RegionsHL").hide();
		$("#russia").hide();
		$("#russia_map").hide();
		$("#world_small").hide();
		return false;
	});
	
	$(".ToolTip A:not(.close)").click(function() {
		var t = $(this).attr('href').substring(1);
		$(".Ents").hide();
		$("#Ents_"+t).fadeIn(function() {
			$("#Ents_"+t).css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+sHtmlRoot+"_i/geomap/transp.png', sizingMethod='scale')");
		});
		return false;
	}); 
	$(".ToolTip A.close").click(function() {
		$(this).parents('.ToolTip').hide().next().hide();
		$(".Yaz").css('filter', 'none');
		$(".Ents").hide();
		$("#"+FixedReg).fadeOut('fast');
		FixedReg = null;
		return false;
	}); 
	
	$(".Ents H2 A").click(function() {
		$(this).parents('.Ents').fadeOut();
		return false;
	});
}


function InitGraph() {
	$("#Graph .Navigation A").click(function() {
		$("#Graph IMG.Graph").attr('src', "./draw.phtml?period="+$("#Graph SELECT[name=Period]").val())
	});
}

function InitPhotoGallery() {
	$(".photogallery A.photo").click(function() {

		var ImgID = $(this).attr('href').substring(1);
		if(ImgID) {
			$.ajax({
				type: "GET",
				url: "./?GetPhoto="+ImgID,
				dataType: "json",
				success: function( Result ) {
					if(Result) {
						ShowPhoto(Result);
						OverlayShow( $(".photoOne") );
					}
				}
			});
		}
		
		return false;
	});
	
	
	$(".photoOne A.Next, .photoOne A.Prev").click(function() {
		var ImgID = $(this).attr('href').substring(1);
		if(ImgID) {
			$.ajax({
				type: "GET",
				url: "./?GetPhoto="+ImgID,
				dataType: "json",
				success: function( Result ) {
					if(Result) {
						ShowPhoto(Result);
					}
				}
			});
		}
		
		return false;
	});
	
	
}

function ShowPhoto( Result ) {
	WinW = Result.Width + 100;
	WinH = Result.Height + 110;
	
	iLeft = ($('html').scrollLeft()?$('html').scrollLeft():$('body').scrollLeft()) + $(window).width()/2-WinW/2;
	iTop = ($('html').scrollTop()?$('html').scrollTop():$('body').scrollTop()) + $(window).height()/2-WinH/2;
	
	
	$(".photoOne TD.photo").html('<img src="'+sHtmlRoot+'_upload/il/'+Result.ImgID+'.'+Result.Ext+'">');
	$(".photoOne TD.text").html(Result.Descr);
	$(".photoOne").css('left', iLeft).css('top', iTop<5?5:iTop)
	
	if( Result.Next ) {
		$(".photoOne A.Next").attr('href', "#"+Result.Next).css('opacity', 1);
	} else {
		$(".photoOne A.Next").removeAttr('href').css('opacity', 0.2);
	}
	if( Result.Prev ) {
		$(".photoOne A.Prev").attr('href', "#"+Result.Prev).css('opacity', 1);
	} else {
		$(".photoOne A.Prev").removeAttr('href').css('opacity', 0.2);
	}
}


function InitEnts() {
	$("#InfoLayer").hide();
	$("A.EntLink").click(function() {
		var Height = $(this).parent().height();

		var AA = $(this).parents('TR:first')
		
		$("#InfoLayer").find('TD').load("./?GetEntInfo="+$(this).attr('href').substring(1), function() {
			$(".calendar TR:hidden").show();
			$("#InfoLayer").insertAfter(AA);
			$("#InfoLayer").find('TD').css('height', Height);
			AA.hide();
			$("#InfoLayer").show();
			$("#InfoLayer A.close").click(function() {
				$(".calendar TR:hidden").show();
				$("#InfoLayer").hide();
			});
		});
		
	});
}

function InitArchive() {
	$(".ArchiveForYear .Year A").click(function() {
		var ArchEl = $(this).parents('.ArchiveForYear');
		var ElToShow;
		
		if( $(this).attr('href')=='#prev' ) {
			 ElToShow = ArchEl.prev();
		} else {
			if( $(this).attr('href')=='#next' ) {
				ElToShow = ArchEl.next();
			} else {
				return true;
			}
		}
		
		ArchEl.hide();
		ElToShow.show();
		
		return false;
		
	});
}

























