var imgdims=new Array();
var cache=new Array();

var ol_fullhtml=1;
var ol_hauto=1;
var ol_vauto=1;
var pageTitle="";

if(typeof PHPSESSID == 'undefined') {
	PHPSESSID = "";
}

   function keyPressHandler(e) {
      var kC  = (window.event) ?    // MSIE or Firefox?
                 event.keyCode : e.keyCode;
      var Esc = (window.event) ?   
                27 : e.DOM_VK_ESCAPE // MSIE : Firefox
      if(kC==Esc)
       {
		  hideArticleDetail(); 
	   }
   }


document.onkeydown= keyPressHandler;
function hideArticleDetail()
{
	
	document.getElementById("mainOpacity").setAttribute('opacity',1);
	document.getElementById("mainOpacity").setAttribute('filter','alpha(opacity=100)');	
	document.getElementById("mainOpacity").setAttribute('class','mainContainerFull');	
	
	hideElement('showArticleDetail');
}
// Behelfsfunktion: ie mag keine Fensternamen, die - enthalten (!!!)
function filterString(input) {
	var output='';
	for (i=0; i<input.length; i++) {
		if ((input.charAt(i).toLowerCase()>='a') && (input.charAt(i).toLowerCase()<='z')) {
			output += input.charAt(i);
		} else {
			output += '_';
		}
	}

	return output;
}

// switcht alle Checkboxen im Formular die mit "name" beginnen auf den Status des angegebenen Elements
// <input type="checkbox" onClick="checkAll(this,'check')" />
function checkAll(elm,name){
  for (var i = 0; i < elm.form.elements.length; i++)
  	if (elm.form.elements[i].name.indexOf(name) == 0)
	    elm.form.elements[i].checked = elm.checked;
}

// checkThese(this,'cal', '1,2,3')
function checkThese(elm,prefix,which){
	var ids=which.split(",");

 	for (var i = 0; i < ids.length; i++)
  		elm.form.elements[prefix+ids[i]].checked = elm.checked;
}

function openChatWindow() {
	window.open("/ChatHandler",'chat',
					'width=820,height=570,left=50,top=50,dependent=no,menubar=no,status=no,scrollbars=auto,resizable=yes').focus();
}

// obsolete! wird nur noch im chat gebraucht
function smileyPopup() {
	window.open('/SmileyHelp'+PHPSESSID,'smileyhelp','height=600,width=330,left=110,top=90,dependent,menubar=no,status=no,scrollbars=yes').focus();
}

function smileyPopup(inputname) {
	window.open('/SmileyHelp?input='+inputname+PHPSESSID,'smileyhelp','height=600,width=330,left=110,top=90,dependent,menubar=no,status=no,scrollbars=yes').focus();
}

function reloadStatusWindow() {
	loadFragmentInToElement("/Login?"+PHPSESSID, "statusWindow", false);
}

function replyMessagePopup(to, subject, inreplyto) {
		while(subject.substring(0, 3)=="Re:")
				{
					subject=subject.substring(3, subject.length);
				}
				subject= "Re:"+subject;
	window.open('/MessageSend?to='+to+'&inreplyto='+inreplyto+'&subject='+subject+PHPSESSID, '', 'width=500,height=470,left=100,top=100,dependent=no,menubar=no,status=no,scrollbars=auto,resizable=yes').focus();
}

function showHelpPopup (scriptid) {
	window.open('/HelpShow?scriptid='+scriptid+''+PHPSESSID, 'help'+scriptid, 'width=500,height=470,dependent=no,menubar=no,status=no,scrollbars=auto,resizable=yes').focus();
}


function newMessagePopup(to, subject, message) {

	var url="/MessageSend?";
	if (to) {
		url=url+"&to="+to;
	}
	if (subject) {
		url=url+"&subject="+subject;
	}
	if (message) {
		url=url+"&message="+message;
	}

	window.open(url+PHPSESSID,'',
				'width=500,height=470,left=100,top=100,dependent=no,menubar=no,status=no,scrollbars=auto,resizable=yes').focus();
}

