/**
 * SZENE1 BEHAVIOUR
 *
 */

Szene1BehaviourGlobal = {
	'.searchUser' : function(element)
	{
		element = $(element);
		var isRequest	= false;
		var lstRqTxt	= '';
		var time		= 0;
		var markedElm	= 1;
//		var regexp		= /^[a-zA-Z0-9_-\s]{3,}$/;

		modeArray		= element.className.split(' ');
		mode			= modeArray[1];

		var kCode;

		element.onkeyup = function(EventHdl)
		{
			//SPECIAL HACK FOR TOPSEARCH USER BOX
			if(element.id=='topSearchId' &&
			   $('szene1SearchType') &&
			   $F('szene1SearchType') != 'user')
			{
				return;
			}

			if(!EventHdl)
			{
				EventHdl = window.event;
			}

			if (EventHdl.which)
			{
				kCode = EventHdl.which;
			}
			else if (EventHdl.keyCode)
			{
				kCode = EventHdl.keyCode;
			}

			doSearch();
		}

		function doSearch()
		{
			if(kCode==13 && element.value.empty())
			{
				return;
			}
			else if((kCode==40 || kCode==38))
			{
				if(markedElm==1 && kCode==38)
				{
					return;
				}

				if(kCode==38)
				{
					var newMarkedElm = markedElm - 1;
				}
				else if(kCode==40)
				{
					var newMarkedElm = markedElm + 1;
				}

				if($('user_sugg_elm_'+newMarkedElm))
				{
					$('user_sugg_elm_'+newMarkedElm).style.backgroundColor = '#666666';
					$('user_sugg_elm_'+markedElm).style.backgroundColor    = 'transparent';
					//$('user_sugg_elm_'+newMarkedElm).style.color = '#891112';
					//$('user_sugg_elm_'+markedElm).style.color    = '#ffffff';
					markedElm = newMarkedElm;
				}
			}
			else if(kCode==13 &&
					$('user_sugg_elm_'+markedElm) &&
					$('user_sugg_elm_'+markedElm).readAttribute('title'))
			{
				if(mode && mode=='fillin')
				{
					element.value = $('user_sugg_elm_'+markedElm).readAttribute('title');
				}
				else if(mode && mode=='searchUser')
				{
					window.location.href = '/user/'+$('user_sugg_elm_'+markedElm).readAttribute('title');
				}
				else if(mode && !mode.blank() && mode!='fillin')
				{
					var username = $('user_sugg_elm_'+markedElm).readAttribute('title');
					eval(mode+'("'+username+'");');
				}
				else
				{
					window.location.href = '/user/'+$('user_sugg_elm_'+markedElm).readAttribute('title');
				}

				//alert($('searchuser_container'));

				if($('searchuser_container'))
				{
					$('searchuser_container').remove();
				}
			}
			else if((new Date()).getTime()-time > 500 &&
					lstRqTxt != element.value)
			{
				doRequest();
			}
		}

		function doRequest()
		{
			if(!isRequest)
			{
				var fillinId = '';
				if(mode && mode=='fillin')
				{
					fillinId = 'inputId='+element.id+'&';
				}
				else if(mode &&
						!mode.empty() &&
						mode!='fillin' &&
						element.id!='topSearchId')				// ANOTHER HACK FOR THE TOPSEARCH BOX
				{
					fillinId = 'func='+mode+'&';
				}
				isRequest	= true;
				time		= (new Date()).getTime();
				lstRqTxt	= element.value;

				if($('searchuser_container'))
				{
					$('searchuser_container').remove();
				}

				var xy = Position.cumulativeOffset(element);
				var x  = xy[0];
				var y  = xy[1] + 25;

				var boxCon;
				boxCon  = '<div class="szene1_quickinfo" id="searchuser_container" ';
				boxCon += '     style="position: absolute; width: 150px; z-index: 1000;';
				boxCon += '            top: '+y+'px; left: '+x+'px;">';
				boxCon += '  <div class="edge_top"><div><div>&nbsp;</div></div></div>';
				boxCon += '  <div class="body" style="padding:8px; font-size: 11px;" ';
				boxCon += '       id="userSearch_body">';
				boxCon += '    <center>';
				boxCon += '      <img src="/images/icons/ajax-loader-white.gif" border="0" /> ' + Szene1Translator.translate('loadingDots',null) + "\n";
				boxCon += '    </center>';
				boxCon += '  </div>';
				boxCon += '  <div class="edge_bottom"><div><div>&nbsp;</div></div></div>';
				boxCon += '</div>';

				new Insertion.Bottom('szene1AdditionalWindows',boxCon);
				//Effect.Appear($('searchuser_container'), {duration:1.0});
				new Draggable($('searchuser_container'));

				new Ajax.Request('/ajax/user/search', {
					method: 'post',
					parameters: fillinId + 'phrase=' + element.value,
					onSuccess: function(t){ buildSuggestion(t); }
				});
			}
		}

		element.onblur = function()
		{
			/*if(!mode || mode!='fillin')
			//{
				//window.setTimeout('$(\''+element.id+'_suggestion\').hide();',500);
				//$(element.id+'_suggestion').hide();
				//$(element.id+'_suggestion').innerHTML = '';
			}*/
			if($('searchuser_container'))
			{
				//alert('Container hide called!');
				setTimeout("$('searchuser_container').hide()",200);
			}
			lstRqTxt = '';
			//element.value = 'Usersuche...';
		}

		element.onfocus = function()
		{
			if(element.value == 'Usersuche...')
			{
				element.value = '';
			}

			if($('searchuser_container') && $('userSearch_body') &&
			   element.id!='topSearchId')				// JUST ANOTHER HACK FOR THE TOPSEARCH BOX
			{
				if(lstRqTxt != element.value)
				{
					var boxCon;
					boxCon  = '<center>';
					boxCon += '  <img src="/images/icons/ajax-loader-white.gif" border="0"> ' + Szene1Translator.translate('loadingDots',null) + "\n";
					boxCon += '</center>';
					$('userSearch_body').innerHTML = boxCon;
					doRequest();
				}
				$('searchuser_container').show();
			}
			else if($('searchuser_container'))
			{
				$('searchuser_container').remove();
			}
		}

		function buildSuggestion(t)
		{
			//alert('test');
			isRequest = false;
			/**
			* if the request is ready, but focus is on another element meanwhile ->
			* we return and so do not show the suggestion-box
			if(!element.hasFocus())
			{
				alert('test');
				return;
			}
			*/

			markedElm = 1;
			//$(element.id+'_suggestion').show();
			//$(element.id+'_suggestion').innerHTML = t.responseText;

			if($('searchuser_container'))
			{
				$('searchuser_container').remove();
			}

			var xy = Position.cumulativeOffset(element);
			var x  = xy[0];
			var y  = xy[1] + 25;

			var boxCon;
			boxCon  = '<div class="szene1_quickinfo" id="searchuser_container" ';
			boxCon += '     style="position: absolute; width: 150px; ';
			boxCon += '            top: '+y+'px; left: '+x+'px; z-index: 1000;">';
			boxCon += '  <div class="edge_top"><div><div>&nbsp;</div></div></div>';
			boxCon += '  <div class="body" style="padding:8px; font-size: 11px;" ';
			boxCon += '       id="userSearch_body">';
			boxCon += '    '+ t.responseText;
			boxCon += '  </div>';
			boxCon += '  <div class="edge_bottom"><div><div>&nbsp;</div></div></div>';
			boxCon += '</div>';

			//textbox  = '<div class="szene1_quickinfo" id="notificationbox_container" style="position: absolute; width:450px; display:none;top:'+y+'px;left:'+x+'px">';
			//textbox += t.responseText;
			//textbox += '</div>';

			new Insertion.Bottom('szene1AdditionalWindows',boxCon);
			//Effect.Appear($('searchuser_container'), {duration:1.0});
			new Draggable($('searchuser_container'));
			//Behaviour.applySheet(Szene1Behaviour);

			if(lstRqTxt != element.value)
			{
				var boxCon;
				boxCon  = '<center>';
				boxCon += '  <img src="/images/icons/ajax-loader-white.gif" border="0"> ' + Szene1Translator.translate('loadingDots',null) + "\n";
				boxCon += '</center>';
				$('userSearch_body').innerHTML = boxCon;
				doRequest();
			}
		}
	},
	doajaxpnpost : function(element)
	{

			var pn_rec = $F('quickform_pn_recipient');
			var pn_msg = $F('quickform_pn_text').strip();
			    pn_msg = encodeURIComponent(pn_msg);

			if(pn_msg && pn_rec && pn_msg != '' && pn_rec != '')
			{
				new Ajax.Request('/ajax/pn/quickform/send', {
					method: 'post',
					postBody: 'rec='+pn_rec+'&msg='+pn_msg,
					onSuccess:
						function(t)
						{
							$('notificationbox_container').innerHTML = t.responseText;
							eval(t.responseText);
							Behaviour.applySheet(Szene1BehaviourGlobal);
						},
					onFailure:
						function(t)
						{
							alert(Szene1Translator.translate('anErrorOccurred',null));
							$('notificationbox_container').remove();
						}
				});
			}
			else
			{
				alert(Szene1Translator.translate('noValidAddresseeOrMessageMissing',null));
			}

	},
	openAjaxPNForm : function(element)
	{

			new Ajax.Request('/ajax/pn/quickform', {
				method: 'post',
				postBody: 'user='+element.id.split('#').last(),
				onSuccess: success,
				onFailure: failed
			});


		function success(t)
		{
			if($('notificationbox_container'))
			{
				$('notificationbox_container').remove();
			}

			var xy = Position.cumulativeOffset(element);
			var x  = xy[0] + 0;
			var y  = xy[1] + 20;

			textbox  = '<div class="szene1_quickinfo" id="notificationbox_container" style="position: absolute; width:450px; display:none;top:'+y+'px;left:'+x+'px">';
			textbox += t.responseText;
			textbox += '</div>';

			new Insertion.Bottom('szene1AdditionalWindows',textbox);
			Position.center($('notificationbox_container'));
			Effect.Appear($('notificationbox_container'), {duration:1.0});
			new Draggable($('notificationbox_container'));
			Behaviour.applySheet(Szene1BehaviourGlobal);
		}

		function failed(id)
		{
			alert(Szene1Translator.translate('errorLoadingPMForm',null));
		}
	}

}//Szene1BehaviourGlobal
	Behaviour.register(Szene1BehaviourGlobal);
    /**
     *
     * ########################################################################
     *
     *    IGNORELIST
     *
     * ########################################################################
     *
     */
