/* scripts */
var sTop = 0;
var no_body_overlay = 0;

$(function(){


$('a.nwabla').hover(function(){
	if(!$(this).is('.hover'))
		$(this).addClass('hover');
},function(){
	//setTimeout(function(){
		if(!$(this).parent().children('table').is(':visible'))
			$(this).removeClass('hover');
//	},500)
	
}).click(function(){

	if($(this).next('table').length)
	{
		hmen($(this).parents('li'));
		return false;
	}

});


	function sHide(href){
		$('.header').animate({left:-$(window).width(),opacity:0},500);
		$('#microfon').animate({right:-$('#resize2').width()},600);
		$('#resize2').animate({right:-$('#resize2').width()},500,function(){
			$('.content').animate({left:-$(window).width()},400,function(){
				$('.background').fadeOut(500,function(){
					//$(this).show().append('<div class="newMainBg"></div>');
					window.location = href;
				})
			});
		});
		
	}

	$('.menu2 td a').click(function(){
		if(!$('.oleshko_news').length)
		{
			if(!$('.oleshko_dnevnik').length)
			{
				var href = $(this).attr('href');
				sHide(href);
				return false;
			}
		}
	});



	function mainAnimate(){
		var x = 0;
		var y = 0;
		var bw = $('body').width();
		var bh = $('body').height();
		var bg = $('.newMainBg');
		var sneg = $('.newSneg');
		var oleshko = $('.newOleshko');
		var logo = $('.newLogo');
		var lampa = $('.newLampa');
		var smile = $('.newNastroenie');
		
		if(bh > 794)
		{
			$('.newContent').css({
				height:$(this).height()+((bh-794)/2)-25
			})
			//sneg.css({top:794-bh});
		}

		$(document).mousemove(function(e){
			x = (e.pageX-(bw/2))*100/bw;
			y = (e.pageY-(bh/2))*100/bh;
			
			bg.css({
				marginLeft:-645-x/1	
			});
			sneg.css({
				marginLeft:-1000+x*2
			});
			oleshko.css({
				right:40-x/2
			});
			logo.css({
				left:0+x*1.1
			});
			lampa.css({
				right:0-x/1.4
			});
			smile.css({
				left:16+x*0.7
			});
				/*
			if($('.tvor4estvo_detstvo').length)
			{
				if(x < 0)
				{
					$('.tvor4estvo_detstvo').css({
						backgroundPosition:100-x+'% 0'
					});
					$('.tvor4estvo_detstvo #resize2').css({
						right:0+x
					});
					$('.tvor4estvo_detstvo .logo').css({
						left:0-x/2.5
					});
				}
			}
			*/
		});
	}
	mainAnimate();

	//$('.fakeBody').height($('body').height()).css('position','relative');
	
	var scrolltop;

$('#proekts').click(function(){

if($.browser.opera){
	$('body').attr('onmousewheel','return false;');
	scrolltop = $(window).scrollTop();
} else if($.browser.msie)
	{
		scrolltop = $(window).scrollTop();
	}
else
	scrolltop = $('body').scrollTop();
	

	
	$('body').overlay({opacity:0.8}).show();
	$('html,body').css({overflow:'hidden'});
	$('.proekt').show().width($(window).width()).height($(window).height()).css({
		top:scrolltop
	})/*.attr('onmousewheel','return true;');*/;
	$('.proekt table').height($(window).height());
});

$('.proekt').live('click', function(){
	if($.browser.opera){
		$('body').attr('onmousewheel','return true;');
	}
	$('html,body').css({overflow:'visible'}).scrollTop(scrolltop);
	$(this).hide();
	$('#m5_overlay').remove();	
});


	var r_id = $('.pospis');
	var r_width = r_id.width();
	var r_height = r_id.height();
	var rw;
	var rw2;
	var rw3;


$('#preview_play_list a').click(function(){
	$('body').overlay().fadeIn(150);
	//$('body').append('<img src="'+$(this).attr('href')+'" id="play_list_foto" style="display:none;"/>');
	$('body').append('<div style="display:none; text-align:center; font-size:16px; color:red; font-weight:bold;" id="play_list_foto"><img src="/images/disk_full.jpg" style="margin-bottom:5px;" /><br /><span id="new-font">2011 год<span></div>');
	$('#play_list_foto img').load(function(){
		$('#play_list_foto').middle('window').fadeIn(150);
		Cufon.replace("#new-font", { fontFamily: 'OfficinaSerifBlackC' });
	//}).attr('src',$(this).attr('href'));
	}).attr('src','/images/disk_full.jpg');


	return false;
});

function podpis_resize()
	{
		var rw_height = $(window).height();
		var rw_width = $(window).width();

		$('#resize2, #resize2 img').height(rw_height-60).css('width','auto');

		rw = rw_width/r_width;
		rw3 = rw_height/r_height;

		rw2 = (rw3)*220-50;
		$('.pospis').width(rw2).height(rw2).css({
			top:'50%',
			marginLeft: 0,
			marginTop: ((rw2/2)*-1)+60
		});
			/*
		if ($.browser.msie && $.browser.version.substr(0,1)<8) {
			$('.pospis').width(rw2).height(rw2).css({
				marginTop: (rw2/2*-1)+60
			});
		}
		*/

	}

	if(r_id.length)
	{
		podpis_resize();
		$(window).resize(function(){
			podpis_resize();
		});
	}
	
	if($('.main').length)
	{
		$('html,body').css({overflow:'hidden'})
		var x = 0;
		var y = 0;
		var test = $('.test');
		var bw = $('body').width();
		var bh = $('body').height();
		var le = $('.logo');
		var oe = $('#resize2');
		var bg = $('.main .background');
		var po = $('.pospis');
		var poo = $('.pospis').offset().top;
		

		$(document).mousemove(function(e){
			x = (e.pageX-(bw/2))*100/bw;
			y = (e.pageY-(bh/2))*100/bh;
			test.html('x: '+x+'&nbsp;&nbsp;&nbsp; y: '+y);
			
			le.css({
				top:136-(y/1),
				left:0-(x/1)
			});
			oe.css({
				bottom:0+(y/1),
				right:40+(x/1)
			});
/*
			po.css({
				top:poo-(y/1),
				left:0-(x/1)
			});*/
			bg.css({
				left:0+(x/2),
				top:0+(y/2)
			});
		});
	}

	$('.preview_slide a').live('click', function(){
		var a = $(this).parents('.preview_slide').find('a');
		var i = a.index($(this));
		show_big_slide(a,i);
		return false;
	});	

	if($('.slide_conteiner td').size() > 5)
			$('.slide_prev, .slide_next').show();
	slidegallery();

	var l;
	var time;
	if($('.gallery_review').length)
	{
		$('.gallery_review').hover(function(){
			var id = $(this);
			var rel = id.find('img').attr('rel');
			if(!id.find('.preview_bg, .preview_img').length)
			{
				id.find('.insert_html').addClass('insert_html_loading');
				time = setTimeout(function(){
					id.find('.insert_html').unbind('load').load('http://oleshko.media5.com/gallery/ajax/?id='+rel,function(){
						getHover();
					}).html('');
				},1000);
			}
			else
				getHover();

			function getHover()
			{
				$('.preview_bg, .preview_img').hide();
				id.find('.insert_html').removeClass('insert_html_loading').removeClass('insert_html');
				if(!id.is('.noget'))
				{
					id.addClass('noget');
					smallgallery(id);
				}
				else
				id.find('.preview_bg, .preview_img').fadeIn(200);
			}
		},function(){
				clearInterval(time);
				$(this).find('.insert_html').removeClass('insert_html_loading');
				$('.preview_bg, .preview_img').hide();
		});
	}

	$('.menu2 .active').parents('li').addClass('hover');


	$('.jslistli').not('.liopen').click(function(){
		$('.jslistli.liopen').removeClass('liopen').find('ul').stop().slideUp(300);
		if($(this).find('ul').length)
		{
			var t = $(this);
				t.find('ul').stop().slideDown(300,function(){
					t.addClass('liopen');
				});
		}
	}).hover(function(){
		if($(this).find('ul').length)
			$(this).addClass('pointer');
	});

	$('.liopen').live('click', function(){
		if($(this).find('ul').length)
		{
			var t = $(this);
				t.find('ul').slideUp(500,function(){
					t.removeClass('liopen');
				});
		}
	})

	var menuArray = $('.menu2 li');
	var menuBackground = $('.menu2 .menu_background');
	var menuStrelka = $('.menu2 .menu_strelka');
	var menuTimeout;
	var menuOpen;
	var t = 0;

	menuArray.find('a:eq(0)').click(function(){	
		if($(this).parent().find('table').length)
		{
			menuBackground.hide();
			menuStrelka.hide();	
			$('.menu2 li').find('table').hide();
			//$('.menu2 li').find('a').removeClass('hover').removeClass('bgno');
			hmen($(this).parents('li'));
			return false;
		}
	}/*,function(){
		menuTimeout = setTimeout(function(){
			menuBackground.hide();
			menuStrelka.hide();	
			//menuArray.find('a.hover').removeClass('hover');
			menuArray.find('table').hide();
			menuArray.find('a').removeClass('hover').removeClass('bgno');
			if(menuOpen != null)
				hmen(menuOpen);
		},400);
		return false;
	}*/);
	
	/*
	
	$('.menu2 li').not('.menu2 li li').hover(function(){
		if(!$(this).find('a:eq(0)').is('hover'))
		{
			if($(this).find('a:eq(0)').is('.bgno'))
			{
				t = 1;
				$(this).find('a:eq(0)').removeClass('bgno');
			}
			//$(this).find('a:eq(0)').addClass('hover');
		}
	},function(){
		if(!$(this).find('a:eq(0)').is('hover') && $(this).find('table:hidden'))
		{
			if(t == 1)
			{	
				$(this).find('a:eq(0)').addClass('bgno');
				t = 0;
			}
			//$(this).find('a:eq(0)').removeClass('hover');
		}
	});
*/
	
	function hmen(id)
	{
		clearInterval(menuTimeout);
		var	thisTable = id.find('table');
		var menuRelative = $('.relative2').offset().left;
		menuArray.find('a').removeClass('hover')./*removeClass('bgno').*/end().find('table').hide();
		menuBackground.hide();
		menuStrelka.hide();
		
		id.find('a').eq(0).addClass('hover');
		id.next('li').find('a')/*.addClass('bgno')*/;		
		if(thisTable.length)
		{
			var ofleft = id.offset().left-menuRelative;
			var table_width = thisTable.width();
			var table_height = thisTable.height();
			id.find('table').css({left:ofleft+5}).show();
			menuStrelka.css({
				left:ofleft+45,
				top:table_height+50,
				display:'block'
			});
			menuBackground.css({
				left:ofleft,
				height:table_height+10,
				width:table_width+10,
				display:'block'
			});
		}
	}


if($('.menu2 li.hover').length)
	{
		hmen($('.menu2 li.hover'));
		menuOpen = $('.menu2 li.hover');
	}



	$('.play a').click(function(){
		show_player($(this).attr('href'),155,66);
		return false;
	
	});
	
	$('.videolink').click(function(){
		show_player($(this).attr('href'),500,281);
		return false;
	});

	$('#m5_overlay').live('click',function(){
		hide_pop_up();
		return false;
	});

	$('#show_big_image a').click(function(){
		show_big_image($(this).attr('href'));
		return false;
	});


});


