debug = true;
curcgroup = 0;
cgroups = [];
txtlist = true;

function logit(msg) {
	if (debug)
		console.log(msg);
}

function loadgroups(groups) {
	$("#groups").empty().append("<option selected='selected' value=''>-</option>");
	groups.forEach( function (item, index) {
		$("#groups").append("<option value='"+item+"'>"+item+"</option>");
	});
}

function loadcgroups(groups) {
	cgroups = [];
	$("#schans").empty();
	$("#cgroupstd").empty();
	$("#cgroupstd").append("<table id='lstc'></table>");
	groups.forEach( function (item, index, array) {
		cgroups[item.idg] = item.name;
		html = "<tr><td><button id='rn"+item.idg+"'>&#9998;</button> <label><input type='radio' name='cgroupr' value='"+item.idg+"'>"+item.name+"</label></td><td> <button id='rm"+item.idg+"'>X</button>";

		if (index)
			html += "<button id='up"+item.idg+"'>&uarr;</button>";
		if (index != array.length - 1)
			html += "<button id='dn"+item.idg+"'>&darr;</button>";
		html += "</td></tr>";
		$("#lstc").append(html);
	});
}

function loadschans(chans) {
	$("#schans").empty();
	$("#schans").append("<table id='schanst'></table>");
	chans.forEach( function (item, index, array) {
		html = "<tr><td><img width='50' src='"+item.logo+"' alt='-X-'> "+item.name+" </td><td><button id='rmc"+item.idc+"'>X</button>";

		if (index)
			html += "<button id='upc"+item.idc+"'>&uarr;</button>";
		if (index != array.length - 1)
			html += "<button id='dnc"+item.idc+"'>&darr;</button>";
		html += "</td></tr>";
		$("#schanst").append(html);
	});
}


