// JavaScript Document

function $(element) { return document.getElementById(element); }

// dodaje do danego elementu zdarzenie
function addEvent(element, eventName, func) {
	if (element.addEventListener) {
		element.addEventListener(eventName, func, false);
	} else {
		element.attachEvent("on" + eventName, func);
	}
}

function showProgress(element, flag) {
	if (flag) element.innerHTML = "<center><img src='http://nietsnie.com/asradio/progressf.gif' alt='progress...'></img></center>";
	else element.innerHTML = "";
}

function showLoginDlg() {
	var djPanelLogin = $("dj_panel_login");
	djPanelLogin.style.display = "block";
	
	var header = $("login_header");
	var errors = $("login_errors");
	var loginField = $("input_login");
	var passwdField = $("input_passwd");
	
	header.innerHTML = "Wpisz login i hasło, nacisnij ENTER";
	loginField.value = "Podaj login";
	passwdField.value = "Podaj hasło";
	passwdField.type = "text";
	
	function serviceKey(ev) {
		if (ev.keyCode == 13) {
			//showProgress(errors, true);
			var res = new KamAjax();
			res.getPlainText("panel/services/login_service.php?cmd=login&login=" + loginField.value + "&passwd=" + passwdField.value, 
					function(xhr) {
						if (xhr.responseText == "good") {
							//showProgress(errors, false);
							djPanelLogin.style.display = "none";
							showContent();
						}
						else {
							errors.innerHTML = "zły login lub hasło";							//alert(xhr.responseText);
							showLoginDlg();
						}
					} );
		}
	}
	
	
	addEvent(loginField, "keypress", serviceKey);
	addEvent(passwdField, "keypress", serviceKey);
	addEvent(loginField, "focus", function() {
			loginField.value = "";
		});
	addEvent(passwdField, "focus", function() {
			passwdField.value = "";
			passwdField.type = "password";
		});
}	

var oldBtn;

function showContent() {
	var timer;
	var djPanelLogin = $("dj_panel_login");
	var djPanelCont = $("dj_panel_content");
	djPanelLogin.style.display = "none";
	djPanelCont.style.display = "block";
	
	var contentText = $("content_text");
	var logoutBtn = $("button_logout");
	
	addEvent(logoutBtn, "mouseup", function() {
			var res = new KamAjax();
			res.getPlainText("panel/services/login_service.php?cmd=logout", function(){});
			djPanelCont.style.display = "none";
			showLoginDlg();
		});
	
	oldBtn =  $("bar_" + "info");
	showContentText("info");
	addEvent($("bar_kicker"), "click", function() {showContentText("kicker");});
	addEvent($("bar_spontan"), "click", function() {showContentText("spontan");});
	addEvent($("bar_hasla"), "click", function() {showContentText("hasla");});
	addEvent($("bar_info"), "click", function() {showContentText("info");});
	addEvent($("bar_pozdro"), "click", function() {showContentText("pozdro");});
	addEvent($("bar_stats"), "click", function() {showContentText("stats");});
}

function showContentText(cont) {
	var activeBtn = $("bar_" + cont);
	oldBtn.style.backgroundColor = "#444444";
	var contText = $("content_text");
	var res = new KamAjax();
	
	activeBtn.style.backgroundColor = "#888888";
	oldBtn = activeBtn;
	showProgress(contText, true);
	res.getPlainText("panel/content/" + cont + ".php", 
			function(xhr) {
				contText.innerHTML = xhr.responseText;
				showProgress("content_progress", false);
			} );
}

function kickerglownyService(action) {
	var info = $("content_info");
	var res = new KamAjax();
	res.getPlainText("panel/services/kickerglowny_service.php?cmd=" + action, 
			function(xhr) {
				if(xhr.responseText == "good") info.innerHTML = "Autopilot wywalony!";
				
			} );
}

function kickerliveService(action) {
	var info = $("content_info2");
	var res = new KamAjax();
	res.getPlainText("panel/services/kickerlive_service.php?cmd=" + action, 
			function(xhr) {
				if(xhr.responseText == "good") info.innerHTML = "DJ Wywalony!";
				
			} );
}

function pozdroService(action, id) {
	var info = $("content_info");
	var res = new KamAjax();
	var pozdr = $("pozdr_" + id);
	res.getPlainText("panel/services/pozdro_service.php?cmd=" + action + "&id=" + id, 
			function(xhr) {
				if(xhr.responseText == "good") {
					info.innerHTML = "Pozdrowienie usuniete!";
					var parent = pozdr.parentNode;
					parent.removeChild(pozdr);
				}
				
			} );
}
