var controller = {
	
	explorer : null,
	workspace: null,
	url : 'sys/content.php?action=',
	load: 0,
	
	initialize : function () {		
		if($('menu')) {
			$$('#menu li').each(function(menu){
				menu.observe('click', controller.setContent);
			});			
			// get explorer and workspace
			controller.explorer  = $('main');
			controller.workspace = $('work');			
			controller.getContent('sett', controller.explorer);				
			// menu select 
			$('sett').addClassName('selected');
			// loading
			controller.loading();				
		}			
	},
	
	login : function (form)	{
		form.request({
  			onSuccess: function(data){ 
				controller.explorer.update(data.responseText);
				//controller.message(); 
			},			
			onFailure: controller.error
		});
	},
	
	logout : function () {
		new Ajax.Request(controller.url + 'logout', 
		{
		  	method: 'get',			
		  	onSuccess: function(data){
				controller.update(data.responseText, controller.explorer);
			},
			onFailure: controller.error
		});
	},
	
	help : function () {
		new Ajax.Request(controller.url + 'help', 
		{
		  	method: 'get',			
		  	onSuccess: function(data){
				controller.update(data.responseText, controller.explorer);
			},
			onFailure: controller.error
		});
	},
		
	setContent : function (event) {
		clicked = event.element();		
		controller.setMenu(clicked);
		controller.getContent(clicked.identify(), controller.explorer);
		controller.update('', controller.workspace);
	},
	
	setMenu : function (clicked) {		
		$$('#menu li').each(function(li){
			if(li !== clicked) li.removeClassName('selected');
		});
		clicked.addClassName('selected');		
	},	
	
	getContent : function(what, where) {		
		new Ajax.Request(controller.url + what, 
		{
		  	method: 'get',			
		  	onSuccess: function(data){
				controller.update(data.responseText, where);
				controller.targetfrm();
			},
			onFailure: controller.error
		});
	},
	
	update : function (what, where)	{		
		where.innerHTML = what;
	},
	
	loading : function () {		
		Ajax.Responders.register({
			onCreate: function() { $('loading' ).show(); },
			onComplete: function() {
				if (0 == Ajax.activeRequestCount) {
					$('loading' ).hide();
				}
				controller.message();				
			}
		});
	},	
	
	refresh : function (type, both) {
		controller.getContent(type, controller.explorer);
		if(both) {
			controller.clear.delay(1.3);
		}
	},
	
	sending : function () {
		$('status').update('<span class="note red">Odesílám data..</span>').show();
	},	
	
	message : function () {		
		if($('sysmess')) {
			Effect.Pulsate('sysmess', { pulses:2, duration:0.5 });
		}
	},
	
	targetfrm : function ()	{
		if(form = $('wrkfrm')) {
			form.focusFirstElement();
		}
	},
	
	clear : function ()	{
		controller.update('', controller.workspace);
		return; // false
	},
	
	error : function () {
		alert('Error');
	},

	sortable : function (id, tag) {
		if($(id)) {
			Sortable.create(id,
			{	
				tag: tag,
				onUpdate: function(list) {
					$('itemord').value = Sortable.serialize(list).escapeHTML();
					$('srtsave').removeAttribute('disabled');
				}
			});
		}
	}
	
};