function revokeMessage(id,isKon) {
	if (confirm('Soll diese Nachricht wirklich zurückgezogen werden?')) {
		window.open("/MessageRevoke?revokeMsg="+id,'revokeMsg'+id+PHPSESSID,
			'width=470,height=360,left=100,top=100,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
		if(isKon)return true;
	}
	
}
function revokeEditMessage(id) {
	if (confirm('Diese Nachricht zurückziehen und editieren?')) {
		window.open("/MessageSend?action=revokeEdit&mid="+id,'revokeEditMsg'+id+PHPSESSID,
			'width=500,height=470,left=100,top=100,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
	}
}

function showMessagesPopup() {
	window.open("/MessageInbox?folder=inbox"+PHPSESSID,'Messages',
				'width=700,height=600,left=10,top=10,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}


function viewUnreadMessagePopup() {
	window.open("/MessageView?folder=inbox&action=displayUnread"+PHPSESSID,'',
				"width=570,height=460,left=100,top=100,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes").focus();
}

function viewMessagePopup(id, folder) {
	if(!folder){
		folder="inbox";
	}
	window.open("/MessageView?folder="+folder+"&readMsg="+id+PHPSESSID,'viewMsg'+id,
				"width=570,height=460,left=100,top=100,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes").focus();
}

function onlineListPopup() {
	window.open("/UserOnline"+PHPSESSID,'userlist',
				'width=500,height=430,left=150,top=150,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}
function addToIgnoreListPopup(username) {
	window.open("/IgnoreListNoFrame?action=addIgnore&user="+username+""+PHPSESSID,'editIgnorelist',
				'width=550,height=530,left=150,top=150,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}

function edit_Gewinnspiel(id) {
	window.open("/editGewinnspiel?eventid="+id+PHPSESSID,'editEvent'+id,'width=750,height=650,left=20,top=10,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}


function editHelpTextPopup (scriptid) {
	window.open('/HelpEdit?scriptid='+scriptid+''+PHPSESSID, 'edithelp'+scriptid, 'width=500,height=470,left=30,top=20').focus();

}

function showProfilePopup(name,tab,profileimageid) {
	if(!tab){
		tab=1;
	}
	var url="/ProfileShow?username="+name+"&tab="+tab;
	if(profileimageid){
		url=url+"&imageDetail="+profileimageid;
	}
	window.open(url+PHPSESSID,'profile_'+filterString(name),'width=1000,height=680,left=20,top=20,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}
function showProfilePopupVideo(name,tab,videoid) {
	if(!tab){
		tab=1;
	}
	var url="/ProfileShow?username="+name+"&tab="+tab;
	if(videoid){
		url=url+"&videoid="+videoid;
	}
	window.open(url+PHPSESSID,'profile_'+filterString(name),'width=1000,height=680,left=20,top=20,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}

function showProfilePopupChat(chatuser,tab) {
	if(!tab){
		tab=1;
	}
	var url="http://www.schneckenhof.de/ProfileShow?chatuser="+chatuser+"&tab="+tab;

	window.open(url+PHPSESSID,'profile_'+filterString(name),'width=1000,height=680,left=20,top=20,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}

function showProfileVoting(imageid, username) {
	window.open('/ProfileVotingDetails?imageid='+imageid+'&username='+username+PHPSESSID,'vote'+imageid,'height=600,width=600,left=200,top=200,resizable=yes,menubar=no,status=no,scrollbars=yes').focus();
}

function showAdminHistory(scriptname, id) {
	window.open('/AdminShowHistory?scriptname='+scriptname+'&referring_id='+id+PHPSESSID,'',
				'width=450,height=250,left=200,top=200,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}

function showProfileImagePopup(username, image) {
	window.open('/ProfileImageShow?username='+username+'&image='+image+PHPSESSID,image,'height=560,width=500,left=50,top=50,dependent,menubar=no,status=no,scrollbars=yes').focus();
}

function editProfilePopup(checkLevel, tab, username, intro) {
	url="/ProfileEdit?check_level="+checkLevel+PHPSESSID;
	if (tab) {
		url=url+"&tab="+tab;
	}
	if (username) {
		url=url+"&username="+username;
	}
	if (intro) {
		url=url+"&intro="+intro;
	}
	window.open(url,
				"editProfil","width=970,height=730,left=10,top=10,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes").focus();
}
function addProfileGaleryImagePopup(username, imageid) {
	url="/ProfileEdit?tab=4&data=4&username"+username+"&action=addGaleryImage&imageid="+imageid;

	window.open(url+PHPSESSID,
				"editProfil","width=970,height=730,left=10,top=10,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes").focus();
}
function newECardPopup(imageid) {
	window.open("/EcardSenden?imageid="+imageid+PHPSESSID,
				"", "height=450,width=470,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes").focus();
}

function locationPopup(id, tab) {
	window.open("/GastroGuideDetail?location_id="+id+"&tab="+tab+PHPSESSID,
				"", "height=600,width=900,top=20,left=30,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes").focus();
}

function editLocationPopup(locationid, tab) {
  window.open('/GastroGuideLocationEdit?location_id='+locationid+'&tab='+tab+PHPSESSID,'', 'height=700,width=900,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}

function editSpecialPopup(locationid, specialid) {
  window.open('/GastroGuideLocationEdit?select_special_id='+specialid+'&location_id='+locationid+'&action=edit&tab=4'+PHPSESSID,'', 'height=700,width=900,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}

function addEventToGoogle(id) {
	window.open('/Events?&f=gcal&id='+id+PHPSESSID,'DetailView'+id,'height=550,width=800,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}

function newEvent() {
	window.open('/EditEvent?action=new'+PHPSESSID,'','height=700,width=730,left=100,top=20,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}

function editFreeGewinnspiel() {
	window.open('/editFreeGewinnspiel?action=new'+PHPSESSID,'','height=700,width=730,left=100,top=20,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}

function editEFreetPopup(id) {
	window.open("/editFreeGewinnspiel?action=edit&id="+id+PHPSESSID,
				"edit"+id,"width=700,height=730,left=10,top=10,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes").focus();
}

function editEventCategories() {
	window.open('/EditEvent?action=editCat'+PHPSESSID,'CategoryEdit','height=585,width=610,left=100,top=100,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes').focus();
}

function editEventPopup(id) {
	window.open("/EditEvent?action=edit&id="+id+PHPSESSID,
				"edit"+id,"width=700,height=730,left=10,top=10,dependent=no,menubar=no,status=no,scrollbars=yes,resizable=yes").focus();
}

function chatInfoPopup(chatid, username) {
	window.open('http://chat.schneckenhof.de/cgi-bin/gtchat/chat.pl?id='+chatid+'&action=modifyprofile&username='+username,'','resizable=yes,scrollbars=yes,width=430,height=480').focus();
}

function chatEditPopup(chatid, username) {
	window.open('http://chat.schneckenhof.de/cgi-bin/gtchat/chat.pl?id='+chatid+'&action=viewprofile&username='+username,'','resizable=yes,scrollbars=yes,width=430,height=480').focus();
}

function showDisclaimer() {
	window.open("/Disclaimer",'','resizable=yes,scrollbars=yes,width=450,height=480').focus();
}

function simpleOver(caption, main, small) {
	if (small) {
		return overlib('<div class="popupContainerSmall"><div class="popupHeader">'+caption+'</div><div class="popupBody">'+main+'</div></div>', WIDTH,150);
	} else {
		return overlib('<div class="popupContainer"><div class="popupHeader">'+caption+'</div><div class="popupBody">'+main+'</div></div>', WIDTH,300);
	}
}

function eventOver(id) {
	overlib('<div class="popupContainer"><div class="popupHeader">Infos werden geladen...</div></div>', WIDTH,300);
	loadFragmentDelayed("/EventRollover?eventid="+id+PHPSESSID, "overDiv");
	return false;
}

function specialOver(id) {
	overlib('<div class="popupContainer"><div class="popupHeader">Infos werden geladen...</div></div>', WIDTH,300);
	loadFragmentDelayed("/SpecialRollover?specialId="+id+PHPSESSID, "overDiv");
	return false;
}

function gastroSpecialOver(id) {
	overlib('<div class="popupContainer"><div class="popupHeader">Infos werden geladen...</div></div>', WIDTH,300);
	loadFragmentDelayed("/GastroSpecialRollover?specialId="+id+PHPSESSID, "overDiv");
	return false;
}

function userOver(name) {
	overlib('<div class="popupContainer"><div class="popupHeader">Infos werden geladen...</div></div>', WIDTH,300);
	loadFragmentDelayed("/UserRollover?username="+name+PHPSESSID, "overDiv");
	return false;
}
function showVotingPic(picURL) {
	overlib('<div class="popupContainer"><div class="popupHeader">Infos werden geladen...</div></div>', WIDTH,300);
	loadFragmentDelayed("/VotingRollover?picURL="+picURL, "overDiv");
	return false;
}
function userOverChat(name) {
	overlib('<div class="popupContainer"><div class="popupHeader">Infos werden geladen...</div></div>', WIDTH,300);
	loadFragmentDelayed("/UserRollover?chatname="+name, "overDiv");
	return false;
}

// zeigt Event-Rollover für den Kalender für den entsprechenden Tag an
function calOver(count, day) {
	if (count==1) {
		return simpleOver(day, '1 Event eingetragen', true);
	} else {
		return simpleOver(day, count+' Events eingetragen', true);
	}
}

// lässt das Overlay wieder verschwinden, setzt Timer zurück
function ndo() {
	stopDelayHandler();
	return nd();
}

function checkIfEmpty (textField, text) {
	var wert = textField.value + '';
	if ( wert.length == 0 ) {
		alert("Bitte das Feld " + text + " ausf\xFCllen!");
		textField.focus();
		return 1;
	}
	return 0;
}

function getGETVar(win, name) {
  var query = win.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == name) {
	  return pair[1];
    }
  }
  return "";
}

function setSession(win) {
	i=0;
	try {
		while (!win.PHPSESSID) {
			i++;
			if (i>10) {
				break;
			}
			if (win.opener && !win.opener.closed) {
				win = win.opener;
			} else {
				break;
			}
		}
		if (win.PHPSESSID) {
			return win.PHPSESSID;
		} else {
			//alert("Konnte Session nicht bestimmen! Das Hauptfenster darf nicht geschlossen werden!");
			return "";
		}
	}
	catch(error) {
		//alert("JS Execption");
		return ""; // keine session gefunden

	}
}


function updateMailStatus(startwin, value) {
	if (pageTitle=="") {
		pageTitle=startwin.document.title;
	}
	i=0;
	temp = "";
	if (value==1) {
		temp = value + " neue Nachricht - ";
	} else if (value>1) {
		temp = value + " neue Nachrichten - ";
	}
	temp = temp + pageTitle;

	while (!startwin.updateSidebarStatus) {
		i++;
		// Title nur in "Hauptfenstern" (erkennbar an menudiv) aendern
		if (startwin.document.menuDiv || document.menuDiv) {
			startwin.document.title = temp;
		}
		if (i>10) {
			break;
		}
		if (startwin.opener && !startwin.opener.closed) {
			startwin = startwin.opener;
		} else {
			break;
		}
	}
	startwin.document.title = temp;
	if (startwin.updateSidebarStatus) {
		startwin.updateSidebarStatus(value);
	}
}

function initimg(imgid, resize) {
	embedimg=document.getElementById('embedimg' + imgid);
	imgwid=embedimg.width;
	imghei=embedimg.height;

	if(imgdims.length<imgid) {
		imgdims[imgid]=[imgwid,imghei,resize];
	} else if(!imgdims[imgid]) {
		imgdims[imgid]=[imgwid,imghei,resize];
	}
	expandimg(imgid, 0);
}

function expandimg(imgid,flag) {
	imgcontain=document.getElementById('imgcontain' + imgid);
	bufferimg=document.getElementById('bufferimg' + imgid);
	embedimg=document.getElementById('embedimg' + imgid);
	imgicon=document.getElementById('iconimg'+imgid);

	if(flag==2)	if(imgcontain.style.position=='absolute') { flag=0 } else { flag=1 };
	if(flag==3)	if(imgcontain.style.position=='absolute') { flag=1 } else { flag=0 };

	if (imgdims[imgid]){
		imgwid=imgdims[imgid][0];
		imghei=imgdims[imgid][1];
		maxwidth = 420-imgdims[imgid][2];
	} else {
		imgwid=embedimg.width;
		imghei=embedimg.height;
		maxwidth = 420;
	}

	if(imgwid>0) {
		if(flag==1) {
			if(imgwid>maxwidth) {

				var myWidth = 0, myHeight = 0;
				if( typeof( window.innerWidth ) == 'number' ) {
					//Non-IE
					myWidth = window.innerWidth;
					myHeight = window.innerHeight;
				} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
					//IE 6+ in 'standards compliant mode'
					myWidth = document.documentElement.clientWidth;
					myHeight = document.documentElement.clientHeight;
				} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
					myWidth = document.body.clientWidth;
					myHeight = document.body.clientHeight;
				}

				midpoint=Math.round((myWidth-imgwid)/2);
				if (midpoint<10) midpoint=10;
				embedimg.width = imgwid;
				imgcontain.style.width=imgwid+4;
				imgcontain.style.left=midpoint+"px";
				imgcontain.style.position='absolute';
				imgcontain.style.borderStyle='solid';
				imgcontain.style.borderColor='#333333';
				imgcontain.style.borderWidth=2;
				bufferimg.height=imghei+7;
				if (imgicon.width==22) imgicon.src="media/shared/imgexpand1.gif";
			} else {
				imgcontain.style.width=maxwidth;
				bufferimg.height=1;
				imgcontain.style.borderStyle='none';
				imgcontain.style.position='static';
				if (imgicon.width==22) imgicon.src="media/shared/imgexpand0.gif";
			}
		} else {
			if (imgwid>maxwidth) {
				embedimg.width=maxwidth;
			}
			imgcontain.style.width=maxwidth;
			bufferimg.height=1;
			imgcontain.style.borderStyle='none';
			imgcontain.style.position='static';
			if (imgicon.width==22) imgicon.src="media/shared/imgexpand0.gif";
		}
	}
}

function mouseoverimg(imgid) {
		imgcontain=document.getElementById('imgcontain' + imgid);
		imgicon=document.getElementById('iconimg'+imgid);
		embedimg=document.getElementById('embedimg' + imgid);

		if (imgdims[imgid]){
			imgwid=imgdims[imgid][0];
		} else {
			imgwid=embedimg.width;
		}

		if (imgwid>420)	{
			imgicon.width=22;
			if(imgcontain.style.position=='absolute') {
				imgicon.src="media/shared/imgexpand1.gif";
			} else {
				imgicon.src="media/shared/imgexpand0.gif";
			}
		}
}

function mouseoutimg(imgid) {
		imgicon=document.getElementById('iconimg'+imgid);
		imgicon.src="media/shared/pix.gif";
		imgicon.width=13;
}

function evt_showArea(id) {
	if (document.getElementById) {
	  if (navigator.appName=="Netscape"){ 
		  document.getElementById(id).style.display = 'table';}
		else{
		  document.getElementById(id).style.display = 'inline';}
	}
}

function evt_hideArea(id) {
	if (document.getElementById) {
		document.getElementById(id).style.display = 'none';
	}
}

function setTheme(color) {
	jQuery.get('/?theme='+color, function() {
		location.reload(true);
	});
}

function setSenseless(val) {
	jQuery.get('/ForumLatest?_ckey=7&_cval='+val, function() {
		location.reload(true);
	});
}

function addSmiley(field, value) {
	if (document.selection) {
		//IE
		field.focus();
		sel = document.selection.createRange();
		sel.text = value;
		field.focus();
	} else if (field.selectionStart || field.selectionStart == '0') {
		//MOZILLA/NETSCAPE
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		field.value = field.value.substring(0, startPos) + value + field.value.substring(endPos, field.value.length);
		field.setSelectionRange(startPos+value.length, startPos+value.length);
	} else {
		field.value += value;
		field.focus();
	}
}

function blinkElement(elementid) {
	element = $('#'+elementid);
	var active;
	var src = element.attr('src');
	var iconid = src.replace(/.*\/([0-9]*).*/,'$1');
	if (!$(document).hasTime(elementid)) {;
		$(document).everyTime(1000, elementid, function() {
			if (newmessagecount>0 && !active) {
				element.attr('src', 'media/themes/'+theme+'/icons/'+iconid+'_a.png');
				active = 1;
			} else {
				element.attr('src', 'media/themes/'+theme+'/icons/'+iconid+'.png');
				active = 0;
			}
		});
	}
}
function condScrollTop(elementid, speed) {
	if(speed==null) speed=200;
	statustop=$('#'+elementid).offset().top;
	windowtop=$(window).scrollTop();
	if(windowtop > statustop) {
		$('html,body').animate({scrollTop:statustop-4}, speed);
	}
}

function showElement(id) {
	jQuery('#'+id).show();
}
function hideElement(id) {
	jQuery('#'+id).hide();
}
