var openpopupWindow = null;
var IEScrollTop = null;
var checkMessage = null;
var messengerIsActive = false;

function openPopup(mypage, myname, mywidth, myheight, pos, scrolling, resizable, closeOld){
	if (openpopupWindow && closeOld == "yes") {
		openpopupWindow.close();
	}
	if (mywidth == 'max') mywidth = screen.width;
	if (myheight == 'max') myheight = screen.height;
	if(pos == 'center'){
		LeftPosition	=	(screen.width)	?	(screen.width - mywidth) / 2 	: 100;
		TopPosition		=	(screen.height)	?	(screen.height - myheight) / 2	: 100;
	} else {
		LeftPosition	=	0;
		TopPosition		=	0;
	}
	settings = 'width=' + mywidth + ',height=' + myheight + ',top=' + TopPosition + ',left=' + LeftPosition + ',location=no,directories=no,menubar=no,toolbar=no,status=no,scrollbars=' + scrolling + ',resizable=' + resizable + ',dependent=no';
	openpopupWindow = window.open('',myname,settings);
	openpopupWindow.focus();
	openpopupWindow.location = mypage;
	return false;
}

function maximizeWindow() {
	window.moveTo(0,0);
	window.resizeTo(screen.width, screen.height);
}

function switchStyleByResolution() {
	var global_screenwidth = (window.innerWidth) ? window.innerWidth : document.documentElement.clientWidth;
	if (global_screenwidth >= 1280) {
		document.getElementById('styleResource').setAttribute("href", '{$extPath}/css/{$global.language_set}/style003big.css');
	}
	var mylocation = self.document.location + "";
	mysymbol = (mylocation.indexOf("\?") >= 0) ? "&" : "?";
	mylocation += mysymbol + "set_global_screenwidth=" + global_screenwidth;
	location.href = mylocation;
}

function confirmThis(msg) { 
	if(confirm(msg) == false) return false;
}

function external_init() {
	var a;
	
	if(!document.getElementsByTagName) return;
	a = document.getElementsByTagName("a");

	for(i = 0; i < a.length; i++) {
		if(!a[i].getAttribute("href")) {
			continue;
		} else if (a[i].getAttribute("rel") == "top") {
			a[i].target = "_top";
		} else if (a[i].getAttribute("rel") == "external") {
			a[i].target = "_blank";
		} else if (typeof(a[i].getAttribute("rel")) == "string") {
			a[i].target = a[i].getAttribute("rel");
		}
	}
}

function textLimiter(myarea, myIndicator, maxlimit) {
	x = document.getElementById(myarea).value;
	if (x.length > maxlimit) {
		document.getElementById(myarea).value = x.substring(0, maxlimit);
	} else if (myIndicator) {
		document.getElementById(myIndicator).value = maxlimit - x.length;
	}
}

function submitProcedure() {
	document.getElementById('submitButton').style.display = 'none';
	document.getElementById('loadBar').style.display = 'inline';
	return true;
}

function markAllCheckboxes(trigger, target) {
	newStatus = document.getElementById(trigger).checked;
	myFields = document.getElementsByName(target);
	for (i = 0; i < myFields.length; i++) {
		myFields[i].checked = newStatus;
	}
}

myToolTipp = null;
activeTimeOut = null;
var bump = null;

var docEl = (
	typeof document.compatMode != "undefined" &&
	document.compatMode		!= "BackCompat"
) ? "documentElement" : "body";


function imageDescription(mymode, mytarget) {
	myToolTipp = document.getElementById(mytarget);
	myToolTipp.style.display = (mymode == 1) ? "block" : "none";
	if (document.getElementById(mytarget + "image")) {
		document.getElementById(mytarget).style.width = document.getElementById(mytarget + "image").width + "px";
		bump = document.getElementById(mytarget + "image").height - 10;
	} else {
		bump = null;
	}
}

function loadImageDescription(mytarget, myimage) {
	myToolTipp = document.getElementById(mytarget);
	if (document.getElementById(mytarget + "image") != null) {
		document.getElementById(mytarget + "image").src = myimage;
		bump = document.getElementById(mytarget + "image").height - 10;
	}
	myToolTipp.style.display = "block";
}

