var ie = 0;
var ns = 0;
var ns6 = 0;
var hideMenu = null;
var ContentInfo = '';

detectBrowser();
function detectBrowser() {
	if (document.all) { ie = 1; }
	else if (document.layers) { ns = 1; }
	else if (document.getElementById) { ns6 = 1; }
	else { ns = 1; }
}
function Fensterweite()
{
 if (window.innerWidth) return window.innerWidth;
 else if (document.body && document.body.offsetWidth) return document.body.offsetWidth;
 else return 0;
}

function ShowL(div){
	if (ie) { eval('document.all.'+div+'.style.visibility = "visible"'); }
	if (ns)	{ eval('document.layers.'+div+'.visibility="show"');}
	if (ns6)	eval('document.getElementById("'+div+'").style.visibility="visible"');
}

function HideL(div){
	if (ie) { eval('document.all.'+div+'.style.visibility="hidden"'); }
	if (ns) { eval('document.layers.'+div+'.visibility="hide"'); }
	if (ns6) {eval('document.getElementById("'+div+'").style.visibility="hidden"');}
}

if (ie == 1) {doc = "document.all."; sty = ".style"}
if (ns == 1) {doc = "document."; sty = ""; }
if (ns6 == 1) {doc = "document.getElementById('"; sty = "').style"; }

var initialize = 0
var Ex, Ey, topColor, subColor, ContentInfo, InsertFormname, bbcodetype, sel, colswitch = true

if(ie){
	Ex = "event.x"
	Ey = "event.y"
	topColor = "#CC0000"
	subColor = "#CCCCCC"
}

if (ns){
	Ex = "e.pageX"
	Ey = "e.pageY"
	window.captureEvents(Event.MOUSEMOVE)
//	window.onmousemove=overhere
	topColor = "#CC0000"
	subColor = "#CCCCCC"
}

if (ns6){
	Ex = "e.pageX"
	Ey = "e.pageY"
	window.captureEvents(Event.MOUSEMOVE)
//	window.onmousemove=overhere
	topColor = "#CC0000"
	subColor = "#CCCCCC"
}

function MoveToolTip(layerName, FromTop, FromLeft, e){

	if(ie){eval(doc + layerName + sty + ".top = "  + (eval(FromTop)))}
	if(ns){eval(doc + layerName + sty + ".top = "  + (eval(FromTop)))}
	if(ns6){eval(doc + layerName + sty + ".top = "  + (eval(FromTop)))}
	Weite = Fensterweite();

	if((Ex+175)>Weite)
		xpos = -150
	else
		xpos = 0
	
	eval( doc + layerName + sty + ".left = " + (eval(FromLeft) + xpos))
}

function ReplaceContent(layerName){
	if (ie == 1) { document.all[layerName].innerHTML = ContentInfo; }
	if (ns6 == 1) { document.getElementById(layerName).innerHTML = ContentInfo; }
	if (ns == 1) {
		with(document.layers[layerName].document) 
		{ 
			open(); 
		   	write(ContentInfo); 
		   	close(); 
		}
	}
}

function iDisable()
{
        document.getElementById(InsertFormname).readOnly = (document.getElementById(InsertFormname).readOnly) ? false : true;
        if(colswitch == false){
        	colswitch=true;
        	document.getElementById(InsertFormname).style.backgroundColor='#FFFFFF';
        }
        else{
        	colswitch=false;
        	document.getElementById(InsertFormname).style.backgroundColor='#DDDDDD';
        }
        
        return void(0);
}

function Activate(type, formname){

	bbcodetype=type;
	
	if(type == 'slideshow'){
		EnterContent(type);
		ShowL("BBCODE");
		MoveToolTip("BBCODE", Ey , Ex);
	}
	else {
		document.getElementById(formname).focus();
		//IE support
		if (document.selection) {
			sel = document.selection.createRange();
		}
	
		InsertFormname=formname;
		EnterContent();
		ShowL("BBCODE");
		MoveToolTip("BBCODE", Ey , Ex);
		iDisable();
		document.getElementById('bbcodetext').focus();
	}
}
function deActivate(){
	HideL("BBCODE");

}

