
var nameplateObject = new Class({

	photo: null,
	nickname: null,
	gender: null,
	nameplate: null,
	nameplates: null,
	overlay: null,
	friends: null,
	origFriends: null,
	found: [],
	suggestTimer: null,
	markedFriend: null,
	pattern: null,
	patternChanged: false,
	ipt: null,
	timer: null,
	searching: false,
	research: false,
	dummy: false,
	allowautorotate: true,
	hideTimer1: null,
	hideTimer2: null,
	editUnknown: false,
	setUnknown: false,
	
	'initialize': function(photo, nameplate, overlay, nickname) {
		if(!photo) {
			return false;
		}
		this.photo = $(photo);
		if(!this.photo) {
			this.photo = $('photo');
		}
		
		if(!nameplate) {
			return false;
		}
		this.nameplate = $(nameplate);

		if(!overlay) {		
			return false;
		}
		
		this.overlay = overlay;
		this.nickname = nickname;
		if(!this.nickname || this.nickname == "") {
			this.nickname = $('nameplate-content').get('text');
		}
		this.gender = $('nameplate-gender').get('value');
		
		this.form = $('nameplate-friends');
		this.photo.addClass('nameplates-enabled');
		this.ipt = $('nameplate_nickname');
		this.dummy = $$('.dummy');
		
		this.overlay.getElementById('nameplate_nickname').removeEvents('choiceSelect').addEvent('choiceSelect', this.changeTitle.bind(this));
		/*this.overlay.getElementById('nameplate_nickname').addEvent('change', this.changeTitle.bind(this));*/
		this.overlay.getElementById('nameplate_set').removeEvents('click').addEvent('click', this.releaseNameplate.bind(this));
		this.overlay.getElementById('invitefriend').removeEvents('click').addEvent('click', this.invitefriend.bind(this));		
		this.overlay.getElementById('nameplate_unknown').removeEvents('click').addEvent('click', this.releaseUnknown.bind(this));
		this.overlay.getElementById('nameplate_hide').removeEvents('click').addEvent('click', this.hideOverlay.bind(this));
		
		if (this.overlay.getElementById('nameplate_set_fb'))
			this.overlay.getElementById('nameplate_set_fb').addEvent('click', this.hideOverlay.bind(this));
		this.overlay.getElementById('nameplate_hide').removeEvents('click').addEvent('click', this.hideOverlay.bind(this));
		this.nameplate.getElementById('nameplate_rotate').removeEvents('click').addEvent('click', this.rotate.bind(this));
		$$('.openUnknown').removeEvents('click').addEvent('click', this.showUnknown.bind(this));
		if($('setnameplate')){
			$('setnameplate').removeEvents('click').addEvent('click', this.showit.bind(this));
		}
		
		/*blättern mit den pfeiltasten*/
		this.overlay.getElementById('nameplate_nickname').removeEvents('focus').addEvent('focus', this.keysoff.bind(this));
		this.overlay.getElementById('nameplate_nickname').removeEvents('blur').addEvent('blur', this.keyson.bind(this));
		this.overlay.getElementById('nameplate_email').removeEvents('focus').addEvent('focus', this.keysoff.bind(this));
		this.overlay.getElementById('nameplate_email').removeEvents('blur').addEvent('blur', this.keyson.bind(this));
		
		/*this.overlay.makeDraggable({
			onDrag: function() {
				this.updateDummyPosition();
			}.bind(this)
		});*/
		
		this.dummy.each(function(item) {
			item.makeDraggable({
				'container': this.photo,
				'onStart': function(d) {
					this.allowautorotate = false;
					this.nameplate.position({
						relativeTo: item
					});
					// class tauschen
					this.nameplate.removeProperty('class');
					this.nameplate.set('class', d.get('class'));
					this.nameplate.removeClass('dummy');
					
					this.hideDummy();
					this.overlay.hide();
					
				}.bind(this),
				'onDrag': function(d) {
					this.nameplate.position({
						'x': d.getPosition().x - 194,
						// tp mit branding oder ohne -.-
						'y': d.getPosition().y - 88 - $$('.bigsizebanner_container')[0].getStyle('height').toInt()
					});
				}.bind(this),
				'onComplete': function(d) {
					this.showDummy();
					this.onDragComplete(d);
					this.updateDummyPosition();
				}.bind(this)
			}, this);
		}, this);
		
		this.nameplate.makeDraggable({
			'container': this.photo,
			'onStart': function() {
				this.hideDummy();
				this.overlay.hide();				
			}.bind(this),
			'onDrag': function(d) {
				// nur dann auto.drehen wenn erlaubt 
				if(this.allowautorotate) {
					clickPosX = (d.getPosition().x - this.photo.getPosition().x);
					clickPosY = (d.getPosition().y - this.photo.getPosition().y);
					this.clearDirectionClass(this.nameplate);
					if(clickPosX < photo.width/2) {
						if(clickPosY < photo.height/2) {
							this.nameplate.addClass('right-bottom');
						}
						else {
							this.nameplate.addClass('right-top');
						}
					}
					else {
						if(clickPosY < photo.height/2) {
							this.nameplate.addClass('left-bottom');
						}
						else {
							this.nameplate.addClass('left-top');
						}
					}
				}
			}.bind(this),
			'onComplete': function(f) {
				this.showDummy();
				this.onDragComplete(f);
				this.updateDummyPosition();
			}.bind(this)
		});
		this.photo.removeEvents('click').addEvent('click', this.evtClick.bind(this));
		this.nameplates = $$('.nameplate');
			
	},
	
	hideDummy: function() {
		this.dummy.setStyle('visibility', 'hidden');
	},
	
	showDummy: function() {
		this.editUnknown = false;
		this.dummy.setStyle('visibility', 'visible');
	},
	
	updateDummyPosition: function() {
		this.dummy.position({
			relativeTo: $('nameplate-overlay'),
			offset: { y: '-55', x: '55' }
		});
		
		this.dummy.each(function(item) {
			if(item.hasClass('right-top') || item.hasClass('left-bottom')) {
				item.setStyle('left', item.getStyle('left').toInt() + 100 + 'px');
			}
			if(item.hasClass('right-bottom') || item.hasClass('left-bottom')) {
				item.setStyle('top', item.getStyle('top').toInt() - 40 + 'px');
			}
		}, this);
	},
	
	onDragComplete: function(f) {
		var photo = $('photo');
		var canvas = $('photo-canvas').getCoordinates();
		var x = this.nameplate.getPosition().x;
		var y = this.nameplate.getPosition().y;
		var position = this.nameplate.getPosition();
		var clickPosX = x - photo.getPosition().x;
		var clickPosY = y - photo.getPosition().y;
		
		if(this.allowautorotate) {
			this.clearDirectionClass(this.nameplate);
			this.showDummy();
		}
		
		if(clickPosX < photo.width/2) { // linke oder rechte seite?
			offset = 80; // fenster geht rechts vom np auf
			if(this.allowautorotate) {
				// oben oder unten
				if(clickPosY < photo.height/2) {
					this.nameplate.addClass('right-bottom');
					$$('.nameplate.dummy.right-bottom')[0].setStyle('visibility', 'hidden'); // dummy tauschen
					direction = 1;
				}
				else {
					this.nameplate.addClass('right-top');
					$$('.nameplate.dummy.right-top')[0].setStyle('visibility', 'hidden'); // dummy tauschen	
					direction = 2;
				}
			}
		}
		else {
			offset = 685; // fenster geht links vom np auf
			if(this.allowautorotate) {
				// oben oder unten
				if(clickPosY < photo.height/2) {						
					this.nameplate.addClass('left-bottom');
					$$('.nameplate.dummy.left-bottom')[0].setStyle('visibility', 'hidden'); // dummy tauschen	
					direction = 0;
				}
				else {
					this.nameplate.addClass('left-top');
					$$('.nameplate.dummy.left-top')[0].setStyle('visibility', 'hidden'); // dummy tauschen
					direction = 3;
				}
			}
		}
		
		// overlay ausrichten
		this.overlay.setStyles({
			'top': position.y -178,
			'left': position.x - offset
		});
		
		this.overlay.show();
		this.updateInfo();
		this.checkDirection();
	},
	
	clearDirectionClass: function(el) {
		el.removeClass('left-bottom');
		el.removeClass('left-top');
		el.removeClass('right-bottom');
		el.removeClass('right-top');
		
		return el;
	},
	
	updateFriends: function(response, ownname) {
		
		ul = $('np-friends');
		ul.getElements('li').dispose();
		
		if($('load_np_friends')) {
			$('load_np_friends').dispose();
		}		
		
		if(ownname) {
			li = new Element('li', {
				'class': 'nameplate-friend own',
				'styles': {
					'border-bottom': '1px solid #ddd'
				}
			});
			cb = new Element('input', {
				'type': 'checkbox',
				'class': 'np_friend_suggest',
				'value': this.gender,
				'name': this.nickname,
				'id': 'cb_' + this.nickname,
				'checked': 'checked'
			});
			
			lbl = new Element('label', {
				'for': 'cb_' + this.nickname,
				'text':  this.nickname + ' (ich)'
			});
			
			cb.inject(li);
			lbl.inject(li);
			li.inject(ul);
			cb.addEvent('click', this.markFriend.bind(this, [cb]));
			li.addEvent('click', this.markFriend.bind(this, [cb]));
			this.markFriend(cb, true);			
		}
		
		var friendCount = 0;
		var check = false;
		var checked = false;
		var lastCB = false;
		response.each(function(friend, idx) {
			if(!friend) {
				return;
			}
			line = '';
			extraClass = '';

			// freunde von nicht freunden trennen
			if(idx == this.origFriends.length) {
				line = 'line';
			}
			
			// nicht freunde
			if(friend[4]=='m') {
				// zum checken vorbereiten
				if(friendCount == 1 && !check) {
					check = true;
				}
				// andere farbe bei nicht-freunden
				extraClass = 'nofriend';
			}
			else {
				// freunde zählen
				friendCount++;
			}
			li = new Element('li', {
				'class': 'nameplate-friend ' + extraClass + ' ' + line
			});
			
			cb = new Element('input', {
				'type': 'checkbox',
				'class': 'np_friend_suggest',
				'value': friend[7],
				'name': friend[0],
				'id': 'cb_' + friend[0]
			});
			
			lbl = new Element('label', {
				'for': 'cb_' + friend[0],
				'text': friend[0] + ' (' + friend[1] + ' ' + friend[2].substr(0,1) + '.' + ')'
			});
			
			cb.inject(li);
			cb.addEvent('click', this.markFriend.bind(this, [cb]));
			li.addEvent('click', this.markFriend.bind(this, [cb]));
			lbl.inject(li);
			li.inject(ul);
			
			// markieren wenn nur 1 freund gefunden und andere nicht-freunde
			if(check && !checked) {
				this.markFriend(lastCB, true);
				checked = true;
			}
			
			// markieren wenn nur 1 freund gefunden und sonst nichts
			if(!response[idx+1] && friendCount == 1 && !checked) {
				this.markFriend(cb, true);
				checked = true;
			}
			
			lastCB = cb;
		}, this);
		
				
	},
	
	markFriend: function(el, auto) {
		$$('.np_friend_suggest').removeProperty('checked');
		el.set('checked', 'checked');
		this.markedFriend = el.get('name');
		if(!auto) {
			this.ipt.set('value', el.get('name'));
		}
		if(this.editUnknown) {
			np = this.editUnknown.getParent('span.nameplate');
			np.removeClass('female');
			np.removeClass('male');
			if(el.get('value') == 1) {
				np.addClass('male');
			}
			else {
				np.addClass('female');
			}
			this.editUnknown.set('text', el.get('name'));
			
			if(np.hasClass('left-bottom')) {
				direction = 0;
			} else if(np.hasClass('right-bottom')) {
				direction = 1;
			} else if(np.hasClass('right-top')) {
				direction = 2;
			} else if(np.hasClass('left-top')) {
				direction = 3;
			}
			$('nameplate_t').set('value', direction);
		}
		else {
			if(this.setUnknown) {
				$('nameplate_unknown_ipt').set('value', 0);
				this.nameplate.removeClass('unknown');
			}
		}
		this.nameplates.each(function(item) {
			if(!item.hasClass('editable')) {
				return;
			}
			item.removeClass('female');
			item.removeClass('male');
			if(el.get('value') == 1) {
				item.addClass('male');
			}
			else {
				item.addClass('female');
			}
			name = this.get('name'); // 10 zeichen
			if(name.length > 10) {
				name = name.substr(0,10) + '...';
			}
			item.getElement('span.center').set('text', name);
		}, el);
		
		// this.updateInfo();
		$('user_id').set('value', el.get('name'));
	},
	
	keyUp: function(e) {
		this.pattern = this.ipt.value;
		this.patternChanged = (this.pattern.length != this.lastLength);
		
		if(!this.patternChanged && e) {
			return;
		}
		
		if(this.pattern.length == 0) {
			$clear(this.timer);
			$clear(this.suggestTimer);
			this.updateFriends(this.origFriends, true);
			return;
		}

		if(this.pattern.length < this.lastLength && this.found.length > 0) {
			this.found.empty();
		}
		
		this.lastLength = this.pattern.length;
		
		$clear(this.timer);
		this.timer = this.checkString.delay(100, this, [this]);
		if(this.patternChanged) {
			$clear(this.suggestTimer);
			this.suggestTimer = this.getSuggested.delay(300, this, [this]);
		}
	},
	
	getSuggested: function() {
		if(this.pattern.length < 2 || this.pattern == this.lastSuggest) {
			return false;
		}
		this.request = new Request.JSON({
			url: '/suche/suggest/useronly,1/quick,' + this.pattern,
			method: 'get',
			onComplete: this.addSuggested.bind(this)
		}).send();
	},
	
	addSuggested: function(response) {
		if(response && this.friends && response.length > 0) {
			this.friends.combine(response);			
		}
		this.found.empty();
		$clear(this.timer);
		this.timer = this.checkString.delay(100, this, [this]);		
	},
	
	checkString: function() {
		if(this.searching) {
			this.research = true;
		}
		else {		
			this.research = false;
			this.searching = true;
		}
		if(this.pattern.length == 0) {
			return false;
		}		
		
		// this.found.empty();
		this.choiceCnt = 0;
		this.pattern = this.pattern.toLowerCase();
		
		if(this.friends) {
			this.research = false;
			var item = null;
			
			// suche nur in suchergebnissen
			if(this.found.length > 0) {
				var search = this.found;
			}
			else {
				var search = this.friends;
			}
			
			var matches = 0;
			var tmp = [];
			
			for(i=0; i < search.length; i++) {
				if(!search[i]) {
					continue;
				}
				item = search[i];
				found = false;
				if(this.matchPattern(item)) {
					if(tmp.indexOf(item[0]+'_'+item[5]) >= 0) {
						continue;
					}
					if(item) {
						tmp[i] = item[0]+'_'+item[5];
					}
					this.found[i] = item;
					matches++;
					found = true;
				}
				if(!found && this.found[i]) {
					delete this.found[i];
				}
			}
		}
		if(matches > 0) {
			this.update();
		}
		this.searching = false;
	},
	
	update: function() {
		this.updateFriends(this.found, true);
	},
	
	matchPattern: function(item) {
		if(!item[0]) {
			return false;
		}
			
		if(item[0].toLowerCase().startsWith(this.pattern)) {
			return true;
		}
		
		if(item[4] == 'u' || item[4] == 'm') {
			if(item[1].toLowerCase().startsWith(this.pattern) || item[2].toLowerCase().startsWith(this.pattern)) {
				return true;
			}
			
			if(item[1].toLowerCase().indexOf(' '+this.pattern) >= 0 || item[2].toLowerCase().indexOf(' '+this.pattern) >= 0) {
				return true;
			}
		}
		
		if(item[0].toLowerCase().indexOf(' '+this.pattern) >= 0) {
			return true;
		}
		
		return false;
	},
	
	search: function(pattern) {
		
	},

	'rotate': function(e) {
		this.allowautorotate = false;
		this.nameplate.rotateClass(['left-bottom', 'right-bottom', 'right-top', 'left-top']);
		this.showDummy();
		this.checkDirection();
	},
	
	checkDirection: function() {
		if(this.nameplate.hasClass('left-bottom')) {
			direction = 0;
			$$('.nameplate.dummy.left-bottom').setStyle('visibility', 'hidden');
		} else if(this.nameplate.hasClass('right-bottom')) {
			direction = 1;
			$$('.nameplate.dummy.right-bottom').setStyle('visibility', 'hidden');
		} else if(this.nameplate.hasClass('right-top')) {
			direction = 2;
			$$('.nameplate.dummy.right-top').setStyle('visibility', 'hidden');
		} else if(this.nameplate.hasClass('left-top')) {
			direction = 3;
			$$('.nameplate.dummy.left-top').setStyle('visibility', 'hidden');
		}
		$('nameplate_t').set('value', direction);
	},

	evtClick: function(e) {
		if (!e.target.hasClass('photo')) 
			return false;
		
		$clear(this.hideTimer1);
		$clear(this.hideTimer2);
		this.showDummy();
		this.showOverlay(e.page.x, e.page.y);		
	},
	
	onInit: function(response) {
		this.ipt.removeProperty('disabled');
		this.ipt.focus();
		this.ipt.addEvent('keyup', this.keyUp.bind(this));
		this.friends = response;
		this.origFriends = $A(this.friends);
		this.updateFriends(this.friends, true);
	},

	showOverlayCentered: function() {
		this.showDummy();
		var canvas = $('photo-canvas').getCoordinates();
		this.showOverlay(canvas.left + Math.floor(canvas.width/2) + 20, canvas.top + Math.floor(canvas.height/3));
	},
	
	showOverlay: function(x, y, wanted) {
		if(!this.friends) {
			initrequest = new Request.JSON({
				url: '/suche/suggest/initfriends',
				method: 'post',
				onComplete: this.onInit.bind(this)
			});
			
			initrequest.send.delay(500, initrequest);
		}
		$$('.nameplate').show();
		var canvas = $$('.maincol')[0].getPosition();
		var photo = $('photo');
		
		this.nameplate.setStyles({
			'top': y - canvas.y - 55,
			'left': x - canvas.x - 10,
			'visibility': 'hidden',
			'display': 'block',
			'opacity': 0
		});
		
		if(!wanted) {
			this.nameplate.fade('in');
			$('nameplate_id_unknown').set('value', 0);
			$('nameplate_unknown').setStyle('display', 'block');
			$('invitefriend').setStyle('display', 'none');
			$('email-text').setStyle('display', 'none');
			$('unbekannt-text').setStyle('display', 'block');
			$('nameplate_email').setStyle('display', 'none');
			$('nsinfo-text').setStyle('display', 'none');
			$('nameplate_message').set('text', '');
			$('placeholder_dummy').show();
		}
		else {
			$('placeholder_dummy').hide();
		}
		
		try {
			this.nameplate.getElements('img')[0].setStyle('display', 'block');
		} catch(e) {}
		this.correctPosition();
		var position = this.nameplate.getPosition();
		var clickPosX = x - photo.getPosition().x;
		var clickPosY = y - photo.getPosition().y;
		if(clickPosX < photo.width/2) { // fenster rechts vom np auf
			if(clickPosY < photo.height/2) {
				offsetL = 80; // fenster geht rechts oben vom np auf
				newNameplate = $$('.right-bottom.dummy')[0];
				direction = 1;
			}
			else {
				offsetL = 80; // fenster geht rechts unten vom np auf
				newNameplate = $$('.right-top.dummy')[0];
				direction = 2;
			}
		}
		else {
			if(clickPosY < photo.height/2) {
				offsetL = 680; // fenster geht links vom np auf
				newNameplate = $$('.left-bottom.dummy')[0];
				direction = 0;
			}
			else {
				offsetL = 685; // fenster geht links vom np auf
				newNameplate = $$('.left-top.dummy')[0];
				direction = 3;
			}
		}
		$('nameplate_t').set('value', direction);
		var left = position.x - offsetL;
		var offsetTop = 0;
		
		if($$('.bigsizebanner_container')) {
			offsetTop = $$('.bigsizebanner_container')[0].getStyle('height').toInt();
		}	
		
		this.overlay.setStyles({
			'top': position.y - 178 - offsetTop,
			'left': left,
			'display': 'none'
		});
		
		this.overlay.show(); // overlay zeigen
		this.updateDummyPosition(); // dummies platzieren
		this.updateInfo(); // infos updaten
		
		if(!wanted) {
			this.changeNameplate(this.nameplate, newNameplate); // nameplates tauschen
		}
	},
	
	changeNameplate: function(oldNP, newNP, right) {
		this.correctPosition();
		oldPos = oldNP.getCoordinates();
		
		oldNP.set('class', newNP.get('class')); // ns umwandeln
		oldNP.removeClass('dummy'); // und die klasse dummy entfernen
		oldNP.position({
			relativeTo: newNP
		});
		
		newPos = oldNP.getCoordinates();
		var trw = new Fx.Transition(Fx.Transitions.Bounce, 1);
		fxw = new Fx.Morph(this.nameplate, { transition: trw.easeOut });
		
		var dir = $('nameplate_t').get('value');
		if(dir == 0) { // left-bottom
			offsetL = 194;
			offsetT = 0;
		}
		else if(dir == 1) { // right-bottom
			offsetL = 194 + 38;
			offsetT = 0;
		}
		else if(dir == 2) { // right-top
			offsetL = 194 + 32;
			offsetT =  -46;
		}
		else { // left-top
			offsetL = 194;
			offsetT = -46;
		}
		
		var oLeft = oldPos.left - offsetL; // bitte nicht fragen warum
		if(oLeft < 12) {
			oLeft = 12;
		}
		
		var oTop = oldPos.top - offsetT - 78 - $$('.bigsizebanner_container')[0].getStyle('height').toInt(); // tp mit oder ohne branding...
		if(oTop > 600) {
			oTop = 600;
		}
		
		fxw.start.delay(500, fxw, [{
			'left': oLeft, 
			'top': oTop,
			'position': 'absolute'
		}]);
		
		newNP.setStyle('visibility', 'hidden');		
	},

	'hideOverlay': function() {
		this.nameplate.fade('out');
		this.overlay.hide();
		this.hideDummy();
	},
	
	'releaseUnknown': function(e) {
		$('nameplate_unknown_ipt').set('value', 1);
		this.nameplate.removeClass('female');
		this.nameplate.removeClass('male');
		this.nameplate.addClass('unknown');
		$('nameplate-content').set('text', 'Wer ist das ... ?');
		$('user_id').set('value', 'Wer ist das ... ?');
		this.setUnknown = true;
		this.dummy.each(function(item) {
			item.removeClass('female');
			item.removeClass('male');
			item.getElement('span.nameplate-content').set('text', 'Wer ist das?');
			item.addClass('unknown');
		}, this);
	},

	
	'keyson': function() {
		keysDisabled = false;
	},  
	
	'keysoff': function() {
		keysDisabled = true;
	},
	
	'releaseNameplate': function(e) {
		this.set(e);
		var nameplate = $('nameplate-content');
		$('user_id').set('value', $('user_id').get('value'));
		nameplate.set('text', $('user_id').get('value'));
	},

	'showUnknown': function(e) {
		this.editUnknown = e.target;
		$('nameplate_unknown').hide();
		$('email-text').show();
		$('unbekannt-text').hide();
		$('invitefriend').show();
		$('nameplate_email').show();
		$('nsinfo-text').show();
		this.hideDummy();

		this.showOverlay(e.target.getPosition().x, e.target.getPosition().y + 50, true);
		$('nameplate_y').set('value', $('nameplate_y').get('value') - 19); // ? -19 durch neue beschriftung
		$('nameplate_x').set('value', $('nameplate_x').get('value').toInt() + 2); // ? +2
	},

	'invitefriend': function(e) {
		$('nameplate_invite').set('value', 1);
		this.set(e);
		$('nameplate_invite').set('value', 0);
	},
		
	'showit': function(e) {
		$('nameplate_id_unknown').set('value', 0);
		$('nameplate_unknown').setStyle('display', 'block');
		this.nameplate.fade('in');
		this.overlay.show();
	},		
	
	'set': function(e) {
		e.stop();
		this.ipt.set('value', this.markedFriend);
		$('nameplate_set_p').hide();
		$('nameplate_set_wait_p').show();

		this.form.get('send').removeEvents('complete');

		this.form.set('send', {
			'url': this.form.get('action'),
			'onComplete': function() {
				
				try{
					if(pag != undefined && pag != null){
						n = JSON.decode(this.form.get('send').xhr.responseText);
						if(typeof n == 'object'){
							pag.photos[n.photo].nameplates = n.data.nameplates;
							pag.photos[n.photo].unknownNameplates = n.data.unknown;
							pag.photos[n.photo].suggestedNameplates = n.data.suggested;
						}
					}
				}
				catch(e){}
				
				var clone = this.nameplate.clone(true);
				clone.removeClass('editable');
				clone.addClass('temporarySetNameplate');
				this.nameplate.parentNode.appendChild(clone);
				this.nameplate.setStyle('visibility', 'hidden');
				clone.getElements('img')[0].fade('out');

				if(this.form.get('send').status == 201) {
					$('nameplate_message').set('text', 'Das Namensschild wurde gesetzt!').highlight('#EBDE00');
					var fbtext = false; 
					if(typeof sessionuser != 'undefined' && sessionuser.hasFbtoken) {
						var fbtext = prompt('Dein Namensschild wurde erfolgreich gesetzt!\nMöchtest du es mit deinen Freunden auf Facebook teilen?\n\n', 'Ich habe mich auf einem Foto auf eraffe entdeckt!');
					}					
					this.overlay.chains().highlight('#00ff00');
					if(fbtext) {
						new Request({
							'url': this.form.get('action'),
							'method': 'post'
						}).send('wallpost='+fbtext);
					}
				} else if(this.form.get('send').status == 202) {
					$('nameplate_message').set('text', this.form.get('send').xhr.responseText).highlight('#EBDE00');
					clone.removeClass('male');
					clone.removeClass('female');
				} else if(this.form.get('send').status == 404) {
					$('nameplate_message').set('text', this.form.get('send').xhr.responseText).highlight('#ff0000');
					clone.fade('out');
					clone.destroy();
				} else {
					clone.fade('out');
				}
				
				if(clone) {
					clone.setStyle('z-index', 41);
				}
				
				$('nameplate_set_p').show();
				$('nameplate_set_wait_p').hide();
				
				if(this.form.get('send').status == 404) {				
					this.hideTimer1 = this.overlay.hide.delay(4500, this.overlay);
					this.hideTimer2 = this.hideDummy.delay(4500, this);
				}
				else {
					this.overlay.hide();
					this.hideDummy();
				}

			}.bind(this)
		});
		result = this.form.send();
	},

	'changeTitle': function(nickname, gender) {
		if(gender == 1) {
			this.nameplate.removeClass('female');
			this.nameplate.addClass('male');
		} else if(gender == 2) {
			this.nameplate.removeClass('male');
			this.nameplate.addClass('female');
		} else {
			this.nameplate.removeClass('male');
			this.nameplate.removeClass('female');
		}
		
		var nameplate = $('nameplate-content');
		$('user_id').set('value', nickname);
		nameplate.set('text', nickname);
	},

	'updateInfo': function() {
		var position = $('nameplate').getPosition($('photo-canvas'));
		$('nameplate_x').set('value', position.x);
		$('nameplate_y').set('value', position.y);
	},

	'correctPosition': function() {
		var dimNameplate = this.nameplate.getCoordinates();
		var dimPhoto = this.photo.getCoordinates();
		var x = dimNameplate.left;
		var y = dimNameplate.top;

		if(dimNameplate.left < dimPhoto.left) {
			x = dimPhoto.left;
		}

		if(dimNameplate.left + dimNameplate.width > dimPhoto.left + dimPhoto.width) {
			x = x - ((dimNameplate.left + dimNameplate.width) - (dimPhoto.left + dimPhoto.width));
		}

		if(dimNameplate.top < dimPhoto.top) {
			y = dimPhoto.top;
		}

		if(dimNameplate.top + dimNameplate.height > dimPhoto.top + dimPhoto.height) {
			y = y - ((dimNameplate.top + dimNameplate.height) - (dimPhoto.top + dimPhoto.height));
		}

		if(y != dimNameplate.top || x != dimNameplate.left) {
			var dimCanvas = $$('.maincol')[0].getCoordinates();
			y = y - dimCanvas.top;
			x = x - dimCanvas.left;
			this.nameplate.setStyles({
				'top': y,
				'left': x
			});
		}
	}
		
});

function toggleNameplates() {
	return $$('.nameplate').each(function(item, idx) {
		if(!item.hasClass('dummy')) {
			item.fade();
		}
	});
}


