$(function() {

	//vote posts & comments
	$("a[rev='vote-on'], a[rev='vote-against']").click( function() {
		var mark = $(this).attr("title") == "+"? 1:-1;
//		var item_id =  $(this).attr("value");
		var href = $(this);
		var url = $(this).attr('href');
		var is_comment = $(this).hasClass('comm');

		$.ajax({
			type: "POST",
//			url: $(this).hasClass('comm')? '/comment/vote': '/post/vote',
			url: url,
//			data: ( is_comment? {comment_id : item_id, mark:mark}: {post_id : item_id, mark:mark} ),
			dataType: "json",
			success: function(res) {
				if(res.result == "1") {//success
					var el = href.parent();
					el.children("strong").empty().append(res.rating);
					if (-1 == mark) {
//						$(href).attr("class",'voted-against');
//						$(href).parent().find("a[rev='vote-on']").attr("class",'vote-on');
						if (is_comment) {
							$(href).parent().removeClass("cr_voteplus").addClass("cr_voteminus");
//							$(href).parent().find("a[rev='vote-on']").addClass('comm');
//							$(href).addClass('comm');
						} else {
							$(href).parent().removeClass("voteplus").addClass("voteminus");
						}
					} else if (1 == mark) {
//						$(href).attr("class",'voted-on');
//						$(href).parent().find("a[rev='vote-against']").attr("class",'vote-against');
						if (is_comment) {
							$(href).parent().removeClass("cr_voteminus").addClass("cr_voteplus");
//							$(href).parent().find("a[rev='vote-against']").addClass('comm');
//							$(href).addClass('comm');
						} else {
							$(href).parent().removeClass("voteminus").addClass("voteplus");
						}
					}
				}
				if(res.result == 2) { //validation error
					alert("validation error");
				}
			},
			error: function(msg){
				alert("Error: you must be logged in ");
			}
		});
		return false;
	});
});

$(function(){
	if (!jQuery.parseQuery){
		jQuery.parseQuery = function(qs,options) {
			var q = (typeof qs === 'string'?qs:window.location.search), o = {'f':function(v){return unescape(v).replace(/\+/g,' ');}}, options = (typeof qs === 'object' && typeof options === 'undefined')?qs:options, o = jQuery.extend({}, o, options), params = {};
			qs = q.match(/\?([^\#]*)/);
			jQuery.each((qs?qs[1]:'').split('&'),function(i,p){
				p = p.split('=');
				p[1] = o.f(p[1]);
				params[p[0]] = params[p[0]]?((params[p[0]] instanceof Array)?(params[p[0]].push(p[1])&&params[p[0]]):[params[p[0]],p[1]]):p[1];
			});
			return params;
		};
	}
	
	var GETS = jQuery.parseQuery(location.href);
	if (GETS.unsub && GETS.unsub.length > 30) {
		if (!window.confirm('Подтвердите ваше намерение отписаться от рассылки!')){
			return;
		}
		
		$.ajax({
		    'url'   : '/unsubscribe',
		    'type'  : "POST",
		    'cache'	: false,
		    'data'  : {'hash' : GETS.unsub},
		    'success': function(data) {
				if (data && data.status == 'ok') {
					alert('Вы успешно отписались от рассылки');
				}else{
					alert(data.errors || 'Ошибка сервера. Повторите попытку позже.');
				}
			}
		})
	}
})