function closeit(){
	deActivate();
	if(bbcodetype != 'slideshow')
	iDisable();
}

//function overhere(){
//}

function EnterContent(){
	
	if(bbcodetype != 'slideshow'){
		if(sel)
		var fieldvalue=sel.text;
		else
		var fieldvalue=document.getElementById(InsertFormname).value.substring(document.getElementById(InsertFormname).selectionStart,document.getElementById(InsertFormname).selectionEnd);;
	}
	
	ContentInfo = '<table border="0" width="200" cellspacing="0" cellpadding="0">'+
	'<tr><td width="100%">'+
	'<table border="0" width="100%" cellspacing="0" cellpadding="0">'+	
	'<tr><td width="100%">'+
	'<table border="0" width="100%" cellspacing="0" cellpadding="0" bgcolor='+topColor+' class="bbcodeheaderborder">'+
	'<tr><td width="50%"><font class="bbcodetitle">&nbsp;';
	if(bbcodetype=='b'){			
		ContentInfo+='Fett';
	}
	else if(bbcodetype=='i'){
		ContentInfo+='Kursiv';
	}
	else if(bbcodetype=='u'){
		ContentInfo+='Unterstrichen';
	}
	else if(bbcodetype=='a'){
		ContentInfo+='Link';
	}
	else if(bbcodetype=='us'){
		ContentInfo+='Benutzername';
	}
	else if(bbcodetype=='slideshow'){
		ContentInfo+='Slideshow';
	}	
	
	ContentInfo+='</font></td><td width="50%" align="right">'+
		'<a href="javascript:void(0)" onClick="closeit()" class="bbcodeclose">Schließen</a>&nbsp;'+
		'</td></tr>'+
		'</table>'+
		'</td></tr>'+
		'<tr><td width="100%" bgcolor='+subColor+' class="bbcodeborder">';
	if(bbcodetype=='b' || bbcodetype=='i' || bbcodetype=='u'){
		ContentInfo+='<table border="0" width="100%" cellpadding="0" cellspacing="1">'+
			'<tr><td width="20%" align="right">'+
			'<font class="bbcodecontent">Text:</font>'+
			'</td>'+
			'<td width="80%" align="right">'+
			'<input id="bbcodetext" type="text" name="text" size="23" class="bbcodefield" value="'+fieldvalue+'">'+
			'</td></tr>'+
			'<tr><td colspan="2" align="right"><input type="button" name="insert" value="einfügen" class="bbcodebutton" onClick="insertBBCode()">'+
			'</td></tr>'+
			'</table>';
	}
	else if(bbcodetype=='a'){
		ContentInfo+='<table border="0" width="100%" cellpadding="0" cellspacing="1">'+
			'<tr><td width="20%" align="right">'+
			'<font class="bbcodecontent">Text:</font>'+
			'</td>'+
			'<td width="80%" align="right">'+
			'<input id="bbcodetext" type="text" name="text" size="23" class="bbcodefield" value="'+fieldvalue+'">'+
			'</td></tr>'+
			'<tr><td width="20%" align="right">'+
			'<font class="bbcodecontent">URL:</font>'+
			'</td>'+
			'<td width="80%" align="right">'+
			'<input id="bbcodeurl" type="text" name="url" size="23" class="bbcodefield" value="http://">'+
			'</td></tr>'+
			'<tr><td colspan="2" align="right"><input type="button" name="insert" value="einfügen" class="bbcodebutton" onClick="insertBBCode()">'+
			'</td></tr>'+
			'</table>';
	}
	else if(bbcodetype=='us'){
		ContentInfo+='<table border="0" width="100%" cellpadding="0" cellspacing="1">'+
			'<tr><td width="20%" align="right">'+
			'<font class="bbcodecontent">Benutzer:</font>'+
			'</td>'+
			'<td width="80%" align="right">'+
			'<input id="bbcodetext" type="text" name="text" size="23" class="bbcodefield" value="'+fieldvalue+'">'+
			'</td></tr>'+
			'<tr><td colspan="2" align="right"><input type="button" name="insert" value="einfügen" class="bbcodebutton" onClick="insertBBCode()">'+
			'</td></tr>'+
			'</table>';
	}
	else if(bbcodetype=='slideshow'){
		ContentInfo+='<table border="0" width="100%" cellpadding="2" cellspacing="2">'+
			'<tr><td width="20%" align="right">'+
			'<font class="bbcodecontent">Zeitintervall:</font>'+
			'</td>'+
			'<td width="80%">'+
			'<select id="tintervall" name="timeintervall"><option value="3">3</option><option value="5">5</option><option value="10">10</option value="20"><option>20</option><option value="30">30</option></select> Sekunden'+
			'</td></tr>'+
			'<tr><td colspan="2" align="right"><input type="button" name="insert" value="Start Slideshow" class="bbcodebutton" onClick="startSlideshow()">'+
			'</td></tr>'+
			'</table>';		
	}
	ContentInfo+='</td></tr>'+	
	'</table>'+
	'</td></tr>'+
	'</table>';
	
	layerName='BBCODE';
	ReplaceContent(layerName)
}

