/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

function showCommentForm (relId, sTypeName, postId) {
	var oWrap = document.getElementById('new_comment_' + relId);
	
	if (typeof oWrap == 'undefined') {
		return false;
	}
	
	if (oWrap.style.display == '') {
		return false;
	}
	
	var oForm = document.createElement ('form');
	oForm.className = 'add_comment';
	oForm.action = 'javascript:void(0);';
	oForm.method = 'post';
	
	oForm.onsubmit = function () {
		postComment (relId, sTypeName, postId);
	};

	var oTextarea = document.createElement ('textarea');
	oTextarea.id = 'comment_text_' + relId;
	
	oForm.appendChild (oTextarea);
	
	var oWrapError = document.createElement ('div');
	oWrapError.style.display = 'none';
	oWrapError.className = 'comment_error';
	oWrapError.id = 'commenting_error_' + relId;
	oWrapError.innerHTML = 'Вы должны сначала написать комментарий';
	
	oForm.appendChild (oWrapError);
	
	var oSubmit = document.createElement ('input');
	oSubmit.type = 'submit';
	oSubmit.value = 'Написать';
	
	oForm.appendChild (oSubmit);
	
	var oCloseButton = document.createElement ('input');
	oCloseButton.type = 'button';
	oCloseButton.value = 'Отмена';
	oCloseButton.onclick = function () {
		oWrap.innerHTML = '';
		oWrap.style.display = 'none';
	};
	
	oForm.appendChild (oCloseButton);
	
	oWrap.appendChild (oForm);
	oWrap.style.display = '';
}

function deleteComment (id) {
	$.ajax ({
		url: baseURI + '/comments/ajax_delete/' + id + '?scode=' + scode,
		dataType: 'xml',
		type: 'get',
		error: function () { new Error ('Произошла ошибка!');  },
		success: function (xml) {
			var result = $(xml).find('result').text();
			
			if (result == '0') {
				new Error ('Произошла ошибка!');
			} else {
				$('#comment_inner_'+id).html ('<div class="comment_deleted">Комментарий удален</div>');
			}
		}
	});
}

function voteComment (id, mark) {
	if (mark != 1) {
		mark = 0;
	}
	
	$.ajax ({
		url: baseURI + '/comments/ajax_vote/' + id + '/' + mark + '?scode=' + scode,
		dataType: 'xml',
		type: 'get',
		error: function () { new Error ('Произошла ошибка!');  },
		success: function (xml) {
			var result = $(xml).find('result').text();
			
			if (result == '0') {
				new Error ('Произошла ошибка!');
			} else {
				var rating = $(xml).find('rating').text();
				if (rating > 0) {
					$('#comment_rating_'+id).text ('+' + rating);
				} else {
					$('#comment_rating_'+id).text (rating);
				}
				
				if (mark == 1) {
					$('#comment_vote_plus_'+id).find('img').attr ('src', baseURI + '/images/comment-voted-plus.png');
					$('#comment_vote_minus_'+id).find('img').attr ('src', baseURI + '/images/comment-vote-minus-cant.png');
				} else {
					$('#comment_vote_plus_'+id).find('img').attr ('src', baseURI + '/images/comment-vote-plus-cant.png');
					$('#comment_vote_minus_'+id).find('img').attr ('src', baseURI + '/images/comment-voted-minus.png');
				}
				
				document.getElementById('comment_vote_plus_'+id).onclick = function () {return false;};
				document.getElementById('comment_vote_minus_'+id).onclick = function () {return false;};
				
				$('#comment_vote_plus_'+id).click (function () { new Error ('Повторное голосование запрещено!'); });
				$('#comment_vote_minus_'+id).click (function () { new Error ('Повторное голосование запрещено!'); });
			}
		}
	});
}