$(document).ready(function() {
		$.ajax({url:"ajax.php?s=groups",
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
						groups = JSON.parse(ret);
						loadgroups(groups);
					}
			}
		});
		$.ajax({url:"ajax.php?s=cgroups",
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
						groups = JSON.parse(ret);
						loadcgroups(groups);
					}
			}
		});
	
	$("#download").click(function (e) {
		logit("update");
		$("#update").html("Downloading ...");
		$.ajax({url:"ajax.php?s=DOWNLOAD",
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
						$("#update").html("");
						groups = JSON.parse(ret);
						loadgroups(groups);
						$("#update").html("Done.");
					}
			}
		});
	});
	
	$("#groups").change(function (e) {
		group = $("#groups").val();
		logit(group);
		if (group != "") {

			$.ajax({url:"ajax.php?s=channels&g="+group,
				success: function (ret) {
						if (ret.substring(0,3) == "ERR") {
							logit(ret);
						}
						else {
							result = JSON.parse(ret);
							if (!txtlist) {
								$("#rawgrp").prop("checked", result.raw);
								$("#channels").empty();
								logit(result);
								result.chans.forEach( function (item, index) {
									$("#channels").append("<img id='chan_"+item.name+"' width='100px' src='"+item.logo+"' alt='"+item.name+"'>\n");
								});
								
							}
							else {
								$("#rawgrp").prop("checked", result.raw);
								$("#channels").empty();
								logit(result);
								result.chans.forEach( function (item, index) {
									$("#channels").append("<img id='chan_"+item.name+"' width='20px' src='"+item.logo+"' alt='-X-'>"+item.name+"<br/>\n");
								});								
							}
						}
					}
			});
		
		}
		else {
			$("#rawgrp").prop("checked", false);
			$("#channels").empty();			
		}
		
	});
	
	$("#newgrpfrm").submit(function (e) {
		e.preventDefault();
		var form = $(this);
		var url = form.attr('action');

		$.ajax({
			   type: "POST",
			   url: url,
			   data: form.serialize(), // serializes the form's elements.
			   success: function(ret)
			   {
					if (ret.substring(0,3) == "ERR") { logit(ret); }
					else {
						groups = JSON.parse(ret);
						loadcgroups(groups);
					}
			   }
         });
	});
	
	$("#cgroupstd").on('click', '[id^="dn"]', function(){
		var id = jQuery(this).attr("id").substring(2);
		$.ajax({url:"ajax.php?s=dng&id="+id,
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
						groups = JSON.parse(ret);
						loadcgroups(groups);
					}
			}
		});
	});
	
	$("#cgroupstd").on('click', '[id^="up"]', function(){
		var id = jQuery(this).attr("id").substring(2);
		$.ajax({url:"ajax.php?s=upg&id="+id,
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
						groups = JSON.parse(ret);
						loadcgroups(groups);
					}
			}
		});
	});
	
	$("#cgroupstd").on('click', '[id^="rm"]', function(){
		var id = jQuery(this).attr("id").substring(2);
		$.ajax({url:"ajax.php?s=rmg&id="+id,
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
						groups = JSON.parse(ret);
						loadcgroups(groups);
					}
			}
		});
	});
	
	$("#cgroupstd").on('click', '[id^="rn"]', function(){
		var id = jQuery(this).attr("id").substring(2);
		var name = prompt("Renommer "+cgroups[id], cgroups[id]);
		if (name != null) {
			name = encodeURIComponent(name);
			$.ajax({url:"ajax.php?s=rng&id="+id+"&n="+name,
				success: function (ret) {
						if (ret.substring(0,3) == "ERR") {
							logit(ret);
						}
						else {
							groups = JSON.parse(ret);
							loadcgroups(groups);
						}
				}
			});
		}
	});
	
	$("#schans").on('click', '[id^="dnc"]', function(){
		var id = jQuery(this).attr("id").substring(3);
		$.ajax({url:"ajax.php?s=dnc&id="+id,
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
							chans = JSON.parse(ret);
							loadschans(chans);
					}
			}
		});
	});
	
	$("#schans").on('click', '[id^="upc"]', function(){
		var id = jQuery(this).attr("id").substring(3);
		$.ajax({url:"ajax.php?s=upc&id="+id,
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
							chans = JSON.parse(ret);
							loadschans(chans);
					}
			}
		});
	});
	
	$("#schans").on('click', '[id^="rmc"]', function(){
		var id = jQuery(this).attr("id").substring(3);
		$.ajax({url:"ajax.php?s=rmc&id="+id,
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
							chans = JSON.parse(ret);
							loadschans(chans);
					}
			}
		});
	});
	
	$("#channels").on('click', '[id^="chan_"]', function(){
		var name = encodeURIComponent(jQuery(this).attr("id").substring(5));
		if (curcgroup) {
			$.ajax({url:"ajax.php?s=addschan&idg="+curcgroup+"&name="+name,
				success: function (ret) {
						if (ret.substring(0,3) == "ERR") {
							logit(ret);
						}
						else {
							chans = JSON.parse(ret);
							loadschans(chans);
						}
				}
			});
		}
	});
	
	$("#cgroupstd").on('change', 'input[type=radio][name="cgroupr"]', function(){
		curcgroup=this.value;
	    $.ajax({url:"ajax.php?s=schans&id="+this.value,
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
						chans = JSON.parse(ret);
						loadschans(chans);
					}
			}
		});
	});
	
	
	$("#rawgrp").change(function (e) {
		group = $("#groups").val();
		if (group != "") {
			v = (this.checked?"1":"0");
			$.ajax({url:"ajax.php?s=setraw&g="+group+"&v="+v,
				success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
				}
			});
		}
	});
	$("#rawlst").click(function (e) {
		 $.ajax({url:"ajax.php?s=rawlst",
			success: function (ret) {
					if (ret.substring(0,3) == "ERR") {
						logit(ret);
					}
					else {
						alert (ret);
					}
			}
		});
		
	});
});