$(document).ready(function() {
	for(var i=0; i < Ids.length; ++i) {
		//set slider
		$("#slider_" + Ids[i]).slider({
	    	orientation: 'horizontal',
	    	range: 'min',
	    	max: 10,
	    	change: function(event, ui) { $(this).next().text(ui.value); }  
	    });
	    //set tooltip
	    $("#slideroff_" + Ids[i]).tooltip({
	    	bodyHandler: function() {
       			return $("<div>" +  Math.round($(this).children().css('width').replace('%', '')) + "%</div>");
       		}  
	    });
	}
});


function save(objectId, typeId) {
	$("#cityView .errors").empty();
	var value = $("#slider_" + objectId).slider('value');
	

	var url = '/' + lang + '/rating/rateajax/object_id/' + objectId + '/type_id/' + 
			typeId + '/value/' + value;
	$.getJSON(url,
				function(data) {
					if (Boolean(data['res'])) {
						if (data['rating']) {
							var oParent = $("#slider_" + objectId).parent();
							oParent.empty();
							oParent.append('<div id="slideroff_' + objectId + '" class="slider ui-slider ui-slider-horizontal ' + 
													'ui-widget ui-widget-content ui-corner-all">' +
												'<div class="ui-slider-range ui-widget-header" style="width: ' + 
													data['rating'] * 10 + '%;"></div>' +
											'</div>');
							oParent.children().tooltip({
					    		bodyHandler: function() {
			        				return $("<div>" +  Math.round($(this).children().css('width').replace('%', '')) + "%</div>");
			        			}
					    	});
						}
						$("#save_" + objectId).replaceWith('<li class="save" id="rerate_' + objectId + 
															'"><a href="javascript:rerate(' + objectId + ', ' + typeId + ');">' + 
															rerateName + '</a></li>');
					}
					$("#cityView .errors").html(Boolean(data['message']) ? data['message'] : ""); 
				});

}

function rerate(objectId, typeId) {
	$("#cityView .errors").empty();

	if ($("#slideroff_" + objectId).length) {
		var oParent = $("#slideroff_" + objectId).parent();
		oParent.empty();
		oParent.append('<div id="slider_' + objectId + 
						'" class="slider"></div><div class="value">0</div>');
		$("#slider_" + objectId).slider({
	    	orientation: 'horizontal',
	    	range: 'min',
	    	max: 10,
	    	change: function(event, ui) { $(this).next().text(ui.value); }  
	    });
	}

	$("#rerate_" + objectId).replaceWith('<li class="save" id="save_' + objectId + 
			'"><a href="javascript:save(' + objectId + ', ' + typeId + ');">' + saveName + '</a></li>');
} 
