var fusionsessioninfourl = '/common/fusion/ajax/fusionproxy.php';
var fusionLoginBoxPath = '/common/fusion/fusionloginbox/';
if ( fusionLoginBoxImgPath == undefined ) {
	var fusionLoginBoxImgPath = '/common/fusion/fusionloginbox/';
}
if ( fusionLoginBoxCSSPath == undefined ) {
	var fusionLoginBoxCSSPath = '/common/fusion/fusionloginbox/';
}
if ( showFusionFacebookButton == undefined ) {
	var showFusionFacebookButton = false;
}
if(typeof(advancedMessageNotify)=='undefined')
{
	var advancedMessageNotify = false;
}

var agid;
var tms;
var achievementCrypt;

if ( showProfileNameIdent == undefined ) {
	var showProfileNameIdent = true;
}

// Styles laden
document.write('<link rel="stylesheet" href="' + fusionLoginBoxCSSPath + 'skins/fusionloginbox.css" type="text/css" />');

// Login-URL
if ( loginURL == undefined || !loginURL) {
	var loginURL = 'https://login.4players.de/4players.php/login/';
}
// Logout-URL
if (!logoutURL)
{
	var logoutURL = 'https://login.4players.de/4players.php/logout/';
}
// Registrierungs-URL
if (!registerURL)
{
	var registerURL = 'https://login.4players.de/4players.php/register/';
}
// Profile-URL
var profilesURL = 'https://login.4players.de/4players.php/editprofile/';
// Fehler-URL
var errorURL = 'https://login.4players.de/loginfailed/';

