// blog.js

var msg_noname = 'Please enter your name.';
var msg_noemail = 'Please enter a valid email address.';
var msg_nocomment = 'You must have something to say.';
var total_comments;
var accordion;

// show & hide comments
window.addEvent('domready', function() {
	// comments togglers
	$$('.toggler').addEvent('click', function() {
		
		var id = this.id.split('toggler_').join('');
		var togglee = 'togglee_'+id;
		var view_comments = 'view_comments_'+id;
		
		if ($(togglee).hasClass('hidden')) {
			var style = new Fx.Style(togglee, 'opacity', {
				onStart: function() {
					$(view_comments).setStyle('display', 'none');
					$(togglee).setStyle('opacity', 0); // only a problem in ie
					$(togglee).setStyle('display', 'block');
					$(togglee).removeClass('hidden');
				},
				onComplete: function() {
					if (window.webkit) { // safari is messing the numbers up
						$$('.number').setStyle('display', 'block');
					}
				}
			}).start(0, 1);
		
		} else {
			var style = new Fx.Style(togglee, 'opacity', {
				onStart: function() {
					$(view_comments).setStyle('display', 'inline');
					if (window.webkit) { // safari is messing the numbers up
						$$('.number').setStyle('display', 'none');
					}
				},
				onComplete: function() {
					$(togglee).setStyle('display', 'none');
					$(togglee).addClass('hidden');
				}
			}).start(1, 0);
		}
	});
	
	// fix ie comment placement
	if (window.ie) {
		//$$('.number').setStyle('bottom', '1.6em');
		$$('fieldset.comments div.repeater').setStyles({
			'overflow': 'hidden'
		});
		$$('fieldset.comments').setStyles({
			'padding-top': '1em'
		});
	}
	
	// fix comment number treatment
	if (!window.webkit) { // show comment numbers
		$$('.number').setStyle('display', 'block');
	} else { // position comment numbers
		$$('.number').setStyle('margin-bottom', '-1.3em');
	}
	
	// commenting form behavior
	$$('form.comment_form #name').addEvent('focus', function() {
		if (this.getValue() == 'Name *') {
			this.value = '';
		}
		this.removeClass('dormant');
	});
	$$('form.comment_form #name').addEvent('blur', function() {
		if (!this.getValue()) {
			this.value = 'Name *';
			this.addClass('dormant');
		}
	});
	//element.onkeyup = function(e) { // ie doesn't like this
	//	var unicode = e.charCode ? e.charCode : e.keyCode;
	//	if (unicode == 13) {
	//		submitComment();
	//	}
	//}
	
	$$('form.comment_form #email').addEvent('focus', function() {
		if (this.getValue() == 'Email *') {
			this.value = '';
		}
		this.removeClass('dormant');
	});
	$$('form.comment_form #email').addEvent('blur', function() {
		if (!this.getValue()) {
			this.value = 'Email *';
			this.addClass('dormant');
		}
	});
	//element.onkeyup = function(e) { // ie doesn't like this
	//	var unicode = e.charCode ? e.charCode : e.keyCode;
	//	if (unicode == 13) {
	//		submitComment();
	//	}
	//}
	
	$$('form.comment_form #website').addEvent('focus', function() {
		if (this.getValue() == 'Website') {
			this.value = '';
		}
		this.removeClass('dormant');
	});
	$$('form.comment_form #website').addEvent('blur', function() {
		if (!this.getValue()) {
			this.value = 'Website';
			this.addClass('dormant');
		}
	});
	//element.onkeyup = function(e) { // ie doesn't like this
	//	var unicode = e.charCode ? e.charCode : e.keyCode;
	//	if (unicode == 13) {
	//		submitComment();
	//	}
	//}
	
	$$('form.comment_form #comment').addEvent('focus', function() {
		if (this.getValue() == 'Comment *') {
			this.value = '';
		}
		this.removeClass('dormant');
	});
	$$('form.comment_form #comment').addEvent('blur', function() {
		if (!this.getValue()) {
			this.value = 'Comment *';
			this.addClass('dormant');
		}
	});
	$$('form.comment_form a.icon').addEvent('click', function() {
		var id = this.id.split('button_').join('');
		var form = $('comment_form_'+id);
		submitComment(form);
	});
	
});

