function toggleComment() {
	if ( $('#addCommentForm').is(':hidden') ) {
		$('#addCommentForm').slideDown('slow');
		$('#addCommentForm').queue(function () {
			$('textarea:first', this).focus();
			$(this).dequeue();
		});
		$('#addCommentButton').fadeOut('slow');
	} else {
		$('#addCommentButton').fadeIn('slow');
		$('#addCommentForm').slideUp('slow');
	}
}

function updateTime(obj) {
	switch ( obj.id.substr(0,1) ) {
	case 'm':
		var min = $(obj);
		var hour = $(obj).prev();
		break;
	case 'h':
		var hour = $(obj);
		var min = $(obj).next();
		break;
	default:
		return false;
	}
	var dispObj = $(hour).prev();
	var valObj = $(hour).prev().prev();
	
	dt = $(valObj).val().substr(0,10);
	dt += ' ' + hour.val() + ':' + min.val() + ':00';
	$(valObj).val(dt);
}

var RecaptchaOptions = {theme : 'clean'};

$(document).ready(function() {
	//Spam 
	$('.email').each(function() {  
		$(this).children('img[src$=at.png]').replaceWith('@');
		$(this).children('img').replaceWith('.');
		$(this).replaceWith('<a href="mailto:'+$(this).text()+'" title="'+$(this).attr('title')+'">'+$(this).text()+'</a>');
	});

	//Tooltips
	$('a.external:not(a.nourl)').tooltip({delay:0,showURL: true});
	$('a:not(a.external),a.nourl').tooltip({delay:0,showBody:': '});
	$('abbr, cite, li, .info').tooltip({showBody:': '});
	
	//Action buttons
	$('.summaryItem').hover(function() {
		var toolbar = $('> .actions a', this);
		toolbar.css('visibility','visible');
	}, function () {
		var toolbar = $('> .actions a', this);
		toolbar.css('visibility','hidden');
	});
	$('.summaryDesc').hover(function() {
		var toolbar = $('> .actions a', $(this).prev());
		toolbar.css('visibility','visible');
	}, function () {
		var toolbar = $('> .actions a', $(this).prev());
		toolbar.css('visibility','hidden');
	});
	$('.expandText a').click(function() {
		$(this).parent().fadeOut('fast');
		$(this).parent().next('.moreinfo').slideDown();
	});
	
	//Date/time
	$('.date').datepicker({
		dateFormat: 'dd/mm/yy'
		, onSelect: function () {
			$(this).prev().val($.datepicker.formatDate('yy-mm-dd', $(this).datepicker('getDate')));
		}
	});
	$('.datetime').datepicker({
		dateFormat: 'dd/mm/yy'
		, onSelect: function () {
			dt = $.datepicker.formatDate('yy-mm-dd', $(this).datepicker('getDate'));
			dt += ' ' + $(this).next().val() + ':' + $(this).next().next().val() + ':00';
			$(this).prev().val(dt);
		}
	});
	
	//Validation
	$('#recaptcha_response_field').addClass('{required:true}');
	$('form.validationForm').each(function() {
    	$(this).validate({
			errorPlacement: function(error, element) {
				if ( element.is(":radio") )
					error.appendTo( element.parent().next().next() );
				else if ( element.is(":checkbox") )
					error.appendTo ( element.next() );
				else if ( element.is('#recaptcha_response_field') ) {
					error.appendTo ( $('#recaptcha_widget_div').parent().next() );
				} else
					error.appendTo( element.parent().next() );
			}
			, highlight: function(element, errorClass) {
		     	$(element).addClass(errorClass);
		     	$(element).highlightFade('red');
		  	}
		  	, onfocusout: false
		  	, success: function(label) {  
	            label.html(" ").addClass("checked"); 
	        } 
		});
	});	

	//Messages
	$('.flash').each(function() {
		var text = $(this).html();
		var theme = $(this).attr('class').split(' ').slice(-1).toString();
		$.jGrowl(text, {theme: theme, sticky: (theme=='error'||theme=='noticeStay')});
	});

	//Search
	$('#searchText').each(function() {
	    var defaultValue = this.value;
	    $(this).focus(function() {
	        if( this.value == defaultValue ) {
	            this.value = '';
    	        $(this).css('color', '#000');
	        }
    	});
	    $(this).blur(function() {
	        if ( this.value == '' ) {
    	        $(this).css('color', '');
	            this.value = defaultValue;
    	    }
	    });
	});
	
	//Misc
	$('.unit-rating li').click(function(){
		$(this).siblings('.current-rating').width(parseInt($(this).text(), 10) * 30);
		$(this).parent().next('.rating-value').val($(this).text());
	});
	$('.needLogin, .magazine').click(function() { $('#loginForm input[name=username]').highlightFade({start:'#85BB11'}).focus(); } )
	.tooltip({bodyHandler:'This magazine is restricted to members only. Please login if you are DWCV member.', delay: 0});
	$('.needLogin:not(.magazine)').click(function() { $('#loginForm input[name=username]').highlightFade({start:'#85BB11'}).focus(); } )
	.tooltip({bodyHandler:'You need to login to use this feature', delay: 0});	
});
