var links = 0;
var oben = 0;
var dontclose = false;

function bocmsrly(s) {
	var answer = confirm (s);
	if (answer) 
		return true;
	else
		return false;
}

function bocmsGetElementById(s) {
	var ele = document.getElementById ? document.getElementById(s) : document.all[s];
	return ele;
}

function bocmsShowSitemap() {
	$('#bocmsSitemapLink').slideToggle();
	$('#bocmsSitemap').slideToggle();
}

jQuery(document).ready(function(){
   $().mousemove(function(e){
   	links = e.pageX;
   	oben = e.pageY;
   }); 
})


/* Neuigkeiten bewerten Anfang */
function bocmsStartRating(a,url,sprache) {
	$('#untermenu').css({left:links,top:oben});
	$('#untermenu').fadeIn();
	dontclose = true;

	$.post( url+'/ajax/news_rating.php', { id: a, sprachex: sprache },
  		function(data){
    		$('#untermenu').html(data);
    	}
   );
}

function clearMessageList(to_user_id) {
	$('#messagesWith'+to_user_id).children().remove();	
}

function chatAddMessageToMessageList(to_user_id, username, s, receive_time) {
	msgs = $('#messagesWith'+to_user_id);
	newMsg = $('<li></i>');
	s = '<b>'+username+'</b>: '+s;
	
	if (receive_time == -1) {
		newMsg.append(s+' <span style="color: #bbb">(...wird gesendet)</span>');
	} else if (receive_time) {
		newMsg.append(s);
	} else {
		newMsg.append(s+' <span style="color: #bbb">(...wird empfangen)</span>');
	}
	msgs.append(newMsg);	
} 

function chatSendMessage(username, to_user_id) {
	s = $('#chat_'+to_user_id).val();
	
	if (s != "") {
		$.post('index.php', { action: 'chatSendMessage', to_user_id: to_user_id, message: s },
	  		function(data){
				if (data) {
					chatAddMessageToMessageList(to_user_id, username, s, -1);
					$('#chat_'+to_user_id).val("")
					chatScrollDown(to_user_id);
				} else {
					alert("Nachricht konnte nicht verschickt werden.");
				}				
	    	}
	    );
	}
}

function chatHandleEnterKey(e, to_user_id, username) {
	var key=e.keyCode || e.which;
	if (key==13){
		chatSendMessage(username, to_user_id);
	}
}

function saveChatWindowPosition(to_user_id, x, y) {
	$.post('index.php', { action: 'saveChatWindowPosition', to_user_id: to_user_id, x: x, y: y },
  		function(data){
    		//
    	}
   );
}

function closeChatWindow(d, to_user_id) {
	$('#' + d).remove();
	$('#messagesWith'+to_user_id).remove();
	$.post('index.php', { action: 'closeChatWindow', to_user_id: to_user_id },
  		function(data){
    		//
    	}
   );
}

function chatResizeText(to_user_id, width) {
	$('#chat_' + to_user_id).width(width-52);
}

function chatResizeMessagelist(to_user_id, height) {
	$('#messageDiv' + to_user_id).height(height-40);
}

function saveChatWindowSize(to_user_id, width, height) {
	$.post('index.php', { action: 'saveChatWindowSize', to_user_id: to_user_id, width: width, height: height },
  		function(data){
    		//
    	}
   );
}

function chatScrollDown(to_user_id) {
	$('#messageDiv'+to_user_id).attr({ scrollTop: $('#messageDiv'+to_user_id).attr("scrollHeight") });
}

function chatMessageReloader(first, to_user_id) {
	if (!first || true) {
		if ($('#messagesWith'+to_user_id).length == 0) {
			return;
		}
		$.post('index.php', { action: "getChatMessages", to_user_id: to_user_id },
			function(data){
				clearMessageList(to_user_id);
				$.each(data, function(i, item) {
					chatAddMessageToMessageList(to_user_id, item.from_user_name, item.message, item.receive_time)
				});
			}, "json"
		);	
		chatScrollDown(to_user_id);
	}
	window.setTimeout("chatMessageReloader(false, "+to_user_id+")", 3000);
}

function bocmsSendRating(id,punkte,punktebisher,votings,url,sprache) {
	$.post( url+'/ajax/news_rating.php', { id: id, punkte: punkte, sprachex: sprache },
  		function(data){
    		$('#untermenu').html(data);
    	}
   );
   dontclose=false;

   punktebisher += punkte;

   votings++;
	a = Math.round(punktebisher / votings);
	neuercode = '';
	for (var i = 0; i < a; i++) {
   	neuercode += '<img src="'+url+'/gfx/sterngelb.gif" absalign="middle" border="0">';
	}
	for (var j = a; j < 5; j++) {
   	neuercode += '<img src="'+url+'/gfx/sterngrau.gif" absalign="middle" border="0">';
	}
	$('#ratings'+id).html(neuercode);
}

function bocmsStopRating() {
	dontclose=false;
	$('#untermenu').fadeOut();
}
/* Neuigkeiten bewerten Ende */

/* Untermenü innerhalb der Seite */
function bocmsUntermenue(menu,template,nr,url) {
	if( $('#cmsuntermenue_'+nr).is(':visible') ) {
		$('#cmsuntermenue_'+nr+'_span').css({display:"inline"});
	} else {
		$('#cmsuntermenue_'+nr+'_span').css({display:"none"});
	} 
	$('#cmsuntermenue_'+nr).slideToggle();
	
	$.post( url+'/req.php', { name: menu, template_last: template, self: 'true' },
  		function(data){}
   );

	linktitel = $('#untermenuelink_'+nr).html();
	
	if (linktitel.substring(0,1) == '+') {
		$('#untermenuelink_'+nr).html( '-' + linktitel.substring(1) );
	}
	else if (linktitel.substring(0,1) == '-') {
		$('#untermenuelink_'+nr).html( '+' + linktitel.substring(1) );
	}
}

function bocmsPopup(div,menu,template,url,sprache) {
	if( $('#'+div).is(':visible') ) {
		$('#'+div).fadeOut();
	} else {
		$('[rel=bocmsPopups]').fadeOut();
		i = $('div[rel=bocmsPopups]:visible').length + 1;
		$('#'+div).css({left:links,top:oben,zIndex: i});
		$('#'+div).fadeIn();
		$.post( url+'/req.php', { name: menu, template_last: template },
	  		function(data){
	  			$('#'+div).html(data);
	  		}
	   );
	}
}


function bocmsPopupOnOverXY(div,menu,template,x,y,centerfix,url,sprache) {
	var fensterBreite = 0;
	
	if (navigator.appName.indexOf('Microsoft') != -1) {
   	fensterBreite = document.body.clientWidth;
   }
   else
   {
   	fensterBreite = self.innerWidth;
   }

	fensterBreite /= 2;

	if (centerfix == 1) x = fensterBreite + Number(x);
	if (centerfix == 2) x = (fensterBreite * 2) + Number(x);

	if (x <= 0) x = links;
	if (y <= 0) y = oben;
	
	links = x;
	oben = y;
	bocmsPopup(div,menu,template,url,sprache);
}

function sendClose(a) {
	if( !dontclose ) {
		$('#'+a).fadeOut();
	}
}
