// *-----------------------------------------------------------------------*
// | This file is part of the actSite, interacive Pages project.           |
// | Copyright (c) 2004-2009 Nemint (see authors). All rights reserved     |
// |                                                                       |
// | http://www.actSite.de, http://www.acsite.de/LICENCE.txt               |
// |                                                                       |
// | Änderungen sind erlaubt, solange wie dieser Copyright Hinweis         |
// | unverändert erhalten bleibt und die Lizenzbestimmungen beachtet       |
// | werden.                                                               |
// |                                                                       |
// | You may freely modify the source code under the terms of the License. |
// | Don`t edit, delete or modify this copyright header                    |
// |                                                                       |
// | $Id: actsiteextra.js 43 2009-07-11 13:14:43Z nemint $                                                                 |
// *-----------------------------------------------------------------------*

/**
 *
 *
 * @copyright      (c) the authors
 * @author         Mesut Cftci <mc AT nemint DOT de>
 * @license        http://www.acsite.de/LICENCE.txt
 * @package        base_functions
 * @version        $Rev: 43 $, $Date:: 2009-07-11 15:14:43 #$
 * @todo
 */

var clockElement = null;

var noalertmsg = 0;
var noalertfriends = 0;
var noalertgb = 0;
var alertPop = 0;
var countmsg_entries = 0;
var countmsg2_entries = 0;
var countgb_entries = 0;
var countfriend_entries = 0;
var session_id = 0;

var popbynewmsg = 0;
var popbynewgb = 0;
var requestisgb = 0;
var requestismsg = 0;
var requestisfriend = 0;

var msgRefresh = 0;

var auto_open_msg = 0;
var msglinkid = 0;
var msglink = my_URI_Path + "/community.php?do=messenger&what=messages&upcomm=1";
var actSiteCommCGI = my_URI_Path + "/community.php?";
var PopupH = 500;
var PopupW = 566;
var usePopupCC = 0;
var actSiteLanguage = 'de';
var my_actSiteTheme= 'standard';
var my_actSiteThemeGfxURL= my_URI_Path + '/themes/' + my_actSiteTheme + '/_media';

