
function task_update() {
	$.ajax({
        type: "POST",
        url: "index.php",
        data: "&site=overview/ajax_trigger&action=get_tasks",
        dataType: 'json',
        success: process_task_update
    });
}


function process_task_update($msg) {
	  var $max = 0;
	  
	  if($msg != null) {
		  $.each($msg, function(key, val) {
			  $max++;
		  });
	  }
	  
	  var $tableinput = '';
	  
	  if($max > 0) {
		  $('#tasktitle').text('Task (' + $max + ')');

		  $.each($msg, function($key, $val) {
			  $tableinput += "<tr><td>" + $val['objectname'] + "</td>"
			  if($val['taskstate'] == 'running') {
				  $tableinput += "<td><img src=\"images/task_running.gif\"></td>";
			  } else {
				  $tableinput += "<td><img src=\"images/table_active.gif\"></td>";
			  }
			  $tableinput += "<td>" + $val['started'] + "</td>";
			  $tableinput += "<td>" + $val['tasktype'] + "</td>";
			  $tableinput += "</tr>";
		  });
			
		  $('#taskcontainer').removeClass('taskcontainerhidden');
	  } else {
		  $('#tasktitle').text('Task');
	  }
	  
	  $('#taskbody').empty();
	  $('#taskbody').append(' \
		<table> \
		<tr> \
		<th width="200px">Name</th> \
		<th width="50px">Status</th> \
		<th width="150px">Gestartet</th> \
		<th width="350px">Information</th> \
		</tr> \
		' + $tableinput + ' \
	</table>');

	window.setTimeout("task_update()", 5 * 1000); // 5 sec
}


function toggle_task_container() {
	var $close = $('#taskcontainer').hasClass('taskcontainerclose');
	if($close) {
		$('#taskcontainer').removeClass('taskcontainerclose');
		$('#taskcontainer').addClass('taskcontaineropen');
		$.ajax({
	        type: "POST",
	        url: "index.php",
	        data: "&site=overview/ajax_trigger&action=trigger_taskbar_open"
	    });
	} else {
		$('#taskcontainer').addClass('taskcontainerclose');
		$('#taskcontainer').removeClass('taskcontaineropen');
		$.ajax({
	        type: "POST",
	        url: "index.php",
	        data: "&site=overview/ajax_trigger&action=trigger_taskbar_close"
	    });
	}
}


function toggle_menu($msg) {
	
	var $div = "#" + $msg;
	var $parent_div = "#" + $msg + "_parent"; 
	
	$($parent_div).slideToggle("slow", function() {
		if($($parent_div).is(":hidden")) {

			$($div).addClass("menuhead_close");
			$($div).removeClass("menuhead_open");
		} else {
			$($div).addClass("menuhead_open");
			$($div).removeClass("menuhead_close");
		}
	});
}


function switch_kvm_port($s_id) {
	$("#dialog_kvm_switch").dialog({
		bgiframe: true,
		height: 80,
		modal: true
	});
	
	$.ajax({
        type: "POST",
        url: "index.php",
        data: "&site=server/ajax_helper&action=switch_kvm&s_id=" + $s_id,
        async: true,
        success: function(data) {
			$("#dialog_kvm_switch").dialog('close');
		}
    });
	
	return true;
}

