// JavaScript Document

var shoutbox = $H({
	
	pInit: function(){
		this.el = false;
		this.sh = $$('#widget-shoutbox .sb-shout');
		if(!this.sh[0] || !this.sh[0].getElement('.close')) return false;
		this.attachRem();
	},
	
	init: function(){
		this.el = $('shoutbox-input');
		this.content = $('ws-shouts');
		if($('groupid')) this.type = 'group';
		else this.type = 'profile';
		this.id = $(this.type+'id').value;
	},
	
	attachRem: function(rf){
		this.sh = $$('#widget-shoutbox .sb-shout');
		if(!this.sh || !this.sh[0].getElement('.close')) return false;
		this.sh.each(function(el){
			if(rf) el.removeEvents();
			el.addEvents({
				mouseenter: function(){ el.getElement('.close').removeClass('hide'); },
				mouseleave: function(){ el.getElement('.close').addClass('hide') }
			});
		});
	},
	
	checkLength: function(){
		if(!this.changed || !$('shoutbox-input')) return false;
		var len = $('shoutbox-input').value.length;
		$('ws-count').innerHTML = len;
	},
	
	shout: function(event){
		this.changed = true;
		if(!this.el) this.init();
		if(!this.checking) this.checking = this.checkLength.periodical(1000,this);
		
		event = new Event(event);
		if(event.key != 'enter') return false;
		
		var shout = this.el.value.clean();
		if(shout.length < 1) return this.el.highlight('#FF8888','#FFFFFF');
		this.el.dis();
		
		new Request({url:CMS_AJAXURL+'widgets/shoutbox/shout',
			onSuccess: function(rText){
				this.content.innerHTML = rText+this.content.innerHTML;
				this.el.disabled = false;
				this.el.value = '';
				$('ws-count').innerHTML = '0';
				$clear(this.checking);
				this.checking = false;
				this.attachRem();
			}.bind(this),
			onFailure: function(data){
				if(data.responseText == 'spam') calert('Slow down! You\'re trying to shout too soon after a previous post.');
				else calert('Oops! There was a problem and your shout was not sent, please refresh the page and try again.');
				this.el.disabled = false;
			}.bind(this)
		}).send('type='+this.type+'&id='+this.id+'&shout='+encodeURIComponent(shout));
	},
	
	update: function(shouts){
		if(!this.el) this.init();
		this.content.innerHTML = shouts+this.content.innerHTML;
		this.attachRem();
	},
	
	remShout: function(id,admin){
		if(!this.el) this.init();
		if(!this.rsPop) this.rsPop = new Modal({'classPrefix':'popquestion'});
		this.rsPop.show('<div style="text-align: center;">'+(admin?'<br /><input type="text" size="50" maxlength="140" id="w-s-reason" /><br /><br /><label><input type="checkbox" id="w-s-warn" /> Send warning</label><br /><br />':'')+'<button onclick="shoutbox.doRemShout('+id+','+admin+');">Delete</button> &nbsp; <button onclick="shoutbox.rsPop.hide(event);">Cancel</button></div>', {title: (admin?'Please give a short explanation for deleting this shout':'Are you sure you want to delete this shout?'), hideClose: true});
	},
	doRemShout: function(id,admin){
		
		var reason = '';
		if(admin){
			reason = encodeURIComponent($('w-s-reason').value);
			if(reason.length < 1) return alert('Please give a reason for deleting this shout');
			var warn = ($('w-s-warn').checked?1:0);
		}
		
		this.rsPop.hide();
		
		new Request({url:CMS_AJAXURL+'widgets/shoutbox/rem',
			onSuccess: function(){
				$('sb-shout-'+id).slideOff();
			}.bind(this),
			onFailure: function(){
				calert('Oops! There was a problem and the shout was not removed, please refresh the page and try again.');
			}
		}).send('type='+this.type+'&id='+this.id+'&shoutid='+id+'&reason='+reason+(admin?'&warn='+warn:''));
	},
	
	manage: function(){
		$('w-sb-manage').toggle();
	},
	
	saveSettings: function(){
		var size = $('w-sb-m-size').value;
		var private = $('w-sb-m-private').value;
		this.manage();
		$('ws-shouts').tween('height',(size==9?450:(size==6?300:150)));
		new Request({url:CMS_AJAXURL+'widgets/shoutbox/settings'}).send('type=profile&size='+size+'&private='+private);
	}
	
});

var widgetTabs = $H({
	init: function(pfx){
		this.sels = {};
		this.tabs = {};
		this.sels[pfx] = $(pfx+'-selectors').getElements('a');
		this.tabs[pfx] = $(pfx+'-tabs').getElements('div');
		this.showing = 0;
	},
	show: function(pfx,id){
		if(!this.showing) this.init(pfx);
		this.sels[pfx][this.showing].className = 'sel'+this.showing;
		this.sels[pfx][id].className = 'selon'+id;
		this.tabs[pfx][this.showing].setStyle('display','none');
		this.tabs[pfx][id].setStyle('display','block');
		this.showing = id;
	}
});

