$(document).ready(function()
{
	var yAdd = 0;
	var height = $(document).height();
	var bodyOffset = $('body').scrollTop();
	var windowOffset = $(window).scrollTop();
	
	if(navigator.userAgent.split('/')[0] == 'Opera')
	{
		var scrollElement = 'html';
	} else {
		var scrollElement = 'html, body';
	}
	
	
	checkActive(windowOffset);
	
	$('#promo .rules').hide();
	
	$('#promo a.keskopromo-rules').click(function(e)
	{
		e.preventDefault();
		if($('#promo .rules').css('display') == 'block')
		{
			$('#promo .rules').slideUp('fast');
		} else {
			$('#promo .rules').slideDown('medium');
		}
	});
	
	$('#promo .rules a').click(function(e)
	{
		e.preventDefault();
		$('#promo .rules').slideUp('fast');
	});
	
	$('a.blank').each(function(e)
	{
		$(this).attr('target','_blank');
	});
	
	$('header nav a').click(function(e)
	{
		e.preventDefault();

		scrollTarget = $($(this).attr('href')).position().top;
		$(scrollElement).animate({scrollTop:scrollTarget},700,function()
		{

		})
	});
	
	
	
	$(window).scroll(function()
	{
		windowOffset = $(window).scrollTop();
		
		checkActive(windowOffset);
		
		bodyOffset = $(document).scrollTop();
		
		bgMove = (height-bodyOffset)/25;

		$('body').css('background-position','center -'+Math.ceil(bgMove)+'px');
	});
	
	function checkActive(windowOffset)
	{
		if(windowOffset >= $('#mainokset').position().top - $('#mainokset').attr('data-position'))
		{
			activateSection('#mainokset');
		} else if(windowOffset >= $('#tuotteet').position().top - $('#tuotteet').attr('data-position')) {
			activateSection('#tuotteet');
		} else {
			activateSection('#olohuone');
		}
	}
	
	function activateSection(target)
	{
		if(!$('header nav li a[href="'+target+'"]').hasClass('active'))
		{
			$('header nav li a.active').removeClass('active');
			$('header nav li a[href="'+target+'"]').addClass('active');
		}
	}
	
	/* Slider */
	var sliderBusy = false;
	
	$('.slider-container').each(function()
	{
		checkActiveProduct();
		
		var width = $(this).parent('.tuotteet-slider').width();
		var count = $(this).children('.slider-item').size();
		$(this).css('width',width * count+'px');
		var maxLeft = 0;
		var maxRight = (width * count - width) * -1;
		
		$('.slider-controls a').click(function(e)
		{
			e.preventDefault();
			var slider = $(this).parents('.tuotteet-slider').children('.slider-container');
			var moveAmount = 0;
			var slideTarget = 0;
			if($(this).hasClass('prev'))
			{
				moveAmount = width;
				
				if(slider.position().left + moveAmount > maxLeft)
				{
					slideTarget = maxRight;
				} else {
					slideTarget = slider.position().left + moveAmount;
				}
				
			} else {
				moveAmount = width * -1;
				
				if(slider.position().left + moveAmount < maxRight)
				{
					slideTarget = maxLeft;
				} else {
					slideTarget = slider.position().left + moveAmount;
				}
				
			}
			
			
			goSlider(slider,slideTarget);

		});
	});
	
	$('#tuotteet nav.tuotteet-list a').click(function(e)
	{
		e.preventDefault();	
		var slider = $(this).parents('#tuotteet').find('.slider-container');
		var slideTarget = slider.children($(this).attr('href')).position().left *-1;
		goSlider(slider,slideTarget);
	});
	
	function goSlider(slider,slideTarget,callback)
	{
		if(!sliderBusy)
		{
			sliderBusy = true;
			var slideSpeed = Math.abs(slideTarget - slider.position().left) / 3;
		
			slider.animate(
				{left:slideTarget},slideSpeed,function()
				{
					sliderBusy = false;
					checkActiveProduct();
				});
		}
		
	}
	
	function checkActiveProduct()
	{
		var slider = $('#tuotteet .slider-container');
		var currentSlide = Math.ceil(Math.abs(slider.position().left / slider.parent().width())) +1;

		$('#tuotteet .tuotteet-list a.active').removeClass('active').animate({marginTop:0},100);
		var active = $('#tuotteet .tuotteet-list li:nth-child('+currentSlide+') a');
		active.addClass('active');
		active.animate({marginTop:-7},150);
	}
	
	/* Video */
	$('#mainokset .thumb').click(function(e)
	{
		e.preventDefault();
		var target = $(this).attr('data-target');
		$('#dimmer').fadeIn('medium',function()
		{
			
			$('#lightbox').css('top',$(window).scrollTop() + 160);
			$('#lightbox').fadeIn('fast',function()
			{
				openModal(target);
			});
		});
	});
	
	function openModal(target)
	{
		switch(target)
		{
			case "#mainos-olohuone":
				$('#videoplayer').html('<object width="640" height="360"><param name="movie" value="http://www.youtube.com/v/9GoqeVZwBAo?version=3&amp;hl=fi_FI&amp;rel=0&amp;autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/9GoqeVZwBAo?version=3&amp;hl=fi_FI&amp;rel=0&amp;autoplay=1" type="application/x-shockwave-flash" width="640" height="360" allowscriptaccess="always" allowfullscreen="true"></embed></object>')
					.fadeIn('fast');
				break;
			case "#mainos-brand":
				$('#videoplayer').html('<object width="640" height="360"><param name="movie" value="http://www.youtube.com/v/FADUk_d7IqE?version=3&amp;hl=fi_FI&amp;rel=0&amp;autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/FADUk_d7IqE?version=3&amp;hl=fi_FI&amp;rel=0&amp;autoplay=1" type="application/x-shockwave-flash" width="640" height="360" allowscriptaccess="always" allowfullscreen="true"></embed></object>')
					.fadeIn('fast');
				break;
		}
	}
	function closeModal(callBack)
	{
		$('#videoplayer').fadeOut('fast').html('');
		$('#lightbox').hide();
		$('#dimmer').fadeOut('fast');
	}
	$('.closeModal,#dimmer').click(function(e)
	{
		e.preventDefault();
		if($('.modal').css('display') == 'block')
		{
			closeModal();
		}
	});
	
	$('#agecheck .text').focus(function()
	{
		if($(this).val() == $(this).attr('data-default')) $(this).val('');
	});
	
	$('#agecheck .text').blur(function()
	{
		if($(this).val() == '') $(this).val($(this).attr('data-default'));
	});
	
	$('#agecheck .text').keyup(function()
	{
		if($(this).val().length >= $(this).attr('maxlength'))
		{
			if($(this).attr('maxlength') == 4)
			{
				$(this).parent('form').submit();
			} else {
				$(this).next('input').focus()
			}
		}
	});
	
	$('#agecheck form').submit(function(e)
	{
		var error = false;
		$('.text',this).each(function()
		{
			if($(this).val() == '' || $(this).val() == $(this).attr('data-default')) {
				error = true
			}
		});
		if(error) e.preventDefault();
	});
	
});