var QuickPage = {
	reloadquickpage : function(element)
	{
		/*
		element.onclick = function()
		{
		*/
			var userid = element.id.split('_').last();
			var mode   = element.id.split('_').first();

			if(mode=='quickpageform')
			{
				var url = '/ajax/user/quickpage/'+userid;
			}
			else
			{
				var url = '/ajax/pn/quickpage';
			}

			var html;
			html  = '    <div class="edge_top">' + "\n";
			html += '        <div><div>&nbsp;</div></div>' + "\n";
			html += '    </div>' + "\n";
			html += '    <div class="body"><center>' + "\n";
			html += '            <img src="/images/icons/ajax-loader-white.gif" border="0"> ' + Szene1Translator.translate('loadingDots',null) + "\n";
			html += '    </center></div>' + "\n";
			html += '    <div class="edge_bottom">' + "\n";
			html += '        <div><div>&nbsp;</div></div>' + "\n";
			html += '    </div>' + "\n";
			$('notificationbox_container').innerHTML = html;

			new Ajax.Request(url, {
				method: 'post',
				postBody: 'userid='+userid,
				onSuccess:
					function(t)
					{
						$('notificationbox_container').innerHTML = t.responseText;
						Behaviour.applySheet(Szene1BehaviourGlobal);
					},
				onFailure:
					function(t)
					{
						alert(Szene1Translator.translate('errorLoadingUserQuickpage',null));
					}
			});
		/*}*/

	},
	userQuickpage : function(element)
	{

		if($('notificationbox_container'))
		{
			$('notificationbox_container').remove();
		}

		var xy = Position.cumulativeOffset(element);
		var x  = xy[0] + 0;
		var y  = xy[1] + 20;

		var boxCon;
		boxCon  = '<div class="szene1_quickinfo" id="notificationbox_container" ';
		boxCon += '     style="position: absolute; width:450px;  ';
		boxCon += '            top: '+y+'px; left: '+x+'px;">';
		boxCon += '  <div class="edge_top"><div><div>&nbsp;</div></div></div>';
		boxCon += '  <div class="body" style="padding:8px; font-size: 11px;" ';
		boxCon += '       id="userSearch_body">';
		boxCon += '    <center>';
		boxCon += '      <img src="/images/icons/ajax-loader-white.gif" border="0"> ' + Szene1Translator.translate('loadingDots',null) + "\n";
		boxCon += '    </center>';
		boxCon += '  </div>';
		boxCon += '  <div class="edge_bottom"><div><div>&nbsp;</div></div></div>';
		boxCon += '</div>';

		new Insertion.Bottom('szene1AdditionalWindows',boxCon);
		var newWin = element.readAttribute('id').split('#');
		var url = '/ajax/user/quickpage/'+ element.readAttribute('id').split('#').last();
		if (newWin[1] != "true")
		{
			newWin[1] = "false";
		}

		new Ajax.Request(url, {
		    method: 'post',
           	postBody:'newWindow='+newWin[1],
			onSuccess: success,
			onFailure: failed
		});


		function success(t)
		{
			if($('notificationbox_container'))
			{
				$('notificationbox_container').remove();
			}

			//var xy = Position.page($(element));
			var xy = Position.cumulativeOffset(element);
			var x  = xy[0] + 0;
			var y  = xy[1] + 20;

			//quickpage  = '<div id="userQuickpage_container" style="position:absolute;top:'+y+'px;left:'+x+'px;background-color:#DDD;">';
			//quickpage += '<a href="#" onClick="javascript:$(\'userQuickpage_container\').style.display=\'none\';return false;">close</a><div id="userQuickpage">' + t.responseText + '</div></div>';

			quickpage  = '<div class="szene1_quickinfo szene1_quickinfo_userpage" id="notificationbox_container" style="position:absolute;top:'+y+'px;left:'+x+'px">';
			quickpage += t.responseText;
			quickpage += '</div>';

			new Insertion.Bottom(document.body,quickpage);
			new Draggable($('notificationbox_container'));
			Behaviour.applySheet(Szene1BehaviourGlobal);
			//new Draggable($('userQuickpage_container'),{starteffect:100, starteffect:100});
		}

		function failed(id)
		{
			alert(Szene1Translator.translate('errorLoadingUserQuickpage',null));
		}
	}


}// QucikPage






