$(document).ready(function() {

		nova = {
			init: function() {
				$("*").removeClass("no-js");

				this.navigation.init();
				
				this.logoInit();
				this.gridInit();
				
				this.resizeInit();
				
				this.mapAnimation();
			},

			mapAnimation: function() {
				var mapImg = $('FOOTER .map img');
				
				setTimeout(function() {
					mapImg.animate({
						opacity: "1"
					}, 1000);

					setTimeout(function() {
						mapImg.animate({
							opacity: "0.2"
						}, 2000);
					}, 2000);
				}, 500);
				
				mapImg.hover(
					// Over
					function() {
						$(this).stop(true, false).animate({
							"opacity": 1
						}, 200);
					},

					// Out
					function() {
						$(this).stop(true, false).animate({
							"opacity": "0.2"
						}, 2000);				
					}
				);
			},
			
			getColumnsCount: function() {
				if($(window).width() < 1250) {
					return 4;
				} else {
					return 5;
				}
			},
			
			resizeInit: function() {
				var _self = this;
				
				$(window).resize(function() {
					if(_self.getColumnsCount() == 4) {
						$("#page").css({width: "960px"});
						$("#page").addClass("four-columns");
					} else {
						$("#page").css({width: "1200px"});
						$("#page").removeClass("four-columns");
					}
				});
			},
			
			gridInit: function() {
				$(document).bind("keyup", function(e) {
					if(document.getElementById("grid"))
						return false;
					
					if(e.keyCode == 119) {
						$("body").append(
							"<div id='grid'></div>"
						);
					}						
				});
			},

			logoInit: function() {
				var interval = false;
				var hoverInterval = false;
					
				var logo = $("#logo");
				var interactive = $(".interactive");

				logo.hover(
					// Over
					function() {
						hoverInterval = setTimeout(function() {
							interval = setInterval(function() {
									interactive.animate({
										"margin-top": "-=48px"
									}, "fast",

									// Callback
									function() {
										var pos = interactive.css("margin-top");

										if(pos == "-528px") {
											
											setTimeout(function() {
												interactive.animate({
												   "margin-top": 0
												});
											}, 1000);

											clearInterval(interval);
										}
									});
							}, 500);
						}, 1000);
					},

					// Out
					function() {
						clearTimeout(hoverInterval);
						clearInterval(interval);

						interactive.animate({
							"margin-top": 0
						});
					}
				);
			},

			navigation: {
				pagePath: "/",
				pageSection: "/",
				
				nav: $("NAV"),
				navUl: $("NAV UL"),
				navLi: $("NAV LI"),
				navOpener: $("#nav-opener"),
				
				// Количество минус один текущий скрытый раздел
				size: $("NAV LI").size()-1,
				
				// Для более чистой анимации, при переходе между страницами
				allowNavHoverEvent: true,
				
				canShowContent: false,
				
				initNav: function() {		
					this.navLi.removeAttr("class");
				
					var current = "/" + this.pageSection;
					var _self = this;
					
					this.navLi.each(function(index, el) {
						if($(el).find("a").attr("href") == current) {
							_self.navOpener.text($(el).text());
							$(el).addClass("current");
						}
					});
					
					//replace( /^\s+/g, '') == trim workaround
					if(_self.navOpener.text().replace( /^\s+/g, '') == "") {
						var current = _self.navLi.first();
						
						current.addClass("current");
						_self.navOpener.text(current.text().replace( /^\s+/g, ''));
					}
					
					_self.nav.find("LI").not(".current").first().addClass("first");
					_self.nav.find("LI").not(".current").last().addClass("last");
					
					return _self.nav.find(".current");
				},
				
				parseUrl: function() {
					// Hooray for HTML5 support!
					if(Histry.supportsHistoryPushState())
						this.pagePath = window.location.pathname;
					else
						// Oh, nooooo
						this.pagePath = window.location.hash;
					
					if(this.pagePath != "") {
						this.pageSection = this.pagePath.split("/");
						this.pageSection = (this.pageSection[0] == "") ? this.pageSection [1] : this.pageSection[0];
					}
				},
				
				setUrl: function(url) {
					Histry.navigateToPath(url);
					this.parseUrl();
				},
				
				displayContent: function(content) {
					var _self = this;
					var section = section;

					// some voodoo magic happens here
					// NEED TO TEST!
					$("#content").html(content);

					var loadedImages = 0;
					var totalImages = $("#content img").size();
					
					$("#content img").each(function(ind, el) {
						$(el).bind("load", function() {
							loadedImages++;
						});
					});

					var loadingInterval = setInterval(function() {
						if(loadedImages == totalImages) {
							clearInterval(loadingInterval);
						
							$("#loader img").animate({
								opacity: 0
							}, function() {
								$("#loader").animate({
									height: 0
								}, function() {
									$("#content").animate({
										height: $("#content")[0].scrollHeight + "px",
										opacity: 1
									}, function() {
										pages.init(_self.pageSection);
									});
								});
							});
						}
					}, 100);
				},
				
				navigate: function(url) {
					if(url == this.pagePath)
						return false;
					
					var _self = this;
					
					this.setUrl(url);
					
					this.allowNavHoverEvent = false;
					
					this.navSlide("up", function() {
						_self.allowNavHoverEvent = true;
					
						_self.initNav();
					
						/*
						if(currentLi) {
							_self.navLi.removeAttr("class");
							$(currentLi).addClass("current");
							var currentPageTitle = $(currentLi).text();

							_self.navOpener.fadeOut(function() {
								_self.navOpener.text(currentPageTitle);
								_self.navOpener.fadeIn();						
							});
						}
						*/
						
						$("#content").animate({
							opacity: "0",
							height: "0"
						}, function() {
							$("#content").html("");
							_self.canShowContent = true;
						});

						$("#loader").animate({
							height: "480px",
							opacity: "1"
						}, function() {
							_self.canShowContent = true;
							$(this).find("img").animate({
								opacity: 1
							}, 1000);
							
							// Make request
							$.ajax({
								url: url,
								cache: false,
								success: function(data) {
									_self.displayContent(data);
									return true;
								},
								error: function() {

								},
								dataType: "html"
							});
						});
					});
				},
				
				navSlide: function(direction, callback) {
					var _self = this;
					
					if(!callback)
						callback = function() {};

					var animation = false;
					if(direction == "up")
						animation = {height: 0};
					else
						animation = {height: "48" * _self.size + "px"};
						
					this.navUl.clearQueue().animate(animation, callback);
				
					this.navLi.removeClass("fade1");
					this.navLi.removeClass("fade2");
					this.navLi.removeClass("fade3");
				},
				
				init: function() {
					// Is it correct?!
					// Redirect from site.tld/section#/otherSection to site.tld/otherSection
					var hash = window.location.hash;
					if(hash.length > 0)
						hash = hash.substr(1, hash.length);

					if(hash != "" && window.location.pathname != "/") {
						window.location = hash;
					}

					var _self = this;
					
					this.parseUrl();
					this.initNav();
					
					var nav = this.nav;
					var navLi = this.navLi;

					var size = this.size;
					
					pages.init(this.pageSection);

					setInterval(function() {
						if(Histry.supportsHistoryPushState())
							var url = window.location.pathname;
						else
							var url = window.location.hash;

						
						_self.navigate(url);
					}, 300);
					
					$(".navigate").live("click", function(e) {
						e.preventDefault();
						
						var url = $(this).attr("href") ? $(this).attr("href") : $(this).data("href");
						
						_self.navigate(url);
					});
					
					nav.hover(
						// Over
						function(e) {
							if(_self.allowNavHoverEvent)
								_self.navSlide("down");
						},

						// Out
						function(e) {
							if(_self.allowNavHoverEvent)
								_self.navSlide("up");
						}
					);

					navLi.hover(function() {
						navLi.removeClass("fade1");
						navLi.removeClass("fade2");
						navLi.removeClass("fade3");

						var index = $(this).index();

						for(var i = 1; i < 4; i++) {
							if(index-i >= 0)
								nav.find("li").eq(index-i).addClass("fade" + i);

							if(index+i <= size)
								nav.find("li").eq(index+i).addClass("fade" + i);
						}
					});

					navLi.click(function(e) {
						if($(this).children('A').hasClass('external')){
							return true;
						}
						e.preventDefault();
						var url = $(this).find('A').attr("href");
						_self.navigate(url);
						
					});
					
					$("#logo").click(function() {
						_self.navigate("/");
					});
				}
			}
		}
		
		nova.init();
		$(window).resize();
});