var subject = {
	
	// returns insertion forms
	add : function (type) {	
		new Ajax.Request(controller.url + type +'-add', 
		{
			method: 'get',
			onSuccess: function(data){
				controller.update(data.responseText, controller.workspace);
				controller.targetfrm();				
			},
			onFailure: controller.error
		});		
	},	
	
	// returns edit forms
	mod : function (type, id) {	
		new Ajax.Request(controller.url + type + '-mod' + '&id=' + id, 
		{
			method: 'get',
			onSuccess: function(data){
				controller.update(data.responseText, controller.workspace);
				controller.targetfrm();
			},
			onFailure: controller.error
		});	
	},
	
	del : function (type, form) {
		
		if((typeof form) != 'object') {
			
			new Ajax.Request(controller.url + type + '-del' + '&id=' + form,
			{
				method: 'get',
				onSuccess: function(data){
					controller.update(data.responseText, controller.workspace);
				},
				onFailure: controller.error
			});
		} 
		else {
			form.request({
	  			onSuccess: function(data){ 
					controller.workspace.update(data.responseText);
					controller.getContent(type, controller.explorer);					
				},
				onFailure: controller.error
			});
		}
				
	},
	
	archive : function (type, form) {
		
		if((typeof form) != 'object') {
			
			new Ajax.Request(controller.url + type + '-archive' + '&id=' + form,
			{
				method: 'get',
				onSuccess: function(data){
					controller.update(data.responseText, controller.workspace);
					controller.getContent(type, controller.explorer);
				},
				onFailure: controller.error
			});
			
		} 
		else {
			form.request({
	  			onSuccess: function(data){ 
					controller.workspace.update(data.responseText);
					controller.getContent(type, controller.explorer);					
				},
				onFailure: controller.error
			});
		}
				
	},
	
	send : function (type, form) {	// sends form
		form.request({
  			onSuccess: function(data){ 
				controller.workspace.update(data.responseText);
				controller.getContent(type, controller.explorer);					
			},
			onFailure: controller.error
		});
	},
	
	save : function (type, form) {	// sends form with edits
		form.request({
  			onSuccess: function(data){ 
				controller.workspace.update(data.responseText);
				controller.getContent(type, controller.explorer);
			},		
			onFailure: controller.error
		});
	},
	
	pic_remove : function (type, id) {		
		new Ajax.Request(controller.url + type + '-pic-rem' + '&id=' + id, 
		{
			method: 'get',
			onSuccess: function(data){
				controller.update(data.responseText, $('acts-pic').parentNode);				
			},
			onFailure: controller.error
		});
	},
	
	pic_change : function (type, id) {
		new Ajax.Request(controller.url + type + '-pic-add' + '&id=' + id, 
		{
			method: 'get',
			onSuccess: function(data){
				controller.update(data.responseText, controller.workspace);
				if(refresh = $('refresh'))
				{
					refresh.appear();
				}
			},
			onFailure: controller.error
		});
	},
	
	file_remove : function (type, id) {		
		new Ajax.Request(controller.url + type + '-file-rem' + '&id=' + id, 
		{
			method: 'get',
			onSuccess: function(data){
				controller.update(data.responseText, $('down-file').parentNode);
				controller.refresh(type, false);
			},
			onFailure: controller.error
		});
	},
	
	file_change : function (type, id) {
		new Ajax.Request(controller.url + type + '-file-add' + '&id=' + id, 
		{
			method: 'get',
			onSuccess: function(data){
				controller.update(data.responseText, controller.workspace);
			},
			onFailure: controller.error
		});
	},
	
	gall_edit : function (id) {
		new Ajax.Request(controller.url + 'gall-cont' + '&id=' + id, 
		{
			method: 'get',
			onSuccess: function(data){
				controller.update(data.responseText, controller.explorer);
				controller.update.delay(2, '', controller.workspace);
			},
			onFailure: controller.error,
			onComplete: function () {
				controller.sortable('gallsrt', 'li');
			}
		});
	},
	
	gall_save : function (id, order) {
		new Ajax.Request(controller.url + 'gall-save' + '&id=' + id + '&' + order.replace(/amp;/gi, ''), 
		{
			method: 'get',
			onSuccess: function(data){
				controller.update(data.responseText, controller.workspace);
				$('srtsave').setAttribute('disabled', 'disabled');
			},
			onFailure: controller.error
		});
	},
	
	pict_edit : function (form) {
		form.request({
  			onSuccess: function(data){ 
				controller.workspace.update(data.responseText);
				if($('sysmess'))
				{
					new Effect.Highlight('sysmess', {startcolor:'#d1e0ef'});
				}
			},
			onFailure: controller.error
		});
	},
	
	pict_del : function (form, idp, idg) {
		if(typeof form != 'object') {
			new Ajax.Request(controller.url + form + '&idp=' + idp + '&idg=' + idg, 
			{
				method: 'get',
				onSuccess: function(data){
					controller.update(data.responseText, controller.workspace);
					new Effect.Highlight('wrkfrm', {startcolor:'#d1e0ef'});
				},
				onFailure: controller.error
				
			});
		} else
		{
			form.request({
	  			onSuccess: function(data){ 
					controller.workspace.update(data.responseText);
					subject.gall_edit(idg);					
				},
				onFailure: controller.error
			});
		}		
	}
	
};

// document.observe("dom:loaded", function() { controller.initialize(); });
document.observe("dom:loaded", controller.initialize);