/*!
 * Telemaque JavaScript Library v0.3
 * http://www.telemaque.fr
 *
 * Copyright (c) 2010 Telemaque
 * Author : Florent Manas
 * Dual licensed under the MIT and GPL licenses.
 * http://www.telemaque.fr/cgv
 *
 * Date: 2010-03-23 15:43:52 -0500 (Tue, 23 Mar 2010)
 * Revision: 852
 */
 

 
function switchMessage(id, type) {
	switch(type) {
		case 'highlight':
			subtype = 'info';
			submessage = 'Fécitations !';
			break;
		case 'error':
			subtype = 'alert';
			submessage = 'Attention :';
			break;
		default:
			subtype = 'info';
			break;
	}
	
	pattern =  '#post_comment_' + id;

	$(pattern + ' .post_response').attr('class', 'post_response ui-corner-all ui-state-' + type).children('strong').html(submessage);
	$(pattern + ' .ui-icon').attr('class', 'post_response ui-icon ui-icon-' + subtype);
	if ($(pattern).parents('.content').first().children(':last').attr('tagName') != 'BR' && $(pattern + ' input[name="email"]').val() != '') $(pattern).after('<br class="clear" />');
}

function rateForms() {
	$('form[class*="rating_form"]').applyRating({});
}

function getComments(id, limit, offset) {
	$.ajax({
			type: 'post',
			url: '/lab/comment',
			data: {'action': 'get_comment', 'id': id, 'limit': limit, 'comment_page': offset},
			beforeSend: function() { 
				
			},
			complete: function() {
				$('#product_comment_' + id).slideDown('slow', showPager);
			},
			success: function(html) {
				$('#product_comment_' + id).slideUp('normal', function() {
			  	$('#product_comment_' + id).html(html);		
					rateForms();
				});
			}
	});
}

function validate(formData, jqForm, options) {
	var form = jqForm[0];

	var fields = ['name', 'email', 'comment'];
	var type   = ['input', 'input', 'textarea'];
	var values = [];
	
	valid = true;

	for(name in fields) {
		field = $('#' + $(form).attr('id') + ' ' + type[name] +'[name="' + fields[name] + '"]');
		value = field.fieldValue();

		if (!value[0]) {
			field.addClass('error');
			valid = false;
		} else {
			field.removeClass('error');
		}
	}
	if (!valid) {
		switchMessage($(form).children('input[name="id"]').val(), 'error');
		$(form).prev().fadeIn('normal');
	}	else {
		$(form).prev().fadeOut('normal');
	}
	
	return valid;
}

function showPager() {
	if ($('.comments_pager').children().length > 0) {
		$('.comments_pager').fadeTo(250, 0.75).children().css({opacity: 0}).first().animate({opacity: 1}, 300, showNextItemPager);
	}
}

function showNextItemPager() { if ($(this).next().length > 0) $(this).next().animate({opacity: 1}, 300, showNextItemPager); }