function actSiteCodeEdit(obj,smiliesdiv,selectsdiv,iconsdiv) {
	var actSiteCode = new Object();
	actSiteCode.htmlobject		= obj;
	actSiteCode.smiliesdiv		= smiliesdiv;
	actSiteCode.selectsdiv		= selectsdiv;
	actSiteCode.iconsdiv		= iconsdiv;
	actSiteCode.top		= obj.scrollTop;
	actSiteCode.left		= obj.scrollLeft;

	obj.onmousedown = function() {actSiteCode.storeStr(obj);};
	obj.onmouseup = function() {actSiteCode.storeStr(obj);};
	obj.onselect = function() {actSiteCode.storeStr(obj);};
	obj.onclick = function() {actSiteCode.storeStr(obj);};
	obj.onkeyup = function() {actSiteCode.storeStr(obj);};
	obj.onchange= function() {actSiteCode.storeStr(obj);};
	
	actSiteCode.insSmilie = function () {actSiteCode.insert_str(this.title);}

	actSiteCode.writecodes =	function (smiliesdiv,selectsdiv,iconsdiv) {
																	if(typeof(smiliesdiv) == 'string') {
																		actSiteCode.smiliesdiv = smiliesdiv;
																	}
																	if(typeof(selectsdiv) == 'string') {
																		actSiteCode.selectsdiv = selectsdiv;
																	}
																	if(typeof(iconsdiv) == 'string') {
																		actSiteCode.iconsdiv = iconsdiv;
																	}
																	if(typeof(actSiteCode.smiliesdiv) == 'string' && document.getElementById(actSiteCode.smiliesdiv)) {
																		actSiteCodeEdit.Instanzes[actSiteCode.smiliesdiv] = actSiteCode;
																		document.getElementById(actSiteCode.smiliesdiv).innerHTML = '';
																		var i = 0;
																		for(index in SmiliesA) {
																			i++;
																			var newpic = document.createElement("img");
																			newpic.src = my_actSiteMediaURL + '/' + SmiliesA[index];
																			newpic.alt = index;
																			newpic.title = index;
																			newpic.border = '0';
																			newpic.onmouseover = function() {this.style.cursor='pointer';}
																			newpic.onclick = function () {
																	 											actSiteCode.insert_str(this.title);
																											}
																			document.getElementById(actSiteCode.smiliesdiv).appendChild(newpic);
																			var space = document.createTextNode(" ");
																			document.getElementById(actSiteCode.smiliesdiv).appendChild(space);
																			if(i >= 13) {
																				break;
																			}
																		}
																		var link = document.createElement("a");
																		link.innerHTML = 'more smilies';
																		link.id = actSiteCode.smiliesdiv +'_more';
																		link.onmouseover = function() {this.style.cursor='pointer';}
																		link.onclick = function() {
																		                            	document.getElementById(actSiteCode.smiliesdiv + '_list_tmp').style.display = '';
																																	InlinePopup.Open({'id':'smiliesdiv_list','replace_id':actSiteCode.smiliesdiv + '_list_tmp','width':400,'title':'more smilies','height':320});
																															};
																															
																		document.getElementById(actSiteCode.smiliesdiv).appendChild(link);
																		
																			var newdiv = document.createElement("div");
																			newdiv.id = actSiteCode.smiliesdiv + '_list_tmp';
																			document.body.appendChild(newdiv);
																			newdiv.style.display = 'none';
																			for(index in SmiliesA) {
																				i++;
																				newdiv.innerHTML = newdiv.innerHTML + "<img src=\"" + my_actSiteMediaURL + "/" + SmiliesA[index] + "\" title=\"" + index + "\" border=\"0\" alt=\"" + index + "\" onclick=\"actSiteCodeEdit.Instanzes['" + actSiteCode.smiliesdiv + "'].insert_str(this.title);\" /> ";
																			}
																	}
																};

	actSiteCode.restore_cursor =	function (end) {
																	if(!document.all && navigator.appName.indexOf('Internet Explorer')==-1) {
																		if(typeof end != 'undefined') {
																			obj.scrollTop = 100000;
																			obj.scrollLeft = 100000;
																		}
																		else {
																			obj.scrollTop = actSiteCode.top;
																			obj.scrollLeft = actSiteCode.left;
																		}
																	}
																	obj.focus();
																};

	actSiteCode.storeStr =	function () {
														actSiteCode.top = obj.scrollTop;
														actSiteCode.left = obj.scrollLeft;
														if (typeof obj.createTextRange != 'undefined') {
															obj.caretPos = document.selection.createRange().duplicate();
															obj.focus();
														}
													};

	actSiteCode.insert_str =	function (txt) {
															obj.focus();
															if(typeof obj.createTextRange != 'undefined' && typeof obj.caretPos != 'undefined') {
																var caretPos = obj.caretPos;
																str = ' ' + txt;
																caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? str : str;
															}
															else if(typeof obj.selectionStart != 'undefined') {
																posttextarea = obj;
																selected=(posttextarea.value).substring(posttextarea.selectionStart,posttextarea.selectionEnd);
																str = ' ' + txt;
																posttextarea.focus();
																start=posttextarea.selectionStart;
																end=posttextarea.textLength;
																endtext=posttextarea.value.substring(posttextarea.selectionEnd,end);
																starttext=posttextarea.value.substring(0,start);
																posttextarea.value=starttext + str + endtext;
																posttextarea.selectionStart=start;
																posttextarea.selectionEnd=start;
															}
															else {
																obj.value = obj.value + ' ' + txt;
															}
															actSiteCode.restore_cursor();
															obj.focus();
														};
	return actSiteCode;
};

