var TextMarket = {
	init : function(){
        $('.tm-opinion-img').click(TextMarket.openResponse);
        $('.post-response').click(TextMarket.postResponse)
    },
	openResponse : function() {
        var id = $(this).attr('id').replace('item-','');
        $.get('/',{
				mode		: 'front-gateway',
				module		: 'text_market_2',
				controller	: 'api',
				format		: 'json',
				action		: 'get_response_text',
				id			: id
			},
			function (result) {
				if (result.success){
					var div = $('<div>'+result.content+'</div>');
					div.dialog({
						title		: 'Отзыв',
						width		: 300,
						height		: 200,
						modal		: true,
						autoResize	: true,
						resizable	: true,
						buttons		: {
							'Закрыть' : function(){
								div.dialog('destroy');
							}
						}
					});
				} else {
					alert(result.errors[0]);
				}
			}, 'json'
		);
    },
    postResponse : function() {

        var id = $(this).attr('id').replace('item-','');
        $.get('/', {
				mode		: 'front-gateway',
				module		: 'text_market_2',
				controller	: 'api',
				format		: 'json',
				action		: 'get_postresponse',
				id			: id
			},
			function (result) {
				if (result.success){
					var div = $('<div>'+result.content+'</div>');
					div.dialog({
						title		: 'Отзыв',
						width		: 400,
						height		: 200,
						modal		: true,
						autoResize	: true,
						resizable	: true,
						buttons		: {
							'Закрыть' : function(){
								div.dialog('destroy');
							},
                            'Оставить отзыв' : function() {
                                div.find('form').submit();
                            }
						}
					});
				} else {
					alert(result.errors[0]);
				}
			}, 'json'
		);
        return false;
    }
};

$(document).ready(function(){
	TextMarket.init();

	$("a.view_part").click(function(){
		var id = $(this).attr("id");
		$(".states_result").css("display", "none");
		$("#state" + id.substr(1)).css("display", 'block');
	}).css("color", "#a00000").hover(function(){
		$(this).css("text-decoration", "none");
	}, function(){
		$(this).css("text-decoration", "underline");
	});

	if ($(".uniq_status").length > 0) {		var TimerRefresh = setTimeout('refresh_uniq_status()', 1000);
        }});

function refresh_uniq_status() {		var id = $(".uniq_status").attr("id");

                $.get('/', {
				mode		: 'front-gateway',
				module		: 'text_market_2',
				controller	: 'uniq',
				format		: 'json',
				action		: 'get_status',
				id		: id.substr(1)
			},
			function (result) {
				if (result.success) $(".uniq_status").html(result.content);
				else alert(result.errors[0]);

				if (result.refresh) {					TimerRefresh = setTimeout('refresh_uniq_status()', 10000);
		                        $(".uniq_status").append(" Пожалуйста, подождите...");
		                        $(".uniq_status").append("<div />");
		                        $(".uniq_status div").css("width", "54px").css("height", "18px").css("background", "url(/common/preloaders/grey_hor.gif) left top no-repeat").css("float", "right").css("margin-top", "3px");
                                }
				else clearTimeout(TimerRefresh);
			}, 'json'
		);
}
