// Содержание


function Content () {
	
	this.data = new Array();

	this.create = function (name, content) {
		this.data[name] = content;
	}
}

// Запуск скриптов
Content.prototype.RunJava = function (action) {
	eval(action);
}

// Debuger
Content.prototype.Echo = function (text) {
	var echo_content = $('echo');
	
	if ( echo_content === null) {
		var tempDiv = document.createElement('div');
		tempDiv.id = 'echo';
		tempDiv.style.position = 'absolute';
		tempDiv.style.right = '0px';
		tempDiv.style.zIndex = '1000';
		tempDiv.style.minWidth = '200px';
		tempDiv.style.background = 'red';
		tempDiv.style.color = 'white';
		tempDiv.style.padding = '5px';
		tempDiv.onclick = function () { tempDiv.remove(); };
		document.body.appendChild(tempDiv);
		var echo_content = $('echo');
	}
	
	var currentOffset = document.documentElement.scrollTop || document.body.scrollTop;	
	echo_content.style.top = currentOffset + 'px';
	
	echo_content.innerHTML = text + '<br />' + echo_content.innerHTML;
	echo_content.show();
	
	//setTimeout("$('echo').hide();", 3000);
}

 
Content.prototype.aj = function (action, params, callback) {
	var aj = new Ajax.Request('/aj/' + action, 
		 { method:'post', parameters: params,
		   onSuccess: function(transport) {
				callback(transport);
		   }, 
		   onFailure: function() { 
		   		callback(null);
				alert('Ошибочка ... повторите попытку позже');
		   } });
}


// Получение json данных
Content.prototype.ajJson = function (action, params, callback) {
	
	// var callback;
	
	new Ajax.Request('/aj/' + action, 
		 { method:'post', parameters: params,
		   onSuccess: function(transport) {
		   
		   		// Content.Echo(transport.responseText);
		   
				var json  = transport.responseText.evalJSON();
				
				// Content.Echo(transport.responseText);
				// При некорректных данных выход
				if ( !json || json === null ) {
					Content.Echo(transport.responseText);
					return;
				}
				// Обратка				
				if (typeof(callback) != 'undefined') callback(json);				
				
				// Запуск скрипта
				if ( json.RunJava ) Content.RunJava(json.RunJava);
				
		   }, 
		   onFailure: function() { 
		   		if (typeof(callback) != 'undefined') callback(null);				
				al('Ошибочка ... повторите попытку позже');
		   } });	
}


// ajax
function as(params) {
	var aj = new Ajax.Request('/aj/' + params.action, 
		 { method:'post', parameters: params.data,
		   onSuccess: function(transport) {
		   	
		   		if ( params.type == 'text' ) {
		   			var request = transport.responseText;
		   		} else {
		   			var json  = transport.responseText.evalJSON();

		   			// При некорректных данных выход
					if ( !json || json === null ) {
						// Content.Echo(transport.responseText);
						return;
					}
					
					// Запуск скрипта
					if ( json.RunJava ) Content.RunJava(json.RunJava);					
		   			
		   			var request = json;
		   		}
		   		
		   		// возврат
		   		if ( params.callback ) params.callback(request);
		   }, 
		   onFailure: function() { 
				al('Ошибочка ... повторите попытку позже');
		   } });	
}

var Content = new Content;