// submit a comment
function submitComment(form) {
	var form_id = form.getProperty('id');

	var name_wrap = $$('#'+form_id+' #name_wrap')[0];
	var email_wrap = $$('#'+form_id+' #email_wrap')[0];
	var comment_wrap = $$('#'+form_id+' #comment_wrap')[0];
	
	name_wrap.removeClass('invalid');
	email_wrap.removeClass('invalid');
	comment_wrap.removeClass('invalid');
	
	var name_input = $$('#'+form_id+' #name')[0];
	var email_input = $$('#'+form_id+' #email')[0];
	var website_input = $$('#'+form_id+' #website')[0];
	var comment_input = $$('#'+form_id+' #comment')[0];
	var id_input = $$('#'+form_id+' #id')[0];
	
	var name = name_input.getValue();
	var email = email_input.getValue();
	var website = website_input.getValue();
	var comment = comment_input.getValue();
	var id = id_input.getValue();
	
	var email_test = /(@\w[-._\w]*\w\.\w{2,3})$/;
	
	var invalids = new Array();
	
	if (!name || name == 'Name *' || name == msg_noname) { // invalid name
		invalids.push('name');
	}
		
	if (!email || email == 'Email *' || email == msg_noemail || !email_test.test(email)) { // invalid email
		invalids.push('email');
	}
		
	if (!comment || comment == 'Comment *' || comment == msg_nocomment) { //invalid comment
		invalids.push('comment');
	}
	
	if (invalids.length) { // invalid
		for (var i = 0; i < invalids.length; i ++) {
			
			var invalid_wrap = eval(invalids[i]+'_wrap');
			var invalid_input = eval(invalids[i]+'_input');
			
			invalid_wrap.addClass('invalid');
			invalid_input.value = eval('msg_no'+invalids[i]);
			
			if (i == 0) {
				$(invalid_input).focus();
				$(invalid_input).select();
			}
		}
		
	} else { // valid
		var url = '/comment';
		var action = 'submit_comment';
		
		var toggler = 'toggler_'+id;
		var togglee = 'togglee_'+id;
		var view_comments = 'view_comments_'+id;
		var spinner = 'spinner_'+id;
		
		// show the comments if they are hidden
		if ($(togglee).hasClass('hidden')) {
			$(toggler).fireEvent('click');
		}
		
		// set up ajax
		var post = 'a='+action+'&b='+id+'&n='+name+'&e='+email+'&w='+website+'&c='+comment;
		var ajax = new Ajax(url, {method: 'post', data: post, evalScripts: true, onComplete: function(response) {
																									  
			// comments legend
			if (total_comments == 0) {
				var legend_html = 'No Comments<span id="'+view_comments+'" class="view_comments"></span>';
			} else if (total_comments == 1) {
				var legend_html = '1 Comment<span id="'+view_comments+'" class="view_comments"> - Click to Read</span>';
			} else {
				var legend_html = total_comments+' Comments<span id="'+view_comments+'" class="view_comments"> - Click to Read</span>';
			}
			$(toggler).setHTML(legend_html);
			$(view_comments).setStyle('display', 'none');
			
			// new comment div
			if (own) {
				var css_class = 'repeater last own';
			} else {
				var css_class = 'repeater last';
			}
			var new_comment = new Element('div', {
				'class': css_class
			}).setHTML(response);
			
			// hide the spinner & show the comment
			new Fx.Style(spinner, 'opacity', {onComplete: function() {
				form.setStyle('display', 'none');
				new_comment.injectInside(togglee);
				$(spinner).setStyle('display', 'none');
			}}).start(0);
			
		}});
		
		// remove comment form
		new Fx.Style(form, 'opacity', {onComplete: function() {
			ajax.request();
		}}).start(1, 0);
		
		// show spinner
		new Fx.Style(spinner, 'opacity').start(1);
		
	}
}