actSiteCodeEdit.Instanzes = new Object();

	function updateMSG()
	{
		var zeit = new Date();
		var link = my_URI_Path + '/modules/smsg/index.php?' + zeit.getTime() + '&msgs=' + countmsg_entries + '&friends=' + countfriend_entries + '&gbs=' + countgb_entries + '&SID=' + session_id;
		NemintAjax.sendServerJSON({'url':link,'nextjob':mresponse});
	}

	function mresponse(data, obj)
	{
	}

	function chatinvite(txt, nick) {
		Check = confirm(txt);
		if(Check == true) {
			Pop = window.open("", "Pop", "height=520,width=790,location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=no");
			Pop.location.href = my_URI_Path + "/chat.php?";
		}
		else {
			Pop = window.open("", "Pop", "height=200,width=300,location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=no");
			Pop.location.href = my_URI_Path + "/chat.php?do=nochat&=" + nick;
		}
	}

	function shownewmsg() 
	{
		if(countmsg_entries == 0) 
		{
			document.getElementById('shownewmsg').className = 'shownonewfriends';
			return;
		}
		document.getElementById('shownewmsg').className = '';
		document.getElementById('shownewmsg').title = countmsg_entries + DictionaryA['new_messages'];
		if(usePopupCC > 0) 
		{
			document.getElementById('shownewmsg').onclick = function() {Popup(actSiteCommCGI + '&do=messenger',PopupH,PopupW,'msgPop',1);};
		}
		else 
		{
			document.getElementById('shownewmsg').onclick = function() {location.href=actSiteCommCGI + '&do=messenger';};
		}
	}
	function shownonewmsg() 
	{
		document.getElementById('shownewmsg').className = 'shownonewmsg';
	}

	function shownewgb() 
	{
		if(countgb_entries == 0) 
		{
			document.getElementById('shownewgb').className = 'shownonewfriends';
			return;
		}
		document.getElementById('shownewgb').className = '';
		document.getElementById('shownewgb').title = countgb_entries + DictionaryA['new_gb'];
		if(usePopupCC > 0) 
		{
			document.getElementById('shownewgb').onclick = function() {Popup(actSiteCommCGI + '&do=gb&what=gbshow',PopupH,PopupW,'gbPop',1);};
		}
		else 
		{
			document.getElementById('shownewgb').onclick = function() {location.href=actSiteCommCGI + '&do=gb&what=gbshow';};
		}
	}

	function shownewfriends() {
		if(countfriend_entries == 0) {
			document.getElementById('shownewfriends').className = 'shownonewfriends';
			return;
		}
		document.getElementById('shownewfriends').className = '';
		document.getElementById('shownewfriends').title = countfriend_entries + DictionaryA['new_friend'];
		if(usePopupCC > 0) {
			document.getElementById('shownewfriends').onclick = function() {Popup(actSiteCommCGI + '&do=friends&sub=confirms',PopupH,PopupW,'friendPop',1);};
		}
		else {
			document.getElementById('shownewfriends').onclick = function() {location.href=actSiteCommCGI + '&do=friends&sub=confirms';};
		}
	}

	function newGBEntries(count) {
		count = parseInt(count);
		if(count > countgb_entries) {
			noalertgb = 0;
		}
		countgb_entries = count;
		var Checkgb = false;
		if(document.getElementById('ngbcount') && count > 0) {
			document.getElementById('ngbcount').innerHTML = count;
		}
		if(document.getElementById('shownewgb')) {
			shownewgb();
		}
		if(count < 1) {
			return;
		}
		if(noalertgb > 0 || requestisgb > 0) {
			return;
		}
		if(popbynewgb < 1) {
			return;
		}
		if(count == 1 && noalertgb == 0) {
			noalertgb = 1;
			Checkgb = confirm(count + DictionaryA['new_ingb1']);
		}
		else if(count > 1 && noalertgb == 0)
		{
			noalertgb = 1;
			Checkgb = confirm(count + DictionaryA['new_ingb']);
		}
		if(Checkgb == true)
		{
			noalertgb = 0;
			if(usePopupCC > 0)
			{
				Popup(actSiteCommCGI + '&do=show&what=gbshow',PopupH,PopupW,'gbPop',1);
			}
			else
			{
				location.href = actSiteCommCGI + "do=gb";
			}
		}
		if(Checkgb == false || noalertgb == 0)
		{
			noalertgb = 1;
			NemintAjax.sendServerJSON({'url':my_URI_Path + '/modules/smsg/index.php?nonewgb=1&SID=' + session_id});
		}
	}

	function newFriendEntries(count)
	{
		count = parseInt(count);
		if(count > countfriend_entries)
		{
			noalertfriends = 0;
		}
		else
		{
			noalertfriends = 1;
		}
		countfriend_entries = count;
		var Checkfriends = false;
		if(document.getElementById('shownewfriends'))
		{
			shownewfriends();
		}
		if(noalertfriends > 0 )
		{
			return;
		}
		if(count < 1 || requestisfriend > 0)
		{
			return;
		}
		if(count == 1 && noalertfriends == 0)
		{
		  noalertfriends = 1;
			Checkfriends = confirm(count + DictionaryA['new_infriend1']);
		}
		else if(noalertfriends == 0)
		{
		  noalertfriends = 1;
			Checkfriends = confirm(count + DictionaryA['new_infriend']);
		}
		if(Checkfriends == true)
		{
		  noalertfriends = 0;
			if(usePopupCC != 0)
			{
				Popup(actSiteCommCGI + '&do=friends&sub=confirms',PopupH,PopupW,'friendPop',1);
			}
			else
			{
				location.href = actSiteCommCGI + "&do=friends&sub=confirms";
			}
		}
		if(Checkfriends == false || noalertfriends == 0)
		{
			noalertfriends = 1;
			NemintAjax.sendServerJSON({'url':my_URI_Path + '/modules/smsg/index.php?nonewfriends=1&SID=' + session_id});
		}
	}

	function newMSGEntries(count,count2,count3,firstcall)
	{
		var Check = false;
		count = parseInt(count);
		count2 = parseInt(count2);
		count3 = parseInt(count3);
		if(count3 > 0)
		{
			alertPop = 1;
		}
		try
		{
			if((typeof(msgPop) != 'undefined' && typeof(msgPop.name) != 'undefined'))
			{
				alertPop = 0;
			}
		}
		catch(e)
		{
			if(alertPop == 0)
			{
				//Check = true;
			}
		}
		countmsg_entries = count;
		countmsg2_entries = count2;
		if(document.getElementById('nmsgcount'))
		{
			document.getElementById('nmsgcount').innerHTML = count;
		}
		if(document.getElementById('nmsgcount2'))
		{
			document.getElementById('nmsgcount2').innerHTML = count2;
		}
		if(document.getElementById('shownewmsg'))
		{
			shownewmsg();
		}
		if(count3 < 1 || requestismsg > 0)
		{
			return;
		}
		if(popbynewmsg < 1 && count3 > 0 )
		{
			NemintAjax.sendServerJSON({'url':my_URI_Path + '/modules/smsg/index.php?nonewmsg=1&SID=' + session_id});
			playsound('msg.mp3');
			return;
		}
		if(noalertmsg > 0 || alertPop < 1)
		{
			return;
		}
		if(parseInt(auto_open_msg) > 0)
		{
			Check = true;
		}
		if(Check == false)
		{
			if(count3 == 1 )
			{
				Check = confirm(count3 + DictionaryA['new_inmsg1']);
			}
			else
			{
				Check = confirm(count3 + DictionaryA['new_inmsg']);
			}
		}
		if(Check == true)
		{
			var link = '';
			if(auto_open_msg > 0)
			{
				link = msglinkid + msgid;
			}
			else
			{
				link = msglink;
			}
			if(usePopupCC > 0)
			{
				msgPop = window.open("", "msgPop","height=" + PopupH + ",width=" + PopupW + ",location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=no");
				msgPop.location.href = link;
			}
			else
			{
				location.href = link;
			}
		}
		else
		{
			noalertmsg = 1;
		}
		if(Check == false)
		{
			NemintAjax.sendServerJSON({'url':my_URI_Path + '/modules/smsg/index.php?nonewmsg=1&SID=' + session_id});
		}
	}

