var profile = {
	
	profiles : [],

	showPopup : function (obj)
	{
		if (flash)
		{
			if (cCert && cLogin) {
				obj.href += "#" + cLogin + "#" + cCert;
			}
			return true; // для перенаправления
		}

	//	var oContainer = $('profile_viewer_block');
	//	var oContentContainer = $('profile-content');

	//	if (!(oContainer || oContentContainer))
	//	{
	//		return true;
	//	}

	//	var oScrollPos = Common.getScrollXY();

	//	oContainer.style.display = 'block';
	//	oContainer.style.left = ( ( document.body.offsetWidth - oContainer.offsetWidth ) / 2 ) + 'px';
		//oContainer.style.top = (oScrollPos.y + 100) + 'px';

		//oContentContainer.innerHTML = 
		//alert('jjjj');
		var Errortext='<p><a target="_blank" href="http://www.adobe.com/go/getflashplayer"><img width="56" height="56" align="left" style="margin:-0.5em 1em 0 0;" alt="" src="/i/flashplayer_100x100_2.jpg" /></a>Игра разработана с использованием Macromedia Flash и требует установленной программы Adobe Flash Player.<br/>Установить последнюю версию вы можете с <a target="_blank" href="http://www.adobe.com/go/getflashplayer">официального сайта Adobe.</a></p>';
		Messages.showError(Errortext);
		return false; //блокируем переход по ссылке
	},

	setAvatar : function ()
	{
		var oSexContainer = document.getElementById('profile_sex');
		var oFractionContainer = document.getElementById('profile_fraction');
		var oDestinyContainer = document.getElementById('profile_destiny');

		var profileAvatar = document.getElementById('profile_choise_avatar');

		if (!(oSexContainer || oDestinyContainer || profileAvatar || oFractionContainer))
		{
			return false;
		}

		var oSexContainerValue = oSexContainer.options[oSexContainer.selectedIndex].value;
		var oFractionContainerValue = oFractionContainer.options[oFractionContainer.selectedIndex].value;
		var oDestinyContainerValue = oDestinyContainer.options[oDestinyContainer.selectedIndex].value;

		profileAvatar.src = '/data/avatars/' + oSexContainerValue + oFractionContainerValue + oDestinyContainerValue + '.jpg';

		return true;
	},

	closeViewer : function ()
	{	
		var obj = document.getElementById('profile_viewer_block');
		if(obj.style.display != 'none')
		{
			obj.style.display = 'none';
			Common.Event.remove(document.body, 'mouseup', this.closeViewer);
	        unBlockScreen();
		}
		return true;
	},

	showAvatars : function ()
	{
		var oContainer = document.getElementById('profile_avatar_viewer_block');

		if (!oContainer)
		{
			return false;
		}

		var oScrollPos = Common.getScrollXY();

		oContainer.style.display = 'block';
		oContainer.style.left = ( ( document.body.offsetWidth - oContainer.offsetWidth ) / 2 ) + 'px';
		oContainer.style.top = (oScrollPos.y + 150) + 'px';

		return true;
	},

	closeAvatars : function ()
	{
		var oContainer = document.getElementById('profile_avatar_viewer_block');

		if (!oContainer)
		{
			return false;
		}

		oContainer.style.display = 'none';

		return true;
	},
	
	showAvatarChangerWindow : function(profileName)
	{
		var oContainer = $('avatar_changer_viewer_block');

		if (!oContainer)
		{
			return false;
		}

		var iterateBlock = $('avatarListBlock');
		var template = '<form name="avatarchangeform" method="post"><input name="nick" value="' + profileName + '" type="hidden" />';
		
		for (var i = 0; i < this.profiles.length; i++) {
			if (this.profiles[i].nickName == profileName) {
				var profile = this.profiles[i];
				template += '<div style="float: left"><div class="avatar-pica-prof"><img src="/data/avatars/' + profile.default_avatar + '.jpg" alt="" style="margin-right: 15px" /></div><div><input type="radio" name="avatar_id" value="" ' + ((profile.default_avatar == profile.avatar) ? 'checked="true"' : '') + ' /> Нет</div></div>';
				break;
			}
		}

		for (var i = 0; i < avatarList.length; i++) {
			template += '<div style="float: left"><div class="avatar-pica-prof"><img src="/data/avatars/' + avatarList[i].id + '.jpg" alt="" style="margin-right: 15px" /></div><div><input type="radio" name="avatar_id" value="' + avatarList[i].id + '" ' + ((avatarList[i].id == profile.avatar) ? 'checked="true"' : '') + ' /> ' + avatarList[i].cost + '</div></div>';
		}
		
		template += '<div style="clear: both"></div></form>';
		iterateBlock.innerHTML = template;

		var oScrollPos = Common.getScrollXY();
		blockScreen(oContainer);
		oContainer.style.display = 'block';
		oContainer.style.left = '15px';
		oContainer.style.top = (oScrollPos.y + 20) + 'px';
		return true;
	},
	
	saveAvatarChanges : function()
	{
		var avatar_id = "";
		var radio = document.avatarchangeform.elements.avatar_id;
		var nick = document.avatarchangeform.elements.nick.value;
		
		for (var i = 0; i < radio.length; i++) {
			if (radio[i].checked) avatar_id = radio[i].value;
		}
		
		var cfm = true;
		if (avatar_id) {
			for (var i = 0; i < avatarList.length; i++) {
				if (avatarList[i].id == avatar_id) {
					var cost = avatarList[i].cost;
				}
			}
			cfm = confirm("Действительно хотите потратить " + cost + " сапфиров на аватару?");
		}
		
		if (cfm) {		
			var args = 'mode=change_avatar&nick=' + nick + '&avatar=' + avatar_id;
			var answ = Common.AJAX.getContent('POST', '/ajax.php', args);
			if (answ == "success") {
				window.location.reload();
			}
		}
	},

	closeAvatarChangerWindow : function()
	{
		var oContainer = $('avatar_changer_viewer_block');

		if (!oContainer)
		{
			return false;
		}

		unBlockScreen(oContainer);
		oContainer.style.display = 'none';
		return true;
	},

	getInfo : function (login, nick)
	{
		var args = 'mode=profile_info&nick=' + nick + '&login=' + login;

		var answ = Common.AJAX.getContent('POST', '/ajax.php', args);

		if (!answ)
		{
			return false;
		}

		var oContainer = document.getElementById('profile_viewer_block');
		var oContentContainer = document.getElementById('profile-content');

		if (!(oContainer || oContentContainer))
		{
			return false;
		}

		oContentContainer.innerHTML = answ;

		var oScrollPos = Common.getScrollXY();

		oContainer.style.display = 'block';
		oContainer.style.left = ( ( document.body.offsetWidth - oContainer.offsetWidth ) / 2 ) + 'px';
		oContainer.style.top = (oScrollPos.y + 100) + 'px';
		
		blockScreen(oContainer);
		return true;
	},

	setInfo : function (data)
	{
		return this.profiles[this.profiles.length] = data;
	},

	avatarOver : function (obj, index)
	{
		var profile = this.profiles[index];

		if (!(profile || obj))
		{
			return false;
		}
		
		var link_holder_window = $('profile_avatar_link_holder_window');
	
		if ( link_holder_window )
		{
			link_holder_window.innerHTML = profile.nickName;
		}

		Common.CSSClass.set(obj, 'selected');

		if (profile.is_moderator == '1')
		{
			obj.style.borderColor = 'blue';
		}

		var avatarTitle = $('profile_avatar_title');

		if (avatarTitle)
		{
			avatarTitle.innerHTML = profile.nickName;
		}
		
	},

	avatarOut : function (obj, index)
	{
		var profile = this.profiles[index];

		if (!(profile || obj))
		{
			return false;
		}

		Common.CSSClass.remove(obj, 'selected');

		if (profile.is_moderator == '1')
		{
			obj.style.borderColor = '';
		}

		var avatarTitle = $('profile_avatar_title');

		if (!avatarTitle)
		{
			return false;
		}

		avatarTitle.innerHTML = '&nbsp;';
	},

	set : function (index)
	{
		var activeProfileAvatar = document.getElementById('active_profile_avatar');
		var activeProfileLevel = document.getElementById('active_profile_level');
		var activeProfileName = document.getElementById('active_profile_name');
		var activeProfileFraction = document.getElementById('active_profile_fraction');
		var active_profile_destiny= document.getElementById('active_profile_destiny');
		var activeProfileAvatarPr = document.getElementById('active_profile_avatar_pr');
		var activeProfileLevelPr = document.getElementById('active_profile_level_pr');
		var activeProfileNamePr = document.getElementById('active_profile_name_pr');
		var active_profile_destinyPr = document.getElementById('active_profile_destiny_pr');
		var profile = this.profiles[index];
		var message=true;
		if (!profile)
		{
			return false;
		}

		if (activeProfileName)
		{
			activeProfileName.innerHTML = profile.nickName;
			
		}

		if (activeProfileLevel)
		{
			activeProfileLevel.innerHTML = profile.level;
		
		}

		if (activeProfileFraction)
		{
			activeProfileFraction.src = '/data/fractions/' + profile.fraction + '.png';
		}
		if (active_profile_destiny)
		{
			active_profile_destiny.src = '/data/destinity/' + profile.destiny + '.png';
			active_profile_destiny.title =profile.destiny_title;
		}
		if (active_profile_destinyPr  && activeProfileAvatarPr && active_profile_destinyPr  && activeProfileNamePr)
		{
			active_profile_destinyPr.src = '/data/destinity/' + profile.destiny + '.png';
			active_profile_destinyPr.title =profile.destiny_title;
			activeProfileAvatarPr.src = '/data/avatars/small/' + profile.avatar + '.jpg';
			if (activeProfileLevelPr) {
				activeProfileLevelPr.innerHTML = profile.level;
			}
			activeProfileNamePr.innerHTML = profile.nickName;
			message = false;
		}
		if (activeProfileAvatar)
		{	if (message) activeProfileAvatar.src = '/data/avatars/' + profile.avatar + '.jpg';
			else activeProfileAvatar.src = '/data/avatars/small/' + profile.avatar + '.jpg';
		}
		this.setCookie(index);

		this.closeAvatars();
		//if (message) window.location.reload();
		return true;
	},

	setAvatarPage : function (index)
	{
		if (!index)
		{
			return false;
		}

		var obj = null;

		var i = 1;

		while (obj = $('profile_avatar_page_container_' + (i++)))
		{
			obj.style.display = 'none';
		}

		$('profile_avatar_page_container_' + index).style.display = '';

		return true;
	},

	setCookie : function (index)
	{
		var thisDate = new Date();
		var expTimestamp = (thisDate.getTime() + ((3600*24*30) * 1000)); // 30 days

		var cookieExpDate = new Date(expTimestamp);
		var cookieVar = 'active_profile[' + cLogin + ']';
		var cookieValue = index;
		var cookiePath = '/';
		var cookieDomain = '';

		return Common.Cookie.set(cookieVar, cookieValue, cookieExpDate, cookiePath, cookieDomain);
	},
	
	formsubmit : function() {
		this.closeViewer();
	}
};