function startSlideshow(){	
	deActivate();
	window.location.href=window.location.href+'/slideshow='+document.getElementById('tintervall').value;
}

function insertBBCode(){
	var myField = document.getElementById(InsertFormname);
	var myValue;
	iDisable();
	if(bbcodetype == 'b' && document.getElementById('bbcodetext').value.length>0){		
		myValue = '[B]'+document.getElementById('bbcodetext').value+'[/B]';
		insertForm(myValue);
	}
	else if(bbcodetype == 'i' && document.getElementById('bbcodetext').value){		
		myValue = '[I]'+document.getElementById('bbcodetext').value+'[/I]';
		insertForm(myValue);
	}
	else if(bbcodetype == 'u' && document.getElementById('bbcodetext').value){		
		myValue = '[U]'+document.getElementById('bbcodetext').value+'[/U]';
		insertForm(myValue);
	}
	else if(bbcodetype == 'a' && (document.getElementById('bbcodetext').value || document.getElementById('bbcodeurl').value)){		
		if(document.getElementById('bbcodetext').value.length>0){
			myValue = '[URL='+document.getElementById('bbcodeurl').value+']'+document.getElementById('bbcodetext').value+'[/URL]';
		}
		else {
			myValue = '[URL]'+document.getElementById('bbcodeurl').value+'[/URL]';
		}
		insertForm(myValue);
	}
	else if(bbcodetype == 'us' && document.getElementById('bbcodetext').value){		
		myValue = '[USER]'+document.getElementById('bbcodetext').value+'[/USER]';
		insertForm(myValue);
	}	
	deActivate();
}

function insertForm(myValue){
	var myField = document.getElementById(InsertFormname);	

	//IE support
	if (sel) {
		sel.text = myValue;
		sel.moveStart('character', sel.text.length);
		sel.select();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue 
		+ myField.value.substring(endPos, myField.value.length);
	} 
	else {
		myField.value += myValue;
	}

}

function Position(Ereignis)
{
	if(ie == 1) {
 		Ex = window.event.x + document.body.scrollLeft;
 		Ey = window.event.y + document.body.scrollTop;
	}
	
	if (ns6 == 1 || ns == 1) {
 		Ex = Ereignis.pageX;
 		Ey = Ereignis.pageY;
	}
	
}
document.onmousemove = Position;

function smilie(Field, myValue) {
	var myField = document.getElementById(Field);
	if(colswitch == true){
		//IE support
		if (document.selection) {
			myField.focus();
			sel = document.selection.createRange();
			sel.text = myValue;
		}
		//MOZILLA/NETSCAPE support
		else if (myField.selectionStart || myField.selectionStart == '0') {
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			myField.value = myField.value.substring(0, startPos)
			+ myValue 
			+ myField.value.substring(endPos, myField.value.length);
			} else {
			myField.value += myValue;
		}
		myField.focus();
	}
}