function hideImageDescription(mytarget) {
	myToolTipp = document.getElementById(mytarget);
	myToolTipp.style.display = "none";
	bump = null;
}

function updateMousePos(e) {
	var xPos	=e? e.pageX : window.event.x;
	var yPos	=e? e.pageY : window.event.y;

	if (document.all && !document.captureEvents) {
		xPos += document[docEl].scrollLeft;
		yPos += (IEScrollTop != null) ? IEScrollTop : document[docEl].scrollTop;
	}

	if (myToolTipp != null && bump == null) {
		myToolTipp.style.left = (xPos + 10) + "px";
		myToolTipp.style.top = (yPos + 10) + "px";
	} else if (myToolTipp != null) {
		myToolTipp.style.left = (xPos + 10) + "px";
		myToolTipp.style.top = (yPos - bump) + "px";
	}
}

function simpleTooltipDelayed (mymode, mytarget) {
	activeTimeOut = window.setTimeout("simpleTooltip("+ mymode +", '" + mytarget + "');", 250);
}

function simpleTooltip(mymode, mytarget) {
	document.getElementById(mytarget).style.display = (mymode == 1) ? "block" : "none";
	if (activeTimeOut) window.clearTimeout(activeTimeOut);
}

function clearingChangeCountry(myCountry, myForm) {
	var radioOffer = myForm.offer;
	var myOffer = "";
	for(i = 0; radioOffer[i]; i++) {
		if(radioOffer[i].checked) myOffer = radioOffer[i].value;
		radioOffer[i].disabled = true;
	}
	var radioPayment = myForm.payment;
	var myPayment = "";
	if (radioPayment[1]) {
		for(i = 0; radioPayment[i]; i++) {
			if(radioPayment[i].checked) myPayment = radioPayment[i].value;
			radioPayment[i].disabled = true;
		}
	} else {
		myPayment = radioPayment.value;
	}
	location.href='/clearing/index.html?action=changeCountry&country=' + myCountry + '&offer=' + myOffer + '&payment=' + myPayment;
}

function markAndInsertInTextarea(starttag, endtag, textid) {
	var mypointer = document.getElementById(textid);
	var mycontent = mypointer.value;
	if (window.getSelection) {
		var myselection = mycontent.substring(mypointer.selectionStart, mypointer.selectionEnd);
	} else if (document.selection) {
		var myselection = document.selection.createRange().text;
	}else if (document.getSelection) { 
		var myselection = document.getSelection();
	}
	var mystart = mycontent.substring(0, mycontent.indexOf(myselection));
	var myend = mycontent.substring(mycontent.indexOf(myselection) + myselection.length, mycontent.length);
	myselection = starttag + myselection + endtag;
	mycontent = mystart + myselection + myend;
	document.getElementById(textid).value = mycontent;
	return false;
}

var eventteasercounter = 1;
var eventteasershow = 1;
var eventteasernumber;

function eventteaserbox_initiate() {
	eventteasernumber = 0;
	for (i = 1; i <= 3; i++) {
		if (document.getElementById('eventBox' + i) != null) eventteasernumber++;
	}
	eventteaserinterval = window.setInterval("eventteaserbox_switch()", 3000);
}

function eventteaserbox_switch(mynumber) {
	for (i = 1; i <= eventteasernumber; i++) {
		document.getElementById('eventBox' + i).className = "hidden";
	}
	document.getElementById('eventBox' + eventteasercounter).className = "image";
	
	eventteasercounter += 1;
	if (eventteasercounter > eventteasernumber) eventteasercounter = 1;
}

function eventteaserbox_point(mytarget) {
	eventteasercounter = mytarget;
	eventteaserbox_stop();
	eventteaserbox_switch();
}

function eventteaserbox_stop() {
	 window.clearInterval(eventteaserinterval);
}

