$(function(){
	
	
	$('#questionPages').hide();
	
	$('#Play').click(function() {
	 	setTimeout(function() {  
			$('#startPage').fadeOut('slow');
		},300);
		setTimeout(function() {  
			$('#questionPages').fadeIn('slow');
			$('#pelco-logo').fadeIn('slow');
		},900);
	});	
	
	var currentPage = 0;
	var sliderPages = 0;
	
	$('#slider').anythingSlider({
		theme: "alkuviisas",
		buildStartStop      : false,
		enableKeyboard      : false,
		hashTags            : false,
		infiniteSlides      : false,
		stopAtEnd           : true,
		delayBeforeAnimate  : 600,
		enableNavigation    : false,
		onSlideBegin : function(e, slider) {
			//alert ("test");
		}, onSlideComplete : function(slider){
 			 $('#slider li.activePage').find('.answers').fadeIn();
			 currentPage = slider.currentPage;
			 sliderPages = slider.pages;
 		}
		
	}).anythingSliderFx({
	   '.fade' : [ 'fade' ],
	   inFx : {
		'.question h3'  : { opacity: 1, top  : 0, duration: 400, easing : 'easeOutBounce' },
		'.textSlide img' : { opacity: 1, duration: 400 },
		'.animoitu'      : { top : '50px', duration: 400, easing : 'easeOutElastic' },
		'.expand'        : { width: '100%', top: '0%', left: '0%', duration: 400, easing : 'easeOutBounce' }
	   },
	   outFx : {
		'.question h3'      : { opacity: 0, top  : '-100px', duration: 400 },
		'.textSlide li:odd'  : { opacity: 0, left : '-200px', duration: 350 },
		'.textSlide li:even' : { opacity: 0, left : '200px',  duration: 350 },
		'.textSlide img'     : { opacity: 0, duration: 350 },
		'.quoteSlide:first'  : { top : '-500px', duration: 350 },
		'.quoteSlide:last'   : { top : '500px', duration: 350 },
		'.expand'            : { width: '10%', top: '50%', left: '50%', duration: 350 },
		'.animoitu'      : { top : '-500px', duration: 400, easing : 'easeOutElastic' }
	   }
	  });

	// Asetataan vastaukset klikattaviksi
	$('.slider-content').each(function(i) {
		$(this).find('ul.answer-set li').each(function (n) {
			$(this).click(function() {
			  $("#slider li.activePage").find('ul.answer-set li').each(function (i) {	
			  		if ($(this).hasClass('selectedValue')) {
						$(this).removeClass('selectedValue');
						$(this).find('div.selectBox').removeClass('selected');
					}
			  });
			  $(this).addClass('selectedValue');
			  $("#slider li.activePage").find('input[type=hidden]').val(n+1);
			  setAnswerSelected();
			});
			$(this).hover(
			  function () {
				$(this).find('div.selectBox').addClass('selected-hover');
				$(this).css('cursor','pointer');
			  }, 
			  function () {
				$(this).find('div.selectBox').removeClass('selected-hover');
			  }
			);
			
		});
	});

	function setAnswerSelected() {
		$("#slider li.activePage").find('ul.answer-set li').each(function (i) {
			if ($(this).hasClass('selectedValue')) {
				$(this).find('div.selectBox').addClass('selected');
				$(this).fadeTo('slow', 1);
			} else {
				$(this).fadeTo('slow', 0.4);
			}
		});
		nextSlide = currentPage+1;
		if (nextSlide > sliderPages) {
			setTimeout(function() {  
				$('#questionPages').fadeOut('slow');
			},1200);
			setTimeout(function() {  
				$('#endPage').fadeIn('slow');
				showEndPage();
			},2000);
		}
		else {
			setTimeout(function() { $('#slider').anythingSlider(nextSlide); },800);
		}
	}
	
	$('#show-rulesBox').hide();
	$('#show-rules').click(function() {
		$('#show-rulesBox').fadeIn();
	});
	
	$('#close-rules').click(function() {
		$('#show-rulesBox').fadeOut();
	});
	
	
	
	function showEndPage () {
		a = 0; b = 0; c=0;
		$('#slider').find('input[type=hidden]').each(function(i) {
			if ($(this).val()==1) { a++; }
			if ($(this).val()==2) { b++; }
			if ($(this).val()==3) { c++; }
		});
		if (a>=b && a>=c) { $('#a-1').show(); } 
		else if (b>a && b>c) { $('#b-1').show();  }
		else { $('#c-1').show(); }
	}
	
	$('#btn-submit').click(function() {           
		$(".error").hide();        
		var hasError = false;         
		var emailaddressVal = $("#nimi").val();        
		if(emailaddressVal == '') {            
			$("#nimi").after('<span class="error" style="color: #cd3b35;">Syötä nimesi</span>');            
			hasError = true;        
		}
		
		var emailaddressVal = $("#yritys").val();        
		if(emailaddressVal == '') {            
			$("#yritys").after('<span class="error" style="color: #cd3b35;">Syötä yrityksesi nimi</span>');            
			hasError = true;        
		}
		
		var emailaddressVal = $("#sahkoposti").val();        
		if(emailaddressVal == '') {            
			$("#sahkoposti").after('<span class="error" style="color: #cd3b35;">Syötä email-osoitteesi</span>');            
			hasError = true;        
		} 
		
		var emailaddressVal = $("#puhelin").val();        
		if(emailaddressVal == '') {            
			$("#puhelin").after('<span class="error" style="color: #cd3b35;">Syötä puhelinnumerosi</span>');            
			hasError = true;        
		}
		if(hasError == true) { return false; }     
	})
});
