$(document).ready(function()
	{
		$("a[rel^='zoom']").fancybox({speedIn: 500, speedOut: 500, showCloseButton: false, padding: 10, overlayColor: '#000', overlayOpacity: 0.85});
		$("#download").fancybox({scrolling: 'no', titleShow: false, padding: 0, overlayColor: '#000', overlayOpacity: 0.85, speedIn: 500, speedOut: 500});
		initPage();
		if ($('#main-page')[0]) {initArrow();}
		initSound();
		initVideo();
		initScroll();
		checkScroll();
	});

$(window).resize(function()
	{
		checkScroll();
	});

var scroll = 0;
var page = 1;
var bg1 = 1;
var bg2 = 2;
var anim = 0;

function next()
	{
		void(0);
	}

function initPage()
	{
		var i = 1; for (i = 1; i <= 10; i++) {$('#problem_' + i).css('display', 'none')}
		$('#text').css('opacity', '0');
		if (window.location.href.match(/#(\d+)$/) && $('#main-page')[0])
			{
				page = window.location.href.match(/#(\d+)$/)[1];
				$('#image_2').css('opacity', '0');
				$('#image_1').css('background-image', 'url(/lib/img/problems/bg_' + page + '.jpg)');
				$('#alyosha').css('background-image', 'url(/lib/img/problems/circle_' + page + '.png)');
			}
		$('#logo').animate({top: '5'}, 1000, function()
			{
				$('#menu').animate({left: '537'}, 1000, function() {});
			});
		if ($('#custom-page')[0])
			{
				window.setTimeout(function()
					{
						$("#down").removeClass("visible");
						$('#problem').animate({left: '32'}, 1000, function()
							{
								$("#bottom").css('display', 'block');
								$('#text').animate({opacity: '1'}, 500, function() {});
								checkScroll();
							});
					}, 500);
			}
		else
			{
				var i = 1;
				for (i = 1; i <= 10; i++)
					{
						$('#tab_' + i)[0].num = i;
						$('#tab_' + i).click(function()
							{
								if (anim)	{return false}
								page = $(this)[0].num;
								viewPage(page);
							});
					}
				window.setTimeout(function()
					{
						$('#problem').animate({left: '32'}, 1000, function()
							{
								$("#bottom").css('display', 'block');
								$('#text').animate({opacity: '1'}, 500, function() {});
								$("#tab_" + page).addClass("active");
								$('#problem_' + page).css('display', 'block');
								var p = page;
								if (p > 9) {p = 1} else {p++}
								$("#arr")[0].href = '#' + p;
								checkScroll();
							});
					}, 1000);
			}
	}

function initArrow()
	{
		$('#arr').hover(function()
			{
				$('#arrow-left').css('left', '500px');
				$('#arrow-left').css('display', 'block');
				$('#arrow-left').animate({left: '100'}, 400);
			}, function()
			{
				$('#arrow-left').css('display', 'none');
				$('#arrow-left').stop();
			});
		$('#arr').click(function()
			{
				if (page > 9) {page = 1} else {page++}
				viewPage(page);
			});
	}

function initSound()
	{
		$("#player").jPlayer(
			{
				ready: function()
					{	
						this.element.jPlayer("setFile", "/lib/mp3/sweet_people.mp3");
//						if ($('#main-page')[0]) {this.element.jPlayer("play");}
						$("#player").jPlayer("onSoundComplete", function() {this.element.jPlayer("stop"); this.element.jPlayer("play")});
					},
				swfPath: "/lib/swf"
			});

		$('#sound').click(function()
			{
				if ($("#player").jPlayer("getData", "diag.isPlaying"))
					{
						$('#player').jPlayer('pause');
						$('#sound').removeClass('off');
						$('#sound').addClass('on');
					}
				else
					{
						$('#player').jPlayer('play');
						$('#sound').removeClass('on');
						$('#sound').addClass('off');
					}
			});
	}

function initVideo()
	{
		$("a[rel^='video']").click(function()
			{
				$.fancybox(
					{
						'padding': 0, 'overlayColor': '#000', 'overlayOpacity': 0.85, 'transitionIn': 'none', 'transitionOut': 'none', 'showCloseButton': false, 'autoScale': false, 'title': this.title, 'width': 640, 'height': 385,
						'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'), 'type': 'swf', 'swf': {'wmode': 'transparent', 'allowfullscreen': 'true'}
					});
				$('#player').jPlayer('pause');
				$('#sound').removeClass('off');
				$('#sound').addClass('on');
				return false;
			});
	}

function initScroll()
	{
		$("#up").mouseover(function()
			{
				if (!scroll) scroll = setInterval(function()
					{
						$('#text').scrollTop($('#text').scrollTop() - 7);
						checkScroll();
					}, 25);
		  });
		$("#up").mouseout(function()
			{
				if (scroll) {clearInterval(scroll); scroll = 0}
		  });
		$("#down").mouseover(function()
			{
				if (!scroll) scroll = setInterval(function()
					{
						$('#text').scrollTop($('#text').scrollTop() + 7);
						checkScroll();
					}, 25);
		  });
		$("#down").mouseout(function()
			{
				if (scroll) {clearInterval(scroll); scroll = 0}
		  });
	}

function checkScroll()
	{
		var txt = $('#text');
		if (txt.scrollTop() == 0) {$("#up").removeClass("visible")}
		else {$("#up").addClass("visible")}
		if (txt[0].scrollHeight - txt.scrollTop() == txt.outerHeight()) {$("#down").removeClass("visible")}
		else if (txt[0].scrollHeight > txt.outerHeight()) {$("#down").addClass("visible")}
	}

function viewPage(p)
	{
		anim = 1;
		if (bg1 == 1) {bg1 = 2; bg2 = 1} else {bg1 = 1; bg2 = 2}
		$('#image_' + bg2).css({'z-index': '1'});
		$('#image_' + bg1).css({'background-image': 'url(/lib/img/problems/bg_' + p + '.jpg)', 'z-index': '0', 'opacity' : '1'});
		var i = 1; for (i = 1; i <= 10; i++) {$("#tab_" + i).removeClass("active")}
		$('#problem').animate({left: '-3000', right: 3000 - 250}, 1000, function()
			{
				$('#text').css('opacity', '0');
				var i = 1; for (i = 1; i <= 10; i++) {$('#problem_' + i).css('display', 'none')}
				checkScroll();
				$('#alyosha').css('background-image', 'url(/lib/img/problems/circle_' + p + '.png)');
				$('#image_' + bg2).animate({opacity: '0'}, 1500, function()
					{
						$("#descr_" + p).css('display', 'block');
						$(".photo-descrs").css('display', 'block');
						$('#image_' + bg1).css({'opacity': '1'});
						window.setTimeout(function()
							{
								$("#descr_" + p).css('display', 'none');
								$(".photo-descrs").css('display', 'none');
								$('#problem').css({'left': '3000px', 'right': '-250px'});
								$('#problem').animate({left: '32', right: '-250'}, 1000, function()
									{
										$("#tab_" + p).addClass("active");
										$('#problem_' + p).css('display', 'block');
										$('#text').scrollTop(0);
										$('#text').animate({opacity: '1'}, 500, function() {});
										if ($('#main-page')[0])
											{
												if (p > 9) {p = 1} else {p++}
												$("#arr")[0].href = '#' + p;
											}
										checkScroll();
										anim = 0;
									});
								}, 7000);
					});
			});
	}


