function at_bump(vote)
{
	var id = $F('art_id');
	var comment = '';

	if(vote == 'minus' && language == 'pl_PL')
	{
		comment = $F('notecomment_text');
		if( comment == '')
		{
			$('note_upd').innerHTML = _('Proszę uzupełnić treść komentarza!');
			return false;
		}
	}

	new Ajax.Request(siteurl + 'ajax/cast_vote.php',
  	{
		asynchronous: true,
   		method: 'get',
   		parameters: "id=" + id + "&vote=" + vote + "&comment=" + comment,
   		onComplete: function(obj) { article_vote_complete(obj); },
   		onLoading: function(obj) { article_vote_loading(obj); },
   		onFailure: function(obj) { article_vote_failure(obj); }
   	});

	if(node = $('notecomment_tags'))
	{
		$('tag').value = $F('notecomment_tags');
		add_tag(id);
	}
	return false;
}

function at_quickbump(vote)
{
    var id = $F('art_id');
	
	new Ajax.Request(siteurl + 'ajax/cast_vote.php',
  	{
		asynchronous: true,
   		method: 'get',
   		parameters: "id=" + id + "&vote=" + vote + "&comment=",
   		onComplete: function(obj) { article_vote_complete(obj); },
   		onFailure: function(obj) { alert( ('Wystąpił nieoczekiwany błąd. Przepraszamy i prosimy spróbować później.')); }
   	});
   	return false;
}


function article_vote_switch(val)
{
	$('notecomment_tags').disabled = val;
	$('notecomment_text').disabled = val;
	$('notecomment_submit').disabled = val;
	$('notecomment_cancel').disabled = val;
}

function article_vote_failure(obj)
{
	$('note_upd').innerHTML = ' (' + obj.status + ')';
	$('notecomment_preloader').hide();
	article_vote_switch(false);
}

function article_vote_loading(obj)
{
    $('note_upd').innerHTML = '&nbsp;';
	$('notecomment_preloader').show();
	article_vote_switch(true);
}

function article_vote_complete(obj)
{
	ithinkOutput = obj.responseText;

	if(node = $('notecomment_preloader'))
	{
		$('notecomment_preloader').hide();
	}

    if(ithinkOutput.indexOf('ERROR') != -1)
	{
	    if(node = $('note_upd'))
	    {
		    article_vote_switch(false);
		    $('note_upd').innerHTML = ithinkOutput.substr(6);
        }
        else
        {
			alert(ithinkOutput.substr(6));
		}
        return false;
    }
    else
    {
    	r = ithinkOutput.split('#');
    	var user_guid = r[1];
    	var user_name = r[0];
    	var send_date = r[2];
    	var number = r[3];
    	var vote = r[4];
    	var note = '';
    	if(vote > 0)
    		note = 'note plus';
    	else if(vote < 0)
    		note = 'note minus';
    	else
    		note = 'note';

		if(node = $('notecomment_text') && ($F('notecomment_text') != ''))
		{
        	attach_comment(user_guid, user_name, send_date, number, $F('notecomment_text'));
        }

        $('wykop').innerHTML = '<div class="' + note + '"><a href="#" class="plus" title="' + _('plus') + '">+</a> <a href="#" class="minus" title="' + _('minus') + '">-</a> ' + vote + '</div>';
        $('wykop2').innerHTML = '<div class="' + note + '"><a href="#" class="plus" title="' + _('plus') + '">+</a> <a href="#" class="minus" title="' + _('minus') + '">-</a> ' + vote + '</div>';
        hide_comment_note();
        return false;
    }
}

function attach_comment(user_guid, user_name, send_date, number, comment_text)
{
	comment_text = comment_text.replace(/\n/g, "<br />");
	span_header = document.createElement('span');
	span_header.style.display = 'none';
	span_header.className = 'comment_timeauthor_stamp';
	if (user_guid != '') {
	    span_header.innerHTML =  number + '. <a href="' + siteurl + user_guid + '/">'+user_name+'</a> | ' + send_date;
	} else {
	    span_header.innerHTML =  number + '. ' + user_name + ' | ' + send_date;
	}

	p_comment = document.createElement('p');
	p_comment.style.display = 'none';
	p_comment.className = 'comment_entry';
	p_comment.innerHTML = comment_text;

	write_root = $('komentarz');
	write_root.appendChild(span_header);
	write_root.appendChild(p_comment);

	$('liczba_komentarzy').innerHTML = number;

	new Effect.Appear(p_comment,{duration: 0.250});
	new Effect.Appear(span_header,{duration: 0.250});
}

/* -------------------------------------------------------------------------- */

function add_tag(art_id)
{
	var tag = $("tag");
	if(tag.value != "")
	{
	new Ajax.Request(siteurl + 'ajax/cast_tag.php',
  	{asynchronous: true,
   	method: 'get',
   	parameters: "id=" + art_id + "&tag=" + tag.value,
   	onComplete: function(obj) { handleResponse_tag(obj.responseText); },
   	onFailure: function(obj) { alert("Error: " + obj.status); }
   	});
   	}
}

function handleResponse_tag(ithinkOutput)
{
    if(ithinkOutput.indexOf('error') != -1)
	{
		alert(ithinkOutput);
        return;
    }
    else
    {
    	r = ithinkOutput.split('#');
    	var ile = r.length;
    	var answer = '';
    	for(var i = 0; i < ile; i++)
    	{
    		tag = r[i];
    		answer += ', <a href="' + siteurl + 'tag/' + tag + '/">' + tag + '</a>';
    	}
    	var similar = $("pokrewne").innerHTML;
    	$("pokrewne").style.display = "block";
        $("pokrewne").innerHTML = similar + answer;
		$('tag').value = '';
    }
}

