/* scripts */


$(function(){

	loading(1);
	var bodyWidth = $('body').width();
	var bodyHeight = $('body').height();
	var windowHeight = $(window).height();
	var container = $('#conteiner');
	var container2;
	var scrollContainer2;
	var page = "#main";
	var tomsHeight;
	var mw = 0;
	
	$('.book_main, #tom1, #tom2, .about, #news').css('top',bodyHeight).hide();
	$('.tom_1, .tom_2, .tom_1_2').css('top',bodyHeight).css('left',(bodyWidth-617)/2);
	$('#scroll .relative').height(bodyHeight-80);



	
	function getBodyScroll(tomsHeight)
	{
		var bodyScroll = $('#scroll');
		var scroll = $('#scroll .scroll_');
		var scrollContainer = tomsHeight-bodyHeight;
		var drag;
		scroll.mousedown(function(e){
			drag = $(this);
			x = bodyScroll.offset().top;
			h = bodyScroll.height();
			s = scroll.height();
			$(document).mousemove(function(e){
				var position = parseFloat(scroll.css('top'));
				var px = scrollContainer/100*(position/(h-s)*100);
				container.css({top:px*-1})
				if(e.pageY-x-s/2 < 0 ) drag.css('top',0);
				else if(e.pageY-x-s/2 > h-s ) drag.css('top',h-s);
				else drag.css('top',e.pageY-x-s/2);
				mw = parseInt(container.css('top'));
			});
			return false;
		});
		$(document).mouseup(function(){ $(this).unbind('mousemove'); });
		bodyScroll.show().find('.relative').height(bodyHeight-80);
	}
	


	
	$('body').mousewheel(function(event, delta) {
		if(delta < 0 && mw*-1 < tomsHeight-bodyHeight) 
			{
				mw-=30;
				container.animate({
					top:mw
				},20,'linear');
				if (mw*-1>tomsHeight-bodyHeight)
					get_position_scroll(tomsHeight-bodyHeight, tomsHeight-bodyHeight)
				else
					get_position_scroll(mw*-1, tomsHeight-bodyHeight);
			}
		else
		if(delta > 0 && mw < 0) 
			{
				if(mw < -30)
					mw+=30;
				else
					mw=0;
				container.animate({
					top:mw
				},20,'linear');
				if (mw*-1>tomsHeight-bodyHeight)
					get_position_scroll(tomsHeight-bodyHeight, tomsHeight-bodyHeight)
				else
					get_position_scroll(mw*-1, tomsHeight-bodyHeight);
			}
	});
	
	function get_position_scroll(pos,height)
	{
		var c = ($('#scroll').height()-$('#scroll .scroll_').height())/100*(pos/height*100);
		$('#scroll .scroll_').animate({
			top:c
		},20,'linear');
	}

	function scrolle(scroll2)
	{
		scroll2.mousedown(function(e){
			drag = $(this);
			x = $('#scroll2').offset().top;
			h = $('#scroll2').height();
			s = scroll2.height();
		
			$(document).mousemove(function(e){
				var position = parseFloat(scroll2.css('top'));
				var px = scrollContainer2/100*(position/(h-s)*100);
				container2.css({top:px*-1})
				if(e.pageY-x-s/2 < 0 ) drag.css('top',0);
				else if(e.pageY-x-s/2 > h-s ) drag.css('top',h-s);
				else drag.css('top',e.pageY-x-s/2);
			});
			return false;
		});
	}
	$(document).mouseup(function(){ $(this).unbind('mousemove'); });



	


	function loading(i){
		$('.load').animate({ opacity:i },500,function(){
			if (i == 1) loading(0.5); else loading(1);
		});
	}
	
	function show_block_tom2(name){
		$(name+', .tom_1').show().animate({top:207},500,function(){
				$('.tom_1').animate({left:((bodyWidth-850)/2)-360},{ queue:false, duration:400 });
				if($('#scroll2').length)
				{
					scrolle($('#scroll2 .scroll_'));
					container2 = $('.overflow_content .scroll2_slide');
					scrollContainer2 = container2.height() - $('.overflow_content').height();
				}
		});	
	}
	
	function show_block_tom1(name){
		$(name+', .tom_1_2').show().animate({top:207},500,function(){
				$('.tom_1_2').css('top',200).animate({left:((bodyWidth-850)/2)-360},{ queue:false, duration:400 });
				if($('#scroll2').length)
				{
					scrolle($('#scroll2 .scroll_'));
					container2 = $('.overflow_content .scroll2_slide');
					scrollContainer2 = container2.height() - $('.overflow_content').height();
				}
		});	
	}
	
	function show_block(name){
		$(name+', .tom_1, .tom_2').show().animate({top:230},500,function(){		


				$('.tom_1').css('top',200).animate({left:((bodyWidth-617)/2)-360},{ queue:false, duration:400 });
				$('.tom_2').css('top',200).animate({left:((bodyWidth-617)/2)+350},{ queue:false, duration:400 });
				if($('#scroll2').length)
				{
					scrolle($('#scroll2 .scroll_'));
					container2 = $('.overflow_content .scroll2_slide');
					scrollContainer2 = container2.height() - $('.overflow_content').height();
				}
		});	
	}
	
	function hide_block(name){
		if(container.height() > windowHeight)
			var anm = container.height();
		else
			var anm = windowHeight;
		$(name).animate({top:anm},function(){$(this).hide();});
		$('.tom_1, .tom_1_2').animate({left:-617},{ queue:false, duration:400 });
		$('.tom_2').animate({left:bodyWidth},{ queue:false, duration:400 });
	}

	function anime_top(){
		$('.tom_1, .tom_2').hover(function(){$(this).animate({top:'-=10'},200);	
		},function(){$(this).animate({top:'+=10'},150);	});
	}
	

	$('.menu a, .tom_1, .tom_2, .tom_1_2').not('.lang a').click(function(){
		if($(this).attr('rel') != 'no')
		{
			if(page != $(this).attr('href'))
			{
				$('#scroll').hide();
				$('.menu a').removeClass('active');
				$('.menu td').removeClass('nobg2');
				$(this).addClass('active');	
				$(this).parent('td').addClass('nobg2');
				$(this).parent('td').prev('td').addClass('nobg2');
				
				hide_block(page, $(this).attr('href'));
				page = $(this).attr('href');
				setTimeout(function(){
					
					if(page == '#tom2')
						show_block_tom2(page);
					else
					if(page == '#tom1')
						show_block_tom1(page);
					else
						show_block(page);
				},500);
					
				if(page == '#tom1' || page == '#tom2')
					tomsHeight = $(page).height()+280;
				else if(page == '#main')
					tomsHeight = $(page).height()+166;
				else if(page == '#contacts')
					tomsHeight = $(page).height()+360;
				else
					tomsHeight = $(page).height()+280;
								
				if(tomsHeight > bodyHeight) getBodyScroll(tomsHeight)
		
			}
			 return false;
		}
	});
	
	var imgArray = Array();
/*
	imgArray[0] = "/img/menu.png";
	imgArray[1] = "/img/header_convert.png";
	imgArray[2] = "/img/header_telefon.png";
	imgArray[3] = "/img/header_lampa.png";
	imgArray[4] = "/img/copyright.png";
	imgArray[5] = "/img/scroll.png";
	imgArray[6] = "/img/scroll_top.png";
	imgArray[7] = "/img/scroll_bot.png";
	imgArray[8] = "/img/toms_top.png";
	imgArray[9] = "/img/toms_menu.png";
	imgArray[10] = "/img/toms_bot.png";
	imgArray[11] = "/img/toms_bg2.jpg";
	imgArray[12] = "/img/toms_bg.jpg";
	imgArray[13] = "/img/about_bot.jpg";
	imgArray[14] = "/img/about_top.jpg";
	imgArray[15] = "/img/about_bg.jpg";
	imgArray[16] = "/img/background.jpg";
	imgArray[17] = "/img/tom_1.png";
	imgArray[18] = "/img/tom_2.png";
	imgArray[19] = "/img/tom_1_2.png";
	imgArray[20] = "/img/book_main.png";
*/

	imgArray[0] = "/img/scroll.png";
	imgArray[1] = "/img/header_convert.png";
	imgArray[2] = "/img/scroll_top.png";
	imgArray[3] = "/img/header_telefon.png";
	imgArray[4] = "/img/scroll_bot.png";
	imgArray[5] = "/img/header_lampa.png";
	imgArray[6] = "/img/menu.png";
	imgArray[7] = "/img/book_main.png";

	function preload(i)
	{
		$('.preload img').unbind('load')
		.load(function(){		
			if(i<imgArray.length)
			{
					preload(i+1);
					$('.loaderProgress span').text(Math.floor((i+1)/imgArray.length*100));
			}
			if(i == imgArray.length-1)
			{
				$('body').fadeOut(200,function(){
					$(this).removeClass('bodyLoader');
					container.show();
					setTimeout(function(){
						show_block(page);
						$('body').fadeIn(200,function(){
							tomsHeight = $(page).height()+130;
							if(tomsHeight > bodyHeight) getBodyScroll(tomsHeight);
						});
					},500);
				});

			}
		}).attr('src',imgArray[i]); 

	}

/*
	function preload()
	{
	 for(var i = 0; i<imgArray.length; i++)
	 {
	   $("<img>").attr("src", imgArray[i]);
	 }
	}
*/
preload(0);

//container.show();
//show_block(page);

});



