function RadarWidget(containerId) {
	this.container = document.getElementById(containerId);
	this.items = [];
	this.defaultPositions = [
		{width:167,height:125,left:10,top:65,opacity:1,depth:3, marginleft:10, margintop:65},
		{width:100,height:75,left:120,top:30,opacity:0.8,depth:2, marginleft:120, margintop:30},
		{width:65,height:49,left:105,top:15,opacity:0.5,depth:1, marginleft:105, margintop:15},
		{width:50,height:38,left:60,top:5,opacity:0.5,depth:0, marginleft:65, margintop:5},
		{width:65,height:49,left:10,top:15,opacity:0.5,depth:1, marginleft:25, margintop:15},
		{width:100,height:75,left:-30,top:30,opacity:0.8,depth:2, marginleft:-30, margintop:30}
	];
	this.init();
}

RadarWidget.prototype.init = function() {
	var oThis = this;
	var cacheControl = new Date().getTime();
	$.getJSON("mod_radar_dest.asp?cache="+cacheControl, function(json){
		oThis.items = json;
		oThis.render();
	});
}

RadarWidget.prototype.addBanda = function(id,nome,cidade,estado,imagem,link,votos) {
	this.items.push({
		id:id,
		nome:nome,
		cidade:cidade,
		estado:estado,
		imagem:imagem,
		link:link,
		votos:votos
	});
}

RadarWidget.prototype.render = function() {
	while(this.items.length < 6) {
		this.addBanda('','&nbsp;','&nbsp;','&nbsp;','img/default.jpg','#','&nbsp;');
	}

	$(this.container).empty();

	for(var i = 0; i < this.items.length; i++) {
		var link = document.createElement("a");
		var image = document.createElement("img");
		image.src = this.items[i].imagem;
		image.alt = this.items[i].nome;

		$(image).css({
			"width":this.defaultPositions[i].width,
			"height":"auto",
			"margin-top":this.defaultPositions[i].margintop,
			"margin-left":this.defaultPositions[i].marginleft,
			"opacity":this.defaultPositions[i].opacity,
			"position":"absolute",
			"z-index":this.defaultPositions[i].depth
		});

		link.appendChild(image);
		link.href = this.items[i].link;

		this.container.appendChild(link);

	}
	this.setInfo();
}

RadarWidget.prototype.rotate = function(direction) {
	var container = this.container;
	
	if(direction == "left") {
		this.items.push(this.items.shift());
	} else {
		this.items.splice(0,0,this.items.pop());
	}
	this.render();
	this.setInfo();
}

RadarWidget.prototype.setInfo = function() {
	if(this.items[0]) {
		$("#widgetNome").html(this.items[0].nome);
		if(this.items[0].id != "") {
			$("#widgetLocal").html(this.items[0].cidade + ', ' + this.items[0].estado);
			$("#widgetVotos").html(this.items[0].votos + ' <em>Votos</em>');
		} else {
			$("#widgetLocal").html("&nbsp;");
			$("#widgetVotos").html("&nbsp;");
		}
		$("#id").attr("value",this.items[0].id);
	}
}