addLoadEvent(init);

function init(){	
	
	// Text Controls
	var td = get('text_decrease');
	var tr = get('text_reset');
	var ti = get('text_increase');

	if(td){
		fontSizeInit();
		td.onclick = function(){ fontSize(0); return false; }
		tr.onclick = function(){ fontSize(2); return false; }
		ti.onclick = function(){ fontSize(1); return false; }
	}
	
	contact_form = get('contact_form');
	if(contact_form){
		
		
		$('#contact_submit').click(function(){ 
			
			if( ! $('#contact_name').val() ){ $('#contact_name_alert').show(); error1 = true; } else { $('#contact_name_alert').hide(); error1 = false; }
			if( ! $('#contact_email').val() ){ $('#contact_email_alert').show(); error2 = true; } else { $('#contact_email_alert').hide(); error2 = false; }
			if( ! $('#contact_phone').val() ){ $('#contact_phone_alert').show(); error3 = true; } else { $('#contact_phone_alert').hide(); error3 = false; }
			if( ! $('#contact_message').val() ){ $('#contact_message_alert').show(); error4 = true; } else { $('#contact_message_alert').hide(); error4 = false; }
			if( (!$('#contact_check_human').attr("checked") ) && (!$('#contact_check_robot').attr("checked") ) ){ $('#contact_check_alert').show(); error5 = true; } 
			else { $('#contact_check_alert').hide(); error5 = false; }
			if($('#contact_check_robot').attr("checked")){ $('#contact_robot_alert').show(); error6 = true; }
			else { $('#contact_robot_alert').hide(); error6 = false; }
						
			
			if(error1 + error2 + error3 + error4 + error5 + error6 == 0){
				
				//check email
				 $.ajax({
					type: "POST",
					url: "actions/validate_email.php",
					data: "email="+$('#contact_email').val()+"",
					success: function(msg){
												
						if(msg == 'yes'){
							$('#contact_email_invalid_alert').hide();
							
							sendMessage($('#contact_name').val(), $('#contact_email').val(), $('#contact_phone').val(), $('#contact_message').val());
							
						}
						else {
							$('#contact_email_invalid_alert').show();
						}
					},
					error: function(){
						alert('There was an error checking your email address. Please try again.');
					}
				});
				
			}
			
			return false;

		});
		
	}
}


function sendMessage(name, email, phone, message){
	//check email
	 $.ajax({
		type: "POST",
		url: "actions/share_this.php",
		data: "from_the_right_place=true&name="+name+"&email="+email+"&phone="+phone+"&message="+message+"",
		success: function(msg){
			$('#contact_success_alert').show();
			$('#contact_form_content').hide();
		},
		error: function(){
			alert('There was an error sending your message. Please try again.');
		}
	});	
}

function fontSizeInit(s){
	var s = readCookie('show-lol-fontSize');
	if(s){
		var body = get('body');
		var textsize = get('textsize');
		body.style.fontSize = s+'%';
		textsize.innerHTML = s+'%';	
	}
}

function fontSize(dir) {
	var body = get('body');
	var textsize = get('textsize');
	if(body.style.fontSize) {
		var s = parseInt(body.style.fontSize.replace('%',''));
	} else {
		var s = 100;
	}
      
	if(dir == 2){
		s = 100;
	}
	else {
		if(dir){ s +=10; }
		else{ s -=10; }
	}
	
	if(s<160 && s>70){
		body.style.fontSize = s+'%';
		textsize.innerHTML = s+'%';
		createCookie('show-lol-fontSize', ''+s+'', 10);
	}
}


