var renderedBefore = false;
var star_count = 5;
var bandlist = null;

function BandList() {
	function clear() {
		this.bands = {};
		this.save();
	}
	function save(){
		var cookie = JSON.stringify(this.bands);
		$.cookie('bandlist', cookie, { expires: 7 });
		this.print();
//		console.log("Saved: " + cookie);
	}
	function load(){
		var cookie = $.cookie('bandlist');
		if (cookie == null) return {};
		var parsed = JSON.parse(cookie);
		this.bands = parsed;
//		console.log("Loaded: " + cookie);
	}
	function addBand(bandid, bandname, votes) {
		var band = {"name" : bandname, "votes" : votes};
		this.bands[bandid] = band;
		this.save();
		return band;
	}
	function getBand(bandid) {
		return this.bands[bandid];
	}
	function getBands(){
		return this.bands;
	}
	
	function delBand(bandid) {
		delete this.bands[bandid];
//		console.log(this.bands);
		this.save();
	}
	function findBandKey(bandname){
		for (var bandid in this.bands) {
			if (bandname == this.bands[bandid]["name"]) return bandid;
		}
		return null;
	}
	function vote(bandid, votes) {
//		console.log("Vote(" + bandid + ", " + votes + ")");
		if (this.bands[bandid] == undefined) {
			var bandname = $("[bandname]").attr("bandname");
//			console.log(bandname);
			this.addBand(bandid, bandname, votes);
		} else {
			this.bands[bandid]["votes"] = votes;
		}
		this.save();
	}
	function size(){
		var res = 0;
		for (key in this.bands) res++;
		return res;
	}
	function print(){
		for(id in this.bands) {
			var band = this.bands[id];
			// console.log(id + " -> " + band.name + " " + band.votes);
		}
	}
	
	// Methods
	this.clear = clear;
	this.save = save;
	this.load = load;
	this.addBand = addBand;
	this.getBand = getBand;
	this.getBands = getBands;
	this.delBand = delBand;
	this.findBandKey = findBandKey;
	this.vote = vote;
	this.size = size;
	this.print = print;

	this.bands = {};
}

function remember(bandid){
//	console.log("remember('"+ bandid +"')");
	bandlist.vote(bandid, 3);
	renderRemembered();

}

function forget(bandid, buttonID){
//	console.log("forget('"+ bandid + "', '" + buttonID + "')");
	bandlist.delBand(bandid);
	renderRemembered();
}

function renderRemembered(){
//	console.log("Rendering...");
	
	// Reset
	$("#remembered").html("");
	
	// Update buttons
	var bandname = $("div[bandid="+bandid+"] input")
	.attr("value", "forget")
	.attr("onClick", "forget('" + bandid + "');"
	);		
	if (bandlist.getBand(bandid) != undefined) {
		$("#togglefav").html("Fjern fra favoritter");
	} else {
		$("#togglefav").html("Føj til favoritter");
	}
	
	var i = 0;
	for(bandid in bandlist.getBands()) {
		i++;
//		console.log("Rendering " + bandid);

		// Animate vote column
		if (!renderedBefore) {
			$("#left").animate({"width": "550px"}, function(){
				$("#right").css("visibility", "visible");
			});
			renderedBefore = true;
		}
		
		// Make row of stars
		var band = bandlist.bands[bandid];
		var wrapper_name = "starswrapper_" + i;
		var text = ""
			+"<div class='remember-band'><div class='bandname'>"
				+"<a href=\"javascript:forget(\'"
					+bandid +"')\">"
						+"<img src='remember/delete.png'/>"
				+"</a>" 
				+band.name 
			+"</div>"
			+"<div class='stars' id='"+wrapper_name+"'>";
		for(var j = 1; j <= star_count; j++){
      		text = text + '<input type="radio" title="'+bandid+'"  value="'+j+'" ' + (j == band.votes ? 'checked="checked"':'') +'/>';
		}
		text+="</div></div>";
		$("#remembered").append(text);
		$("#"+wrapper_name).stars({
			captionEl: $("#hover_ocv2"),
			callback: function(ui, type, value, event){
				var bandid = ui.options.title;
				bandlist.vote(bandid, value);
			},
			cancelShow: false
		});	
		
	}
}
var nl = "\r\n";
function email(){
	var emailaddr = $("#emailaddr").val();
	//console.log("Email: '" + emailaddr + "'");
	var body = "This is your Inmusic band checklist" + nl;
	for (var bandid in bandlist.bands) {
		var band = bandlist.getBand(bandid);
		body = body + nl + band.name + ". votes: " + band.votes + " http://www.affiliatebasen.dk/" + bandid;
	}
	body = body + nl + nl + "Kind regards" + nl + "Inmusic";
	body = encodeURIComponent(body);
	
	window.location.href = 
		"mailto:" + emailaddr + 
		"?subject=Inmusic%20band%20checklist" +
		"&body=" +body ;
}

var favoritesShowing = false;
function togglefavorites(){
	$("#togglefavorites").html(favoritesShowing ? "Vis favoritter" : "Skjul favoritter");
	favoritesShowing = ! favoritesShowing;
	// $("#favorites").toggle();
	$("#favorites").animate({
		height: 'toggle'
	}, 100);
}

function togglefav(){
	if (bandlist.getBand(bandid)!=undefined) {
		bandlist.delBand(bandid);
	} else {
		bandlist.vote(bandid, 3);
	}
	renderRemembered();
}
var bandid = null;
$(document).ready(function() {
 	bandid = $("[bandid]").attr("bandid");
	bandlist = new BandList();
	// // bandlist.clear();
	// // bandlist.save();
	bandlist.load();
	// renderRemembered();
});




