// SPManager own JS

var SPManager = new Class({
	eurl: null,
	getUrl: function (){
		return this.eurl + '&rnd=' + this.getRandom(1,999999);
	},
	sendRequest: function (object, action, uid, field, value, callback){
		// example send ('offer', 'set', '343', 'height', '5', messagehandler);
		// example send ('offer', 'get', '343', '', '', messagehandler);
		//if ( store.arguments.length > 4 ) uid = store.arguments[4];
		var req = new Request.JSON({
				method: 'post',
				url: this.getUrl(),
				data: {
					'object' : object,
					'action': action,
					'uid' : uid,
					'field' : field,
					'value' : value
				 },
				onComplete: function (obj){
					// check if theres a message to display
					SPM.processResult(obj);
					// call the callback handler
					if ( callback !== null ) callback(obj);
				}
		});
		spQueue.addRequest('req_' + object + this.getRandom(1,999999), req);
		req.send();
	},

	processResult: function (obj){

		// remove error box
		if ( $('error_box') ) $('error_box').fade('out');
		if ( $('hint_box') ) $('hint_box').fade('out');

		// check if error code has been given
		if ( obj && obj.hint == '1' ) {
			this.say(obj.msg);

		} else if ( obj && obj.hint == '2' ) {
			// display error as box
			var myError = $('hint_box');
			if ( myError ) myError.set('text', obj.msg);
			else {
				myError  = new Element('div', {id: 'hint_box', text: obj.msg});
				$('website').grab(myError);
			}
			myError.fade('hide').fade('in');
			myError.addEvent('click', function(e){e.stop();this.fade('out');});

		} else if ( obj && obj.error == '1' ) {
			this.tell(obj.msg);

		} else if ( obj && obj.error == '2' ) {
			// display error as box
			var myError = $('error_box');
			if ( myError ) myError.set('text', obj.msg);
			else {
				myError  = new Element('div', {id: 'error_box', text: obj.msg});
				$('website').grab(myError);
			}
			myError.fade('hide').fade('in');
			myError.addEvent('click', function(e){e.stop();this.fade('out');});
		}
	},

	tell:function(text){
			new Message({
			  icon: "cautionMedium.png",
			  title: "Achtung",
			  message: text
			}).tell();
	},

	say:function(text){
			new Message({
				  icon: "okMedium.png",
				  title: 'Info',
				  message: text
			}).say();
	},

	ask:function(title, text, callback){
			new Message({
				  icon: "okMedium.png",
				  title: title,
				  message: text,
				  callback: callback
			}).ask();
	},

	getRandom: function ( min, max ) {
		// just returns a random value as int
		if( min > max ) return( -1 );
		if( min == max ) return( min );
		return( min + parseInt( Math.random() * ( max-min+1 ) ) );
	},

	replaceHTML: function (id, object, action, uid, callback){

		if ( typeof(id)=='string' ) id = $(id);

		var htmlreq = new Request.HTML({
				method: 'post',
				url: this.getUrl(),
				data: {
					'object' : object,
					'action': action,
					'uid' : uid
				 },
				onComplete: callback,
				evalScripts: true,
				useSpinner: true,
				update: id
		});
		spQueue.addRequest('html_req_' + object + this.getRandom(1,999999), htmlreq);
		htmlreq.send();
		return;
	},

	sendForm: function(form, callback){

		var myHTMLRequest = new Request.JSON({
				method: 'post',
				url:SPM.getUrl(),
				onComplete: function(obj){
					// check if theres a message to display
					SPM.processResult(obj);
					// call the callback handler
					callback(obj);
				}
		}).post($(form));
	},

	sendFormAndReplaceHTML: function(id, form, callback){
		if ( typeof(id)=='string' ) id = $(id);
		var myHTMLRequest = new Request.HTML({
				method: 'post',
				url:SPM.getUrl(),
				update: id,
				evalScripts: true,
				useSpinner: true,
				onComplete: function(obj){
					// check if theres a message to display
					SPM.processResult(obj);
					// call the callback handler
					if ( callback ) callback(obj);
				}
		}).post($(form));
	},

	dump: function (arr,level){

		/**
		 * Function : dump()
		 * Arguments: The data - array,hash(associative array),object
		 *    The level - OPTIONAL
		 * Returns  : The textual representation of the array.
		 * This function was inspired by the print_r function of PHP.
		 * This will accept some data as the argument and return a
		 * text that will be a more readable version of the
		 * array/hash/object that is given.
		 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
		 */
		var dumped_text = "";
		if(!level) level = 0;

		//The padding given at the beginning of the line.
		var level_padding = "";
		for(var j=0;j<level+1;j++) level_padding += "    ";

		if(typeof(arr) == 'object') { //Array/Hashes/Objects
			for(var item in arr) {
				var value = arr[item];

				if(typeof(value) == 'object') { //If it is an array,
					dumped_text += level_padding + "'" + item + "' ...\n";
					dumped_text += dump(value,level+1);
				} else {
					dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
				}
			}
		} else { //Stings/Chars/Numbers etc.
			dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
		}
		return dumped_text;
	},

	initToolTips: function(id){
		if ( !id ) id = ''; else id = id + ' ';
		/* init tooltips */
		$$(id + '.tips').removeEvents();
		$$(id + '.tips').addEvent('mouseover', function(e){
		  new Message({icon: 'mediumQuestion.png', passEvent: e, callingElement: this, stack: false}).tip();
		});
		/* init tooltips */
		$$(id + '.tips').addEvent('click', function(e){
		  new Message({	icon: 'mediumQuestion.png',	passEvent: e,callingElement: this, stack: false}).tip();
		});
	},

	initCalendars: function(){
		/* init calendars */
		MooTools.lang.set('de-DE', 'Date', {
			months:    ['Januar', 'Februar', 'M&auml;rz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
			days:      ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
			dateOrder: ['date', 'month', 'year', '/']
		});
		MooTools.lang.setLanguage('de-DE');
	},

	clearRequestQueue: function(){
		spQueue.clear();
	}


});

var SPM = new SPManager();
var	spQueue = new Request.Queue();
var realUrl = String(document.location);

if ( realUrl.indexOf("https") == -1 ) SPM.eurl = 'http://www.aufkleber-produktion.de/?eID=spmanager';
else SPM.eurl = 'https://www.aufkleber-produktion.de/?eID=spmanager';