var id_resize2 = $('#resize2');

$(window).resize(function(){
	//resize2(id_resize2);
}).load(function(){
		resize2(id_resize2);
});
	
function resize2(id)
{
		if($('.tvor4estvo_estrada').length)
			if ($.browser.msie)
				var wm1 = $('body').height()-160;
			else
			var wm1 = $('body').height()-160;
		else
			var wm1 = $('body').height();

		$('.main .content, .main .background, .main').height($(window).height());

if($('.tvor4estvo_kontaki').length)
	{
		$('.logo').fadeIn(800,function(){
			id.css({height:wm1,width:'auto'}).css({right:$(this).width()*-1}).find('img').css({height:wm1,width:'auto'}).end().fadeIn(800);
		});
	}
else
	{
		id.css({height:wm1,width:'auto'}).css({right:$(this).width()*-1}).find('img').css({height:wm1,width:'auto'}).end().show().animate({right:0},800,function(){
			if($('.tvor4estvo_micro').length)
			{
				$('.fakeScroll').css('overflow','hidden');
				$('#microfon').show().css({bottom:-$(this).height()}).animate({bottom:0},800).css({zIndex:3});
			}
		});
		if($('.main .oleshko').length)
			{
				$('.logo').fadeIn(400,function(){
					$('.main .oleshko').fadeIn(300,function(){
						$('.main .pospis').fadeIn(300);
					});
				});
			}
			else
			$('.logo').fadeIn(800);
	}


		/*
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			var width = id.width();
			var height = id.height();
			var windowHeight = $('body').height();
			var src = id.find('img').attr('src')
			id.css({
					width:windowHeight/(height/width),
					filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"',sizingMethod='scale');"
			}).find('img').css({display:'none'});
		}
		*/
}