var FusionLoginBox = Class.create();
FusionLoginBox.prototype = {
	container:		  "",
	email:			  "",
	profile_name:	  "",
	nummessages:	  0,
	ageoflastmessage: 0,
	numhints:		  0,
	intervallTimer:   null,
	advancedMessageNotify: false,

	initialize: function(container, advancedMessageNotify) {
		this.container = $(container);
		this.advancedMessageNotify = advancedMessageNotify;
		var self = this;
		//Event.observe(window,"load",function(){self.lookup();});
		if (document.cookie.match(/fusion_sid=[a-z0-9]{32};/))
		{
			self.lookup();
		}
		else
		{
			self.buildLoginBox();
		}
	},

	lookup: function()
	{
		var now = new Date();
		var time = now.getTime();
		var self = this;

		new Ajax.Request(fusionsessioninfourl, {
			method: 'get',
			parameters: {
				SCRIPTNAME: 'sessioninfo',
				time: time,
				agid: agid,
				tms:tms,
				achievementCrypt: achievementCrypt
			},
		  	onSuccess: function(transport) {
				var test = transport.responseText;
	  			json = JSON.parse(transport.responseText);
		  		if (json == null)
		  		{
		  			self.buildLoginBox();
		  		}
		  		else
		  		{
			  		if (json['status'] == 'loggedin')
			  		{
			  			self.email = json['email'];
			  			self.profile_name = json['profile_name'];
			  			self.nummessages = json['nummessages'];
			  			self.ageoflastmessage = json['ageoflastmessage'];
			  			self.buildLogoutBox();
			  			if (typeof json['new_achievement_id'] != 'undefined') {
			  				Event.observe(window,"load",function(e) {
				  				self.newAchievement(json['new_achievement_id'], json['new_achievement_stage'], json['new_achievement_name'], json['new_achievement_points'], json['new_achievement_group_name'], json['new_achievement_areaid']);
				  			});
			  			}
			  		}
			  		else
			  		{
			  			self.buildLoginBox();
			  		}
		  		}
		  	}
		});
	},

	buildLoginBox: function()
	{
		this.loginText = document.createElement('div');
		this.loginText.id = 'LoginText';
		//this.loginText.innerHTML = 'Zum Login: ';
		this.loginDiv = document.createElement('div');
		this.loginDiv.id = 'LoginDiv';
		this.registerDiv = document.createElement('div');
		this.registerDiv.id = 'RegisterDiv';

		this.loginImg = document.createElement('img');
		this.loginImg.id = 'LoginImg';
		this.loginImg.src = fusionLoginBoxImgPath + 'images/dark_4plogin.png';
		this.loginImg.onclick =	function() {
									new Effect.Puff(this, {to: 0.9});
									new Effect.Fade(fusionLoginBox.loginText.id);
									new Effect.Fade(fusionLoginBox.registerImg.id);
										window.setTimeout('window.location.href = loginURL + "?callback=" + escape(window.location.href);', 750);
								};
		this.loginImg.style.display = 'none';

		this.registerImg = document.createElement('img');
		this.registerImg.id = 'RegisterImg';
		this.registerImg.src = fusionLoginBoxImgPath + 'images/dark_registrieren.png';
		this.registerImg.onclick =	function() {
										new Effect.Puff(this, {to: 0.9});
										new Effect.Fade(fusionLoginBox.loginText.id);
										new Effect.Fade(fusionLoginBox.loginImg.id);
										window.setTimeout('window.location.href = registerURL + "?callback=" + escape(window.location.href);', 750);
									};
		this.registerImg.style.display = 'none';

		if ( showFusionFacebookButton )
		{
			this.facebookImg = document.createElement('img');
			this.facebookImg.id = 'FacebookImg';
			this.facebookImg.src = fusionLoginBoxImgPath + 'images/facebooklogin_f.png';
			this.facebookImg.onclick =	function() {
										new Effect.Puff(this, {to: 0.9});
										new Effect.Fade(fusionLoginBox.loginText.id);
										new Effect.Fade(fusionLoginBox.registerImg.id);
											window.setTimeout('window.location.href = loginURL + "?callback=" + escape(window.location.href);', 750);
									};
			this.facebookImg.style.display = 'none';


		}


		this.loginDiv.appendChild(this.loginImg);
		this.registerDiv.appendChild(this.registerImg);

		if ( showFusionFacebookButton )
			this.loginDiv.appendChild(this.facebookImg);

		this.container.appendChild(this.registerDiv);
		this.container.appendChild(this.loginDiv);
		this.container.appendChild(this.loginText);
		new Effect.Appear(this.registerImg.id);
		new Effect.Appear(this.loginImg.id);
		new Effect.Appear(this.loginText.id);

		if ( showFusionFacebookButton )
			new Effect.Appear(this.facebookImg.id);
	},

	// Liefert den Wert eines GET Parameters
	getURLParam: function(paramName){
		var value = '';
		var href = window.location.href;

		if(href.indexOf('?') > -1 ) {
			var queryString = href.substr(href.indexOf('?')).toLowerCase();
			var aQueryString = queryString.split('&');
			for(var iParam = 0; iParam < aQueryString.length; iParam++) {
				if(aQueryString[iParam].indexOf(paramName + '=') > -1 ) {
					var aParam = aQueryString[iParam].split('=');
					value = aParam[1];
					break;
				}
			}
		}
		return value;
	},

	// Ruft die die Profilseiten auf
	profiles: function()
	{
		window.location.href = profilesURL;
	},

	// Ruft die die Logout-Seite auf
	logout: function()
	{
		window.location.href = logoutURL;
	},

	pulsateButton: function()
	{
		new Effect.Pulsate('RadarButton');

		this.numhints++;
		if (this.numhints >= 3)
		{
			clearInterval(this.intervallTimer);
		}
	},

	buildPGAButton: function()
	{
		var self = this;
		var alertmsg = "";
		if (this.ageoflastmessage>0 && (this.ageoflastmessage/3600) < 1)
		{
			//Letzt Nachricht noch nicht �lter als eine Stunde
			alertmsg = "*";
		}
		var pgabutton = document.createElement('div');
		pgabutton.id = "RadarButton";
		//pgabutton.innerHTML = "<img src='" + fusionLoginBoxImgPath + "images/4p_button_radar_left.png' vspace=0 hspace=0 style='float:left;margin-right:0px;display:block;'><div style='background: url(" + fusionLoginBoxImgPath + "images/4p_button_radar_right.png) no-repeat right top; margin-left: 0px;height: 15px; padding-right: 5px; padding-left: 15px; padding-top: 1px; color: #FFFFFF; font-weight: bold;float:left'><span id='PGAButtonNumMessages'>" + this.nummessages + "</span> Nachrichten" + alertmsg + "</div><div class='clear'></div>";

		// show advanced notifier
		if ( this.advancedMessageNotify )
		{
			// check if notifier function is defined
			if(typeof showAdvancedMessageNotify == 'function')
			{
				// call function
				showAdvancedMessageNotify(this.nummessages);
			}
		}

		if(this.nummessages > 0) {pgabutton.innerHTML = "<img src='" + fusionLoginBoxImgPath + "images/dark_buttons_4pradar.png' vspace=0 hspace=0 style='float:left;margin-right:0px;display:block;'></div><div class='clear'></div>";}
		else { pgabutton.innerHTML = "<img src='" + fusionLoginBoxImgPath + "images/dark_buttons_4pradar_off.png' vspace=0 hspace=0 style='float:left;margin-right:0px;display:block;'></div><div class='clear'></div>";}

		pgabutton.onclick =	function() {
											if (pga.visible==false)
											{
												clearInterval(self.intervallTimer);
												pga.showPGA();
											}
											else
											{
												pga.hidePGA();
											}
										};

		this.container.appendChild(pgabutton);

		if (this.ageoflastmessage>0 && (this.ageoflastmessage/3600) < 1)
		{
			//Pulsate
			this.intervallTimer = setInterval(function(){self.pulsateButton()},5000);
		}
	},

	onNumMessagesChanged: function(nummessages)
	{
		// Neue Nachricht
		if (this.nummessages < nummessages) {
			$('RadarButton').update("<img src='" + fusionLoginBoxImgPath + "images/dark_buttons_4pradar.png' vspace=0 hspace=0 style='float:left;margin-right:0px;display:block;'></div><div class='clear'></div>");
			var self = this;
			this.numhints = 0;
			//Pulsate
			this.intervallTimer = setInterval(function(){self.pulsateButton()},5000);
		}
		// Letzte neue Nachricht gelesen
		if ((this.nummessages != 0) && (nummessages == 0)) {
			clearInterval(this.intervallTimer);
			$('RadarButton').update("<img src='" + fusionLoginBoxImgPath + "images/dark_buttons_4pradar_off.png' vspace=0 hspace=0 style='float:left;margin-right:0px;display:block;'></div><div class='clear'></div>");
		}
		this.nummessages = nummessages;
		//$('PGAButtonNumMessages').innerHTML = this.nummessages;
	},

	buildLogoutBox: function()
	{
		var callback = this.getURLParam('callback');
		if(callback != '')
		{
//			document.location.href = callback;
		}
		else
		{
			this.emailDiv = document.createElement('div');
			this.emailDiv.id = 'EmailDiv';
			//this.emailDiv.style.display = 'none';
			if ( showProfileNameIdent ) {
				this.emailDiv.innerHTML = this.profile_name ? this.profile_name : this.email;
			}
			else {
				this.emailDiv.innerHTML = this.email;
			}

			this.profilesDiv = document.createElement('div');
			this.profilesDiv.id = 'ProfilesDiv';
			this.profilesDiv.style.display = 'none';

			this.profilesImg = document.createElement('img');
			this.profilesImg.id = 'ProfilesImg';
			this.profilesImg.src = fusionLoginBoxImgPath + 'images/dark_profil.png';
			this.profilesImg.onclick =	function() {
											new Effect.Puff(this, {to: 0.9});
											new Effect.Fade(fusionLoginBox.logoutDiv.id);
											new Effect.Fade(fusionLoginBox.emailDiv.id);
											window.setTimeout('fusionLoginBox.profiles()', 1500);
										};

			this.logoutDiv = document.createElement('div');
			this.logoutDiv.id = 'LogoutDiv';
			this.logoutDiv.style.display = 'none';

			this.logoutImg = document.createElement('img');
			this.logoutImg.id = 'LogoutImg';
			this.logoutImg.src = fusionLoginBoxImgPath + 'images/dark_buttons_logout.png';
			this.logoutImg.onclick =	function() {
											new Effect.Puff(this, {to: 0.9});
											new Effect.Fade(fusionLoginBox.profilesDiv.id);
											new Effect.Fade(fusionLoginBox.emailDiv.id);
											window.setTimeout('fusionLoginBox.logout()', 1500);
										};
			//this.logoutImg.onmouseover = function(){new Effect.Pulsate(this, {duration: 1, from: 0.4, pulses: 2});};

			this.logoutDiv.appendChild(this.logoutImg);
			this.profilesDiv.appendChild(this.profilesImg);

			this.container.appendChild(this.profilesDiv);
			this.container.appendChild(this.logoutDiv);
			this.container.appendChild(this.emailDiv);
			// hakt im IE
			// new Effect.Appear(this.emailDiv.id);
			new Effect.Appear(this.logoutDiv.id);
			new Effect.Appear(this.profilesDiv.id);

			if(typeof pga!='undefined') {
				this.buildPGAButton();
			}
		}
	},

	newAchievement: function(id, stage, name, points, groupname, areaid) {
		if (typeof showNewAchievement == 'undefined')
		{
			// Achievements
			var achievementDiv = new Element('div', {
				'id': 'AchievementDiv',
				'style' : 'display: none;' +
					'position: absolute;' +
					'top: -95px;' +
					'right: -11px;' +
					'width: 245px;' +
					'height: 90px;'
			//}).update('<img src="http://static.4players.de/sourcen/portal/4players/achievements/' + id + '_' + stage +'.png">');
			});

			var link = 'https://login.4players.de/achievements.php/de/4players/';
			if (areaid == 10)
			{
				link = 'http://www.4players.de/4players.php/bf3rally/index.html';
			}

			var html = '<div>' +
				'	<a href="' + link + '">' +
				'		<img src="http://static.4players.de/sourcen/portal/4players/achievements/erfolgsmeldung01.gif" alt="" />' +
				'		<img style="position: absolute; top: 18px; left: 25px; z-index: 2;" src="http://static.4players.de/sourcen/portal/4players/achievements/' + id + '_' + stage +'.png" alt="" />' +
				'	</a>' +
				'	<div style="position: absolute; top: 60px; left: 96px; z-index:33;"><a href="https://login.4players.de/achievements.php/de/4players/"><b>&quot;' + name + '&quot;</b></a></div>' +
				'</div>';

			achievementDiv.update(html);
			this.container.appendChild(achievementDiv);
			setTimeout(function() {
				new Effect.SlideDown('AchievementDiv');
			}, 1000);
			setTimeout(function() {
				new Effect.SlideUp('AchievementDiv');
			}, 10000);
		}
		else
		{
			showNewAchievement(id,stage,name,points,groupname,areaid);
		}
	}
}


fusionLoginBox = new FusionLoginBox('FusionLoginBoxContainer', advancedMessageNotify);
