/*
Persona script 0.1
By Ola Persson



Copyright Ktectwo Ab 2008
*/

var persona = {
	loginReload : true,
	lg : 0,
	intG : {
		listItems : 10,
		tabSets : [],
		lockAndLoad : 0,
		hostURL : 'http://www.heyscore.com/heyboard/php/'
	},
	s : {
		cipher : ''
	},
	g : {
		
		currentId : 'log_in_id',
		loadId : '',
		previousPageList : [],
		currentFriendTab : 0,
		currentFriendProfile : [],
		pageOffset : 0,
		itemOffset : 0,
		drawListId : '',
		loggedInAs : [],
		searchForString : '',
		dontCache : 0,
		picNr : 0,
		currentList : [],
		conversationName : '',
		currentScrollOffset : 0,
		subTabGlobalTemp : []
	},
	init : function(){

		var html = persona.addLogIn("") + persona.addProfile("none");
		persona.addBox(html);
		
		persona.showglobalCurrentId(persona.g.currentId);

		if(typeof(phpCipher) != 'undefined' && typeof(oldSessionGlobals) != 'undefined'){
			persona.restoreSession(phpCipher, oldSessionGlobals);
		}
		
		
	},
	showglobalCurrentId : function (){
		$(persona.g.currentId).style.display='block';
	},
	setScroll : function(id,pixels){
		
		
		if (id=='friend_profile_id'){
			var addSubTab = ['_wall','_friends',''];
			id=id+addSubTab[persona.g.currentFriendTab[1]];
		}
		
		if ( $(id+'_list_container')){
			$(id+'_list_container').scrollTop = pixels;

		}
		
	},
	saveScroll : function (id){
		if (id=='friend_profile_id'){
			var addSubTab = ['_wall','_friends',''];
			id=id+addSubTab[persona.g.currentFriendTab[1]];
		}
		
		
		if ( $(id+'_list_container')){
			return $(id+'_list_container').scrollTop;
		}
	},
	goToProfile : function(visitId){
		persona.goToId('friend_profile_id',[2,0],null,null,null,visitId);
	},
	goToId : function(id,subTab,listGlobalPageOffset,fromGoToPrevious,prevFriendProfileId,visitProfileId,restoreSession){

			persona.g.loadId = [id,subTab,listGlobalPageOffset,fromGoToPrevious,prevFriendProfileId,visitProfileId];

			persona.lock();
			persona.clearFields();
				
			if (fromGoToPrevious==null){

				persona.g.currentScrollOffset = 0;
				var tempScrollOffset = persona.saveScroll(persona.g.currentId);
				var tempOldSubTab = persona.g.currentFriendTab;
				var tempOldGlobalPageOffset = persona.g.pageOffset;
				
				if(restoreSession!=true){
					persona.g.previousPageList.push([persona.g.currentId,tempOldSubTab,tempOldGlobalPageOffset,persona.g.currentFriendProfile,tempScrollOffset]);	
				}
				
			
				if (id!='friend_profile_id'){
					persona.g.searchForString = '';
				}
				if (id=='conversation_id'){
					persona.g.conversationName = persona.g.currentList[persona.g.itemOffset][4];
				}
			}

			var nextFriendProfileId = null;
			
			if (id=='friend_profile_id' || id=='conversation_id'){
				
				if (fromGoToPrevious==null){
					if(visitProfileId==null){
						
						nextFriendProfileId = persona.g.currentList[persona.g.itemOffset][1];
						
					} else {
						nextFriendProfileId = visitProfileId;
					}
					
					

				} else {
					nextFriendProfileId = prevFriendProfileId;
				}
				
				if (id=='friend_profile_id'){
					persona.loadFriendProfile(nextFriendProfileId);
				}
				
			}
			
			persona.g.currentFriendProfile = nextFriendProfileId;
			

			persona.g.subTabGlobalTemp = [subTab,id,listGlobalPageOffset];
			

			persona.showHideDeleteControl(id);
			
			
			
			if ($(id+'_list_container')){
				persona.drawList(id,0);
			}
			
			if(id=='settings_id'){
				persona.drawSettings();	
			}
			
			if(id=='coins_id'){
				persona.drawCoins();	
				$('form_cipher_1').value = persona.s.cipher;
				$('form_cipher_2').value = persona.s.cipher;
				$('form_cipher_3').value = persona.s.cipher;
			}
			
			if(id=='my_profile_id'){
				persona.drawStatus();
			}
			
			if(id=='friends_id'){
				persona.drawRequestCount();	
			}
			
			
			
			persona.markUnmarkTabs(id);
			
			$(persona.g.currentId).style.display='none';
			$(id).style.display='block';
			
			
			persona.setScroll(id,persona.g.currentScrollOffset); 
			
			persona.g.currentId = id;
			
			

			
			
			if(id=='log_in_id' || id=='join_id' || id=='forgot_password_id' || id=='coins_id'){
				persona.unlock();
			}
			
			
			
			

			
	},
	lock : function(){
		persona.intG.lockAndLoad = 1;
		$('load_curtain').setOpacity(1);
		$('load_curtain').addClassName('p_load_curtain_down');
	},
	lockList : function(){
		persona.intG.lockAndLoad = 1;
		$('load_curtain').setOpacity(1);
		$('load_curtain').addClassName('p_load_curtain_down');
	},
	unlock : function (){
		
	
		
		if(persona.loginReload == true && persona.lg == 1){
			window.location.reload();
		} else {
			
			var frame = 0;
			var frames = 7;

			if (unlockAnimation){
				window.clearInterval(unlockAnimation);
			}

			var unlockAnimation = window.setInterval
			(
				function(){

					var tempOpacity = (frames - frame) / frames;

					$('load_curtain').setOpacity(tempOpacity);

					frame++;
					if (frame > frames) {
						window.clearInterval(unlockAnimation);

						$('load_curtain').removeClassName('p_load_curtain_down');
						persona.intG.lockAndLoad = 0;
					}
				}
			,1);
			
		}
		



	},
	goToPreviousPage : function(){
			
			var tempPreviousPage = persona.g.previousPageList.pop();

			var tempId = tempPreviousPage[0];
			var tempSubTab = tempPreviousPage[1];
			var tempGlobalPageOffset = tempPreviousPage[2];
			var tempProfileId = tempPreviousPage[3];
			persona.g.currentScrollOffset = tempPreviousPage[4];
			
			
			persona.goToId(tempId,tempSubTab,tempGlobalPageOffset,1,tempProfileId);

	},
	loadFriendProfile : function(nextFriendProfileId){
			
			var url = persona.intG.hostURL+'db_friend_profile_id.php?'+
			'id='+persona.g.loggedInAs[0]+
			'&friend_id='+nextFriendProfileId;
			persona.dbCon(url,'script_friend_profile_id');
			
	},
	loadFriendProfileSuccess : function(profileList,cipher){
		
		if (cipher){
			persona.s.cipher = cipher;
		};


		if (profileList.length == 0){
			persona.error('Account Closed');
			persona.goToPreviousPage();
			return;
		}
		
		persona.drawFriendProfile(profileList);
		

		
		if (persona.g.subTabGlobalTemp[0] != null && persona.g.subTabGlobalTemp[0].length==2){
			persona.clickSubTab(persona.g.subTabGlobalTemp[0],persona.g.subTabGlobalTemp[1],persona.g.subTabGlobalTemp[2]);

		}
	
	},
	addFriend : function (friendProfileIdNr){
		


		
		if (!friendProfileIdNr){
			friendProfileIdNr = persona.g.currentList[persona.g.itemOffset][1];

		}
		
		if (persona.g.currentId == 'friend_profile_id'){
			$('add_friend_row').style.display = 'none';
		}

		var errorText = validate(persona.g.loggedInAs[0],'integer') + validate(friendProfileIdNr,'integer');

		
		errorText = errorText.replace(/1/gi,'');
		

		if (errorText.length == 0){
			persona.lock();
			var url = persona.intG.hostURL+'db_add_friend.php?friend_a='+
			persona.g.loggedInAs[0]+'&friend_b='+friendProfileIdNr;
			persona.dbCon(url,'script_add_friend_id');
		} else {
			persona.error('Client Side: ' + errorText);
		}



		


	},
	addFriendSuccess : function (cipher){
		if (cipher){
			persona.s.cipher = cipher;
		};
		persona.drawList(persona.g.currentId,0);
			
	},
	inviteSuccess : function (cipher){
		
		if (cipher){
			persona.s.cipher = cipher;
		};

		

		persona.unlock();		
		persona.error('Invite successfully sent! ');
	},
	acceptFriendButton : function(){
		persona.lockList();
		var tempAcceptId = persona.g.currentList[persona.g.itemOffset][1];

		
		var url = persona.intG.hostURL+'db_accept_friend.php?'+
		'friend_id='+tempAcceptId+'&id='+persona.g.loggedInAs[0];
		
		persona.dbCon(url,'script_accept_friend_id');

	},
	acceptFriendSuccess : function (cipher){
		
		persona.markUnmarkTabs('requests_id');
		
		if (cipher){
			persona.s.cipher = cipher;
		};
		persona.drawList(persona.g.currentId,0);
			
	},
	drawFriendProfile : function(profile){

	
		var subNav = '';
		if (profile[0][4]=='' && persona.g.loggedInAs[0] != profile[0][0]){
			subNav = '<div class="p_x_add_to_friends_button" onclick="persona.addFriend(persona.g.currentFriendProfile);" title="Add To Friends"></div>'; 

		}

		if (profile[0][1]==''){
			var picture = 'nopicture.gif';
		} else {
			var picture = profile[0][1];
		}

		var name = profile[0][2];
		var info = profile[0][3];
		
		var html = ""+
		'<div id="friend_profile_id_info"" class="p_profile_info_container">'+
			'<img src="http://www.heyscore.com/heyboard/images/profilebig/'+picture+'"" class="p_big_profile_picture" />'+
			'<div class="p_profile_info_column">'+
				'<div class="p_form_text">'+
					name+
				'</div>'+
				'<div class="p_form_text">'+
					info+
				'</div>'+
				'<div id="add_friend_row" class="p_sub_row">'+
					subNav+
				'</div>'+
			'</div>'+
		'</div>';
		

		if ($('friend_profile_id_info')){
			$('friend_profile_id_info').remove();
		}
	
		Element.insert($('friend_profile_id_info_container'),html);

	},
	drawMyProfile : function(profile){

		
		var subNav = '<div class="p_x_account_settings_button" onclick="persona.goToId('+"'settings_id'"+');" title="Account Settings"></div>';

		if (profile[1]==''){
			var picture = 'nopicture.gif';
		} else {
			var picture = profile[1];
		}
		
		var name = profile[2];
		

		var html = ''+
			'<div id="my_profile_id_info" class="p_profile_info_container">'+
				'<img src="http://www.heyscore.com/heyboard/images/profilebig/'+picture+'" class="p_big_profile_picture" />'+
				'<div class="p_x_upload_wrapper">'+
					'<form class="p_form">'+
						'<input type="file" name="file" onchange="persona.changePicture(this.form); return false;" class="p_upload_block"/>'+
					'</form>'+
				'</div>'+
				'<div class="p_profile_info_column">'+
					'<div class="p_form_text">'+
						name+
					'</div>'+
					'<div class="p_form_row">'+
						'<form action="javascript:persona.updateStatus();">'+
							'<input type="image" src="http://www.heyscore.com/heyboard/images/empty.gif" title="Update Your Status" class="p_x_update" />'+	
							'<input id="status_field_id" class="p_status_field" type="text" value="" name="Name">'+
						'</form>'+
					'</div>'+
					'<div class="p_form_row">'+
						subNav+
					'</div>'+
				'</div>'+
			'</div>';
		
		
		if ($('my_profile_id_info')){
			$('my_profile_id_info').remove();
		}
	
		Element.insert($('my_profile_id_info_container'),html);
		
	},
	updateStatus : function (){
		
		var status = $('status_field_id').value;
		
		var iWS = isWhitespace(status);
		
		if (iWS!=''){
			var errorText = validate(status,'comment');
			errorText = errorText.replace(/1/gi,'');

			if (errorText.length == 0){
				persona.lock();
				
				var url = persona.intG.hostURL+'db_update_status.php?'+
				'id='+persona.g.loggedInAs[0]+
				'&status='+status;

				persona.dbCon(url,'script_update_status_id');
				
			} else {
				persona.error('Client Side: ' + errorText);
			}
			
		} else {
			persona.error('Client Side: Status Field Is Empty.');
		}
		
		
	},
	updateStatusSuccess : function (cipher){
		if (cipher){
			persona.s.cipher = cipher;
		};
		
		persona.unlock();
		
	},
	drawStatus : function(){
			
			persona.lock();
			var url = persona.intG.hostURL+'db_status.php?'+
			'id='+persona.g.loggedInAs[0];
			
			persona.dbCon(url,'script_status_id');

	},
	statusSuccess : function(status,cipher){
		if (cipher){
			persona.s.cipher = cipher;
		};
		
		$('status_field_id').value = status;
	},
	changePicture : function(form){

		persona.addIframe();
		
		persona.g.picNr++;
		if (persona.g.loggedInAs[1]==""){
			
		} else {
			
			tempOldNr = 0;
			if(persona.g.loggedInAs[1]=!'nopicture.gif'){
				var tempOldNr = persona.g.loggedInAs[1].split('_')[1].substr(0,1);
			}
			
	
			if (persona.g.picNr == tempOldNr){
				persona.g.picNr++;
			}
		}
	
		var tempNr = persona.g.picNr;

		var randomness = persona.s.cipher;
		
		randomness = randomness.replace("/", "");
		randomness = randomness.replace(/:/g, "");
		randomness = randomness.substring(8, 22);
		

		persona.g.loggedInAs[1] = persona.g.loggedInAs[0]+"_"+tempNr+'_'+randomness+'.jpg';
		


		var tempUrl = persona.intG.hostURL+"upload.php?cipher="+persona.s.cipher+"&nr="+tempNr;
		
		form.setAttribute("target","iframe_id");  
		form.setAttribute("action", tempUrl);  
		form.setAttribute("method","post");  
		form.setAttribute("enctype","multipart/form-data");  
		form.setAttribute("encoding","multipart/form-data");  
		

			
		form.submit();
		
		persona.lock();
		

		
		

	},
	changePictureCallback : function(){
		persona.drawMyProfile(persona.g.loggedInAs);

		persona.clickTab([1,0]);
	},
	addIframe : function(){

		if ($('iframe_id')){
			$($('iframe_id')).remove();
		}
		
		Element.insert($('iframe_parent'), '<iframe id="iframe_id" class="p_iframe" name="iframe_id" onload="persona.changePictureCallback()"></iframe>');

	},
	checkFriendAndInboxTab : function(){

			//persona.lock();
			var url = persona.intG.hostURL+'db_friend_and_inbox_tab.php?'+
			'id='+persona.g.loggedInAs[0];
			
			persona.dbCon(url,'script_request_count_id');

			
	},
	drawFriendAndInboxTab : function(requestCount, unreadCount){
		//console.log(requestCount +" "+ unreadCount);
		
		if ($('request_count')){
			$('request_count').remove();
		}
		
		if ($('unread_count')){
			$('unread_count').remove();
		}
		
		if(requestCount != "0"){
			//$('p_x_friends_tab')

			//var html = '<div id="request_count" class="p_tab_notification">'+requestCount+'</div>';
			var html = '<img src="http://www.heyscore.com/heyboard/images/friend_request_heart.gif" id="request_count" class="p_tab_notification"/>';
			Element.insert($('p_x_friends_tab'),html);
		}

		if(unreadCount != "0"){
			//$('p_x_inbox_tab')

			//var html = '<div id="unread_count" class="p_tab_notification">'+unreadCount+'</div>';
			var html = '<img src="http://www.heyscore.com/heyboard/images/new_messages.gif" id="unread_count" class="p_tab_notification"/>';
			Element.insert($('p_x_inbox_tab'),html);
		}

	},
	markUnmarkTabs : function(id){
		

		
		if(persona.s.cipher){
			persona.checkFriendAndInboxTab();
			
		}
		
		
		var tempTab = [];

		for (var i=0;i<persona.intG.tabSets.length-1;i++){
			
			var childElementArray = $('tab_container_'+i).childElements();
			
			for (var j=0;j<persona.intG.tabSets[i].length;j++){
				
				$(childElementArray[j]).removeClassName('p_selected');
				
				if(id==persona.intG.tabSets[i][j][1]){
					tempTab = [i,j];
				}
			}
		}
		
		if (tempTab[0]!=null){

			var childElementArray = $('tab_container_'+tempTab[0]).childElements();
			$(childElementArray[tempTab[1]]).addClassName('p_selected');
			
		}
		
	},
	clearPreviousId : function(){
		persona.g.previousPageList = [];
	},
	addBox : function(contentHtml){


		
		var html = ''+
		'<div class="p_box">'+
			'<div class="p_x_head"></div>'+
			contentHtml+
			'<div id="load_curtain" class="p_load_curtain">'+
				'<div class="p_white_curtain">'+
					'<div class="p_x_loading">'+
					'</div>'+
				'</div>'+
			'</div>'+
		'</div>'+
		'<div id="iframe_parent" class="p_iframe_parent"></div>';
		
		Element.insert($('container'),html);
		

	},
	buttonsHtml : function(buttonList){

		
		var html = '';
		
		for (var i=0;i<buttonList.length;i++){
			html += "<div class='p_button' onclick='javascript:"+buttonList[i][1]+"'>"+buttonList[i][0]+"</div>";
		}
		
		return html;
		
	},
	subNavHtml : function(direction,buttonList){
		

		
		var html = "";
		var buttonListLength = buttonList.length;
	
		for (var i=0;i<buttonListLength;i++){

			html += '<div class="p_sub_button" onclick="javascript:'+buttonList[i][1]+'">'+buttonList[i][0]+'</div>';
			
		}
		
		return html;
	},
	addTabs : function(tabList){	
		

		
		tabSetNum = persona.intG.tabSets.push(tabList);
		
		var selectedStyle = " p_selected";
		var tabHtml = '';

		for (var i=0;i<tabList.length;i++){
			if (i>0){
				selectedStyle="";
			};
			var tabIndex = [tabSetNum-1,i]+'';
			
			var tabClass = tabList[i][2];
			
			tabHtml += '<div class="p_'+tabClass+selectedStyle+'" id="p_'+tabClass+'" onclick="javascript:persona.clickTab(['+tabIndex+'])" title="'+tabList[i][0]+'"></div>';
		}
		
		var html = ""+
		'<div id="tab_container_'+(tabSetNum-1)+'"" class="p_tab_row">'+
			tabHtml+
		'</div>';
		
		return html;
		
	},
	clickTab : function(tabIndex){

	
		var tabSet = persona.intG.tabSets[tabIndex[0]];
		persona.goToId(tabSet[tabIndex[1]][1]);
		persona.clearPreviousId();
	},
	clickSubTab : function(tabIndex,id,offset){
		
	
		persona.lockList();

		var tabSet = persona.intG.tabSets[tabIndex[0]];
		
		persona.g.currentFriendTab = tabIndex;

		if (id==null){
			id = persona.g.currentId;
		}
		
		if (offset==null){
			offset=0;
		}

		if(tabIndex[1]<2){
			persona.drawList(id,offset,null);
			
		}	else {
			persona.unlock();
		}
		

		for (var i = 0; i<tabSet.length;i++){
			$(tabSet[i][1]).style.display='none';

			var tabId = 'subtab_id_'+tabIndex[0]+'-'+i;

			$(tabId).removeClassName('p_selected');	
		}
		
		selectedTabId = 'subtab_id_'+tabIndex[0]+'-'+tabIndex[1];
		$(selectedTabId).addClassName('p_selected');
		$(tabSet[tabIndex[1]][1]).style.display='block';
		
		persona.setScroll(id,persona.g.currentScrollOffset); 
		
	},
	addSubTabs : function(tabList){
		
		tabSetNum = persona.intG.tabSets.push(tabList);
		var tabHtml = "";

		var selectedStyle = " p_selected";
		var tabListLenght = tabList.length;
		
		for (var i=0;i<tabListLenght;i++){
			
			if (i>0){
				selectedStyle="";
			};
			
			
			
			var tabIndex = [tabSetNum-1,i]+'';
			
				var tabClass = tabList[i][2];
			
			var tabId = 'subtab_id_'+(tabSetNum-1)+'-'+i;
			tabHtml += '<div id="'+tabId+'" class="p_'+tabClass+selectedStyle+'" onclick="javascript:persona.clickSubTab(['+tabIndex+'])"  title="'+tabList[i][0]+'"></div>';

		}
		
		var html = ''+
			"<div id='tab_container_"+(tabSetNum-1)+"' class='p_sub_tab_row'>" + 
				tabHtml + 
			"</div>";
			
		return html;

	},
	addLogIn : function(displayStyle){
		


		var tabList = [['Log In','log_in_id','x_log_in_tab'],['Join','join_id','x_join_tab'],['Forgot Password?','forgot_password_id','x_forgot_tab']];
		var tabHtml = persona.addTabs(tabList);
		var loginHtml = persona.loginHtml(tabList[0][1]);
		var joinHtml = persona.joinHtml(tabList[1][1]);
		var forgotHtml = persona.forgotHtml(tabList[2][1]);
		
		var html = "<div id='login_container_id' style='display:"+displayStyle+";'>"+ tabHtml + loginHtml + joinHtml + forgotHtml + "</div>";
		return html;

	},
	loginHtml : function(id){
		

		var buttonHtml = '<input type="image" src="http://www.heyscore.com/heyboard/images/empty.gif" title="Update Your Status" class="p_x_log_in_button" />';	 

		html=''+
		'<div id="'+id+'">'+
			'<form action="javascript:persona.loginButton();">'+
				'<div class="p_form_row">'+
					'e-mail<input id="login_email" type="text" class="p_field" value="">'+
				'</div>'+
				'<div class="p_form_row">'+
					'Password <input id="login_password" class="p_field" type="password" value="">'+
				'</div>'+
				'<div class="p_form_row">'+buttonHtml+'</div>'+
			'</form>'+
			'<div class="p_x_welcome_header" title="Welcome To Heyscore!"></div>'+
			'<img src="http://www.heyscore.com/heyboard/images/welcome_picture.gif"/>'+
		'</div>';
		
		return html;

	},
	loginButton : function(){


		var tempEmail = $('login_email').value;
		var tempPassword = $('login_password').value;
		

		
		
		
		var errorText = validate(tempEmail,'e-mail') + validate(tempPassword,'password');
		
		tempPassword = SHA1(tempPassword);
		
		errorText = errorText.replace(/1/gi,'');
		var iWS = isWhitespace(tempEmail) + isWhitespace(tempPassword);
		if (iWS!=''){
			if (errorText.length == 0){
				persona.lock();
				var url = persona.intG.hostURL+'db_login.php?user='+
				tempEmail+'&password='+tempPassword;
				persona.dbCon(url,'script_login_id');
			} else {
				persona.error('Client Side: ' + errorText);
			}
		}
	},
	dbCon : function(url,scriptId){
		
		if ($('scriptId')){
			$('scriptId').remove();
			
		}
		
		persona.g.dontCache++;
		

		
		for (var i = 0; i<persona.g.currentList.length;i++){
			persona.g.currentList[i][4] = '';
		}
		
		
		
		
		var tempGlobalsObjString = Object.toJSON(persona.g);
		
		tempGlobalsObjString = tempGlobalsObjString.replace(/&/g, "-andsign-");
		
		
		var tempUrl = url+'&cipher='+persona.s.cipher+'&globals='+tempGlobalsObjString+'&dont_cache='+persona.g.dontCache,scriptId;

		persona.addJavascript(tempUrl);
		
	},
	addJavascript : function(url,scriptId){
		
		var th = document.getElementsByTagName('body')[0];
		var s = document.createElement('script');
		s.setAttribute('type','text/javascript');
		s.setAttribute('src',url);

		s.setAttribute('id',scriptId);
		th.appendChild(s);

	},
	loginSuccess : function(tempUser,cipher){
		
		persona.lg = 1;

		if (cipher){
			persona.s.cipher = cipher;
		};

		persona.g.loggedInAs = tempUser;
		
		if (tempUser){
			
			persona.drawMyProfile(tempUser);
			$('login_container_id').style.display='none';
			$('profile_container_id').style.display='block';
			
			persona.goToId('my_profile_id');
			$('login_email').value = '';
			$('login_password').value = '';
		}
		


		
	},
	joinHtml : function(id){

		var jsAction = 'persona.joinButton();';
		var buttonCss = 'p_x_sign_up_button';
		
		var buttonHtml = '<input type="image" src="http://www.heyscore.com/heyboard/images/empty.gif" title="Update Your Status" class="'+buttonCss+'" />';

		
		html=''+
		'<div id="'+id+'" style="display:none;">'+
			'<div class="p_form_row">'+
				"It's free and anyone can join."+
			'</div>'+
			'<form action="javascript:'+jsAction+'">'+
			'<div class="p_form_row">'+
				'Name / Alias <input id="join_full_name" type="text" class="p_join_field" value="">'+
			'</div>'+
			'<div class="p_form_row">'+
				'Your e-mail<input id="join_email" type="text" class="p_join_field" value="">'+
			'</div>'+
			'<div class="p_form_row">'+
				'New Password<input id="join_password" class="p_join_field" type="password" value="">'+
			'</div>'+
			'<div class="p_form_row">'+buttonHtml+'</div>'+
			'</form>'+
			'<div class="p_x_terms_header" title="Terms Of Use"></div>'+	
			'<div class="p_form_text">'+
				'By clicking Sign Up, you are indicating that you have read and agree to the <br/><a href="http://www.heyscore.com/html/user_agreement.html" class="p_link" target="_blank">Terms of Use</a> and <a href="http://www.heyscore.com/html/user_agreement.html" class="p_link" target="_blank">Privacy Policy</a>.'+
			'</div>'+
		'</div>';
		

		
		return html;
		
		
	},
	joinButton : function(){
		

		
		var tempName = $('join_full_name').value;
		var tempEmail = $('join_email').value;
		var tempPassword = $('join_password').value;
		
		var errorText = validate(tempName,'name') + validate(tempEmail,'e-mail') + validate(tempPassword,'password');

		errorText = errorText.replace(/1/gi,'');
		
		var iWS = isWhitespace(tempName) + isWhitespace(tempEmail) + isWhitespace(tempPassword);
		if (iWS!=''){
			if (errorText.length == 0){
				persona.lock();
				tempPassword = SHA1(tempPassword);
				var url = persona.intG.hostURL+'db_join.php?'+
				'name='+tempName+
				'&email='+tempEmail+
				'&new_password='+tempPassword;				
				persona.dbCon(url,'script_join_id');
			} else {
				persona.error('Client Side: ' +errorText);
			}
		}
	},
	joinSuccess : function(tempResult,cipher){
		if (cipher){
			persona.s.cipher = cipher;
		};
		
		$('join_full_name').value = '';
		$('join_email').value = '';
		$('join_password').value = '';
		persona.loginSuccess(tempResult);
	},
	joinError : function(){
		persona.unlock();
		persona.error('e-mail already in use');
	},
	forgotHtml : function(id){
		

		var jsAction = 'persona.forgotButton();';
		var buttonCss = 'p_x_send_password_button';
		
		var buttonHtml = '<input type="image" src="http://www.heyscore.com/heyboard/images/empty.gif" title="Update Your Status" class="'+buttonCss+'" />';

		
		html=''+
		'<div id="'+id+'" style="display:none;">'+
			'<div class="p_form_text">'+
				"Enter your login e-mail. We will send you a new password."+
			'</div>'+
			'<form action="javascript:'+jsAction+'">'+
				'<div class="p_form_row">'+
					'e-mail <input id="forgot_email" class="p_email_field" type="text" value="" name="Name">'+
				'</div>'+
				'<div class="p_form_row">'+buttonHtml+'</div>'+
			'</form>'+
		'</div>';
		
		return html;
		
	},
	forgotButton : function(){


		
		var tempEmail = $('forgot_email').value;
		var errorText = validate(tempEmail,'e-mail');

		errorText = errorText.replace(/1/gi,'');
		var iWS = isWhitespace(tempEmail);
		if (iWS!=''){
			if (errorText.length == 0){
				
				persona.lock();
				var url = persona.intG.hostURL+'db_forgot.php?'+
				'email='+tempEmail;	
				
				persona.dbCon(url,'script_forgot_id');
			} else {
	
				persona.error('Client Side: ' + errorText);
			}
		}

	},
	forgotSuccess : function(){
		persona.unlock();
		$('forgot_email').value = '';
		persona.error('Password Sent!');
		
	},
	addProfile : function(displayStyle){
		

		
		var tabList = [['My Profile','my_profile_id','x_my_profile_tab'],['Friends','friends_id','x_friends_tab'],['Inbox','inbox_id','x_inbox_tab'],['Coins','coins_id','x_coins_tab']];
		var tabHtml = persona.addTabs(tabList);
		
		var profileHtml = persona.profileHtml(tabList[0][1]);
		var friendsHtml = persona.friendsHtml(tabList[1][1]);
		var inboxHtml = persona.inboxHtml(tabList[2][1]);
		var coinsHtml = persona.coinsHtml(tabList[3][1]);
		var settingsHtml = persona.settingsHtml('settings_id');
		var addInviteHtml = persona.addInviteHtml('add_invite_id');
		var requestsHtml = persona.requestsHtml('requests_id');
		var conversationHtml = persona.conversationHtml('conversation_id');
		var friendProfileHtml = persona.friendProfileHtml('friend_profile_id');
		var deleteControlHtml = persona.deleteControlHtml('delete_control_id');
		

		var allHtml = tabHtml + profileHtml + friendsHtml + inboxHtml + coinsHtml + settingsHtml + addInviteHtml + requestsHtml + conversationHtml + friendProfileHtml + deleteControlHtml;
		
		var html = ''+
		'<div id="profile_container_id" class="p_section_container"" style="display:'+displayStyle+';">'+
			allHtml+
			'<div class="p_x_close" title="Log Out" onclick="javascript:persona.confirm('+"'Do you really want to log out?',persona.logOut"+')"></div>'+
		'</div>';
		
		return html;
		
	},
	blueLinkHtml : function(linkList,itemNumber){
		
	
		
		var html = '';
		var linkListLength = linkList.length;
		
		
		
		for (var i=0;i<linkListLength;i++){

			html += '<img src="http://www.heyscore.com/heyboard/images/empty.gif" class="p_'+linkList[i][2]+'" alt="'+linkList[i][0]+'" title="'+linkList[i][0]+'" onclick="javascript:persona.g.itemOffset ='+itemNumber+';'+linkList[i][1]+'"/>';
			
		}

		return html;
		
	},
	friendsHtml : function(id){
		

		
		var pageSelectHtml = persona.pageSelectHtml(id);
		

		var subNav  = '<div class="p_x_add_invite_button" onclick="persona.goToId('+"'add_invite_id'"+');" title="Add & Invite"></div>';
		    subNav += '<div class="p_x_friend_request_button p_x_btn" onclick="persona.goToId('+"'requests_id'"+');" title="New Requests"><span id="request_count_id"></span></div>';
		
		
			var jsAction = 'persona.searchFriendsButton();';
			var buttonCss = 'p_x_filter_button';


		
			var button = '<input type="image" src="http://www.heyscore.com/heyboard/images/empty.gif" title="Update Your Status" class="'+buttonCss+'" />';
		
		
		html=''+
		'<div id='+id+' class="p_section" style="display:none;">'+
			'<div class="p_sub_row">'+subNav+'</div>'+	
			'<div class="p_form_text">Filter friends by name or letters.</div>'+
			'<div class="p_form_row">'+
				'<form action="javascript:'+jsAction+'">'+
				button+	
				'<input id="'+id+'_field" class="p_filter_field" type="text" value="" name="Name">'+
				'<div class="p_x_clear" onclick="javascript:persona.searchFriendsButton(1);" title="Clear Field"></div>'+
				'</form>'+
			'</div>'+
			'<div class="p_x_empty_header"><span id="'+id+'_list_header" class="p_x_header"></span><img src="http://www.heyscore.com/heyboard/images/empty.gif" class="p_x_friends_ht" alt="Friends"/></div>'+
			pageSelectHtml+
			'<div class="p_scroll_area p_sa_friends_html" id="'+id+'_list_container">'+
			'</div>'+
			'<div class="p_full_hr p_zero_margin"></div>'+
		'</div>';
		
		return html;
		
	},
	inboxHtml : function(id){

		var pageSelectHtml = persona.pageSelectHtml(id);

		
		var jsAction = 'persona.searchFriendsButton();';
		var buttonCss = 'p_x_filter_button';


		
		var button = '<input type="image" src="http://www.heyscore.com/heyboard/images/empty.gif" title="Update Your Status" class="'+buttonCss+'" />';

		html=''+
		'<div id='+id+' class="p_section" style="display:none;">'+
			'<div class="p_form_text">Filter conversations by name or letters.</div>'+
			'<div class="p_form_row">'+
				'<form action="javascript:'+jsAction+'">'+
					button+	
					'<input id="'+id+'_field" class="p_filter_field" type="text" value="" name="Name">'+
					'<div class="p_x_clear" onclick="javascript:persona.searchFriendsButton(1);" title="Clear Field"></div>'+
				'</form>'+
			'</div>'+
			'<div class="p_x_empty_header"><span id="'+id+'_list_header" class="p_x_header"></span><img src="http://www.heyscore.com/heyboard/images/empty.gif" class="p_x_conversation_threads_ht" alt="Conversation Threads"/></div>'+
			pageSelectHtml+
			'<div class="p_scroll_area p_sa_inbox" id="'+id+'_list_container">'+
			'</div>'+
			'<div class="p_full_hr" style="margin:0px;"></div>'+
		'</div>';
		
		return html;
		
	},
	coinsHtml : function(id){
		
		var formStart = ''+
		'<form action="https://www.paypal.com/cgi-bin/webscr" method="post">'+
			'<input type="hidden" name="business" value="payment@heyscore.com">'+
			'<input type="hidden" name="cmd" value="_xclick">'+
			'<input type="hidden" name="upload" value="1">';

		var formEnd = ''+
			'<input type="hidden" name="cpp_header_image" value="http://www.heyscore.com/images/paypal/heyscore_logo_big.png">'+
			'<input type="hidden" name="no_shipping" value="1">'+
			'<input type="hidden" name="no_note" value="1">'+
			'<input type="hidden" name="currency_code" value="USD">'+
			'<input type="hidden" name="rm" value="2">'+
			'<input type="hidden" name="return" value="http://www.heyscore.com/">'+
			'<input type="hidden" name="cancel_return" value="http://www.heyscore.com/">'+
			'<INPUT TYPE="hidden" name="charset" value="utf-8">'+
			'<input type="image" src="http://www.heyscore.com/heyboard/images/empty.gif" alt="Submit Form" class="p_x_buy_button" />'+
		'</form>';
		
		var button1 = formStart+
		'<input type="hidden" id="form_cipher_1" name="custom" value="">'+
		'<input type="hidden" name="item_name" value="20 Coins">'+
		'<input type="hidden" name="amount" value="2">'+
		formEnd;
		
		var button2 = formStart+
		'<input type="hidden" id="form_cipher_2" name="custom" value="">'+
		'<input type="hidden" name="item_name" value="60 Coins">'+
		'<input type="hidden" name="amount" value="5">'+
		formEnd;
		
		var button3 = formStart+
		'<input type="hidden" id="form_cipher_3" name="custom" value="">'+
		'<input type="hidden" name="item_name" value="130 Coins">'+
		'<input type="hidden" name="amount" value="10">'+
		formEnd;
		
		
		

		html=''+
		'<div id='+id+' class="p_section" style="display:none;">'+
			'<div class="p_top_coin_row">You currently have</div>'+
			'<div id="coin_count_id" class="p_coin_big_row">&nbsp;</div>'+
			'<div class="p_bottom_coin_row">Coins on your account.</div>'+
			'<div class="p_x_buy_coins_header" title="Buy Coins"></div>'+
			
			'<div class="p_form_row">'+
				button1+
				'20 Coins For $2.'+
			'</div>'+
			'<div class="p_form_row">'+
				button2+
				'60 Coins For $5.'+
			'</div>'+
			'<div class="p_form_row">'+
				button3+
				'130 Coins For $10.'+
			'</div>'+
			'<div class="p_hr"></div>'+
			'<div class="p_form_text">'+
				'Pay with credit card or Paypal account. Payment is powered by Paypal.'+
			'</div>'+
			'<div class="p_x_about_coins_header" title="About Coins"></div>'+
			'<div class="p_form_text">'+
			'Coins are used to unlock Heyscore games.'+
			'</div>'+
		'</div>';
		
		return html;
	},
	drawCoins : function(){

			persona.lock();
			var url = persona.intG.hostURL+'db_coins.php?'+
			'id='+persona.g.loggedInAs[0];
			
			persona.dbCon(url,'script_coins_id');

	},
	coinSuccess : function(coins,cipher){
		$('coin_count_id').innerHTML = coins;
		
		if (cipher){
			persona.s.cipher = cipher;
		};

	},
	drawRequestCount : function(){

			persona.lock();
			var url = persona.intG.hostURL+'db_request_count.php?'+
			'id='+persona.g.loggedInAs[0];
			
			persona.dbCon(url,'script_request_count_id');

	},
	requestCountSuccess : function(requests,cipher){
		
		if (cipher){
			persona.s.cipher = cipher;
		};

		
		$('request_count_id').innerHTML = persona.intToPic(requests);
	},
	settingsHtml : function(id){
		
		var logOutText = ""+
		"Do you really want to deactivate this account? ##"+
		"Deactivating the account will log you out & hide #"+
		"all your messages and profile details from all #"+
		"other users. ##"+
		""+
		"Log in to Heyscore to reactivate this account #"+
		"and revert all messages and settings again.##"+
		"Press OK to deactivate account now.##";
		

		var buttonHtml1 = '<div class="p_x_close_account_button" onclick="persona.confirm('+"'"+logOutText+"'"+',persona.closeAccount)" title="Close This Account"></div>'; 

		
		var buttonHtml2 = '<div class="p_x_activate_again_button" onclick="persona.drawSettings(1,'+"'undefined'"+');" title="Activate Again"></div>';


		var logOutText = ""+
		"Are You sure that you dont want mail from Heyscore? ##"+
		"You will miss on alot of fun...##";
		
		var buttonHtml3 = '<div class="p_x_switch_to_no_button" onclick="persona.confirm('+"'"+logOutText+"'"+',persona.noMail);" title="Switch to No"></div>'; 
		
		var buttonHtml4 = '<div class="p_x_switch_to_yes_button" onclick="persona.drawSettings('+"'undefined'"+',1);" title="Switch to Yes"></div>'; 


		
		var subNav = '<div class="p_x_back_to_profile_button" onclick="persona.goToPreviousPage();" title="Back To Profile"></div>';

		
		var jsAction = 'persona.changePasswordButton();';
		var buttonCss = 'p_x_change_password_button';
		
		var buttonHtml5 = '<input type="image" src="http://www.heyscore.com/heyboard/images/empty.gif" title="Update Your Status" class="'+buttonCss+'" />';
		




	
		html=''+
		'<div id='+id+' class="p_section" style="display:none;">'+
			'<div class="p_sub_row">'+subNav+'</div>'+
			'<div class="p_x_change_password_header" title="Change Password"></div>'+
			'<form action="javascript:'+jsAction+'">'+
				'<div class="p_form_row">'+
					'Your Password<input id="settings_password" type="password" class="p_join_field" value="">'+
				'</div>'+
				'<div class="p_form_row">'+
					'New Password<input id="settings_new_password" type="password" class="p_join_field" value="">'+
				'</div>'+
				'<div class="p_form_row">'+
					'Retype New<input id="settings_repeat_new_password" class="p_join_field" type="password" value="">'+
				'</div>'+
				'<div class="p_form_row"><div class="p_valid_txt">Only a-Z, 0-9 And  -_</div>'+buttonHtml5+'</div>'+
			'</form>'+
			'<div class="p_x_account_settings_header" title="Account Settings"></div>'+
			'<div class="p_form_text">By closing your account, your profile will be hidden from all listings, messages, wall posts, friends lists etc. Activating it will revert everything back again.</div>'+
			'<div id="deactivete_account_button_id" class="p_form_row">'+buttonHtml1+'</div>'+
			'<div id="activate_account_button_id" class="p_form_row" style="display:none;">'+buttonHtml2+'Currently Closed!</div>'+
			'<div class="p_x_email_settings_header" title="e-mail Settings"></div>'+
			'<div class="p_form_text">Recive e-mail about new Persona services?</div>'+
			'<div id="dont_email_button_id" class="p_form_row">'+buttonHtml3+'Currently Yes!</div>'+
			'<div id="email_me_button_id" class="p_form_row" style="display:none;">'+buttonHtml4+'Currently No...</div>'+
		'</div>';
		
		return html;
		
	},
	closeAccount : function(){
		persona.drawSettings(0,undefined);
		persona.logOut();
	},
	noMail : function(){
		persona.drawSettings('undefined',0);
	},
	drawSettings : function(account,news){

		account += '';
		news += '';
		
		if ((validate(account,'integer').length == 1 || account=='undefined') && 
		(validate(news,'integer').length == 1 || news=='undefined')){
			persona.lock();
			var url = persona.intG.hostURL+'db_settings.php?'+
			'id='+persona.g.loggedInAs[0]+
			'&news='+news+
			'&account='+account;
							
			persona.dbCon(url,'script_settings_id');
		}
			
	},
	changePasswordButton : function(){
		
		var password = $('settings_password').value;
		var newPass = $('settings_new_password').value;
		var retype = $('settings_repeat_new_password').value;

		var errorText = validate(password,'password');
		errorText += validate(newPass,'password'); 
		errorText += validate(retype,'password'); 
		
		errorText = errorText.replace(/1/gi,'');
		
		if(errorText.length==0 && newPass == retype){
			
			password = SHA1(password);
			newPass = SHA1(newPass);
			retype = SHA1(retype);
			
			persona.lock();
			var url = persona.intG.hostURL+'db_change_password.php?'+
			'id='+persona.g.loggedInAs[0]+
			'&password='+password+
			'&newpass='+newPass+
			'&retype='+retype;
							
			persona.dbCon(url,'script_change_password_id');
			
			$('settings_password').value = '';
			$('settings_new_password').value = '';
			$('settings_repeat_new_password').value = '';
			
		} else {
			if (newPass != retype) {
				errorText += 'Retype did not match. ';
			}
			persona.error('Client Side: ' + errorText);
		}

	},
	changePasswordSuccess : function (cipher){
		if (cipher){
			persona.s.cipher = cipher;
		};
		persona.unlock();
		persona.error('Password Successfully Changed!');
	},
	settingsSuccess : function(active, email, cipher){
		
		if (cipher){
			persona.s.cipher = cipher;
		};
		
		if (active!=1){
			$('deactivete_account_button_id').style.display = 'none';
			$('activate_account_button_id').style.display = 'block';
		} else {
			$('deactivete_account_button_id').style.display = 'block';
			$('activate_account_button_id').style.display = 'none';
		}
	
		if (email!=1){
			$('dont_email_button_id').style.display = 'none';
			$('email_me_button_id').style.display = 'block';
		} else {
			$('dont_email_button_id').style.display = 'block';
			$('email_me_button_id').style.display = 'none';
		}
	
		persona.unlock();	
	},
	profileHtml : function(id){


		var buttonHtml = '<div class="p_x_send_button" onclick="persona.wallSendButton('+"'"+id+"'"+',0);" title="Send"></div>'; 

		var pageSelectHtml = persona.pageSelectHtml(id);
		
		var picture = "1.jpg";
		
		html=''+
		'<div id='+id+' class="p_section" style="display:none;">'+
			'<div id="'+id+'_info_container">'+
			'</div>'+
			'<div class="p_x_empty_header p_zero_margin_top"  title="Your Wall"><img src="http://www.heyscore.com/heyboard/images/empty.gif" class="p_x_your_wall_ht" alt="Your Wall - "/><span id="'+id+'_list_header" class="p_x_header"></span><img src="http://www.heyscore.com/heyboard/images/empty.gif" class="p_x_posts_ht" alt=" Posts"/></div>'+
			pageSelectHtml+
			'<div class="p_scroll_area p_sa_profile_html" id="'+id+'_list_container">'+
			'</div>'+
			'<div class="p_x_write_your_wall_header p_zero_margin_top" title="Write On Your Wall"></div>'+
					'<textarea id="'+id+'_msg" rows="3" class="p_reply_field"></textarea>'+
			'<div class="p_form_row"><div class="p_valid_txt">160 Char Limit</div>'+buttonHtml+'</div>'+
			'<div class="p_full_hr"></div>'+
		'</div>';
		
		return html;
	},
	wallSendButton : function(id){



		var tempToId = null;
		var messageId = null;
		
		if (id == 'my_profile_id'){
			messageId = id;
			tempToId = persona.g.loggedInAs[0];
		} else {
			messageId = 'friend_wall_id';
			tempToId = persona.g.currentFriendProfile;
		}
		
		var tempMessage = $(messageId+'_msg').value;
		
		var errorText = validate(persona.g.loggedInAs[0],'integer');
		
		if(persona.g.loggedInAs[1]!=''){
			errorText += validate(persona.g.loggedInAs[1],'file');
		}

		errorText += validate(persona.g.loggedInAs[2],'name');
		errorText += validate(tempToId,'integer'); 
		errorText += validate(tempMessage,'comment'); 
		

		
		
		errorText = errorText.replace(/1/gi,'');
		
		var iWS = isWhitespace(tempMessage);
		if (iWS!=''){
			if (errorText.length == 0){
				persona.lock();
				
				var url = persona.intG.hostURL+'db_send_wall.php?'+
				'fromId='+persona.g.loggedInAs[0]+
				'&fromPicture='+persona.g.loggedInAs[1]+
				'&fromName='+persona.g.loggedInAs[2]+
				'&toId='+tempToId+
				'&msg='+tempMessage;
								
				persona.dbCon(url,'script_send_wall_id');
				
				$(messageId+'_msg').value = '';
			} else {
				persona.error('Client Side: ' + errorText);
			}
		}

		
	},
	sendMessageButton : function(){

		// Fixat
		
		var tempToId = persona.g.currentFriendProfile;
		var tempMessage = $(persona.g.currentId+'_msg').value;
		
		
		var errorText = validate(persona.g.loggedInAs[0],'integer'); 
		errorText += validate(tempToId,'integer'); 
		errorText += validate(tempMessage,'comment'); 
		
		
		errorText = errorText.replace(/1/gi,'');
		
		var iWS = isWhitespace(tempMessage);
		if (iWS!=''){
			if (errorText.length == 0){
				persona.lock();
				
				var url = persona.intG.hostURL+'db_send_message.php?'+
				'fromId='+persona.g.loggedInAs[0]+
				'&toId='+tempToId+
				'&msg='+tempMessage;				
				persona.dbCon(url,'script_send_message_id');
			
				$(persona.g.currentId+'_msg').value = '';
				persona.unlock();

			} else {
				persona.error('Client Side: ' + errorText);
			}
		}
		
		
		
		
	},
	wallSendSuccess : function (cipher){
		
		if (cipher){
			persona.s.cipher = cipher;
		};
		
		persona.drawList(persona.g.currentId,0,null);
		
	},
	searchFriendsButton : function(clear){

		if(clear!=1){
			var tempSearchString = $(persona.g.currentId+'_field').value;	
		} else {
			var tempSearchString = '';
			$(persona.g.currentId+'_field').value = '';
		}
		
		
		if (persona.g.currentId=='add_invite_id'){
			var errorText = validate(tempSearchString,'filter');
			var errorText2 = validate(tempSearchString,'e-mail');
	
			errorText = errorText.replace(/1/gi,'');
			errorText2 = errorText2.replace(/1/gi,'');
	
			if (errorText.length == 0 || errorText2.length == 0){
				persona.lock();
				persona.g.searchForString = tempSearchString;
				persona.drawList(persona.g.currentId,0);
				persona.unlock();
			} else {
				persona.error('Client Side: Neither e-mail or valid search string.');
			}
		} else {
			var errorText = validate(tempSearchString,'filter');

			errorText = errorText.replace(/1/gi,'');

			if (errorText.length == 0){
				persona.lock();
				persona.g.searchForString = tempSearchString;
				persona.drawList(persona.g.currentId,0);
				persona.unlock();
			} else {
				persona.error('Client Side:' + errorText);
			}
		}

	},
	addInviteHtml : function(id){
		

		var subNav = '<div class="p_x_return_to_friends_list_button" onclick="persona.goToPreviousPage();" title="Return To Friends List"></div>';

		var pageSelectHtml = persona.pageSelectHtml(id);
		
		
		var jsAction = 'persona.searchFriendsButton();';
		var buttonCss = 'p_x_search_button';


		
		var button = '<input type="image" src="http://www.heyscore.com/heyboard/images/empty.gif" title="Update Your Status" class="'+buttonCss+'" />';
		
		html=''+
		'<div id='+id+' class="p_section" style="display:none;">'+
			'<div class="p_sub_row">'+subNav+'</div>'+
			'<div class="p_x_add_invite_header" title="Add & Invite Friends"></div>'+		
			'<div class="p_form_text">Search for friends by alias / name or e-mail. Invite friends by typing their e-mail, an invite will be sent to the adress.</div>'+
			'<div class="p_form_row">'+
				'<form action="javascript:'+jsAction+'">'+
					button+	
					'<input id="'+id+'_field" class="p_search_field" type="text" value="" name="Name">'+
					'<div class="p_x_clear p_cf_search" onclick="javascript:persona.searchFriendsButton(1);" title="Clear Field"></div>'+
				'</form>'+
			'</div>'+
			'<div class="p_x_empty_header" title="Found"><img src="http://www.heyscore.com/heyboard/images/empty.gif" class="p_x_found_ht p_x_header" alt="Found "/><span id="'+id+'_list_header" class="p_x_header"></span></div>'+
			pageSelectHtml+
			'<div class="p_scroll_area p_sa_add_invite" id="'+id+'_list_container">'+
			'</div>'+
		'</div>';
		
		return html;

	}, 
	requestsHtml : function(id){
		

		
		var subNav = '<div class="p_x_return_to_friends_list_button" onclick="persona.goToPreviousPage();" title="Return To Friends List"></div>';

		var pageSelectHtml = persona.pageSelectHtml(id);
		
		html=''+
		'<div id='+id+' class="p_section" style="display:none;">'+
			'<div class="p_sub_row">'+subNav+'</div>'+
			'<div class="p_x_empty_header" title="Friend Requests"><span id="'+id+'_list_header" class="p_x_header"></span><img src="http://www.heyscore.com/heyboard/images/empty.gif" class="p_x_friend_request_ht" alt="Friends Requests"/></div>'+
			pageSelectHtml+
			'<div class="p_scroll_area p_sa_request" id="'+id+'_list_container" >'+
			'</div>'+
		'</div>';
		
		return html;
		
	},
	conversationHtml : function(id){
		
		var subNav = '<div class="p_x_back_to_inbox_button" onclick="persona.goToPreviousPage();" title="Back To Inbox"></div>';
		
		var button = '<div class="p_x_send_reply_button" onclick="persona.sendMessageButton();" title="Send Reply"></div>';
		var pageSelectHtml = persona.pageSelectHtml(id);
		
		html=''+
		'<div id='+id+' class="p_section" style="display:none;">'+
			'<div class="p_sub_row">'+subNav+'</div>'+
			'<div class="p_x_empty_header" title="Conversation">Conversation With <span id="'+id+'_list_header"></span></div>'+
			pageSelectHtml+
			'<div class="p_scroll_area p_sa_conversation" id="'+id+'_list_container" >'+
			'</div>'+
			'<div class="p_full_hr p_zero_margin"></div>'+
			'<textarea id="'+id+'_msg" rows="6" class="p_reply_field"></textarea>'+
			'<div class="p_form_row"><div class="p_valid_txt">160 Char Limit</div>'+button+'</div>'+
			'<div class="p_full_hr"></div>'+
		'</div>';
		
		return html;
		
	},
	friendProfileHtml : function(id){
		

		
		var pageSelectWallHtml = persona.pageSelectHtml(id+'_wall');
		var pageSelectFriendsHtml = persona.pageSelectHtml(id+'_friends');
		var tabList = [['Wall','friend_wall_id','x_friend_wall_tab'],['Friends','friend_friends_id','x_friend_friends_tab'],['Write Message','friend_message_id','x_friend_message_tab']];
		var subTabs = persona.addSubTabs(tabList);
		
		var sendButton = '<div class="p_x_send_message_button" onclick="persona.sendMessageButton();" title="Send Message"></div>';
		
		var postOnWall = '<div class="p_x_post_on_wall_button" onclick="persona.wallSendButton('+"''"+',0);" title="Post On Wall"></div>';

		var subNav = '<div class="p_x_back_to_previous_page_button" onclick="persona.goToPreviousPage();" title="Back To Previous Page"></div>';

		html=''+
		'<div id='+id+' class="p_section" style="display:none;">'+
			'<div class="p_sub_row">'+subNav+'</div>'+
			'<div class="p_full_hr"></div>'+
			'<div id="'+id+'_info_container">'+
			'</div>'+
			subTabs+
			'<div id="friend_wall_id" style="display:;">'+
				pageSelectWallHtml+
				'<div class="p_scroll_area p_sa_friends_wall" id="'+id+'_wall_list_container" >'+
				'</div>'+
				'<div class="p_full_hr p_zero_margin" ></div>'+
				'<textarea rows="3" id="friend_wall_id_msg" class="p_reply_field"></textarea>'+
				'<div class="p_form_row"><div class="p_valid_txt">160 Char Limit</div>'+postOnWall+'</div>'+
			'</div>'+
			'<div id="friend_friends_id" style="display:none;">'+
				pageSelectFriendsHtml+
				'<div class="p_scroll_area p_sa_friend_friends" id="'+id+'_friends_list_container" >'+
				'</div>'+
			'</div>'+
			'<div id="friend_message_id" style="display:none;">'+
				'<textarea id="'+id+'_msg"  rows="6" class="p_reply_field"></textarea>'+
			'<div class="p_form_row"><div class="p_valid_txt">160 Char Limit</div>'+sendButton+'</div>'+
			'</div>'+
		'</div>';

		return html;
		
	},
	enterDeleteMode : function(){
		persona.resetDeleteRadio();
		$('profile_container_id').addClassName('p_delete_active');
		$('delete_control_id').childNodes[0].style.display='none';
		$('delete_control_id').childNodes[1].style.display='block';
	},
	exitDeleteMode : function(){
		persona.resetDeleteRadio();
		$('profile_container_id').removeClassName('p_delete_active');
		$('delete_control_id').childNodes[0].style.display='block';
		$('delete_control_id').childNodes[1].style.display='none';
	},
	resetDeleteRadio : function (){
		if ($(persona.g.currentId+'_radio_0')){
			$(persona.g.currentId+'_radio_0').checked = true;	
		}
	},
	deleteControlHtml : function(id){
		

		
		var subNav = '<div class="p_x_enter_delete_mode_button" onclick="persona.enterDeleteMode();" title="Enter Delete Mode"></div>';

		var subNav2 = '<div class="p_x_exit_delete_mode_button" onclick="persona.exitDeleteMode();" title="Exit Delete Mode"></div>';

		var button = '<div class="p_x_delete_selected_button" onclick="persona.deleteListItem();" title="Delete Selected"></div>';


		
		var html = ''+
		'<div id='+id+'>'+
			'<div class="p_sub_row" style="display:;">'+subNav+'</div>'+
			'<div class="p_sub_row"  style="display:none;">'+subNav2+button+'</div>'+
		'</div>';
		
		return html;
		
	},
	deleteListItem : function(){
		
		var answer = persona.confirm('Warning! This deletes the selected item permanently!','');
		
		
		
		if(answer!=true){

			persona.exitDeleteMode();
			return;
		}
		
		if (persona.g.currentId == 'requests_id'){
			persona.lockList();
			var url = persona.intG.hostURL+'db_delete_friend.php?order_id='+
			persona.g.currentList[persona.g.itemOffset][0];
			
			persona.dbCon(url,'script_delete_friend_id');
			

		}
						
							

		
		for (var i=0;i<=persona.intG.listItems;i++){
			if ($(persona.g.currentId+'_radio_'+i)){
				if ($(persona.g.currentId+'_radio_'+i).checked==true){
					var tempListItem = i-1;
					if (tempListItem > -1){
						
						persona.lockList();
						
						switch(persona.g.currentId)
						{
						case 'my_profile_id':

							var url = persona.intG.hostURL+'db_delete_wall.php?order_id='+
							persona.g.currentList[tempListItem][0];
							persona.dbCon(url,'script_delete_wall_id');
							break;	
						case 'friends_id':
							var url = persona.intG.hostURL+'db_delete_friend.php?order_id='+
							persona.g.currentList[tempListItem][0];
							
							persona.dbCon(url,'script_delete_friend_id');
							break;	
						case 'inbox_id':

							
							
							var url = persona.intG.hostURL+'db_delete_conversation.php?'+
							'id='+persona.g.loggedInAs[0]+
							'&with_id='+persona.g.currentList[tempListItem][1];
							
							persona.dbCon(url,'script_delete_conversation_id');
							
							
							break;	
						case 'conversation_id':
					
							var url = persona.intG.hostURL+'db_delete_message.php?'+
							'id='+persona.g.loggedInAs[0]+
							'&order_id='+persona.g.currentList[tempListItem][0];
							

							
							
							persona.dbCon(url,'script_delete_message_id');
							
							break;	
						}
						
						persona.exitDeleteMode();
					}
				}
			}
		}
		
		persona.resetDeleteRadio();
	},
	deleteSuccess : function (cipher){
		
		persona.markUnmarkTabs(persona.g.currentId);
		// herelol
		
		
		
		if (cipher){
			persona.s.cipher = cipher;
		};
	
		var tempOffset = ($(persona.g.currentId+'_page_select').value - 1);
		persona.drawList(persona.g.currentId,tempOffset,null);		
			
	},
	pictureSuccess : function(){
		
	},
	showHideDeleteControl : function(id){
		if ($('delete_control_id')){
			persona.exitDeleteMode();
			if(id=='my_profile_id' || id=='friends_id' || id=='inbox_id' || id=='conversation_id'){
				$('delete_control_id').style.display='block';
			} else {
				$('delete_control_id').style.display='none';
			}
		}

	},
	drawList : function(id,offset,nextFriendProfileId){
		
		persona.g.pageOffset = offset;
		
		
		if (id=='friend_profile_id'){
			var addSubTab = ['_wall','_friends',''];
			id=id+addSubTab[persona.g.currentFriendTab[1]];
		}
		
		persona.g.drawListId = id;
		

		
		var tempList = persona.loadlist(id,nextFriendProfileId);

	},
	drawListAfterLoad : function(tempList){

		var htmlList = persona.makeListHtml(persona.g.drawListId,tempList);
		
		if ($(persona.g.drawListId+'_list')){
			$(persona.g.drawListId+'_list').remove();
		}
		
		
		Element.insert($(persona.g.drawListId+'_list_container'),htmlList);
		
		persona.setScroll(persona.g.drawListId,persona.g.currentScrollOffset); 	
		

		
		persona.unlock();

	},
	drawPageSelectBox : function(tempWall){
		
		$(persona.g.drawListId+'_page_select').options.length = 0;
		var tempPageCount = Math.round(tempWall.length/persona.intG.listItems+0.49);
		$(persona.g.drawListId+'_page_count').innerHTML = tempPageCount;
		for (var i=0;i<tempPageCount;i++){
   			$(persona.g.drawListId+'_page_select').options[i] = new Option(i+1,i+1);
		}
		$(persona.g.drawListId+'_page_select').selectedIndex = persona.g.pageOffset;
		
	},
	intToPic : function(tempCount){
		
		tempCount = ''+tempCount;
		tempCount = tempCount.split('');
		
		var html = '';
		
		for (var i=0;i<tempCount.length;i++){
			html += '<img src="http://www.heyscore.com/heyboard/images/empty.gif" class="p_x_'+tempCount[i]+'" />';
		}
		
		
		return html;
	},
	drawHeader : function (tempWall){
		
		if($(persona.g.drawListId+'_list_header')){
			if (tempWall){
				var tempCount = tempWall.length;
			} else {
				var tempCount = 0;
			}
	
			
			
			
			tempCount = persona.intToPic(tempCount);
			
				
			var tempInnerHTML = '';
			
			
			var tempInnerHTML = '';
			
			switch (persona.g.drawListId){
				case 'my_profile_id':
					tempInnerHTML = ''+tempCount;
				break;
				case 'friends_id':
					tempInnerHTML = ''+tempCount;
				break;
				case 'inbox_id':
					tempInnerHTML = ''+tempCount;
				break;
				case 'conversation_id':
					tempInnerHTML = persona.g.conversationName;
				break;
				case 'add_invite_id':
					tempInnerHTML = tempCount+' <div class="p_x_header_text">'+persona.g.searchForString+'</div>';
				break;
				case 'requests_id':
					tempInnerHTML = ''+tempCount;
				break;
			}

			if (tempInnerHTML.length>0){
				$(persona.g.drawListId+'_list_header').innerHTML = tempInnerHTML;
			}
			
			
		}
		
	},
	loadlist : function(id){

		switch(id)
		{
		case 'my_profile_id':

			var url = persona.intG.hostURL+'db_load_wall.php?id='+
			persona.g.loggedInAs[0];
			persona.dbCon(url,'script_load_wall_id');
			break;	
		case 'friends_id':
			var url = persona.intG.hostURL+'db_load_friends.php?id='+
			persona.g.loggedInAs[0]+'&search_string='+persona.g.searchForString;
			persona.dbCon(url,'script_load_friends_id');
			break;	
		case 'inbox_id':
			
			var url = persona.intG.hostURL+'db_load_inbox.php?id='+
			persona.g.loggedInAs[0]+'&search_string='+persona.g.searchForString;
			persona.dbCon(url,'script_load_inbox_id');
			
			break;	
		case 'add_invite_id':
			if (persona.g.searchForString.length < 1){
				persona.loadListSuccess([]);
			} else {
				var url = persona.intG.hostURL+'db_search_add_invite.php?'+
				'search_string='+persona.g.searchForString+
				'&id='+persona.g.loggedInAs[0];
				persona.dbCon(url,'script_search_add_invite_id');
			}
			
			break;	
		case 'requests_id':
			
			var url = persona.intG.hostURL+'db_load_friend_requests.php?id='+
			persona.g.loggedInAs[0]+'&search_string='+persona.g.searchForString;
			persona.dbCon(url,'script_load_friend_requests_id');
			
			break;	
		case 'conversation_id':
			var url = persona.intG.hostURL+'db_load_conversation.php?id='+
			persona.g.loggedInAs[0]+'&with_id='+persona.g.currentFriendProfile;
			persona.dbCon(url,'script_load_conversation_id');
			
			break;	
		case 'friend_profile_id_wall':
		
			var url = persona.intG.hostURL+'db_load_wall.php?id='+
			persona.g.currentFriendProfile;
			persona.dbCon(url,'script_load_wall_id');
			
			break;	
		case 'friend_profile_id_friends':
			var url = persona.intG.hostURL+'db_load_friends.php?id='+
			persona.g.currentFriendProfile+'&search_string='+persona.g.searchForString;
			persona.dbCon(url,'script_load_friends_id');
			break;	
		}
	},
	loadListSuccess : function(tempWall,cipher){
		
		if (cipher){
			persona.s.cipher = cipher;
		};

		persona.drawPageSelectBox(tempWall);
		persona.drawHeader(tempWall);
	
		var tempList = [];
		
		for (var i=(persona.g.pageOffset*persona.intG.listItems);i<(persona.intG.listItems+persona.g.pageOffset*persona.intG.listItems);i++){
			if(i<tempWall.length){
			 	tempList.push(tempWall[i]);
			}
		}
		


		persona.g.currentList = tempList;

		persona.drawListAfterLoad(tempList);
		
	},
	makeListHtml : function(id,array){
		

		
		var friendSay = '';
		var verb = '';
		var img = '';
		
		switch(id)
		{
		case 'my_profile_id':
			var blueLinkArray = [["Visit & Reply","persona.goToId('friend_profile_id',[2,0])","visit_reply_link"]];
				friendSay = "<div class='p_friend_say'></div>";
				verb = 'Said:';
			break;	
		case 'friends_id':
			var blueLinkArray = [
				["Write Message","persona.goToId('friend_profile_id',[2,2])","write_message_link"],
				["Visit","persona.goToId('friend_profile_id',[2,0])","visit_link"]];
			break;	
		case 'inbox_id':
			/*
			for(var i=0;i<array.length;i++){
				console.log(array[i]);
			}*/
			
			var blueLinkArray = [
				["Read & Reply","persona.goToId('conversation_id')","read_reply_link"],
				["Visit","persona.goToId('friend_profile_id',[2,0])","visit_link"]];
				friendSay = "<div class='p_friend_say'></div>";
				verb = 'Conversation With';
				img = 'unread';
			break;	
		case 'add_invite_id':
		
			var blueLinkArray = [
				["Add To Friends","persona.addFriend()","add_friends_link"],
				["Visit","persona.goToId('friend_profile_id',[2,0])","visit_link"]];
				
			break;	
		case 'requests_id':
			var blueLinkArray = [
			["Accept","persona.acceptFriendButton()","accept_link"],
			["Visit","persona.goToId('friend_profile_id',[2,0])","visit_link"],
			["Ignore","persona.deleteListItem()","ignore_link"]];
			break;	
		case 'conversation_id':
			var blueLinkArray = [
				["Visit","persona.goToId('friend_profile_id',[2,0])","visit_link"]];
				friendSay = "<div class='p_friend_say'></div>";
				verb = 'Said:';
				img = 'unread';
			break;	
		case 'friend_profile_id_wall':
			var blueLinkArray = [
				["Visit","persona.goToId('friend_profile_id',[2,0])","visit_link"]];
				friendSay = "<div class='p_friend_say'></div>";
				verb = 'Said:';
			break;	
		case 'friend_profile_id_friends':
			var blueLinkArray = [
				["Visit","persona.goToId('friend_profile_id',[2,0])","visit_link"]];
			break;	
		}

		var listLength = array.length;
		var html = '<input type="radio" id="'+id+'_radio_0" name="group1" checked="checked" value="0" class="p_hidden_radio_button" />';

		for (var i=0;i<listLength;i++){
			
				var blueLink = persona.blueLinkHtml(blueLinkArray,i);
			
				if (array[i][2]!=''){
					var picture = array[i][2];
				} else {	
					var picture = 'nopicture.gif';
				}
				
				var read = '';
				if(array[i][5] == 0 && (id=='inbox_id' || id=='conversation_id')){
					var read = 'p_unread';	
					
				}
				
				
				
				var messageHead = array[i][3];

				var conversation = "";
				if (id=='my_profile_id' || id=='friend_profile_id_wall' || id=='inbox_id' || id=='conversation_id' || id=='friends_id' || id=='friend_profile_id_friends'){
					conversation = array[i][4];
				}

				html += ''+
				'<div class="p_list_item '+read+'">'+
					'<img src="http://www.heyscore.com/heyboard/images/profilesmall/'+picture+'" class="p_small_profile_picture" />'+
					'<div class="p_message_column">'+
						'<div class="p_list_row_head">'+messageHead+' '+verb+'</div>'+
						'<div class="p_list_row_head">'+conversation+'</div>'+
						'<div  class="p_list_row_head" _itemNumber="'+i+'">'+blueLink+'</div>'+
					'</div>'+
					friendSay+
					'<div class="p_select_item">'+
						'<input type="radio" id="'+id+'_radio_'+(i+1)+'" name="group1" value="'+(i+1)+'" class="p_delete_radio_button">'+
					'</div>'+
				'</div>';
				
		}
		
		html = '<div id="'+id+'_list" class="p_scroll_container">' + html + '</div>';
		
		return html;
		
	},
	pageSelectHtml : function(id){


		
		selectBoxId = id+'_page_select';
		
		var html = ''+
		'<div class="p_page_select_container">'+
			'<div class="p_x_left_arrow" onclick="javascript:persona.pageSelectArrows(-1,'+"'"+selectBoxId+"'"+')" title="Previous Page"></div>'+
			'Page <select id="'+selectBoxId+'" onchange="persona.pageSelectBox(persona.g.currentId,value-1)">'+
			'</select> Of <span id="'+id+'_page_count">4</span>'+
			'<div class="p_x_right_arrow" onclick="javascript:persona.pageSelectArrows(1,'+"'"+selectBoxId+"'"+')" title="Next Page"></div>'+
		'</div>';
		
		return html;
	},
	pageSelectArrows : function(move,id){
		
	
		
		var currentIndex= $(id).selectedIndex;
		var selectLength = $(id).length;
		
		if((currentIndex+move)<selectLength && (currentIndex+move)>-1){
			var newIndex = currentIndex+move;
			persona.lockList();
			persona.drawList(persona.g.currentId,newIndex);
		}
		
		persona.resetDeleteRadio();
		
	},
	pageSelectBox : function(tempId,tempVal){
		
		persona.lockList();
		persona.drawList(tempId,tempVal);
			
	},
	logOut : function(){

		persona.lock();
		persona.resetGlobals();
		
		var url = persona.intG.hostURL+'db_logout.php?';
		
		persona.dbCon(url,'script_logout');
		


	},
	resetGlobals : function(){
		
		persona.g.previousPageList = [];
		persona.g.currentFriendTab = 0;
		persona.g.currentFriendProfile = [];
		persona.g.currentList = [];
		persona.g.pageOffset = 0;
		persona.g.itemOffset = 0; 
		persona.g.drawListId = '';
		persona.g.loggedInAs = [];
		persona.g.searchForString = '';
		persona.g.picNr = 0;
		persona.g.currentScrollOffset = 0;
		persona.g.subTabGlobalTemp = [];
		persona.clearFields();
		persona.s.cipher = '';

	},
	clearFields : function(){
		
		persona.g.searchForString = '';
		if ($("add_invite_id_field")){$("add_invite_id_field").value = '';};
		if ($("friends_id_field")){$("friends_id_field").value = '';};
		if ($("my_profile_id_msg")){$("my_profile_id_msg").value = '';};
		if ($("status_field_id")){$("status_field_id").value = '';};
		if ($("inbox_id_field")){$("inbox_id_field").value = '';};
		if ($("conversation_id_msg")){$("conversation_id_msg").value = '';};
		if ($("friend_profile_id_msg")){$("friend_profile_id_msg").value = '';};
		if ($("login_email")){$("login_email").value = '';};
		if ($("login_password")){$("login_password").value = '';};
		if ($("join_full_name")){$("join_full_name").value = '';};
		if ($("join_email")){$("join_email").value = '';};
		if ($("join_password")){$("join_password").value = '';};
		if ($("forgot_email")){$("forgot_email").value = '';};
		
	},
	error : function(errorText){
		
		
		errorText = errorText.replace(/#/g, "\r\n");
		
		alert(errorText);

	},
	confirm : function(text, callback){
		
		text = text.replace(/#/g, '\r\n');


		var answer = confirm(text);
		if(answer){
			if(callback!=''){
				callback && callback();
			} else {
				return answer;
			}
			
		}
		
	},
	restoreSession : function(cipher, oldSessionGlobals){
		
		oldSessionGlobals = oldSessionGlobals.replace(/-andsign-/g,"&");

		persona.s.cipher = cipher;
		
		persona.g = oldSessionGlobals.evalJSON();
		
		persona.drawMyProfile(persona.g.loggedInAs);
		
		var ar = persona.g.loadId;
		
		persona.goToId(ar[0],ar[1],ar[2],ar[2],ar[4],ar[5],true);	

		$('login_container_id').style.display='none';
		$('profile_container_id').style.display='block';
		
	}
};




