function loadSectionList(url, id, letter) {
	$('#section_list_owner').html('');
	$('#section_list_category').html('');
	$('#section_list_agency').html('');
	
	$.ajax({
		method: "GET",
		url: url,
		beforeSend: function() {
			$('#'+id).html('<img style="padding-left: 3em;" id="loading" src="/images/wait.gif" />');
		},
		success: function(html) {
			$('#'+id).html(html);
		}
	});
}

function parseResult(html) {
	var el;
	data = $.parseJSON(html);
	// category, owner, agency
	switch (data["table"]) {
		case "category":
			el = $('#tvr_commercial_category_id');
			break;
		case "owner":
			el = $('#tvr_commercial_owners_list');
			break;
		case "agency":
			el = $('#tvr_commercial_agencies_list');
			break;
	}
	if (el) {
		el.find('option').attr('selected=""');
		el.append($("<option></option>").attr("value",data["id"]).attr("selected","selected").text(data["name"]));
		$("#dialog").dialog("close");
	}
	
	// song
	window.location.reload();
}
 
$(document).ready(function() {	
	$(".link_new").click(function(event) {
		event.preventDefault();
		$("body").append(
			$("<div id='dialog'></div>")
		);
		$("#dialog").attr('title', 'test')
			.dialog({
				modal:true,
				resizable:false,
				draggable:false,
				width:400,
				height:200,
				close:function(){$(this).remove();}
			})
			.load($(this).attr('href'), function(response, status, xhr) {
				$("#dialog > form").submit(function(event) {
					event.preventDefault();
					sendForm($(this));
				});
			})
		;
	});
	loadVoteStars();
});

function sendForm(form) {
	$.ajax({
		url: form.attr('action'),
		type: form.attr('method'),
		data: form.serialize(),
		success: function(html) {
			try {
				parseResult(html);				
			}
			catch (e) {
				alert(e);
				$("#dialog").html(html);
				$("#dialog > form").submit(function(event) {
					event.preventDefault();
					sendForm($(this));
				});
			}
		}
	});
}

var bgs = new Array();

function loadVoteStars() {
	var html = "";
	var score = $('#score').text();
	var score1 = score.substr(0,1);
	var score2 = score.substr(2,1);
	for(x = 1; x <= 5; x++) {
		css = "";
		if (score1 >= x) {
			bgs[x] = 100;
		}
		else if (score1 == x - 1 && score2 > 0) {
			bgs[x] = +score2+"0";
		}
		html += "<img src='/images/star.png' class='votestar' "+(css?"style='"+css+"'":"")+" id='vote"+x+"' alt='"+x+"' />";
	}
	html += "<input type='hidden' name='tvr_vote[vote]' id='vote' />";
	$('#votestars').html(html);
	$('#scorediv').css({'display': 'none'});
	
	showScore();

	$('.votestar').click(function(event) {
		$('#vote').attr('value', $(this).attr('id').substr(4,1))
	});
	$('.votestar').hover(
		function(event) {
			var starno = $(this).attr('id').substr(4,1);
			for(x = 1; x <= 5; x++) {
				var img = (x <= starno ? "url(/images/votefull.png)" : "none");
				$('#vote'+x).css({
					'background-image': img
				});
			}
		},
		function(event) {
			showScore();
		}
	);
}

function showMyVote(vote) {
	for(x = 1; x <= 5; x++) {
		var img = (x <= vote ? "url(/images/votefull.png)" : "none");
		$('#vote'+x).css({
			'background-image': img
		});
	}
}

function showScore() {
	if ($('#vote').attr('value')) {
		return showMyVote($('#vote').attr('value'));
	}
		
	for(x = 1; x <= 5; x++) {
		$("#vote"+x).css({"background-image": "url(/images/score"+bgs[x]+".png)"});
	}
}