// Tuotevalintacheckbox 
	
	var kriteerit = { 
	
		"tuoteryhma": [ ],
		"energiataso": [ ],
		"ikakausi": [ ],
		"sensitive": [ ]
		
	};
	

$(document).ready(function() {
	
	$("#tweet li").css({ 'display': 'none' });
	
	$("#yhteystiedot_laheta").click(function() {	
		
		var virhe = Array();
		
		i = 0;
		
		viesti = "";
		
		if($("#nimi").val() == "" || $("#nimi").val() == $("#nimi").attr('placeholder')) {
			
			virhe[i] = "nimi";
			i++;
			
		} 
		
		if($("#spostiyhteys").val() == "" || $("#spostiyhteys").val() == $("#spostiyhteys").attr('placeholder')) {
			
			virhe[i] = "sähköposti";
			i++;
			
		} 	
		
		if($("#viesti").val() == "" || $("#viesti").val() == $("#viesti").attr('placeholder')) {
			
			virhe[i] = "viesti";
			i++;
			
		}
	
		for(j=0;j<virhe.length;j++) {
		
			if(j == 0) {
				viesti += virhe[j];
			} else {
				viesti += ", " + virhe[j];
			}
		}
		
		if(virhe.length < 1) {
		
			/*var osallistumistiedot = {
				
				lomake: "palautelomake",
				nimi: $("#nimi").val(),
				osoite: $("#osoite").val(),
				puhnum: $("#puhnum").val(),
				sposti: $("$spostiyhteys").val(),
				viesti: $("#viesti").val(),
				jasentunnus: $("#jasentunnus").val()
					
			}*/
		
			$('#palautelomake').submit();
			
			
		} else {
		
			alert("Täytä vielä ainakin " + viesti);
			
		}
		
		
	
	});
	
	//aktivoidaan lomakkeiden placeholderit
	
	$('input').focus(function(){ 
		if($(this).val() == $(this).attr('placeholder')){
			$(this).val('');
		}
		
		$(this).css({ 'color': '#865C2D' });
		
	});
	  
	$('input').blur(function(){
		if($(this).val() == ''){
		  $(this).val($(this).attr('placeholder')).css({ 'color': '#af9a83' });;
		}
	});
	
	$('input').blur();
	
	$('textarea').focus(function(){ 
		if($(this).val() == $(this).attr('placeholder')){
			$(this).val('');
		}
		
		$(this).css({ 'color': '#865C2D' });
		
	});
	  
	$('textarea').blur(function(){
		if($(this).val() == '') {
			$(this).val($(this).attr('placeholder')).css({ 'color': '#af9a83' });
		} 
	});
	
	$('textarea').blur();
	
	/*$('#palautelomake').submit(function() {
	  alert('WTF');
	  return false;
	});*/
	
	
		
	
	if(ie == 0) {
	
		$("nav li img").css({ 'opacity': '0', 'display': 'block' });
		$(".checkbox img").css({ 'opacity': '0', 'display': 'block' });
	}
	
	// Päänavigaation alaviiva
	
	 $("nav li").hover(

		  function () {
			  
			if(!$(this).hasClass('valittu')) {  
			
				if(ie == "") {
					
					$(this).find("img").stop();
					
					$(this).find("img").animate({ 
					
						opacity: 1
	
					}, 100 );
					
				} else {
					
					$(this).find("img").css({ 'display': 'block' });
						
				}
				
			}
		  }, 
		  
		  function () {
			  
			$(this).find("img").stop();
			
			if(ie == "") {
				
				$(this).find("img").animate({ 
				
					opacity: 0
					
				}, 270 );
				
			} else {
				
				$(this).find("img").css({ 'display': 'none' });	
			}
			
		  }

    );
	
	
	// Hooverboksien kuvafeidaus
	
	$(".hooveri").hover(
	
		function () {
			$(this).find("img").stop();
			$(this).find("img").animate({ 
				opacity: 0
			}, 100 );
				
		},

		function () {
	
			$(this).find("img").stop();
			$(this).find("img").animate({ 
				opacity: 1
			}, 270);
		  }	
	
	);
	
	
	
	$(".checkbox").live('click', function() {


	var ryhma = $(this).data("ryhma"); 
		
		if(kriteerit[ryhma].length == 0) {
		
			pituus = 0;
			
		} else {
		
			pituus = (kriteerit[ryhma].length);
			
		}
		
		if($(this).find('img').data("valittu") == "ei") {  // jos kyseistä kriteeriä ei ole valittu
			
			kriteerit[ryhma][pituus] = $(this).data("kriteeri");
			
			if(ie == 0) {
				$(this).find('img').animate({ opacity: 1 }, 200).data("valittu", "kylla"); 
			} else {
				$(this).find('img').css({ 'display': 'block' }).data("valittu", "kylla");
			}	
								
		} else { // Jos kyseinen kriteeri on valittu
		
			for(var i = 0; i <= pituus; i++) {
				
				if(kriteerit[ryhma][i] == $(this).data("kriteeri")) {
				
					delete kriteerit[ryhma][i];
					
				}
				
			}
			
			if(ie == 0) {	
				$(this).find('img').animate({ opacity: 0 }, 200).data("valittu", "ei"); 
			} else {
				$(this).find('img').css({ 'display': 'none'	}).data("valittu", "ei");
			}
		}
		
		//alert( kriteerit["tuoteryhma"][0] );
		//var tbl = prettyPrint( kriteerit );

		//$("#asdasdasd").append(tbl);
		
		piilota_tuotteet();
	});
	
	$("#tilaa").live('click', function() {
		
		if($("#sposti").val() != "") {
			
			var data = {
				
				uutiskirje: "tilaa",
				jasentunnus: $("#jasentunnus").val(),
				sposti: $("#sposti").val()
					
			};
			
			$.ajax({  
			
				type: "POST",  
				url: "index.php",  
				data: data,  
				success: function() {  
							
					$("#tilaa").fadeOut("slow");
					
					$("#uutiskirje_teksti").fadeOut("slow");
					
					$("#uutiskirje_tekstialue").fadeOut("slow").queue(function() { $("#uutiskirje_tekstialue").replaceWith("<h2 style='margin-top: 15px;'> Kiitos tilauksesta! </h2>"); });
				
				}
				  
			});				
			
		} else {
		
			alert("Täytä sähköpostiosoite!");
			
		}
		
	});	
	
	
});