/* -------------------------------------------------------------------------- */

function friend_send()
{
	var link = escape(document.URL);
	var name = $F("sf_name");
	var mail = $F("sf_mail");
	var fmail = $F("sf_fmail");
	var msg = $F("sf_msg");
	var addr = siteurl + 'ajax/cast_friend.php';

	new Ajax.Request(addr,
	{
		asynchronous: true,
   		method: 'get',
   		parameters: "name=" + name + "&link=" + link + "&mail=" + mail + "&fmail=" + fmail + "&msg=" + msg,
   		onComplete: function(obj) { friend_send_complete(obj.responseText); },
   		onLoading: function(response) { friend_send_loading(response); },
   		onFailure: function(obj) { }
   	});
}

function friend_send_failed()
{
	$('sf_name', 'sf_mail', 'sf_fmail', 'sf_msg', 'sf_send', 'sf_anul').each(function(e){e.disabled = false;});
	$('sf_preloader').hide();
	$('sf_form_msg').innerHTML = _("Wystąpił nieoczekiwany błąd. Przepraszamy i prosimy spróbować później.") + "(" + obj.status + ")";
	$('sf_form_msg').show();
}

function friend_send_loading(response)
{
	$('label_sf_name', 'label_sf_mail', 'label_sf_fmail').each(function(e){e.className = '';});
	$('sf_name', 'sf_mail', 'sf_fmail', 'sf_msg', 'sf_send', 'sf_anul').each(function(e){e.disabled = true;});
	$('sf_form_msg').hide();
    $('sf_preloader').show();

}

function friend_send_complete(responseText)
{
	$('sf_name', 'sf_mail', 'sf_fmail', 'sf_msg', 'sf_send', 'sf_anul').each(function(e){e.disabled = false;});
    $('sf_preloader').hide();

	if(responseText.indexOf('ERROR') != -1)
	{
		$('sf_form_msg').innerHTML = _('Wypełnij zaznaczone pola!');
		$('sf_form_msg').show();

		if(responseText.indexOf('EMPTY_NAME') != -1)
		{
			$('label_sf_name').className = 'terror';
		}
		if(responseText.indexOf('EMPTY_EMAIL') != -1)
		{
			$('label_sf_mail').className = 'terror';
		}
		if(responseText.indexOf('EMPTY_FMAIL') != -1)
		{
			$('label_sf_fmail').className = 'terror';
		}
        return false;
    }
    else
    {
    	close_sf_window();
    	show_article_monit( _('Twoja wiadomość została wysłana. Dziękujemy!'));
    }
}

/* -------------------------------------------------------------------------- */

function add_fav(art_id)
{
	new Ajax.Request(siteurl + 'ajax/cast_fav.php',
  	{
	  	asynchronous: true,
		method: 'get',
		parameters: "id=" + art_id,
		onComplete: function(obj) { handleResponse_fav(obj.responseText); },
		onFailure: function(obj) { alert("Error: " + obj.status); }
   	});
	return false;
}

function handleResponse_fav(ithinkOutput)
{
	show_article_monit(ithinkOutput);
}

/* -------------------------------------------------------------------------- */

function add_comment(art_id)
{
	var comment = $F('text');
	if(comment != '')
	{
	    new Ajax.Request(siteurl + 'ajax/cast_comment.php',
	  	{
			asynchronous: true,
	   		method: 'post',
	   		parameters: "id=" + art_id + "&comment=" + encodeURIComponent(comment) + (language == 'lt_LT' && $('username') && $('captcha') ? '&username=' + encodeURIComponent($F('username')) + '&captcha=' + encodeURIComponent($F('captcha')) : ''),
	   		onComplete: function(obj) { add_comment_complete(obj.responseText); },
	   		onFailure: function(obj) { add_comment_failure(obj); },
	   		onLoading: function(obj) { add_comment_loading(obj); }
	   	});
	   	if (language != 'lt_LT')
	   	{
	   	   add_tag(art_id);
	   	}
   	}
   	else
   	{
		$('comment_error').innerHTML = _('Proszę wprowadzić treść komentarza!');
		$('comment_error').show();
	}
}

function add_comment_failure(obj)
{
    $('comment_preloader').hide();
	$('text').disabled = false;
	$('tag').disabled = false;
	$('submit_comment').disabled = false;
	$('comment_error').innerHTML = _('Wystąpił nieoczekiwany błąd. Przepraszamy i prosimy spróbować później.');
	$('comment_error').show();
}

function add_comment_loading(obj)
{
    $('comment_error').hide();
	$('comment_preloader').show();
	$('text').disabled = true;
	$('tag').disabled = true;
	$('submit_comment').disabled = true;
}

function add_comment_complete(ithinkOutput)
{
	$('comment_preloader').hide();
	$('text').disabled = false;
	$('tag').disabled = false;
	$('submit_comment').disabled = false;
	if(ithinkOutput.indexOf('ERROR') != -1)
	{
		$('comment_error').innerHTML = ithinkOutput.substr(6);
		$('comment_error').show();
		return;
	}
	else
	{
		r = ithinkOutput.split('#');
		var user_guid = r[1];
		var user_name = r[0];
		var send_date = r[2];
		var number = r[3];

    	attach_comment(user_guid, user_name, send_date, number, $F('text'));

		$('text').value = '';
		$('tags').value = '';
	}
}
