/* ------ start - document.ready ------ */
$(document).ready(function() {

	$(".opacity").fadeTo(0,0.75);
	
	$(".opacity").hover(
		function() {
			$(this).fadeTo(0,1.0);
		},
		function() {
			$(this).fadeTo(0,0.75);
		}
	);	

	if ($(".faktaboksi").length > 0) {
		if ($(".faktaboksi").find("p").not(":first").length > 0) {
			$(".faktaboksi").find("p").not(":first").css("margin-top","10px");
		}
	}

	// remove this when project is goin live
	$("#debug table tr").find("table tr:odd").css("background","#fff");
	$("#debug table tr").find("table tr:even").css("background","#e4e4e4");

	$('a.video_popup').click(function(){
		var link = this.href;
		window.open(link,"mywindow", "menubar=1,resizable=1,width=500,height=380");
		return false;
	});
	

	$('a.new-window').click(function(){
		window.open(this.href);
		return false;
	});
	
	
	
	// nuorisofoorumi competition form items
	
	var firstKlik=0;
	
	$('#nuorisoform .answerItem .radio-0').attr('checked', false);

	$("#nuorisoform .messageTextWrapper3000Chars textarea").charCounter(3000, {
		container: ".letterCount3000",
		format: "%1"
	});	
	$("#nuorisoform .messageTextWrapper3000Chars label span.infoText").text("Kopioi tekstisi tekstinkäsittelyohjelmasta ja liitä tähän:");
			
	$("#nuorisoform .messageTextWrapper500Chars textarea").charCounter(500, {
		container: ".letterCount500",
		format: "%1"
	});
	$("#nuorisoform .messageTextWrapper label span.infoText").text("Perustelut:");
			
	
	$("#nuorisoform .answerItem .radio-0").click(function(){
		if (firstKlik==0) {
			$("#nuorisoform .messageTextWrapper3000Chars").toggle('fast');
			firstKlik=1;
		}

		if($("#nuorisoform .messageTextWrapper500Chars").is(':visible')) {
			$("#nuorisoform .messageTextWrapper500Chars").toggle('fast');
			$("#nuorisoform .messageTextWrapper3000Chars").toggle('fast');
			$("#nuorisoform .messageTextWrapper500Chars").removeClass("required");
			$("#nuorisoform .messageTextWrapper3000Chars").addClass("required");
		}
	})
	
	$("#nuorisoform .answerItem .radio-1").click(function(){
		if (firstKlik==0) {
			$("#nuorisoform .messageTextWrapper500Chars").toggle('fast');
			firstKlik=1;
		}
		
		if($("#nuorisoform .messageTextWrapper3000Chars").is(':visible')) {
			$("#nuorisoform .messageTextWrapper3000Chars").toggle('fast');
			$("#nuorisoform .messageTextWrapper500Chars").toggle('fast');
			$("#nuorisoform .messageTextWrapper3000Chars").removeClass("required");
			$("#nuorisoform .messageTextWrapper500Chars").addClass("required");
		}
	})
	
	$("#nuorisoform").submit(function(){
		var monta=0;
		var is_ok = true;
		
		$("#nuorisoform .required input").each(function(){
			if($(this).val() == ''){
				
				is_ok = false;
			}
		});
		
		
		if(is_ok){
			return true;
		} else {
			alert(compErrorMsg);
			return false;	
		}
		
	})
	
	// end nuorisofoorumi form

	if($("#opoNewQuestion").length>0) {
		$("form#opoNewQuestion").submit(function() {
			var error=0;
			var html = '';
			if($("#name").val().length==0) {
				html += '<span class="name"></span><strong>Nimi/nimimerkki</strong>: Tieto puuttuu.<br /></span>';
			}

			if($("#subject").val().length==0) {
				html += '<span class="subject"></span><strong>Otsikko</strong>: Tieto puuttuu.<br /></span>';
			}

			if($("#email").val().length>0){
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   				if(reg.test($("#email").val()) == false) {
      				html += '<span class="subject"></span><strong>Sähköposti</strong>: Virheellinen<br /></span>';
   }

			}

			if($("#educationLevel").val().length==0) {
				html += '<span class="educationLevel"></span><strong>Tämänhetkinen koulutaso </strong>: Tieto puuttuu.<br /></span>';
			}

			if($("#county").val().length==0) {
				html += '<span class="county"></span><strong>Maakunta</strong>: Tieto puuttuu.<br /></span>';
			}

			if($("#message").val().length==0) {
				html += '<span class="message"></span><strong>Viesti</strong>: Tieto puuttuu.<br /></span>';
			}

			if(!$('#terms').attr('checked')) {
				html += '<span class="terms"></span><strong>Hyväksyn keskustelusäännöt</strong>: Tieto puuttuu.<br /></span>';
			}
			
			if(html!='') {
				$("p.errorMessages").html(html);
				$("p.success").hide();
				$("p.errorMessages").show();
				return false;
			} else {
				return true;
			}
		});
		
		$('form#opoNewQuestion select#educationLevelSelect').change(function() {
 			if($("#educationLevel").length>0) {
 				$("#educationLevel").val($(this).val());
 			
 				if($("#educationLevel").val()=="Jokin muu, mikä?") {
 					$("#educationLevelOther").show();
 				}
 				else {
 					$("#educationLevelOther").hide();
 					$("#educationLevelOther").val("");
 				}
 			}

 			if($("#ezcoa-480_educationlevel").length>0) {
 				$("#ezcoa-480_educationlevel").val($(this).val());
 			}
 			
 			
		});

		$('form#opoNewQuestion select#countySelect').change(function() {
 			if($("#county").length>0) {
 				$("#county").val($(this).val());
 			}

 			if($("#ezcoa-481_questioncounty").length>0) {
 				$("#ezcoa-481_questioncounty").val($(this).val());
  			}
		});
	}
	
	


});
/* ------- end - document.ready ------- */

$(window).load(function() {	
	$(".promo_lappu p a").fitToBackgroundImage();
});