function startClock(element) 
{
	if(typeof element == 'string' && document.getElementById(element)) 
	{
		clockElement = element;
		showClock();
	}
}

function showClock() 
{
	if(!clockElement || !document.getElementById(clockElement))
	{
		return;
	}
	var Jetzt = new Date();
	var Tag = Jetzt.getDate();
	var Monat = Jetzt.getMonth() + 1;
	var Jahr = Jetzt.getYear();
	if(Jahr < 999) Jahr += 1900;
	var Stunden = Jetzt.getHours();
	var Minuten = Jetzt.getMinutes();
	var Sekunden = Jetzt.getSeconds();
	var WoTag = Jetzt.getDay();
	var Vortag  = ((Tag < 10) ? "0" : "");
	var Vormon  = ((Monat < 10) ? ".0" : ".");
	var Vorstd  = ((Stunden < 10) ? "0" : "");
	var Vormin  = ((Minuten < 10) ? ":0" : ":");
	var Vorsek  = ((Sekunden < 10) ? ":0" : ":");
	var Datum = Vortag + Tag + Vormon + Monat  + "." + Jahr;
	var Uhrzeit = Vorstd + Stunden + Vormin + Minuten + Vorsek + Sekunden;
	var Gesamt = DayName[WoTag] + ", " + Datum + ", " + Uhrzeit;

	document.getElementById(clockElement).innerHTML = Gesamt;
	window.setTimeout("showClock()",1000);
}