// JavaScript Document

$(document).ready(
	function()
	{
		
		$(document).pngFix();
		// PNG24 Fix korttikuvalle
		$('div#headkortti').pngFix();
		
		
		// Lomakepainikkeen tekstin poisto
/*		$('input.btn').each(
			function() {
				$(this).attr('value','');
			}
		)*/

		// Tutkitaan näytetäänkö kaikki kappaleet
		if($('#contnavi').attr('class') == 'showall') {
			var showall = true;	
		} else {
			var showall = false;	
		}
		
		
		$('#nav a').each(
			function(index) {
				if(location.pathname.lastIndexOf($(this).attr('href')) != -1) {
					$(this).attr('class','sel');
				} else {
					$(this).attr('class','');	
				}
			}

		)
		
		if($('#nav a.sel').length == 0) {
			$('#nav_etu a').attr('class','sel');
		}	

		
		if($('#content div.p').length > 1) {
			$('#contnavi').append('<ul><li><a href="' + location.pathname + '#container" id="showall">Näytä kaikki</a></li></ul>');
		}
		
		$('#content div.p').each(
			function(index) {
				$(this).attr('id','p'+index);
				if(index > 0 && !showall) {
					$(this).css('display','none');
				}
				$('#contnavi ul').append('<li><a href="' + location.pathname + '#p' + index + '" id="l' + index + '">' + $(this).children().eq(0).text() + '</a></li>');
				setCMenu($('#l'+index));
			}
		
		)
		
		if(location.hash && location.hash != '#container') {
			var target_id = location.hash.substring(1,location.hash.length);		
			$('#content div.p').each(
				function() {
					if($(this).attr('id') != target_id) {
						$(this).css('display','none');
					} else {
						$(this).fadeIn('slow');
					}
				}
						
			)			
			
		}
		
		$("#showall").bind(
			'click',
			function() {
				if($(this).attr('class') != 'p') {
					$(this).text('Näytä kappalettain');
					$(this).attr('class','p');
					$('#content div.p').each(
						function(index) {
							$('#l'+index).unbind('click');
							$(this).fadeIn("slow");	
						}
					)					
				} else {
					$(this).text('Näytä kaikki');
					$(this).removeAttr('class');
					$('#content div.p').each(
						function(index) {
							setCMenu($('#l'+index));
							if(index > 0) {
								$(this).css('display','none');
							}
						}
					
					)
				}
				
				
			}
		);
		
		
		$('#ccontainer').slideDown("slow");	
		
		
		$('#club_submit').bind(
			'click',
			function() {
				var error = false;
				$('#club_form input.r').each(
					function() {
						if(!$(this).attr('value')) {
							$(this).css('background-color','#eee');
							error = true;
						} else if($(this).attr('value').length < 1) {
							$(this).css('background-color','#eee');
							error = true;
						}
					}							 
				)
				if(!error) {
					$('#club_form').submit();
				} else {
					alert('Täytä kaikki pakolliset tiedot!');	
				}
			}					   
		)
		
		$('#select_form input').bind(
			'click',
			function() {
				if($(this).attr('value') == 2) {
					$('#club_form').css('display','block');
					$('#club_data').css('display','none');	
				} else {
					$('#club_form').css('display','none');
					$('#club_data').css('display','block');					
				}	
			}
		)
		
		$("#sendkform").bind(
			'click',
			function() {
				var error = '';
				var e = Array();
				var f = Array('fname','lname','email','pname','userfile');
				
				for(x=0;x<f.length;x++) {
					if(document.getElementById(f[x]).value.length == 0) {
						e.push(f[x]);
					} else {
						if(f[x] == 'email') {
							if(!checkEmail(document.getElementById(f[x]).value)) e.push(f[x]);
						} 						
					}
				}


				if($('#userfile').attr('value')) {
					var uf_length = $('#userfile').attr('value').length;
					var ext = $('#userfile').attr('value').substring(uf_length-3,uf_length).toLowerCase();
					if(ext != 'jpg') {
						e.push('userfile');
						error += '- Kuvan tulee olla JPG-muotoa.'+'\n';	
					}
				}
				
				if(!$('#terms').attr('checked')) {
					e.push('terms');
					//error += '- Sinun tulee hyväksyä säännöt!'+'\n'; 
				}
				
				
				if(e.length > 0) {
					//resetForm(f);
					for(x=0;x<e.length;x++) {
						//$('#'+e[x]).css('border-color','#900');
						//$('#'+e[x]).css('background-color','#f3f3f3');
						error += '- ' + $('#'+e[x]).attr('title') + '\n';
					}
					if(error != '') {
						error = 'Tarkista seuraavat kohdat:'+'\n\n' + error+'\n\n';
						alert(error);	
					}
				} else {
					$("#kheader").text('Kuvaa lähetetään...');
					$("#kformdiv").fadeOut("slow",
						function() {
							$("#kform").submit();		
						}
					)
					
				}
				
			}				
		);
		
		$("#camp_submit").bind(
			'click',
			function() {
				var error = '';
				var e = Array();
				var f = Array('fname','lname','email');
				
				for(x=0;x<f.length;x++) {
					if(document.getElementById(f[x]).value.length == 0) {
						e.push(f[x]);
					} else {
						if(f[x] == 'email') {
							if(!checkEmail(document.getElementById(f[x]).value)) e.push(f[x]);
						} 						
					}
				}
				
				if($('#kilpailu').attr('checked') || $('#contactme').attr('checked') || $('#newsletter').attr('checked')) {
				} else {
					e.push('subjects');
				}
				
				
				if(e.length > 0) {
					for(x=0;x<e.length;x++) {
						error += '- ' + $('#'+e[x]).attr('title') + '\n';
					}
					if(error != '') {
						error = 'Tarkista seuraavat kohdat:'+'\n\n' + error+'\n\n';
						alert(error);	
					}
				} else {
					$("#camp_form").fadeOut("slow",
						function() {
							$("#camp_form").submit();		
						}
					)	
				}
				
			}				
		);
		
		
	}		
);

function setCMenu(jQ) {
	jQ.bind(
		'click',
		function() {

			var target_id = $(this).attr('href').split('#')[1];
					
			$('#content div.p').each(
				function() {
					if($(this).attr('id') != target_id) {
						$(this).css('display','none');
					} else {
						$(this).fadeIn('slow');
					}
				}
						
			)
		}
	);
}
function resetForm(e) {
	for(x=0;x<e.length;x++) {
		$('#'+e[x]).css('border-color','#ccc');
		$('#'+e[x]).css('background-color','#ffffff');
	}	
}

function checkEmail(inputvalue) {	
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(inputvalue)){         
		return true;   
    } else {   
		return false; 
    }
}