function piilota_tuotteet(){
	
	var alertti = "kriteerit:   ";
	
	var tuoteryhmat = Array();
	
	tuoteryhmat = kriteerit["tuoteryhma"];
	energiataso = kriteerit["energiataso"];
	ikakausi = kriteerit["ikakausi"];
	sensitive = kriteerit["sensitive"];
	
	var tuoteryhma_act = false;
	var energiataso_act = false;
	var ikakausi_act = false;
	var sensitive_act = false;
	var sensitive_maara = 0;
	
	$.each( tuoteryhmat, function(key, value) { 
	  if( value ) tuoteryhma_act = true;
	});
	
	$.each( energiataso, function(key, value) { 
	  if( value ) energiataso_act = true;
	});
	
	$.each( ikakausi, function(key, value) { 
	  if( value ) ikakausi_act = true;
	});
	
	$.each( sensitive, function(key, value) { 
	  if( value ) {
		  sensitive_act = true;
		  sensitive_maara++;
	  }
	});
	
	if( tuoteryhma_act ){
		$('.tuote').each(function(index) {
			
			$(this).css('visibility','hidden');
			$(this).css('width','0px');
			$(this).css('height','0px');
			$(this).css('margin','0px');
			
			var ok = false;
			
			var cat = $(this).attr("data-category");
			
			$.each( tuoteryhmat, function(key, value) { 
				if( cat == value ) ok = true;
			});
			
			if( ok )  {
				$(this).css('visibility','visible');
				$(this).css('width','205px');
				$(this).css('height','185px');
				$(this).css('margin','0 10px 40px 10px');
			}
			
		});
	}
	else{
		$('.tuote').each(function(index) {
			$(this).css('visibility','visible');
			$(this).css('width','205px');
			$(this).css('height','185px');
			$(this).css('margin','0 10px 40px 10px');
		});
	}
	
	if( energiataso_act ){
		
		var osumat_array = Array();
		
		$('.tuote').each(function(index) {
			var ominaisuudet_data = $(this).attr("data-attributes");
			var ominaisuudet = ominaisuudet_data.split(",");
			
			var osumat = 0;
			
			$.each( energiataso, function(key1, value1) { 
				$.each( ominaisuudet, function(key2, value2) { 
					if(value1 == value2) osumat++;
				});
			});
			
			if( osumat == 0 ) {
				$(this).css('visibility','hidden');
				$(this).css('width','0px');
				$(this).css('height','0px');
				$(this).css('margin','0px');
			}
	
		});
		
	}
	
	if( ikakausi_act ){
		
		var osumat_array = Array();
		
		$('.tuote').each(function(index) {
			var ominaisuudet_data = $(this).attr("data-attributes");
			var ominaisuudet = ominaisuudet_data.split(",");
			
			var osumat = 0;
			
			$.each( ikakausi, function(key1, value1) { 
				$.each( ominaisuudet, function(key2, value2) { 
					if(value1 == value2) osumat++;
				});
			});
			
			if( osumat == 0 ) {
				$(this).css('visibility','hidden');
				$(this).css('width','0px');
				$(this).css('height','0px');
				$(this).css('margin','0px');
			}
	
		});
		
	}
	
	if( sensitive_act ){
		
		var osumat_array = Array();
		
		$('.tuote').each(function(index) {
			var ominaisuudet_data = $(this).attr("data-attributes");
			var ominaisuudet = ominaisuudet_data.split(",");
			
			var osumat = 0;
			
			$.each( sensitive, function(key1, value1) { 
				$.each( ominaisuudet, function(key2, value2) { 
					if(value1 == value2) osumat++;
				});
			});
			
			if( osumat < sensitive_maara ) {
				$(this).css('visibility','hidden');
				$(this).css('width','0px');
				$(this).css('height','0px');
				$(this).css('margin','0px');
			}
	
		});
		
	}
	
}
