UserComment = {
	processing : false,
	check_process : false,
	construct : function() {
		$(document).ready(function() {
			for (i = 0; i <= 5; i++) {
				// mouse over
				$('#uc_star_' + i).mouseover(function() {
					UserComment.lightStars($(this).attr('title'));
				});
				// click
				$('#uc_star_' + i).click(function() {
					var rate = $(this).attr('title');
					$('#uc_rate').val(rate);
					UserComment.lightStars(rate);
				});
			}
			// mouse out
			$('#uc_star_control').mouseout(function(){
				UserComment.lightStars($('#uc_rate').val());
			});
		})
	},
	lightStars : function(rate) {
		for (i = 1; i <= 5; i++) {
			if (i <= rate) {
				$('#uc_star_' + i).addClass('selected');
			}
			else {
				$('#uc_star_' + i).removeClass('selected');
			}
		}
	},
	showDialog : function(id, shop) {
		if (this.check_process == false) {
			this.check_process = true;
			var url;
			if (shop) {
				url = '/sklepy/komentarz/check.php'
			}
			else {
				url = '/produkt/komentarz/check.php'
			}

			$.ajax({
				type : 'POST',
				url : url,
				data : ({
					id : id
				}),
				success : function(data) {
					if (data == 'ok') {
						UserComment.check_process = false;
						UserComment.showAddCommentDialog(id, shop);
					}
					else if (data == 'exists') {
						if (shop) {
							UserComment.msgWindow('Ten sklep został już przez Ciebie oceniony.');
						}
						else {
							UserComment.msgWindow('Ten produkt został już przez Ciebie oceniony.');
						}
					}
					else if (data == 'not logged') {
						UserComment.msgWindow('Musisz być zalogowany, aby dodawać komentarze.');
					}
					else {
						UserComment.msgWindow('Przepraszamy, ale wystąpił błąd przy dodawaniu komentarza.');
					}
					UserComment.check_process = false;
				},
				error : function(data) {
					UserComment.msgWindow('Przepraszamy, ale wystąpił błąd przy dodawaniu komentarza.');
					UserComment.check_process = false;
				}
			});
		}
	},
	showAddCommentDialog : function(id, shop) {
		var url = '';
		$('#uc_id').val(id);
		
		if (shop) {
			url = '/sklepy/komentarz/';
		}
		else {
			url = '/produkt/komentarz/';
		}

		$('#uc_dialog').dialog({
			modal	: true,
			width	: 300,
			close	: function(event, ui) {
				$(this).dialog('destroy');
			},
			buttons	: {
				Ok : function() {
					if (UserComment.processing == false) {
						var rate = $('#uc_rate').val();
						var comment = $('#uc_comment').val();

						UserComment.processing = true;
						
						if (comment.length >= 10) {
							$.ajax({
								type : 'POST',
								url : url,
								data : ({
									id : id,
									rate : rate,
									comment : comment
								}),
								success : function(data) {
									if (data == 'ok') {
										UserComment.successWindow();
									}
									else if (data == 'not logged') {
										UserComment.showError('Musisz być zalogowany, aby dodawać komentarze.');
									}
									else if (data == 'too short') {
										UserComment.showError('Komentarz musi zawierać conajmniej 10 znaków.');
									}
									else if (data == 'exists') {
										if (shop) {
											UserComment.showError('Ten sklep został już przez Ciebie oceniony.');
										}
										else {
											UserComment.showError('Ten produkt został już przez Ciebie oceniony.');
										}
									}
									else {
										UserComment.showError('Przepraszamy, ale wystąpił błąd przy dodawaniu komentarza.');
									}
									UserComment.processing = false;
								},
								error : function(data) {
									UserComment.showError('Przepraszamy, ale wystąpił błąd przy dodawaniu komentarza.');
									UserComment.processing = false;
								}
							});
							UserComment.showError('');
						}
						else {
							UserComment.showError('Komentarz musi zawierać conajmniej 10 znaków.');
							UserComment.processing = false;
						}
					}
					
				},
				Anuluj : function() {
					$(this).dialog('destroy');
				}
			}
		})
	},
	showError : function(error) {
		$('#uc_error').text(error);
	},
	successWindow : function() {
		$('#uc_dialog').dialog('destroy');
		this.msgWindow('Twój komentarz został dodany.');
	},
	msgWindow : function(msg) {
		$("#main").append('<div id="dialog2" title="TorturaCen.pl" style="display:none;">' + msg + '</div>')
		$('#dialog2').dialog( {
			modal : true,
			width : 300,
			buttons : {
				Ok : function() {
					$(this).dialog('destroy');
				}
			}
		});
	}
}

UserComment.construct();
