
(function($) {

	$.iColMainDelay		= 200;
	$.iColDelay			= 200;
	$.iRowMainDelay		= 75;
	$.iRowDelay			= 75;
	$.aPvByRow			= new Array();
	$.aPvByCol			= new Array();
	$.sMonthToShow		= "";
	$.sMonthWasToShow	= "";
	$.bBigShowing		= false;
	
	$.bSideToMove		= "left";
	
	
	// тангенс угла для быстрого расчета
	$.iMonthTg			= 0;
	$.iMonthesX1		= 0;
	$.iMonthesY1		= 0;
	$.iMonthesX2		= 0;
	$.iMonthesY2		= 0;
	$.oMonthes			= {};
	
	$.InitSite	= function()
		{
			InitPreviews();
			InitBigPicController();
			InitMonthesClick();
			InitBigTitle();
			InitMonthesPositionCheck();

			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, ChangeContentByUrl);

		}

	function InitMonthesClick()
		{
			$("#NavigationByMonthes a").bind("click", function()
				{
					if( !$.bIsInMoving )
					{
						if( $( this ).attr("id") == "NavPreYear" || $( this ).attr("id") == "NavPostYear" )
							{
								var sTargetURL = $( this ).attr("href");
								iTempDelay = MassPreviewMove("out");
								setTimeout(function(){ window.location.href = sTargetURL;}, (iTempDelay + 1500));
							}
						else
							SWFAddress.setValue("/" + this.getAttribute('id').substr(3) + "/");
					}

					return false;
				});
		}
	
	function InitBigPicController()
		{
			$("#BigPicController").css("display","block").hide();
			$("#BigPicController DEL").bind("click", function()
				{
					if( !$.bIsInMoving )
						SWFAddress.up();
//						SWFAddress.setValue("/" + $.sMonthToShow + "/");
					return false;
				});
			$("#BigPicController .BtnLeft").bind("click", function()
				{
					if( !$.bIsInMoving )
						{
							$.bSideToMove	= "left";
							var iRow = SWFAddress.getValue().substr(5,1);
							var iCol = SWFAddress.getValue().substr(7,1);
							var $oTemp = $("#PrePics ul." + sMonth + " li.Pic" + iRow + "" + iCol);
							var iIndex = $("#PrePics ul." + sMonth + " li").index( $oTemp );
							var $oTemp1 = $("#PrePics ul." + sMonth + " li").eq( iIndex - 1 );
								iRow = $oTemp1.attr("class").substr(3,1);
								iCol = $oTemp1.attr("class").substr(4,1);
								SWFAddress.setValue("/" + $.sMonthToShow + "/" + iRow + "-" + iCol + "/");
						}
					return false;
				});
			$("#BigPicController .BtnRight").bind("click", function()
				{
					if( !$.bIsInMoving )
						{
							$.bSideToMove	= "right";
							var iRow = SWFAddress.getValue().substr(5,1);
							var iCol = SWFAddress.getValue().substr(7,1);
							var $oTemp = $("#PrePics ul." + sMonth + " li.Pic" + iRow + "" + iCol);
							var iIndex = $("#PrePics ul." + sMonth + " li").index( $oTemp );
							var $oTemp1 = $("#PrePics ul." + sMonth + " li").eq( iIndex + 1 );
								iRow = $oTemp1.attr("class").substr(3,1);
								iCol = $oTemp1.attr("class").substr(4,1);
								SWFAddress.setValue("/" + $.sMonthToShow + "/" + iRow + "-" + iCol + "/");
						}
					return false;
				});
		}

	function InitBigTitle()
		{
			$("#BigPicTitle").hide();
		}


	function InitPreviews()
		{
			// Сперва заполняем массив, чтобы было понятно кто будет вертикальным фронтом,
			// кто следующей линией, и т.д.
			// И так на каждый месяц
			$('#PrePics ul').each( function(i, v)
				{
					var iTempRow	= -1;
					var iTempCol	= 0;
					var sOldRow		= 0;
					$.aPvByCol[v.className]	= new Array();
					$('li', $( this )).each( function(i1, v1)
						{
							sTempRow	= this.className.substr(3, 1);
							if( sTempRow != sOldRow )
								{
									iTempRow++;
									iTempCol = 0;
									sOldRow	= sTempRow;
								}
							else
								iTempCol++;
							if( !$.aPvByCol[v.className][iTempCol] )
								$.aPvByCol[v.className][iTempCol] = new Array();
							$.aPvByCol[v.className][iTempCol][iTempRow]	= this;
						});
				});

			$("#PrePics li").bind("click", function()
				{
					if( !$.bIsInMoving )
						{
							var sMonth = $("#NavigationByMonthes a.Selected").attr("id").substr(3);
							var iRow = $( this ).attr("class").substr(3,1);
							var iCol = $( this ).attr("class").substr(4,1);
							SWFAddress.setValue("/" + sMonth + "/" + iRow + "-" + iCol + "/");
						}
					return false;
				});

		}

	function ShowMonth( sClass )
		{
			InitPreviewsBeforeIn( sClass );
			if( $.bBigShowing )
				BigPicOut();

			SWFAddress.setTitle( sClass );
			MassPreviewMove("in");

		}

	function MassPreviewMove( sType )
		{
			EnableMovingIndicator();
			if( !sType )
				sType = "in";

			var aMoveTypes	= {"in": PreviewIn, "out": PreviewOut };
			var iMaxDelay	= 0;
			
			var $iTempCol	= $.aPvByCol[$.sMonthToShow].length;


			$.each( $.aPvByCol[$.sMonthToShow], function(i, v)
				{
					$iTemp	= this.length;
					$.each( this, function( i1, v1 )
						{
							if( v1 )
								{
									var iTempDelay = (($.iColMainDelay) + ($.iColDelay * i) + (i1 * $.iRowDelay) + Math.random() * 100);
										if( iTempDelay > iMaxDelay )
											iMaxDelay = iTempDelay;
		
									// из-за ie делаем неизящно.
									// Передаем не объект, а index.
									if( sType == "in" )
										$( v1 ).oneTime( iTempDelay, function(){ PreviewIn( this ); } );
									else
										$( v1 ).oneTime( iTempDelay, function(){ PreviewOut( this ); } );
									$.iRowDelay	= $.iRowDelay - ($.iRowDelay / $iTemp);
								}
						});
						$.iColDelay	= $.iColDelay - ($.iColDelay / $iTempCol);
				});
			$.iColDelay	= $.iColMainDelay;
			if( sType == "out" )
				{
					$.sMonthWasToShow = $.sMonthToShow;
					setTimeout( InitPreviewsAfterOut, iMaxDelay + 1000 );
					return (iMaxDelay - 500);
				}
			else
				{
					setTimeout( DisableMovingIndicator, iMaxDelay );
				}
			return 0;
			
		}

	function PreviewOut( oPv )
		{
			$("#" + oPv.className ).remove();
			$( oPv ).animate({ marginLeft: "-4000px"}, 1500, "easeInOutExpo");
		}

	function PreviewIn( oPv )
		{
//			$oIns	= $("<ins>");
			$( oPv ).animate({ marginLeft: "0px"}, 1500, "easeInOutExpo", function()
				{
					$( this ).oneTime( 10, function(){ AfterPreviewIn( this ); } );
				});
		}

	function AfterPreviewIn( oPv )
		{
			$this = $( oPv );
			$("<ins>").appendTo("#MetaLayout").css(
						{
							"top": $this.offset().top,
							"left": $this.offset().left
						})
					.addClass("PvCopy")
					.attr("id", $this.attr("class"))
					.bind("click", function()
						{
							$("#PrePics .Enabled ." + this.getAttribute("id")).click();
						})
					.bind("mouseover", function()
						{
							$("#PrePics .Enabled ." + this.getAttribute("id")).addClass("Overed");
						})
					.bind("mouseout", function()
						{
							$("#PrePics .Enabled ." + this.getAttribute("id")).removeClass("Overed");
						});
		}

	function InitPreviewsBeforeIn( sClass )
		{
			$oTemp	= $("#PrePics ." + sClass);
			$("li", $oTemp).css("marginLeft", "4000px");
			$oTemp.addClass("Enabled");
			$.sMonthToShow	= sClass;
			// Теперь реакцию на нажитие
		}

	function InitPreviewsAfterOut( )
		{
			$("#PrePics ." + $.sMonthWasToShow).removeClass("Enabled");
			$.bOutComplete	= true;
			
			// по идее тут надо dispatchEvent делать
		}

	function ShowBigPic( sUrl, sText, iIndex )
		{
			if( $("#PrePics .Enabled").length > 0 )
				iTempDelay = MassPreviewMove("out");
			if( iTempDelay < 1 )
				iTempDelay = 1;
			$oTemp		= $("#BigPicContainer1");
			$oTemp.css("marginLeft", "4000px").css("display","block");
			$("img", $oTemp).attr("src", sUrl).data("index", iIndex).attr("alt", sText).fadeTo(0, 0.1);
			setTimeout( BigPicIn, iTempDelay);
		}

	function BigPicIn( )
		{
			$oTemp		= $("#BigPicContainer1");
			$oTemp.animate({ marginLeft: "0px"}, 1500, "easeInOutExpo", DisableMovingIndicator);
			$("img", $oTemp).fadeTo( 1200, 1 );
			$.bBigShowing	= true;
			setTimeout( function(){
					ReInitBigPicController();
					$("#BigPicController").fadeIn();
					$("#BigPicController .BtnLeft").animate({left: "0px"}, 500, "linear");
					$("#BigPicController .BtnRight").animate({left: "0px"}, 500, "linear");
					ShowBigTitle();
				}, 1200);
		}

	function ReInitBigPicController()
		{
			$oImg	= $("#BigPicContainer1 img");
			if( $oImg.data("index") == 0 )
				$("#BigPicController .BtnLeft").addClass("Disabled");
			else
				$("#BigPicController .BtnLeft").removeClass("Disabled");

			if( $oImg.data("index") == ($("#PrePics ." + $.sMonthToShow + " li").length - 1 ))
				$("#BigPicController .BtnRight").addClass("Disabled");
			else
				$("#BigPicController .BtnRight").removeClass("Disabled");
		}

	function BigPicOut()
		{
			$oTemp		= $("#BigPicContainer1");
			$oTemp.animate({ marginLeft: "-4000px"}, 1500, "easeInOutExpo");
			$("img", $oTemp).removeData("index");
			$("#BigPicController").fadeOut();
			HideBigTitle();
			$.bBigShowing	= false;
			$("#BigPicController .BtnLeft").css("left","48px");
			$("#BigPicController .BtnRight").css("left","-49px");
		}

	function NextBigPicInFromLeft()
		{
			EnableMovingIndicator();
			HideBigTitle();
			$oTemp		= $("#BigPicContainer1");
			$oTemp2		= $("#BigPicContainer2");
			$oTemp2.css("marginLeft", "-4000px").css("display","block");

			var iTempIndex	= parseInt($("img", $oTemp).data("index")) - 1;
			var sUrl		= $("a", $("#PrePics ." + $.sMonthToShow + " li").eq( iTempIndex ) ).attr("href");
			$("img", $oTemp2).attr("src", sUrl).data("index", iTempIndex).fadeTo(0, 0.1);

			$oTemp.animate({ marginLeft: "4000px"}, 1500, "easeInOutExpo");
			$("img", $oTemp).fadeTo( 0, 1 );
			NextBigPicIn();
			$.bBigShowing	= true;
		}

	function NextBigPicInFromRight()
		{
			EnableMovingIndicator();
			HideBigTitle();
			$oTemp		= $("#BigPicContainer1");
			$oTemp2		= $("#BigPicContainer2");
			$oTemp2.css("marginLeft", "4000px").css("display","block");

			var iTempIndex	= $("img", $oTemp).data("index") + 1;
			var sUrl		= $("a", $("#PrePics ." + $.sMonthToShow + " li").eq( iTempIndex ) ).attr("href");
			$("img", $oTemp2).attr("src", sUrl).data("index", iTempIndex).fadeTo(0, 0.1);

			$oTemp.animate({ marginLeft: "-4000px"}, 1500, "easeInOutExpo");
			$("img", $oTemp).fadeTo( 0, 1 );
			NextBigPicIn();
			$.bBigShowing	= true;
		}

	function NextBigPicIn()
		{
			$oTemp2		= $("#BigPicContainer2");
			setTimeout( function()
				{
					$("#BigPicContainer1 img").attr("src", $("#BigPicContainer2 img").attr("src"))
											.data("index", $("#BigPicContainer2 img").data("index"));
					ReInitBigPicController();
					ShowBigTitle( $("img", $("#PrePics ." + $.sMonthToShow + " li").eq( $("#BigPicContainer2 img").data("index") ) ).attr("alt")  );
				}, 1000);
			$oTemp2.animate({ marginLeft: "0px"}, 1500, "easeInOutExpo", function()
				{
					$("#BigPicContainer1").css("marginLeft","0px")
											.fadeTo(0,1);
					$( this ).hide();
					DisableMovingIndicator();
				});
			$("img", $oTemp2).fadeTo( 1200, 1 );
		}


	function EnableMovingIndicator()
		{
			$.bIsInMoving	= true;
			$("body").addClass("IsInMoving");
		}

	function DisableMovingIndicator()
		{
			$.bIsInMoving	= false;
			$("body").removeClass("IsInMoving");
		}

	function ShowBigTitle( sText )
		{
			if( !sText )
				sText = $("#BigPicContainer1 img").attr("alt");
			$("#BigPicTitle").text( sText );
			$("#BigPicTitle").fadeIn();
			SWFAddress.setTitle( sText );
		}
	
	function HideBigTitle()
		{
			$("#BigPicTitle").fadeOut("fast", function(){ $("#BigPicTitle").text("") });
		}


	function InitMonthesPositionCheck()
		{
			$.iMonthTg	= 185 / 845;	// соотношение сторон в картинке с месяцами.
			$.oMonthes		= $("#NavigationByMonthes");
			$.iMonthesX1	= $.oMonthes.offset().left;
			$.iMonthesY2	= $.oMonthes.offset().top;

			$.iMonthesX2	= $.iMonthesX1 + 845;
			$.iMonthesY1	= $.iMonthesY2 + 185;
			
			$( document ).mousemove( function( e )
				{
					if(	e.pageX > $.iMonthesX1
							&& e.pageX < $.iMonthesX2
							&& e.pageY > $.iMonthesY2
							&& e.pageY < ($.iMonthesY1 + 15)
					)
						{
							iNewY = $.iMonthesY1 - ((e.pageX - $.iMonthesX1) * $.iMonthTg);
							if( e.pageY > iNewY && e.pageY < (iNewY + 15 ) )
								$.oMonthes.addClass("Higher");
							else
								$.oMonthes.removeClass("Higher");
//							$("#aaa").text( e.pageX + ", " + e.pageY + ", " + iNewY);
						}
				});
		}
////////////////////////////////////////////////////////////////////////////////
// SWFAddress
////////////////////////////////////////////////////////////////////////////////
	function ChangeContentByUrl( e )
		{
			if( e.path == "/" )
				{
					setTimeout( function(){
						var bInited = false;
						$("#NavigationByMonthes a").each( function(i, v )
							{
								if( !bInited &&
									$( this ).attr("id") != "NavPreYear" && 
									$( this ).attr("id") != "NavPostYear"
								)
								$( this ).click();
							});
					}, 100 );
				}
			else if( !$.bIsInMoving && e.path != "/")
				{
					sMonth	= e.path.substr(1,3);
					iRow	= parseInt( e.path.substr(5,1) );
					iCol	= parseInt( e.path.substr(7,1) );

					iTempDelay	= 0;

					if( $("#PrePics .Enabled").length > 0 )
						{
							iTempDelay = MassPreviewMove("out");
							if( iTempDelay < 1 )
								iTempDelay = 1;
						}

					if( !iRow )
						{
				// смена месяца					
							if( iTempDelay )
								$( this ).oneTime( iTempDelay, function(){ ShowMonth( sMonth ) } );
							else
								ShowMonth( sMonth );
						}
					else if( iCol )
						{
							$.sMonthToShow	= sMonth;
							if( $.bBigShowing )
								{
									if( $.bSideToMove == "left" )
										NextBigPicInFromLeft();
									else
										NextBigPicInFromRight();
								}
							else
								{
								var $oTemp = $("#PrePics ul." + sMonth + " li.Pic" + iRow + "" + iCol);
									ShowBigPic( $("a", $oTemp).attr("href"),   $("img", $oTemp).attr("alt"), $("#PrePics ul." + sMonth + " li").index( $oTemp ));
								}
						}
						
					$("#NavigationByMonthes a").removeClass("Selected");
					$("#Nav" + sMonth ).addClass("Selected");
				}
		}


////////////////////////////////////////////////////////////////////////////////
$(document).ready(
	function()
	{
		$.InitSite();
	}
);


})(jQuery);
