var FRE = {
	win: null,
	init: function(){
		var fr = $('friend-email');
		fr.observe('click', function(){
			if(!this.win){
				this.errors_cont = new Element('div', {'class': 'errors-box'});
				this.win = new Element('div', {'class': 'popup-win'}).insert(this.errors_cont);
				var form = new Element('form', {'action': 'ajax_general.php', 'method': 'post', 'id': 'friend-form'}).
				insert(new Element('b').update('Имя друга <br />')).
				insert(new Element('input', {'type': 'text', 'value': '', 'name': 'name', 'class': 'field'})).
				insert(new Element('b').update('<br />Email друга <br />')).
				insert(new Element('input', {'type': 'text', 'value': '', 'name': 'email', 'class': 'field'})).
				insert(new Element('input', {'type': 'submit', 'value': 'Отправить'}));
				this.win.insert(form);
				form.onsubmit = function(){
					var data = form.serialize(true);
					var errors = "";
					if(data.name.length == 0){
						errors = '<p>Имя должно быть введено.</p>';
					}
					if(data.email.length == 0){
						errors += '<p>Email должн быть введен.</p>';
					}
					if(errors.length == 0){
						data['cmd'] = 'send-to-friend';
						data['url'] = _send_url;
						data['title'] = _title;

						Main.request(js_url+'ajax_general.php', {
							parameters: data,
							listeners: {
								onSuccess: function(){
								},
								scope: this
							}
						});
						this.errors_cont.update('<p>Ссылка на эту новость была успешно отправлена Вашему другу.</p>');
						setTimeout(function(){
							this.win.fade();
							this.errors_cont.update("");
							form.reset();
						}.bind(this), 2500);
					}else{
						this.errors_cont.update(errors);
					}
					return false;
				}.bind(this);
				fr.parentNode.appendChild(this.win);
			}else{
				this.win.show();
			}
		}.bind(this));
	}
};
Main.onReady(FRE.init, FRE);

var COMMENT = {
	er: null,
	init: function(){
		COMMENT.ico = $("comment-ico");
		COMMENT.mask = $("comment-mask");
		COMMENT.ico.observe("click", function(){
			if(!COMMENT.ico.showed){
				COMMENT.ico.showed = true;
				COMMENT.mask.appear({duration: 0.5});
				setTimeout(function(){
					var ca = $("comment-area");
					if(ca){
						ca.focus();
					}
				}, 501);
			}else{
				COMMENT.mask.fade({duration: 0.5});
				COMMENT.ico.showed = null;
			}
		}.bind(this));

		new Main.ajaxForm({
			form: "comments-form",
			mask_element: "comment-mask",
			listeners: {
				onSuccess: function(js){
					if(COMMENT.er && COMMENT.er.box){
						COMMENT.er.box.update("");
					}
					new Effect.BlindUp("comments-box");
					setTimeout(function(){
						$("comment-success").appear();
					}, 900);
					setTimeout(function(){
						$("comment-success").fade();
					}, 7000);
					var div = new Element("div");
					div.insert(new Element("h6").update(js['data']['username']+' | '+js['data']['date_added']));
					div.insert(new Element("span").update(js['data']['body']));
					$("comments-list-box").insert(div, {position: "at the top of"});

					var nc = $("no-comments");
					if(nc){
						nc.remove();
					}
				},
				onFail: function(js){
					if(!COMMENT.er){
						COMMENT.er = $("recaptcha_area");
						var error = new Element("div", {"class": "error"});
						COMMENT.er.box = error;
						COMMENT.er.insert(error);
					}
					if(js['messages'] && js['messages']['captcha']){
						COMMENT.er.box.update(js['messages']['captcha']);
					}else{
						COMMENT.er.box.update("");
					}
				},
				fscope: this,
				scope: this
			}
		})
	}
};
Main.onReady(COMMENT.init, COMMENT);

function getSelText()
{
    var txt = '';
     if (window.getSelection)
    {
        txt = window.getSelection();
             }
    else if (document.getSelection)
    {
        txt = document.getSelection();
            }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
            }
    else return;
     if(typeof txt == 'object'){
    	 txt = txt.toString();
     }
     return txt;
}

var ErrorEmail = {
	selected_text: "",
	init: function(){
		this.tip = $("help-tip");
		this.btn = $("error-email");
		this.btn.observe("mouseover", function(){
			this.tip.appear({'duration': 0.4});
		}.bind(this));
		this.btn.observe("mouseout", function(){
			this.tip.fade({'duration': 0.1});
		}.bind(this));
		this.btn.observe("click", function(){
			if(this.selected_text.length > 0){
				Main.request(js_url+'ajax_general.php', {
					parameters: {
						'cmd': 'send-error',
						'text': this.selected_text,
						'news_id': _news_id
					},
					onSuccess: function(data){
						alert(data['message']);
					},
					scope: this
				});
			}else{
				alert("Для того, что бы отправить сообщение об ошибке, необходимо выделить часть текста с ошибкой на странице.");
			}
		}.bind(this));
		this.saveText();
	},
	saveText: function(){
		var txt = getSelText();
		if(txt.length > 0){
			this.selected_text = txt;
		}
		setTimeout(this.saveText.bind(this), 900);
	}
};
Main.onReady(ErrorEmail.init, ErrorEmail);
