function insert(aTag, eTag)
{
	var input = document.forms['kommentarform'].elements['kommentar'];
	input.focus();
  
  	/* für Internet Explorer */
	if(typeof document.selection != 'undefined')
	{
		/* Einfügen des Formatierungscodes */
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = aTag + insText + eTag;
		/* Anpassen der Cursorposition */
		range = document.selection.createRange();
		
		if (insText.length == 0)
		{
		  range.move('character', -eTag.length);
		}
		else
		{
      		range.moveStart('character', aTag.length + insText.length + eTag.length);      
		}
		
		range.select();
	}
	/* für neuere auf Gecko basierende Browser */
	else if(typeof input.selectionStart != 'undefined')
  	{
		/* Einfügen des Formatierungscodes */
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);
		input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
		/* Anpassen der Cursorposition */
		var pos;
		
		if(insText.length == 0)
		{
      		pos = start + aTag.length;
		}
		else
		{
      		pos = start + aTag.length + insText.length + eTag.length;
		}
		
		input.selectionStart = pos;
		input.selectionEnd = pos;
	}
	/* für die übrigen Browser */
	else
	{
		/* Abfrage der Einfügeposition */
		var pos;
		var re = new RegExp('^[0-9]{0,3}$');
		while(!re.test(pos))
		{
			pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
		}
		
		if(pos > input.value.length)
		{
			pos = input.value.length;
		}
		
		/* Einfügen des Formatierungscodes */
		var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
		input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
	}
}

function smilies()
{	
	if(document.getElementById('smilies').style.display == 'block')
	{
		document.getElementById('smilies').style.display = 'none';
		
	}
	else
	{
		document.getElementById('smilies').style.display = 'block';
	}
}

function werbemittel(breite)
{
	
	document.getElementById('container').style.width = breite + 799 + 'px';
	document.getElementById('headright').style.width = breite + 7 + 'px';
	document.getElementById('rightcol').style.width = breite + 'px';
	document.getElementById('footer').style.width = breite + 781 + 'px';
	document.getElementById('sky').style.width = breite + 'px';
}

function change2blank()
{
	var e = document.getElementById('springer');
	
	if(e)
	{
		var a = e.getElementsByTagName('a');
		
		for(var i = 0; i < a.length; i++)
		{
			a[i].setAttribute('target', '_blank');
		}
	}
}

typen = new Array('tag','woche','jahr');

function horoskop(typ)
{
	switch(typ)
	{
		case 'tag':
			document.getElementById('horoskop').style.backgroundPosition = 'left top';
		break;
		
		case 'woche':
			document.getElementById('horoskop').style.backgroundPosition = 'center top';
		break;
		
		case 'jahr':
			document.getElementById('horoskop').style.backgroundPosition = 'right top';
		break;
	}
	
	for(var i = 0; i < typen.length; ++i)
	{
		if(typen[i] == typ)
		{
			document.getElementById(typen[i]).blur();
			document.getElementById(typen[i]).className = typen[i] + 'aktiv';
			document.getElementById('horoskop' + typen[i]).style.display = 'block';
		
		}
		else
		{
			document.getElementById(typen[i]).className = typen[i] + 'inaktiv';
			document.getElementById('horoskop' + typen[i]).style.display = 'none';
		}
	}
}

function themen(typ)
{
	if(typ == 'neu')
	{
		document.getElementById('tab1').className = 'tab1aktiv';
		document.getElementById('tab2').className = 'tab2inaktiv';
		document.getElementById('themenbox').style.backgroundPosition = 'center left';
		document.getElementById('heiss').style.display = 'none';
		document.getElementById('neu').style.display = 'block';
		
	}
	else
	{
		document.getElementById('tab1').className = 'tab1inaktiv';
		document.getElementById('tab2').className = 'tab2aktiv';
		document.getElementById('themenbox').style.backgroundPosition = 'center right';
		document.getElementById('heiss').style.display = 'block';
		document.getElementById('neu').style.display = 'none';
	}
}

function popup(url,h)
{
	window.open(url,'Popup','width=470,height=' + h + ',resizable=yes,scrollbars=no');
}

function ir_popup_newwin(url)
{
	window.open(url, '', 'status=yes, toolbar=yes, menubar=yes, resizable=yes, scrollbars=yes');
}

