var nf = null;
$(document).ready(function(){
	nf = new notification();
});
var privi = new Class({
	element: null,
	link: null,
	initialize: function(element, link){
		this.element = element;
		this.link = link;
		this.link.click(this.popup.bind(this));
	},
	popup: function(){
		var popup = $(' \
			<div id="priviPopUpDiv"> \
				<div class="header"> \
					<p><span></span> privátra hívott</p> \
				</div> \
				<div class="content"> \
					<a class="ok" href="#">Elfogadom</a> \
					<a class="cancel" href="#">Nem fogadom el</a> \
				</div> \
			</div>');
		if ( this.element.status == 'away' ){
			$('.header p', popup).html('Aktív privi <span></span> felhasználóval');
			$('.content a.ok', popup).html('Visszatérek');
			$('.content a.cancel', popup).html('Bezárom');
		}
		$('a.ok', popup).click(this.ok.bind(this));
		$('a.cancel', popup).click(this.cancel.bind(this));
		$('.header span', popup).html('<a href="'+this.element.user.url+'" class="facebox" style="color:white;text-decoration:underline">'+this.element.user.name+'</a>');
		popup.css('top', $('a.priviNoti').position().top+$('a.priviNoti').height());
		popup.css('left', this.element.status == 'away' ? (this.link.position().left + this.link.parent().position().left) : $('#belly .page-left').position().left);
		$('#belly .center-page').append(popup);
		return false;
	},
	ok: function(){
		var tokom = this.link.attr('href').split('/').pop();
		window.open(this.link.attr('href'), tokom, 'width=700,height=500');
		$('#priviPopUpDiv').remove();
		if ( this.element.status != 'away' ){
			this.link.remove();
			nf.priviCount -=1;
		}
		return false;
	},
	cancel: function(){
		$('#priviPopUpDiv').remove();
		var vars = {};
		if ( this.element.status == 'away' ) vars = {close: 1};
		else vars = {forbid: 1};
		$.get(this.link.attr('href'), vars);
		this.link.remove();
		nf.priviCount -=1;
		return false;
	}
});
var notification = new Class({
	runing: false,
	messageId: null,
	checkinterval: null,
	msgCount: 0,
	statiCount: 0,
	priviCount: 0,
	title: null,
	titleNew: null,
	urls: null,
	initialize: function(){
		this.title = $('title').html();
		//if ( !debugMode ){
		if ( loggedIn ){
			this.checkRuning();
			this.checkNotifications();
		}
		//}
	},
	checkNotifications: function(){
		if ( this.runing == false ){
			this.runing = $.get(baseUrl+'notification.php', {messageId: this.messageId}, function(json){
				if ( json == null || !(json.elements.length > 0)){
					this.runing = false;
					return false;
				}
				
				this.titleNew = this.title;
				this.messageid = json.elements[0].id;
				this.urls = json.urls;
				
				for ( var j = 0; j < json.elements.length; j++)
				{
					var pe =  json.elements[j];
					if ( pe.type == 'logout' ){
						document.location.href = pe.url;
						break;
					}
					switch(pe.type){
						case 'message':
							this.msgCount += 1;
							if ( messageVoice != '' ) this.sendVoice(messageVoice, pe.id);
						break;
						case 'privi':
							this.priviCount += 1;
							this.addPriviNotification(pe);
							if ( priviVoice != '' ) this.sendVoice(priviVoice, pe.id);
						break;
						case 'stati':
							this.statiCount += 1;
							if ( statiVoice != '' ) this.sendVoice(statiVoice, pe.id);
						break;
						case 'credit':
							new messageBox(pe.msg);
						break;
						case 'picdel':
							document.location.href = baseUrl+'beallitasok/sajat-kepek';
						break;
						case 'silence':
							if ( pe.msg == 'IN' ){
								document.location.href = baseUrl+'chat?silence';
								break;
							} else {
								document.location.href = baseUrl+'chat';
								break;
							}
						break;
						case 'forbid':
							document.location.href = baseUrl;
						break;
						case 'karanten':
							if ( pe.msg == 'IN' ){
								document.location.href = baseUrl+'chat/karanten';
								break;
							} else {
								document.location.href = baseUrl+'fooldal';
								break;
							}
						break;
					}
					this.messageId = pe.id;
				}
				this.msgNotification();
				this.priviNotification();
				this.statiNotification();
				$('title').html(this.titleNew);
				this.runing = false;
			}.bind(this), 'json');
		}
	},
	addNotificationImg: function(cls, img, msg, url, placeHeader, displayText){
		var img = $('\
			<a class="'+cls+'" href="'+url+'" title="'+msg+'"> \
				<img src="'+baseUrl+'images/'+img+'" width="24" height="24"/> \
				'+( displayText != undefined ? displayText : "" )+' \
			</a> \
		');
		if ( placeHeader != undefined ) $('#belly .page-content .header-notification').append(img);
		else $('#belly .page-left .notification').append(img);
		return img;
	},
	addPriviNotification: function(element){
		var msg = element.status == 'new' ? element.user.name+' Privizni hívott!' : 'Aktív privi '+element.user.name+' felhasználóval';
		var img = this.addNotificationImg('priviNoti', 'newPriviJel.gif', msg, element.url, ( element.status == 'away' ? true : undefined), ( element.status == 'away' ? element.user.name : undefined));
		new privi(element, img);
	},
	msgNotification: function(){
		if ( this.msgCount > 0){
			var msg = '('+this.msgCount+' új Levél)';
			this.titleNew = msg + ' ' + this.titleNew;
			if ( $('#belly .page-left .notification a.msgNoti').length > 0 ) return true;
			this.addNotificationImg('msgNoti', 'newMsgJel.gif', msg, this.urls.message);
		}
	},
	priviNotification: function(){
		if ( this.priviCount > 0){
			this.titleNew = '('+this.priviCount+' új Privi) ' + this.titleNew;
		}
	},
	statiNotification: function(){
		if ( this.statiCount > 0){
			var msg = '('+this.statiCount+' új Stati)';
			this.titleNew = msg + ' ' + this.titleNew;
			if ( $('#belly .page-left .notification a.statiNoti').length > 0 ) return true;
			this.addNotificationImg('statiNoti', 'newStatJel.gif', msg, this.urls.stati);
		}
	},
	sendVoice: function(param, id){
		if ( $.cookie('noti_'+id) == 'sended' ) return false;
		$.cookie('noti_'+id, 'sended', {expires: 7, path: '/'});
		setTimeout(function(){
			niftyplayer('niftyPlayer1').loadAndPlay(baseUrl+'flash/'+param+'.mp3');
		}, 1000);
	},
	checkRuning:function(){
		this.checkinterval = setInterval(function(){
			if ( this.runing == false ) this.checkNotifications();
		}.bind(this), 10000);
	}
});