function slidegallery()
{
	if($('.slide').length)
	{
		var prev = $('.slide_prev');
		var next = $('.slide_next');
		var slide = $('.slide_conteiner table');
		var slide_width = $('.slide_conteiner table').width();
		var slide_count = $('.slide_conteiner td').size();
		var conteiner_width = $('.slide_conteiner').width();
		var speed = slide_count*200;
		var shag = 183;
		
		prev.mousedown(function(){
			slide.stop().animate({left:0},speed,'linear');	
		}).mouseup(function(){
			slide.stop();
		});
		
		next.mousedown(function(){
			slide.stop().animate({left:conteiner_width-slide_width},speed,'linear');	
		}).mouseup(function(){
			//var left = parseFloat(slide.css('left'))*-1;
			//var g = ((Math.round(left/shag)+1)*shag)*-1;
			//alert(slide_count)
			slide.stop();
		});
	}
}

function show_player(url,width,height)
{
	$('body').overlay().show();
	var player = $('#audio_player');

	if(!player.length)
	{
		$('body').append('<div id="audio_player" style="color:#fff; text-align:center;"></div>');
		player = $('#audio_player');
	}

	player.load(url,function(){
		$(this).middle('window',{width:width,height:height});
	}).show();
}

function hide_pop_up()
{
	$('#m5_overlay').remove();
	var audio_player = $('#audio_player');
	var big_image = $('#big_image');
	var slide_big_image = $('#slide_big_image');
	var play_list_foto = $('#play_list_foto');
	var iSlide = $('.iSlide');

	if(audio_player.length) audio_player.hide().html('');
	if(big_image.length) big_image.hide().find('img').removeAttr('src');
	if(slide_big_image.length) slide_big_image.remove();/*hide().find('img').removeAttr('src');*/
	if(play_list_foto.length) play_list_foto.remove();
	if(iSlide.length) iSlide.hide().find('.relative').html('');
	$('.add_msg').hide();
	$('.proekt').hide();
	//FakeScroll('hide');
	$.fakeScroll('hide');
}