function bookmark(dienst)
{
	var url = encodeURIComponent(location.href);
	var title = encodeURIComponent(document.title);
	
	switch(dienst)
	{
		case 'wong':
			window.open('http://www.mister-wong.de/index.php?action=addurl&bm_url=' + url + '&bm_description=' + title);
		break;
		
		case 'yigg':
			window.open('http://yigg.de/neu?exturl=' + url + '&exttitle=' + title);
		break;
		
		case 'linkarena':
			window.open('http://linkarena.com/bookmarks/addlink/?url=' + url + '&title=' + title);
		break;
		
		case 'google': 		
			window.open('http://www.google.com/bookmarks/mark?op=add&hl=de&bkmk=' + url + '&title=' + title);
		break;
		
		case 'delicious':
			window.open('http://del.icio.us/post?url=' + url + '&title=' + title);
		break;
		
		case 'yahoo':
			window.open('http://myweb2.search.yahoo.com/myresults/bookmarklet?u=' + url + '&t=' + title);
		break;
		
		case 'webnews': 		
			window.open('http://www.webnews.de/einstellen?url=' + url + '&title=' + title);
		break;
		
		case 'digg':
			window.open('http://digg.com/submit?phase=2&url=' + url + '&title=' + title);
		break;
		
		case 'shortnews':
			window.open('http://www.shortnews.de/submitsource.cfm?source=' + url + '&title=' + title);
		break;
		
		case 'facebook':
			window.open('http://de.facebook.com/sharer.php?u=' + url + '&t=' + title);
		break;
		
		case 'twitter':
			window.open('http://twitter.com/home?status=' + url);
		break;
	}
}

// AJAX
function getXMLRequester()
{
	var xmlHttp = false;
	
	// try to create a new instance of the xmlhttprequest object        
	try
	{
		// Internet Explorer
		if(window.ActiveXObject)
		{
			for (var v = 5; v; v--)
			{
				try
				{
					// loading of a newer version of msxml dll (msxml3 - msxml5) failed
					// use fallback solution
					// old style msxml version independent, deprecated
					if(v == 2)
					{
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");    
					}
					// try to use the latest msxml dll
					else
					{
						xmlHttp = new ActiveXObject("Msxml2.XMLHTTP." + v + ".0");
					}
					
					break;
				}
				
				catch (excNotLoadable)
				{                        
					xmlHttp = false;
				}
			}
		}
		// Mozilla, Opera und Safari
		else if (window.XMLHttpRequest)
		{
			xmlHttp = new XMLHttpRequest();
		}
	}
	
	// loading of xmlhttp object failed
	catch (excNotLoadable)
	{
		xmlHttp = false;
	}
	
	return xmlHttp;
}

