var kozirniy_counter = 0;
var k_scroll_up;
var k_scroll_down;
var load_image = '/images/image_loading.gif';

jQuery.noConflict();
jQuery(document).ready(
	function()
	{
		if (document.location.href == 'http://haha.pp.ua')
		{
			jQuery('#icons').fadeIn(5000);
			jQuery('#nav').fadeIn(5000);
		}
		else
		{
			jQuery('#icons').fadeIn(1000);
			jQuery('#nav').fadeIn(1000);
		}


		if (document.documentElement.clientHeight > 650)
		{
			jQuery('#wraper').css('marginTop', Math.round((document.documentElement.clientHeight-650)/2));
		}

		if (document.documentElement.clientWidth > 1100)
		{
			jQuery('#wraper').css('marginLeft', Math.round((document.documentElement.clientWidth-980)/2));
		}

		jQuery('#icons a img,#nav a img').each(
			function()
			{
				// Предварительная подгрузка картинок, чтобы небыло лагов.
				img = new Image();
				img.src = jQuery(this).attr('src');

				img_a = new Image();
				img_a.src = jQuery(this).attr('altsrc');

				jQuery(this).attr('src1',jQuery(this).attr('src'));
			}
		)

		jQuery('#icons a img,#nav a img').hover(
			function()
			{
//			soundManager.play('aSound');
			if (jQuery(this).attr('src').length > 0)
				{
					var el = this;
					setTimeout(
						function()
						{
							jQuery(el).attr('src', jQuery(el).attr('altsrc'));
							jQuery(el).animate({opacity: "1"},"500")
						},
						1
					)
				jQuery(el).animate({opacity: "0.7"},"500")
				}
			},
			function()
			{
				var el = this;
				setTimeout(
					function()
					{
						if (jQuery(el).attr('src').length > 0)
						{
							jQuery(el).attr('src', jQuery(el).attr('src1'));
						}
					},
					1
				);
			}
		);

		jQuery('#icons a img').click(
			function()
			{
//				jQuery(this).fadeOut(5000);

			}
		);


//		setInterval(
//			function()
//			{
//				if (kozirniy_counter == 0)
//				{
//					jQuery('#k_design').attr('src', '/images/1_1.jpg');
//				}
//				if (kozirniy_counter == 1)
//				{
//					jQuery('#k_design').attr('src', '/images/1_2.jpg');
//				}
//				if (kozirniy_counter == 2)
//				{
//					jQuery('#k_design').attr('src', '/images/1_3.jpg');
//				}
//				if (kozirniy_counter == 3)
//				{
//					jQuery('#k_design').attr('src', '/images/1_4.jpg');
//				}
//				if (kozirniy_counter == 4)
//				{
//					jQuery('#k_design').attr('src', '/images/1_1.jpg');
//				}
//				if (kozirniy_counter == 5)
//				{
//					jQuery('#k_design').attr('src', '/images/1.jpg');
//					kozirniy_counter = 0;
//				}
//				kozirniy_counter++;
//
//			},
//			1000
//		);

		jQuery('#down').click(
			function()
			{
//				clearInterval(k_scroll_up);
//				k_scroll_up = setInterval(
//					function()
//					{
						var scroll = jQuery('#content').scrollTop();

						var deltaScroll = ((Math.round(jQuery('#content').scrollTop() / 137)) - 1) * 137;

						if (scroll - deltaScroll < 0)
						{
							scroll = 0;
						}
						else
						{
							scroll = deltaScroll;
						}
//						jQuery('#content').scrollTop(scroll);
						jQuery('#content').animate({scrollTop: scroll}, 500);
//					},
//					100
//				)
			}
		);

//		jQuery('#up').mouseup(
//			function()
//			{
//				clearInterval(k_scroll_up);
//			}
//		);

		jQuery('#up').click(
			function()
			{
//				clearInterval(k_scroll_down);
//				k_scroll_down = setInterval(
//					function()
//					{
						var scroll = jQuery('#content').scrollTop();
						var deltaScroll = ((Math.round(jQuery('#content').scrollTop() / 137)) + 1) * 137;
						if (scroll + deltaScroll > jQuery('#content').clientHeight)
						{
							scroll = jQuery('#content').clientHeight;
						}
						else
						{
							scroll = deltaScroll;
						}
//						jQuery('#content').scrollTop(scroll);
						jQuery('#content').animate({scrollTop: scroll}, 500);
//					},
//					100
//				);
			}
		);

//		jQuery('#down').mouseup(
//			function()
//			{
//				clearInterval(k_scroll_down);
//			}
//		);
//
//		jQuery('#down').click(
//			function()
//			{
//				clearInterval(k_scroll_down);
//			}
//		);
//		jQuery('#up').click(
//			function()
//			{
//				clearInterval(k_scroll_up);
//			}
//		);
//
//		jQuery('#content').scroll(
//			function(data)
//			{
//				alert(data.toSource);
////				jQuery('#down').mouseover();
//			}
//		);


//		jQuery('#content').height((Math.round(jQuery('#content').height()/137)+1) * 137);
		jQuery('#content').mousewheel(
			function(event, delta)
			{
				var scroll = jQuery('#content').scrollTop();
				if (delta < 0)
				{

					if (scroll + 137 > jQuery('#content').clientHeight)
					{
						scroll = jQuery('.text').height();
					}
					else
					{
						if (scroll <= jQuery('#content').scrollTop())
						{
							if (jQuery.browser.msie)
							{
								scroll += 134;
							}
							else
							{
								scroll = ((Math.round(jQuery('#content').scrollTop() / 137)) + 1) * 137;
							}

						}
					}

					jQuery('#content').scrollTop(scroll);

				}
				if (delta > 0)
				{

					if (scroll - 134 < 0)
					{
						scroll = 0;
					}
					else
					{

						if (jQuery.browser.msie)
						{
							scroll -= 134;
						}
						else
						{
							scroll = (Math.round(jQuery('#content').scrollTop() / 137) - 1) * 137;
						}

					}
					jQuery('#content').scrollTop(scroll);
//					alert(scroll = Math.round(jQuery('#content').scrollTop() / 137) * 137);
				}
			}
		);

//		jQuery('.kimage').wTooltip({
//			content: "Lorem ipsum — название классического текста-«рыбы».<br/>",
//			className: "wtoolbar",
//			degrade: true,
// 			fadeIn: 600,
//     		fadeOut: "slow",
//     		style: false
//		});

		jQuery('.kimage').hover(
			function()
			{
				jQuery('.announce').html(
					'<strong>' + jQuery(this).find('img').attr('title') + '</strong><br/>' +
					jQuery(this).find('img').attr('text')
				);
				jQuery('.announce').fadeIn(10);
				jQuery('.text').fadeOut(10);
				jQuery('.announce').css('top',jQuery(this).position().top);


			},
			function()
			{

				jQuery('.announce').fadeOut(10);
				jQuery('.text').fadeIn(10);
			}
		);

		jQuery('.kimage').click(
			function()
			{
				setTimeout(
					function()
					{
						jQuery('#content_text').fadeOut('slow');
					},
					1
				);

				jQuery('#content_img').fadeIn(5000)
			}
		);


		jQuery('#up').click(
			function()
			{
				var el = jQuery('#EnlargedImage');
				if (jQuery(el).html() != null)
				{
					var key = el.attr('key');
					var next_href = jQuery('.gallery img[key=' + key +']').parent().parent().parent().next().find('img');

					jQuery('body').append('<div class="loading"><img src="/images/loading.gif"/></div>');;

					setTimeout(
						function()
						{
							jQuery('.loading').remove();
							jQuery(el).attr('src', jQuery(next_href).attr('main'));
							jQuery(el).attr('key', jQuery(next_href).attr('key'));
						},
						500
					);

				}
			}
		);

		jQuery('#down').click(
			function()
			{
				var el = jQuery('#EnlargedImage');
				if (jQuery(el).html() != null)
				{
					var key = el.attr('key');

					jQuery('body').append('<div class="loading"><img src="/images/loading.gif"/></div>');

					var next_href = jQuery('.gallery img[key=' + key +']').parent().parent().parent().prev().find('img');
					setTimeout(
						function()
						{
							jQuery('.loading').remove();
							jQuery(el).attr('src', jQuery(next_href).attr('main'));
							jQuery(el).attr('key', jQuery(next_href).attr('key'));
						},
						500
					);
				}
			}
		);

		jQuery('.knews').click(
			function()
			{
				//alert(jQuery(this).attr('news'));
				jQuery('body').append('<div class="loading"><img src="/images/loading.gif"/></div>');
				var el = jQuery(this).attr('news');

				jQuery('.knews_text').hide();
				jQuery('#knews_basetext').hide();

				setTimeout(
					function()
					{
						jQuery('.announce').hide();
						jQuery('.text').show();

						jQuery('.loading').remove();

						//jQuery('#knews_' + el).show();

						jQuery('#knews_' + el).slideDown(2000);
					},
					500
				);

			}
		);
	}

);