function show_big_image(url)
{
	var big_image = $('#big_image');

	if(!big_image.length)
	{
		$('body').append('<div id="big_image""><img src=""/></div>');
		big_image = $('#big_image');
		big_image.click(function(){
			hide_pop_up();
		});
	}

	$('body').overlay().show();

	big_image.find('img').load(function(){
		big_image.middle('window',{width:$(this).width()}).show();
	}).attr('src',url);

}



function smallgallery(prw)
{
	prw.find('.preview_bg, .preview_img, table').fadeIn(200);
	var preview = prw.find('.preview_img');
	var preview_slide = prw.find('.preview_slide');
	var preview_table = preview_slide.find('table');
	var preview_count = preview_slide.find('table').size();
	var slide_pages =  prw.find('.slide_pages');
	var slide_pages_a;
	var slide_pages_a_size;
	var select_table = 0;
	var preview_width = preview_slide.width();
	var speed = 200;
	
	
	function getPages()
	{
		if(preview_count > 1)
		{
			var i = 1;
			var a = '';
			var active = '';
			while( i <= preview_count)
			{
				if(i == 1) active = 'active'; else active = '';	
				a += '<a href="#'+i+'" class="'+active+'">'+i+'</a>';
				i++;
			}
			slide_pages.show().find('.sa').append(a);
			preview_table.show().css({left:preview_width}).eq(select_table).css({left:0});
			slide_pages_a =  prw.find('.slide_pages .sa a');
			slide_pages_a_size = slide_pages_a.size();
		}
	}
	
	function selectPages(i)
	{
		if(select_table != i-1)
		{
			preview_table.eq(select_table).animate({left:preview_width*-1},speed,function(){
				$(this).css({left:preview_width});
			});
			preview_table.eq(i-1).animate({left:0},speed,function(){
				select_table = i-1;
			});
			
			slide_pages_a.removeClass('active').eq(i-1).addClass('active');
		}
	}
	
	slide_pages.find('.sa a').live('click', function(){
		if(select_table != slide_pages_a.index($(this)))
		{
			selectPages(slide_pages_a.index($(this))+1);
		}
		return false;
	});
	preview.find('.sRight').live('click', function(){
		var index = slide_pages_a.index(slide_pages.find('.sa a.active'));
		
		var ini = index+2; 
		if(ini < slide_pages_a_size+1)
			selectPages(ini);
		else
			{
				ini = 1;
				selectPages(ini);
			}
		return false;
	});
	preview.find('.sLeft').live('click', function(){
		var index = slide_pages_a.index(slide_pages.find('.sa a.active'));
		var ini = index; 
			selectPages(ini);
		return false;
	});
	getPages();
}

