$(document).ready(function(){

// Preload images (for menu)
(function($) {
	  var cache = [];
	  // Arguments are image paths relative to the current page.
	  $.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
		  var cacheImage = document.createElement('img');
		  cacheImage.src = arguments[i];
		  cache.push(cacheImage);
		}
	  }
	})(jQuery)
	jQuery.preLoadImages("/img/menu-0.png","/img/menu-1.png","/img/menu-2.png","/img/menu-3.png");


// Active menu
	var ww = $(window).width();
	$("#front-menu").append('<div id="menusl"></div>');
	$("#menusl").hide();

	$("#front-menu a").each(function () {
			$(this).attr("href","#"+$(this).attr("href"));
		});
	
	if (window.location.hash != "") {
		$("#front-menu a.active").removeClass("active");
		$("#front-menu a[href="+window.location.hash+"]").addClass("active");
		$("#front-content").load($("#front-menu a.active").attr("href").replace('#','')+" #scrollup", function(){fix_title();});
	}



	
	var aa = $("#front-menu a.active");
	
	var bobp = 0;
	
	var xnow = $(aa).position().left + $(aa).width()/2 - 323/2;
	var hcont = $("#scrollup").height() ;
	var na = $(aa).attr("rel");
	var flagdd =0;
	
	var dxs = 0.6;
	
 	$("#menusl").css({"margin-left": xnow+"px","background-image":"url('img/menu-"+na+".png')"}).show();
	
	
	
// News Boble
	//$("body").append('<div id="nboble"><div class="newsbl"><h2 class="title"><a href="/news.php">Новости</a></h2></div></div>');
	var slbpos,xnews,ynews;
	
	function nboble1() {
		slbpos = $("#menusl").offset();
		xnews = slbpos.left;
		if ( xnews > (ww/2-120) ) {xnews-=180;} else {xnews+=280;}
		if ( xnews < 310 ) xnews = 310;
		ynews = slbpos.top-100;
		$("#nboble").animate({
					top: ynews+"px",
					left: xnews+"px"
				},100);	
	}
	

	function nboble_go() {
		xnews = xnow;
		if ( xnews > (ww/2-120) ) {xnews-=180;} else {xnews+=280;}
		if ( xnews < 310 ) xnews = 310;
		$("#nboble").animate({top: "90px", left: xnews+"px"},
					{
						duration: 1000,
						step: function( now, fx ){
										bobp = now;
									}
					}
				);

		}
	function nboble_bulk() {
		$("#nboble").animate({
					top: "-=40px"
				 },{queue:false, duration: 200, complete: function () {
														$("#nboble").animate({
															top: "+=40px"
														 },{queue:false, duration: 400});		
												}});
				 
	}
	
	var ttarr = new Array( "Интер- нет", "Телефо- ния", "Локаль- ные сети", "Обслужива- ние");
	
	function fix_title() {
		$titlew = $("h1.title").width();
		if ( na >1 ) 
			{
				if ( $titlew > xnow-10) {$("h1.title").text(ttarr[na]).css({width:"380px","font-size":"90px","margin": "20px 0"}); };
			}
		else{
				if ( $titlew > ww-(xnow+323) ) {$("h1.title").text(ttarr[na]).css({width:"380px","font-size":"90px","margin": "20px 0"}); };
			}
		
		
	}
	

	
	nboble1();
	
	$(window).hashchange( function(){
		var hash = window.location.hash;
		var href2 = window.location.pathname;
		if (hash == "") {hash ="#"+href2;}
						$(aa).removeClass("active");
						//aa = this;
						aa = "#front-menu a[href="+hash+"]";
						xnow = $(aa).position().left + $(aa).width()/2 - 323/2;
						$(aa).addClass("active");
						na = $(aa).attr("rel");
						flagdd = 1;
						//window.location.hash = $(aa).attr("href");
						
						nboble_go();
						$("#menusl").animate({
								"margin-left": ww+"px"
								},{
									duration: ww*dxs,
									//'linear',
									step: function( now, fx ){
									 if (now > bobp-300 && now < bobp-250) nboble_bulk();
									},
								    complete: function () {
													$(this).css({"margin-left":"-323px","background-image":"url('img/menu-"+na+".png')"});
													$(this).animate({"margin-left": xnow+"px"},{
																								duration:(xnow+623)*dxs,
																								//'linear',
																								step: function( now, fx ){
																								 if (now > bobp-250 && now < bobp-150) nboble_bulk();
																								}
																							   });
												}
								  }
								);
								
						hcont = $("#scrollup").height();
						$("#scrollup").animate({"margin-top": "-"+hcont+"px"},800, 'linear', function () {
																										$("#scrollup").css("margin-top:-10000px");
																										$("#front-content").load($(aa).attr("href").replace(/^#/, '' )+" #scrollup", function () {
																														hcont = $("#scrollup").height();
																														fix_title();
																														$("#scrollup").css({"margin-top":"-"+hcont+"px"});
																														$("#scrollup").animate({"margin-top": "0px"},800);
																											});
																									});


						//return false;

	});
	
	
	$(".front-logo a").click(function (){window.location.hash = "#/";return false;});
	//$('#nboble').click(function () {nboble_bulk();});
});