function openInlinePopup (mylink, mywidth, myheight, mymode, mytitle, myclosetarget) {
	window.clearInterval(checkMessage);
	var show;
	var myframetag;
	var outerCloseTag =' onclick="return closeInlinePopup(' + myclosetarget + ');"';
	
	if (mymode == 'fullImage') {
		myheight = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
		myheight = myheight - 60;
		mywidth = Math.round(myheight / 3 * 4) - 10;
		myframetag = ' scrolling="no"';
	} else if (mymode == "closeRestricted") {
		outerCloseTag = "";
	} else if (mymode == "messenger") {
		messengerIsActive = true;
		outerCloseTag =' onclick="return minimizeMessenger();"';
	}

	show		=	'<div id="inlinePopupArea">';
	show		+=	'	<div id="inlinePopupBackground"' + outerCloseTag +'></div>';
	show		+=	'	<div id="inlinePopupContent">';
	if (mymode == "messenger") {
		show	+=	'		<p class="mize">';
		//show	+=	'			<a href="#" onclick="return closeInlinePopup(' + myclosetarget + ');"><img src="/resources/global/icon.close.png" alt="Close" /></a>';
		show	+=	'			<a href="#" onclick="return minimizeMessenger();"><img src="/resources/global/icon.minimize.png" alt="Minimize" /></a>';
		show	+=	'		</p>';
		show	+=	'		<h2><span>AC Messenger</span></h2>';
	} else if (mytitle == "") {
		show	+=	'		<div class="top"><div></div></div>';
	} else {
		show	+=	'		<p class="mize"><a href="#" onclick="return closeInlinePopup(' + myclosetarget + ');"><img src="/resources/global/icon.close.png" alt="Close" /></a></p>';
		show	+=	'		<h2><span>' + mytitle +'</span></h2>';
	}
	show		+=	'		<div class="main1"><div class="main2">';
	show		+=	'			<div id="inlinePopupFrameArea"><iframe id="inlinePopupiframe" name="inlinePopupiframe" src="about:blank" frameborder="0"'+ myframetag +'></iframe></div>';
	show		+=	'		</div></div>';
	show		+=	'		<div class="bottom"><div></div></div>';
	show		+=	'	</div>';
	show		+=	'</div>';
	
	document.getElementById('inlinePopupTerritory').innerHTML = show;
	document.getElementById('inlinePopupContent').style.width = mywidth + 'px';
	document.getElementById('inlinePopupContent').style.height = myheight + 'px';
	document.getElementById('inlinePopupContent').style.margin = '-' + Math.ceil(parseInt(myheight) / 2 + 10) + 'px 0 0 -' + Math.ceil((parseInt(mywidth) / 2)) + 'px';
	document.getElementById('inlinePopupiframe').width = parseInt(mywidth) - 15;
	document.getElementById('inlinePopupiframe').height = parseInt(myheight) - 15;
	document.getElementById('inlinePopupBackground').style.opacity = .7;
	document.getElementById('inlinePopupBackground').style.filter = 'Alpha(opacity=70, finishopacity=70, style=1)';
	document.getElementById('inlinePopupiframe').src = mylink;
	return false;
}

function closeInlinePopup(myclosetarget) {
	if (myclosetarget == 0) {
		document.getElementById('inlinePopupTerritory').innerHTML = "&nbsp;";
		checkMessageInit();
		return false;
	}
	switch (myclosetarget) {
		case 1:
			break;
	}
	location.href = myurl;
	return false;
}

function resizeFullImage() {
	var img = new Image();
	img.src = document.getElementById('fullImage').src;

	w = img.width;
	h = img.height;
	if (window.innerWidth) {
		myheight = window.innerHeight;
		mywidth = window.innerWidth;
	} else {
		myheight = document.documentElement.clientHeight;
		mywidth = document.documentElement.clientWidth;
	}
	nh = myheight + 10;
	nw = nh / h * w;
	if (nw - 100 > mywidth) {
		nw = mywidth;
		nh = nw / w * h;
	}
	document.getElementById('fullImage').width = nw;
	document.getElementById('fullImage').height = nh;
	document.getElementById('fullImage').style.display = "block";
}

function checkMessageInit() {
	checkNewMessage();
	checkMessage = window.setInterval("checkNewMessage()", 10000);
}