// Notification
function chatnote(receiver,sender)
{
	xmlHttp = getXMLRequester();
	xmlHttp.open("POST","/actions/msgner_response.php", true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('receiver=' + receiver + '&sender=' + sender);
		
	xmlHttp.onreadystatechange = function()
	{		
		if(xmlHttp.readyState == 4)
		{			
			document.getElementById('messenger').innerHTML = xmlHttp.responseText;
			document.getElementById('messenger').style.display = 'inline';
		}
	}
}

// Chat-Request
function chatrequest(userName)
{
	xmlHttp = getXMLRequester();
	xmlHttp.open("POST","/actions/chatuser.php", true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send("id=" + userName);
		
	xmlHttp.onreadystatechange = function()
	{		
		if(xmlHttp.readyState == 4)
		{			
			if (xmlHttp.responseText != '' && isNaN(xmlHttp.responseText))
			{
				chatnote(userName,xmlHttp.responseText);
			}
		}
	}
}

// close Chat Notification
function closeChatrequest(receiver,caller)
{
	xmlHttp = getXMLRequester();
	xmlHttp.open("POST","/actions/chatuser.php", true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send("close=" + receiver + "&user=" + caller);
	
	xmlHttp.onreadystatechange = function()
	{		
		if(xmlHttp.readyState == 4)
		{
			document.getElementById('messenger').style.display = 'none';
			document.getElementById('messenger').innerHTML = '';
		}
	}
}

function chat(user,partner)
{
	var jetzt = new Date();
	window.open('/chat.html?user=' + partner,'Chat' + jetzt.getTime(),'width=513,height=451,resizable=no,scrollbars=no');
}

// geburtstagsliste
function geburtstagsliste()
{
	this.currenttime = new Date().getTime();

	this.loadTag = function(tag)
	{
		ajax = getXMLRequester();
		ajax.open("GET", "/user/geburtstagsliste/" + tag + ".html", false);
		ajax.setRequestHeader("Content-Type", "text/html");
		ajax.send(null);
		
		if(ajax.readyState == 4 && ajax.status == 200)
		{
			document.getElementById("geburtstagsliste").innerHTML = ajax.responseText;
		}
	}
	
	this.firstrun = function()
	{
		this.loadTag(this.sec2Date(this.currenttime));
	}

	this.vor = function()
	{
		this.currenttime = this.currenttime + (84600 * 1000)
		this.loadTag(this.sec2Date(this.currenttime));
	}

	this.zurueck = function()
	{
		this.currenttime = this.currenttime - (84600 * 1000)
		this.loadTag(this.sec2Date(this.currenttime));
	}

	this.sec2Date = function(sec)
	{
		d = new Date(sec);
		dd = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
		mm = (d.getMonth() + 1) < 10 ? "0" + (d.getMonth() + 1) : (d.getMonth() + 1);
		jjjj = d.getFullYear();
		jj = jjjj > 2000 ? "0" + (d.getYear() - 100) : d.getYear();
	
		return jjjj + mm + dd;
	}
}

function loadTracking(ivw_code)
{
	ivw_url = "http://maedchen.ivwbox.de/cgi-bin/ivw/CP/" + ivw_code + ";maedchen";
	ivw_call = ivw_url + "?r=" + escape(document.referrer) + "&d=" + (Math.random() * 100000);
	
	ivw = new Image(1, 1);
	ivw.src = ivw_call;
}

// Cropper Funktionen
function preview(img, selection) { 
	var scaleX = 193 / selection.width; 
	var scaleY = 193 / selection.height;
    
    $('#thumbnailspot > img').css({ 
		width: Math.round(scaleX * document.images["masterImage"].width) + 'px', 
		height: Math.round(scaleY * document.images["masterImage"].height) + 'px',
		marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', 
		marginTop: '-' + Math.round(scaleY * selection.y1) + 'px' 
	});
	$('#x1').val(selection.x1);
	$('#y1').val(selection.y1);
	$('#x2').val(selection.x2);
	$('#y2').val(selection.y2);
	$('#w').val(selection.width);
	$('#h').val(selection.height);
} 

function writeImage(filename){
    $text = '<img src="'+filename+'" id="thumbnail" name="masterImage" alt="Create Thumbnail" onclick="setThumbImage(\''+filename+'\')" />';
    $text = $text + '<div class="space10"></div><a href="#" onClick="location.reload();">Ein anderes Bild hochladen</a>';
    $this= $('#cropper').empty();
	$this.text('').append($text);
    setThumbImage(filename);
    $('#thumbnail').imgAreaSelect({ aspectRatio: '1:1', onSelectChange: preview });
}

function setThumbImage(filename){
    $('#actimage').html('<div id="thumbnailspot" style="border:1px #e5e5e5 solid; float:left; position:relative; overflow:hidden; width:193px; height:193px;"><img src="'+filename+'" style="position: relative;" alt="" /></div>');
    $('#picadd').val(filename);
}

function setDefaultImage(bild){
   $('#actimage').html('<img src="' + bild + '" width="193" height="193" alt="" />');
   $('#picadd').val(bild);
}

// END Cropper Funktionen

// Externer Player Wundermedia-Popup
function openwundermediaplayer()
{
	window.open('http://kunden.wundermedia.de/livedome/player/109/maedchen.de/','MBC',
                                        'height=620,width=990,menubar=no,location=no,resizeable=no,status=no,toolbar=no,dependent=no,scrollbars=no');
}

var VMAds = (function() {
	if ( typeof jQuery == 'undefined' )
	{
		return {};
	}
	var $ = jQuery,
		defaults = {
			url: '',
			banner: true,
			skyscraper: true,
			rectangle: false,
			targetBanner: '#superbanner',
			targetSkyscraper: '#skyscraper',
			targetRectangle: ''
		},
		counter = 0,
		step = 5;
	return {
		Reload : function(o)
		{
			var options = $.extend(defaults, o),
				$iframe = $('#bg_adiframe'),
				source = options.url + '?' + ((options.banner ? '&showbanner=1' : '') + (options.skyscraper ? '&showsky=1' : '') + (options.rectangle ? '&showrect=1' : '')).substr(1),
				$banner = (options.banner && (options.targetBanner || '').length) ? $(options.targetBanner) : {},
				$sky = (options.skyscraper && (options.targetSkyscraper || '').length) ? $(options.targetSkyscraper) : {},
				$rect = (options.rectangle && (options.targetRectangle || '').length) ? $(options.targetRectangle) : {};
			
			if ( ++counter % step != 0 || !(options.url || '').length || ( !$banner.length && !$sky.length && !$rect.length ) )
			{
				return;
			}
			
			if ( !$iframe.length )
			{
				$iframe = $('<iframe></iframe>').attr({
					id: 'bg_adiframe',
					name: 'bg_adiframe',
					src: source
				}).css({width: 0, height: 0, display: 'none'}).bind('load',function(){
					try
					{
						var ifEl = null,
							iframe = $iframe.get(0),
							adEL = null;
						if ( iframe.contentDocument )
						{
							ifEl = iframe.contentDocument;
						}
						else if ( iframe.contentWindow )
						{
							ifEl = iframe.contentWindow.document;
						}
						if ( ifEl )
						{
							if ( $banner && (adEL = ifEl.getElementById('vmads_banner')) )
							{
								$banner.get(0).innerHTML = adEL.innerHTML;
							}
							if ( $sky && (adEL = ifEl.getElementById('vmads_sky')) )
							{
								$sky.get(0).innerHTML = adEL.innerHTML;
							}
							if ( $rect && (adEL = ifEl.getElementById('vmads_rect')) )
							{
								$rect.get(0).innerHTML = adEL.innerHTML;
							}
						}
					} catch (e){}
				}).appendTo($('body'));
			}
			else
			{
				$iframe.attr('src', source + '&rnd=' + Math.random());
			}
		},
		SetStep : function(s)
		{
			if ( (typeof s == 'number' || (s = parseInt(s))) && s > 1 )
			{
				step = s;
			}
		},
		IsNextReload : function()
		{
			return (counter + 1) % step == 0;
		}
	}
})();
