var actionN = 0;

function showLayerRiseRating(actionNum, id) {
	
	var ts = new Date().getTime();
	
	if (id == null) id = 0;
		
	//console.log('showLayerRiseRating');
	//console.log('id = ' + id);
	//console.log('actionNum = ' + actionNum);

	switch (actionNum) {
		case 1 :
			actionN = 1;
			actionName = "Сделать историю звёздной";
			break;
		case 2 :
			actionN = 2;
			actionName = "Поднять историю";
			break;
		case 3 :
			actionN = 3;
			actionName = "Опубликовать в Топ-100";
			break;
		default :
			actionN = 0;
			actionName = "Поднять рейтинг";
			break;
	}

	//console.log('actionName = ' + actionName);

	$("#riseRatingLayer").remove();
	$("body").append('<div class="riseRatingLayer" id="riseRatingLayer"><button class="btnClose">Закрыть</button><h2 class="caption">Выберите историю</h2><ul class="selectFun"></ul><div class="submitButton"><button id="btnRiseRatingFun">' + actionName + '</button></div></div>');

	$('#riseRatingLayer').dialog({
		autoOpen : false,
		modal : true,
		resizable : false,
		width : 270
	}).hide();

	$('#riseRatingLayer .btnClose').click(function(event) {
		$('#riseRatingLayer').dialog('close');
		event.preventDefault();
	});
	
	$('#riseRatingLayer').bind( "dialogbeforeclose", function(event, ui) {
		$("#riseRatingLayer").remove();
	});

	$.getJSON('ajax/sortedFunsById?id=' + id + '&size=1&categoryFilter=thread0&order=1', { data : ts }, function(data) {
		showLayerRiseRatingAction(data);
	});
}

function showLayerRiseRatingAction(data) {

	//console.log('showLayerRiseRatingAction');

	$(".selectFun").html("");

	$.each(data, function() {
		//console.log(data);
		$(".selectFun").append('<li><div class="fun" id="' + this.id + '"><img class="funImage" alt="" src="resources/images/fununits/' + this.pic + '"/>' + '<div class="caption"><h2>' + this.name + '</h2></div></div></li>');
	});

	var layer = $('#riseRatingLayer'), funs = $('.fun', layer), selected;

	$('.btnClose', layer).click(function(event) {
		event.preventDefault();
		layer.dialog('close');
	});

	layer.dialog('open');

	funs.parent('li').mouseover(function(event) {
		$(this).addClass('hover');
	}).mouseout(function(event) {
		id = $('.fun', $(this)).attr('id');
		$(this).removeClass('hover');
	}).click(function(event) {
		$('#' + selected).parent('li').removeClass('selected');
		$(this).addClass('selected');
		selected = $('.fun', $(this)).attr('id').match(/\d+/);
	});

	$('#btnRiseRatingFun').button().click(function(event) {
		var ts = new Date().getTime();
		if (actionN == 1) {
			if (selected) {
				funId = selected;
				$.getJSON('ajax/paid/process?opCode=STARHISTORY1:' + funId, { data : ts }, function(response) {
					if (response.error == null) {
						dialog.messageBox("Поздравляем! Ваша история стала звёздной!<br /><br />На Вашем счёте осталось <span class=\"accountValue\">" + response.moneyLeft + "</span>");
						$(".accountValue").html(response.moneyLeft);
					} else {
						dialog.messageBox(decodeURIComponent(response.error.replace(/\+/g, " ")));
					}
				});
	
				layer.dialog('close');
				$("#riseRatingLayer").remove();
			}
		} else if (actionN == 2) {
			if (selected) {
				funId = selected;
				$.getJSON('ajax/paid/process?opCode=STORYBUMP1:' + funId, { data : ts }, function(response) {
					if (response.error == null) {
						dialog.messageBox("Поздравляем! Ваша история теперь на первом месте на главной странице!<br /><br />На Вашем счёте осталось <span class=\"accountValue\">" + response.moneyLeft + "</span>");
						$(".accountValue").html(response.moneyLeft);
					} else {
						dialog.messageBox(decodeURIComponent(response.error.replace(/\+/g, " ")));
					}
				});
	
				layer.dialog('close');
				$("#riseRatingLayer").remove();
			}
		} else if (actionN == 3) {
			if (selected) {
				funId = selected;
				dlgTop100.funId = funId;
				dlgTop100.successParent = function(likes, rating) {
					$("#likeCounter").html('<span class="arrow"></span>' + likes);
					$(".rating").html('<b>Рейтинг:</b> ' + rating);
				}; 
				
				dlgTop100.options.name = 'Опубликовать в TOP100 <br/>(минимальный рейтинг истории в ТОП100 : ' 
					+ '<b>' + dlgTop100.getMinRat() + '</b>)';
				dlgTop100.init();
				//console.log(dlgTop100);
				//dlgTop100.name = dlgTop100.name + '' + dlgTop100.getMinRat();  
				
				if (dlgTop100) dlgTop100.open();
			}
		} else if (actionN == 0) {
			funId = selected;
			
			dlg = new dialog.SpendMoney('vote');
			dlg.funId = funId;
			dlg.successParent = function(likes, rating) {
				//console.log("view.paidLikeResponse.success()");
				$("#likeCounter").html('<span class="arrow"></span>' + likes);
				$(".rating").html('<b>Рейтинг:</b> ' + rating);
			}; 
			//console.log(dlg.successParent);

			if (dlg) dlg.open();
		}
	});

}
