/************************************************************************
 * 		Datenaustauschschnittstelle via JSON							*
 * 		C 2008 by René Lange & Thomas Müller & Steve Rohrlack @ mindbox	*
 ************************************************************************/

var DataHandler = new Class({
	tmpObj : new Array(),
	phpPath : '../tp/php/db_main.php',
	
	initialize : function(){
		if(location.href.search(/tpdev/) != -1){
			// phpPath automatisch auf Entwicklungsumgebung setzen
			file = location.href.replace(/(.*\/)|(\.html.*)/g,'').replace(/tpdev/,'tp_dev_');
			this.phpPath = this.phpPath.replace(/tp/,file);
		}
	},
	
	// Holt alle Tourdaten aus der Datenbank
	getData : function(id, todo) {
		var request = new Request.JSON({
	    		url: this.phpPath,	
				urlEncoded: true,
				async: false,
				headers: {"Content-type": "application/json"},
	    		onSuccess: function(responseObj, response){
					this.tmpObj = responseObj;
	    		}
	    	}
	    ).send('json='+JSON.encode({'todo':todo, 'id':id}));

		return request.tmpObj;
	},
	
	// Speichert eine Tour und liefert ID der Tour zurück
	setTour : function (tour) {
		var request = new Request.JSON({
	    		url: this.phpPath,	
				urlEncoded: true,
				async: false,
				headers: {"Content-type": "application/json"},
	    		onSuccess: function(responseObj, response){
					this.tmpObj = responseObj;
	    		}
	    	}
	    ).send('json='+JSON.encode({'todo':'savetour', 'id':tour}));

		return request.tmpObj;		
	},
	
	// Löscht eine Tour und liefert ID der Tour zurück
	deleteTour : function (id) {
		var request = new Request.JSON({
	    		url: this.phpPath,	
				urlEncoded: true,
				async: false,
				headers: {"Content-type": "application/json"},
	    		onSuccess: function(responseObj, response){
					this.tmpObj = responseObj;
	    		}
	    	}
	    ).send('json='+JSON.encode({'todo':'deletetour', 'id':id}));

		return request.tmpObj;		
	},	
	
	// Fragt den Nutzerstatus im System ab
	getUserStatus : function() {
		var request = new Request.JSON({
	    		url: this.phpPath,	
				urlEncoded: true,
				async: false,
				headers: {"Content-type": "application/json"},
	    		onSuccess: function(responseObj, response){
					this.tmpObj = responseObj;
	    		}
	    	}
	    ).send('json='+JSON.encode({'todo':'userstatus'}));

		return request.tmpObj;
	},
	
	// Holt ein Objekt mit Tour-Ids für Select-Boxen
	getTourIDs : function() {
		var request = new Request.JSON({
	    		url: this.phpPath,	
				urlEncoded: true,
				async: false,
				headers: {"Content-type": "application/json"},
	    		onSuccess: function(responseObj, response){
					this.tmpObj = responseObj;
	    		}
	    	}
	    ).send('json='+JSON.encode({'todo':'tourids'}));

		return request.tmpObj;
	},

	// Speichert eine Tour und liefert ID der Tour zurück
	bookTour : function (tour) {
		var request = new Request.JSON({
	    		url: '../templates/snippets/anfrage/book_tour_main.php',	
				urlEncoded: true,
				async: false,
				headers: {"Content-type": "application/json"},
	    		onSuccess: function(responseObj, response){
					this.tmpObj = responseObj;
	    		}
	    	}
	    ).send('json='+JSON.encode({'todo':'booktour', 'id':tour}));
		return request.tmpObj;
	}
});