var Ignorelist = {

    ignorelistAction : function(element)
    {

        var action, userId, friendName, url, notify;

            var param    = element.id;
                param    = param.split('#');
                action   = param[1];
                userId   = param[2];
                url      = '/ajax/user/ignorelist/';

            if(param[3])
            {
                friendName = param[3];
            }
            else if($('ignorelist_user_'+userId))
            {
                friendName = $('ignorelist_user#'+userId).innerHTML;
            }
            else
            {
                friendName = '';
            }

            if(action=='delete')
            {
                if(confirm($(element).readAttribute('title')))
                {
                    new Ajax.Request(url +'delete', {
                        method: 'post',
                        postBody: 'user='+userId,
                        onSuccess: function() {
                            alert(Szene1Translator.translate('XWasDeletedFromIgnorelist',friendName));
                            if($('ignore_'+userId))
                            {
                                $('ignore_'+userId).remove();
                            }
                        },
                        onFailure: alertError
                    });
                }
            }
            else if(action=='newuser')
            {
                new Ajax.Request(url+'newuserform', {
                    method: 'post',
                    postBody:'username='+friendName,
                    onSuccess: openWindow,
                    onFailure: alertError
                });
            }


        function openWindow(t)
        {
            if($('notificationbox_container'))
            {
                $('notificationbox_container').remove();
            }

            textbox  = '<div class="szene1_quickinfo" id="notificationbox_container" style="width: 300px;display:none;">';
            textbox += t.responseText;
            textbox += '</div>';

            new Insertion.Bottom('szene1AdditionalWindows',textbox);
            Position.center('notificationbox_container', {update:true});
            Effect.Appear($('notificationbox_container'), {duration:1.0});

            Behaviour.applySheet(Szene1BehaviourGlobal);
        }

        function alertError()
        {
            alert(Szene1Translator.translate('errorOnIgnorelistWhileInteractingWithUserX',friendName));
        }
    },
    
   	ignorelistRemove : function(element)
    {
    	var user = document.getElementById('ignoreUserId');
    	
    	 new Ajax.Request('/ajax/user/ignorelist/delete', {
                    method: 'post',
                    postBody: 'user='+user.value,
                    onSuccess: function(t){
                        if(t.responseText>0)
                        {
                        	alert(Szene1Translator.translate('deleted',null));
                        }
                        else
                        {
                            alert(Szene1Translator.translate('error',null));
                        }
                    },
                    onFailure: function() {
                    	alert(Szene1Translator.translate('error',null));
                    }
                });
		new Effect.Fade($('notificationbox_container'), {duration:1.0});
    },

    ignorelistAdd : function(element)
    {

		element = $(element);
		var notify = '';
        var regex              = /^([a-zA-Z0-9_-]{3,})$/;
        var userAllreadyExists = false;

            if($('ignoreList'))
            {
                var users = document.getElementsByClassName('ignored_users', $('ignoreList'));
                users.each(function(user)
                {
                    if(user.innerHTML.toLowerCase()==$F('ignore_user').toLowerCase())
                    {
                        userAllreadyExists = true;
                    }
                });
            }

            if(userAllreadyExists)
            {
                alert(Szene1Translator.translate('youAlreadyHaveThisUserOnTheIgnorelist',null));
                //$F('ignore_user').clear();
            }
            else if(regex.test($F('ignore_user')))
            {
                notify  = ($F('notify_yes')=='y') ? $F('notify_yes') : '';
                notify += ($F('notify_no')=='n') ? $F('notify_no') : '';

                if(notify=='y' || notify=='n')
                {
                    new Ajax.Request('/ajax/user/ignorelist/getuser', {
                        method: 'post',
                        postBody: 'user='+$F('ignore_user'),
                        onSuccess: function(t) { doAddIgnoreUser(t); },
                        onFailure: alertError
                    });
                }
                else
                {
                    alert(Szene1Translator.translate('pleaseSpecifyWhetherTheUserShouldBeNotified',null));
                }
            }
            else
            {
                alertWrongUserName();
            }


        function doAddIgnoreUser(t)
        {
            if(t.responseText>0)
            {
                userId = t.responseText;

                new Ajax.Request('/ajax/user/ignorelist/adduser', {
                    method: 'post',
                    postBody: 'user='+t.responseText+'&notify='+notify,
                    onSuccess: function(trans){
                        if(trans.responseText>0)
                        {
                            if($('ignoreList'))
                            {
                                window.reload();
                            }
                            alert(Szene1Translator.translate('userAddedToIgnoreListSuccessfully',null));
                        }
                        else if(trans.responseText=='-1')
                        {
                            alert(Szene1Translator.translate('userIsOnTheGlobalWhitelistAndCantBeIgnored',null));
                            $('ignore_user').clear();
                        }
                        else
                        {
                            alertError();
                        }
                    },
                    onFailure: alertError
                });

            }
            else
            {
                alertWrongUserName();
            }
            new Effect.Fade($('notificationbox_container'), {duration:1.0});
        };

        function alertWrongUserName()
        {
            //alert('Komisch');
            new Effect.Shake($('notificationbox_container'));
            $('ignore_user').style.color = '#ff0000';
            $('ignore_user').activate();
        };

        function alertError()
        {
            alert(Szene1Translator.translate('errorSavingUserOnIgnorelist',null));
            Windows.closeAll();
        };

    }



}//Ignorelist