var gameHistory = $H({
	showing: {},
	loadIn: function(id,privacy){
		if(this.showing[id] === true) return this.hide(id);
		new Request.JSON({url:CMS_LOADURL+'results/'+id,
			onSuccess: function(data){
				this.showing[id] = true;
				var el = $('w-gh-gr-'+id).html(data.h+'<div class="close" onclick="gameHistory.hide(\''+id+'\');"></div>').set('slide',{duration:180}).slide('hide').show().slide('in');
				el.getParent('.w-gh-gameline').set('tween',{duration:180}).tween('height',el.getSize().y+68);
			}.bind(this)
		}).send('nosurround=1&privacy='+(privacy?privacy:0));
	},
	hide: function(id){
		delete this.showing[id];
		var el = $('w-gh-gr-'+id).slide('out');
		el.getParent('.w-gh-gameline').tween('height',48);
	}
});

var wScores = $H({
	get: function(cg){
		new Request.JSON({url:CMS_AJAXURL+'widgets/scores/get',
			onSuccess: function(data){
				$('w-s-content').html(data.scores);
				$('w-s-scoregroup').removeEvents('change').destroy();
				$('w-s-scoregroups').html(data.groups);
				$('w-s-scoregroup').getElement('option[value='+data.sel+']').selected = true;
				$('w-s-scoregroup').addEvent('change',function(){ this.get(); }.bind(this));
				$('w-s-content').scrollTo(0,0);
			}.bind(this)
		}).send('groupid='+$('groupid').value+'&gameid='+$('w-s-gameid').value+'&type='+$('w-s-type').value+'&scoregroup='+$('w-s-scoregroup').value+'&cg='+(cg?'1':'0'));
	},
	findme: function(){
		var t = $('w-s-content').getElement('.sb-s-self');
		if(t) new Fx.Scroll('w-s-content',{duration:200}).toElement(t);
	}
});

var wLocation = $H({
	
	init: function(id,lat,lng){
		if(!window['gMaps']) return this.init.delay(100,this,arguments);
		this.map = new gMaps({el:'p-map',pos:[lat,lng],pins:[[lat,lng]],zoom:4});
		this.view = 'user';
		this.id = id;
		this.sels = false;
		this.cPins = false;
	},
	
	collective: function(){
		if(this.view == 'collective' || this.working) return false;
		
		this.working = true;
		
		if(!this.cPins){
			this.uPos = this.map.options.pos;
			this.uPins = this.map.options.pins;
			
			new Request.JSON({url:CMS_AJAXURL+'widgets/location/getcollective',
				onSuccess: function(data){
					this.sels = $('p-map-usel').getElements('a');
					this.setCol(data);
				}.bind(this)
			}).send('id='+this.id);
		}
		else this.setCol(this.cPins);
		
	},
	
	setCol: function(pins){
		this.cPins = pins;
		
		this.map.addPins(pins,1);
		this.map.setPos([50,0],1);
		this.map.resize(0,250);
		
		this.view = 'collective';
		
		this.sels[0].swapClass('usel-grayon','usel-gray');
		this.sels[1].swapClass('usel-gray','usel-grayon');
		
		this.working = false;
	},
	
	user: function(){
		if(this.view == 'user' || this.working) return false;
		
		this.working = true;
		
		this.map.addPins(this.uPins,1);
		this.map.setPos(this.uPos,1);
		this.map.resize(0,150);
		
		this.view = 'user';
		
		this.sels[1].swapClass('usel-grayon','usel-gray');
		this.sels[0].swapClass('usel-gray','usel-grayon');
		
		this.working = false;
	}
	
});

var userMod = $H({
	
	warn: function(id){
		var warning = prompt('Please enter the text for this warning');
		if(!warning || warning.length < 1) return false;
		new Request({url:CMS_AJAXURL+'mod/warn',onSuccess: function(){ alert('Warning added'); }}).send('id='+id+'&warn='+warning);
	},
	
	del: function(p,id){
		if(!this.qp) this.qp = new Modal({'classPrefix':'popquestion'});
		this.qp.show('<div style="text-align: center;"><br /><input type="text" size="50" maxlength="140" id="w-m-reason" /><br /><br /><label><input type="checkbox" id="w-m-warn" /> Send warning</label><br /><br /><button onclick="userMod.doDel(\''+p+'\','+id+');">Delete</button> &nbsp; <button onclick="userMod.qp.hide(event);">Cancel</button></div>', {title: 'Please give an explanation for this removal', hideClose: true});
	},
	
	doDel: function(p,id){
		var reason = encodeURIComponent($('w-m-reason').value);
		if(reason.length < 1) return alert('Please give a reason');
		var warn = ($('w-m-warn').checked?1:0);
		
		this.qp.hide();
		
		new Request({url:CMS_AJAXURL+'mod/del',onSuccess: function(){ alert(p+' deleted'); }}).send('p='+p+'&id='+id+'&reason='+reason+'&warn='+warn);
	},
	
	showInfo: function(id,el){
		new Request({url:CMS_AJAXURL+'mod/showinfo',onSuccess: function(data){ this.doShowInfo(data,el); }.bind(this)}).send('id='+id);
	},
	
	doShowInfo: function(text,el){
		if(!this.p) this.p = new infoPop();
		this.p.show(text,el);
	}
	
});