function checkNewMessage() {
	var req = null;
	try {
		req = new XMLHttpRequest();
	}
	catch (ms) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (nonms) {
			try	{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed) {
				req = null;
			}
		}
	}
	req.open("GET", '/resources/globalCheck.php', true);
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200 && req.responseText && req.responseText != "false") {
				switch (req.responseText) {
					case "force_hello":
						openInlinePopup ('/signup/hello.html', 750, 505, '', 'AC Messenger', 0);
						break;
					case "force_signup":
						openInlinePopup ('/signup/quick.html', 600, 400, 'closeRestricted', '', 0);
						break;
					case "force_quick_update":
						openInlinePopup ('/signup/quick.update.html', 600, 400, 'closeRestricted', '', 0);
						break;
					case "force_external_update":
						openInlinePopup ('/signup/external.update.html', 600, 530, 'closeRestricted', '', 0);
						break;
					case "force_nickname_update":
						openInlinePopup ('/signup/nickname.update.html', 600, 300, 'closeRestricted', '', 0);
						break;
					case "messengerGlow":
						document.getElementById('messengerMaximize').style.display = 'block';
						document.getElementById('messengerMaximize').className = 'glow';
						//playMessengerAlert();
						break;
					default:
						var command = req.responseText.split("|");
						if (command[0] == "messenger") openInlinePopup ('/account/messenger.html?msg=' + command[1], 750, 505, 'messenger', '', 0);
						break;
				}
				
			}
		}
	}
	
	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	req.send(null);
}

function minimizeMessenger() {
	document.getElementById('messengerMaximize').style.display = 'block';
	closeInlinePopup(0);
}

function maximizeMessenger() {
	openInlinePopup ('/account/messenger.html', 750, 505, 'messenger', '', 0);
	document.getElementById('messengerMaximize').style.display = 'none';
	document.getElementById('messengerMaximize').className = 'none';
	return false;
}

function composeMsgViaAS(myrecipient) {
	if (messengerIsActive == true) {
		openInlinePopup ('/account/messenger.html?forceRecipient=' + myrecipient, 750, 505, 'messenger', '', 0);
		document.getElementById('messengerMaximize').style.display = 'none';
		document.getElementById('messengerMaximize').className = 'none';
	} else {
		openInlinePopup('/account/mail.compose.html?recipient=' + myrecipient, 640, 450, '', 'Nachricht senden', 0);
	}
}

function locationHrefViaAS(mytarget) {
	location.href = mytarget;
}

function buddyrequestViaAS(mytarget) {
	openInlinePopup('/account/buddy.action.html?buddy=' + mytarget, 550, 140, '', 'Freundschaftsanfrage', 0);
}

function init_all() {
	sidebar_init();
	external_init();
	if (self == top) checkMessageInit();
	if (typeof(startMessenger) == "boolean" && startMessenger == true) messengerInit();
	if (typeof(startLivecamsCounter) == "boolean" && startLivecamsCounter == true) livecamsCounterInit();
}

function playMessengerAlert() {
	var bgSound = document.createElement('bgsound');
		if (typeof bgSound.src != 'undefined') {
			bgSound.src = '/resources/yougotmail.wav';
			document.body.appendChild(bgSound);
		} else {
		var object = document.createElement('object');
		object.type = 'audio/wav';
		object.data = '/resources/yougotmail.wav';
		object.width = 0;
		object.height = 0;
		document.body.appendChild(object);
	}
}

function sidebar_init() {
	if (typeof(globalScreenWidth) != "undefined" && globalScreenWidth == null) {
		var reqres = null;
		try {
			reqres = new XMLHttpRequest();
		}
		catch (ms) {
			try {
				reqres = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (nonms) {
				try	{
					reqres = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (failed) {
					reqres = null;
				}
			}
		}
		
		reqres.open("GET", '/resources/firstCheck.php?screenWidth=' + screen.width + '&screenHeight=' + screen.height, true);
		reqres.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		reqres.send(null);
		if (screen.width > 1024) {
			document.getElementById('sidebar').style.display = 'block';
			if (screen.width <= 1280) {
				document.getElementById('top').style.margin = '20px 0 0 20px';
				document.getElementById('desktop').style.margin = '0 0 0 20px';
			}
		}
	}
}

window.onload = init_all;
document.onmousemove = updateMousePos;
