var my_time = 600;
var my_check = false;

$(document).ready(function(){
	$('.banner-area, .banner-box').show();
	
	$('A.years-link').click(function(){
		var obj = $(this).parents('.arch-years').find('.arch-month').eq(0);
		if (obj.hasClass('show-y')) {
			obj.slideUp(my_time).removeClass('show-y');
			$(this).parents('.arch-years').find('.show').slideUp(my_time);
		} else {
			obj.slideDown(my_time).addClass('show-y');
		}
		return false;
	});
	$('H5.months A').click(function(){
		var obj = $(this).parents('.arch-years').find('.'+$(this).attr('rel')).eq(0);
		if (obj.hasClass('show')) {
			return false;
		} else {
			if ($(this).parents('.arch-years').find('.show').length) {
			$(this).parents('.arch-years').find('.show').removeClass('show').slideUp(my_time, function(){
				obj.slideDown(my_time).addClass('show');
			});
			} else {	
				obj.slideDown(my_time).addClass('show');
			}
			$(this).parents('.arch-years').find('.selected').removeClass('selected');
			$(this).addClass('selected');
		}
		return false;
	});
	

	$('FORM#comment_form INPUT.form-button').click(function(e){
		
		if ($(this).parents('FORM#comment_form').find('TEXTAREA').val() == '') {
			alert('that was blank...');
			return false;
		}
		var str = $(this).parents('FORM#comment_form').find('TEXTAREA').val();
		if (!testWords(str, 35)) { 
			alert('please use shorter words');
			return false;
		}
		
		if ($(this).parents('FORM#comment_form').find('INPUT#terms:checked').length > 0) {
		} else {
			alert('To comment, you must agree with our "User Agreement and Terms of Use"');
			return false;
		}
		
		var val_captcha = $('INPUT[name=captcha]').val();
		if (val_captcha == '') {
			alert('Just above the "submit" button, on the left, there is a little box with a secret code.  Please enter it in the box on the right.');
			return false;
		} else {
			$('#cpch').load('http://rorotoko.com/check.php?cpch='+val_captcha, function(){
				if 	($('#cpch').html() == 'wrong') {
					alert('please retry entering the secret code');
					return false;
				} else {
					my_check = true;
					var $formId = $(this).parents('FORM#comment_form');
					var formAction = $formId.attr('action');
					$.post(formAction, $formId.serialize(),function(data){
						$('#cpch').html('Thank you for sharing your thoughts!').css({padding: '5px', border: '1px solid green', marginTop: '20px'}).slideDown(600);
					});
					
				}
			});
		}
		e.preventDefault();
		
	});
	
	$('.search-form FORM').submit(function(e){
		e.preventDefault();
		return false;
	});
	
	if ($.browser.msie && parseInt($.browser.version) < 9) {
		$('.search-form FORM #keywords').keyup(function(e){   // ie6, ie7, ie8
			var keycode =  e.keyCode ? e.keyCode : e.which;
			SubmitSearch($(this), keycode);
		});
	} else {
		$('.search-form FORM #keywords').keydown(function(e){
			var keycode =  e.keyCode ? e.keyCode : e.which;
			SubmitSearch($(this), keycode);
		});
	}
});	

function SubmitSearch(obj, charkey){
	if (charkey === 13 || charkey === 10) {
		if (obj.val() == '') {
			alert('What are you looking for?');
			return false;
		}
		var formData = obj.serialize();
		$.ajax({
			type: 'POST',
			url: "http://rorotoko.com/check-time.php",
			data: formData, 
			success: function(responseData){
				if (responseData == 'time_error') {
					alert('please give me a minute');
					return false;
				} else {
					if (responseData == 'badwords_error') {
						alert('you would find that almost anywhere...');
						return false;
					} else {
						$('.search-form FORM').unbind('submit');
						$('.search-form FORM').submit();
						return true;
					}
				}
			}
		});
	}
}

function testWords(str, cnt){	
	str = str.split(' ');
	var i = 0;
	var ok = true;
	for (i=0; i<str.length; i++){
		if (str[i].length > cnt) ok = false;
	}
	return ok;
}