function FakeScroll(param)
{


	if(param == 'show')
	{
		$('.fakeScroll').height($(window).height());
		sTop = $(window).scrollTop();
		$('html, body').addClass('noScroll').find('.fakeScroll').scrollTop(sTop);
	}
	else if(param == 'hide')
	{
		$('html, body').removeClass('noScroll').scrollTop(sTop);
		//$(window)
	}
}

function show_big_slide(array,id,pr,pt)
{
	//alert('df');
	var size = array.size();
	var block = $('#slide_big_image');
	var prev =  block.find('.prev');
	var next =  block.find('.next');
	var img = block.find('img');
	var slideA = $('.iSlide a');
	var slideAsize = $('.iSlide a').size();
	var slideT = $('.iSlide table');
	var animate = 1;
	
	prev.hide().unbind('click');
	next.hide().unbind('click');
	img.unbind('load').unbind('animate').stop();

	if(!pr) pr = 50;
	if(!pt) pt = -20;

	if(!no_body_overlay == 1)
		$('body').overlay({opacity:0.8}).fadeIn(100);
	

	if(!block.length)
	{
		$('body').append('<div id="slide_big_image" style="z-index:99999; position:absolute; overflow:hidden; left:48%; top:48%; width:50px; height:50px; border:0;"><div class="relative"><div class="prev"></div><div class="next"></div><img src="/images/spacer.gif"></div></div>');
		block = $('#slide_big_image');
		prev =  block.find('.prev');
		next =  block.find('.next');
		img = block.find('img');
	}

	prev.hide().unbind('click');
	next.hide().unbind('click');
	img.unbind('load').unbind('animate').stop();


	function load_img(i){
		block.show();
		button(i);
		select(i);
		animate = 0;
		var speed = 200;
		var h = 0;
		img.stop().animate({opacity:0},speed,function(){
			img.attr('src','').unbind('load').load(function(){
				if(img.height() != 0)
				{
					img.proportions({height:pr, window:'yes'});
					block.show().middle('window',{
						width:img.width(), 
						height:img.height(),
						animate:'yes',
						top:pt
					},function(){
						img.stop().animate({opacity:1},speed,function(){
							animate = 1;
							id = i;
						});
					});
				}

			}).attr('src',array.eq(i).attr('href'));
		});
	}
	

	function button(id)
	{
		if(array.length > 1)
		{
			prev.show();
			next.show();
			if(id >= size-1) next.hide();
			if(id <= 0) prev.hide();
		}
	}



	function select(id)
	{
		slideA.removeClass('active').eq(id).addClass('active');
		if(id>3 && id < slideAsize-3)
		{
			slideT.css({left:((id-3)*123)*-1});
		}
			
	}

	$('.inext').click(function(){
		if(id+1 < size && animate == 1) 
			load_img(id+1);
	});
	$('.iprev').click(function(){
		if(id-1 >= 0 && animate == 1) 
			load_img(id-1);
	});

	next.click(function(){
		if(id+1 < size && animate == 1) 
			load_img(id+1);
	});
	prev.click(function(){
		if(id-1 >= 0 && animate == 1) 
			load_img(id-1);
	});

if(slideA.length)
	{
		slideA.click(function(){
			var index = slideA.index($(this));
			load_img(index);
			return false;
		});
	}
	load_img(id);
}






		var media5 = {
			href : function(i){
				var a = $('div.gallery2_pages_a a');
				var img = $('div.gallery2_big_foto img');
				var src = '';
				var navi = {back:$('.gallery2_back'), next:$('.gallery2_next')};
				var l = 1;
				var get_a = {
					size : a.size()/2,
					table : $('div.gallery2_pages_a table'),
					width : $('div.gallery2_pages_a table').parent().width(),
					left : $('div.gallery2_pages_a table').parent().offset().left
				}

				media5.showImage(img,a.eq(i).attr('href'),i);
				
				navi.back.click(function(){
					$('div.gallery2_pages_a a.active').parent().prev().children('a').trigger('click');
					return false;
				})

				navi.next.click(function(){
					$('div.gallery2_pages_a a.active').parent().next().children('a').trigger('click');
					return false;
				})

				a.click(function(){
					if(!$(this).is('.active'))
					{
						a.removeClass('active');
						
						var g = get_a['left']; 
						var gg = $(this).offset().left-g;
						var ggg = get_a['table'].position().left;

						if(a.index($(this)) < get_a['size']-10)
							get_a['table'].animate({left:(gg-ggg)*-1},500)

						Cufon.replace(a, { fontFamily: 'OfficinaSerifBlackC' });
						src = $(this).attr('href');
						var index = $(this).parents('.gallery2_pages_a').find('a').index($(this));
						media5.showImage(img,src,index);
					}
					return false;
				});
			},
			showImage : function(img,src,index){
				var t = $('div.gallery2_pages_a').find('a:eq('+index+')')
				t.addClass('active');
				Cufon.replace(t, { fontFamily: 'OfficinaSerifBlackC' });
				img.hide().removeAttr('src').load(function(){
					$(this).middle('block').fadeIn(400)	;
				}).attr('src',src);
			},
			slide_y : function(){
				var top = $('img.gallery2_slide_top');
				var bot = $('img.gallery2_slide_bot');
				var slide = $('div.galleru2_slide_preview');
				var table = slide.children('table');
				var tr = table.find('tr');
				var size = tr.size();
				var height = {table:table.height()-19, slide:slide.height(), tr:tr.height()};
				var shag = 2;
				var pos = 1;
				if(size > 5)
				{
					bot.click(function(){
						if(!table.is(':animated') && pos >= 1)
						{
							var top = (height.tr*(shag*pos))*-1;
							if(top*-1 > height.table-height.slide) { 
								top = size-5*height.tr-9.5;
								go(top,400);
							} else {
								go(top,400);
								pos++;
							}
						}
					});
					top.click(function(){
						if(!table.is(':animated') && pos > 1)
						{
							pos--;
							var top = (height.tr*(shag*(pos-1)))*-1;
							go(top,400);
						}
					});
				}
				var go = function(a,speed){table.stop().animate({top:a},speed);}
			},
			slide_x : function(){
				var prev = $('img.gallery2_slide_back');
				var next = $('img.gallery2_slide_next');
				var slide = $('div.gallery2_pages_a');
				var table = slide.children('table');
				var tr = table.find('td');
				var size = tr.size();
				if(size > 16)
				{
					var width = {table:table.width(), slide:slide.width(), tr:tr.width()};
					
					next.mousedown(function(){
						table.stop(true, false).animate({left:width.slide-width.table},1000);
					})
					prev.mousedown(function(){
						table.stop(true, false).animate({left:0},1000);
					})
					$('body').mouseup(function(){
						table.stop(true, false);	
					});
				} else {
					prev.click(function(){
						$('div.gallery2_pages_a a.active').parent().prev().children('a').trigger('click');
						return false;
					});

					next.click(function(){
						$('div.gallery2_pages_a a.active').parent().next().children('a').trigger('click');
						return false;
					});
				}
			}
		}



