
/**
 *
 * @author $Author: rutarh $
 * @version $Id: functions.js 4379 2009-02-16 11:09:25Z rutarh $
 */

var offsetfromcursorX = -7;
var offsetfromcursorY = 23;
var offsetdivfrompointerX = 13; 
var offsetdivfrompointerY = 13; 
var ie = document.all;
var ns6 = document.getElementById && !document.all;


var tipObj;
var tipCount = 0;
var tips = new Array();
var enabletip = false;

var lColors  = new Object();
lColors["de"] = "#003282";
lColors["gb"] = "#003282";
lColors["gbde"] = "#003282";
lColors["es"] = "#003282";
lColors["ae"] = "#003282";

var lSprachen  = new Object();
lSprachen["de"] = "Deutsch";
lSprachen["gb"] = "English";
lSprachen["gbde"] = "Engl./Deu.";
lSprachen["es"] = "Espa&ntilde;ol";
lSprachen["ae"] = "&#1593;&#1585;&#1576;&#1610;";

var lLocaltime  = new Object();
lLocaltime["de"] = "Lokale Sendezeit";
lLocaltime["gb"] = "Local broadcast time";
lLocaltime["gbde"] = "Local broadcast time / Lokale Sendezeit";
lLocaltime["es"] = "Hora local de emisi&oacute;n";
lLocaltime["ae"] = "&#1575;&#1604;&#1576;&#1579; &#1576;&#1575;&#1604;&#1578;&#1608;&#1602;&#1610;&#1578; &#1575;&#1604;&#1605;&#1581;&#1604;&#1610;";


function doTip(node, sprache, timestamp, titel, zusatzinfo, bildId) {

	hasNoAtt = (node.getAttribute("tipcount") == null);

	if (hasNoAtt) {

		tipCount++;
		node.setAttribute("tipcount", tipCount);

		tipObj = document.createElement("div");
		tipObj.setAttribute("sprache", sprache);
		tipObj.setAttribute("timestamp", timestamp);
		tipObj.setAttribute("titel", titel);
		tipObj.setAttribute("zusatzinfo", zusatzinfo);
		tipObj.setAttribute("bildId", bildId);

		tipObj.id = "theToolTip";
		document.body.appendChild(tipObj)
		tips[tipCount] = tipObj;

		tipObj.innerHTML = doTipText(tipObj);

	}
	else {
		tipObj = tips[node.getAttribute("tipcount")]; 
	}

	enabletip=true;
	node.onmouseout=function gomouseout() { hideTip(this); };
	node.onmouseover = "";
	document.onmousemove = positionTip;

	return true;

}


function doTipText(tto) {

	sprache = tto.getAttribute("sprache");
	timestamp = tto.getAttribute("timestamp");
	titel = tto.getAttribute("titel");
	zusatzinfo = tto.getAttribute("zusatzinfo");
	bildId = tto.getAttribute("bildId");

	var html = "";
	var textclass = (sprache == "ae") ? "tttextbig" : "ttext";
	tto.style.borderColor = lColors[sprache];

	if (sprache == "ae") {
		titel = titel + " <bdo dir=\"ltr\">)</bdo>"+lSprachen[sprache]+"<bdo dir=\"ltr\">(</bdo><br>";
	}
	else {
		titel = titel + " ("+lSprachen[sprache]+")<br>";
	}

	if (sprache == "ae") {
		localtext = "<bdo dir=\"ltr\"><b>"+localShowTime(timestamp)+"</b></bdo>&nbsp;"+lLocaltime[sprache];
	}
	else {
		localtext = lLocaltime[sprache]+":&nbsp;<b>"+localShowTime(timestamp)+"</b>";
	}

	html = "<span class=\"tttitle\">"+titel+"</span><span class=\"tttext\">"+localtext+"</span><br><br><span class=\""+textclass+"\">"+zusatzinfo+"</span>";

	if (bildId != 0) {
		html = html + '<p><img class="picBoxBorder" src="http://www.dw-world.de/image/0,,'+bildId+'_2,00.jpg" width="122" height="90"></p>';
	}

	return html;

}

function localShowTime(timestamp) {

	dispDate = "";
	if (timestamp > 0) {
		d = new Date((timestamp*1000));
		ms = d.getTime()+(localOffset*60000);
		nd = new Date(ms);

		dispDate = formatTime(nd.getDate())+"."+formatTime(nd.getMonth()+1)+".,&nbsp;"+formatTime(nd.getHours())+":"+formatTime(nd.getMinutes());

	}
	return dispDate;
}



function changeTips() {
	for (var i = 0; i < tips.length; i++) {
		if (typeof(tips[i]) == "object") {
			tto = tips[i]; 
			tto.innerHTML = doTipText(tto);
		}
	}
}



// --------------------------------------------------------------------


function ietruebody() {
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}


function positionTip(e) {

	if (enabletip) {

		var nondefaultpos=false;
		var curX=(ns6) ? e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6) ? e.pageY : event.clientY+ietruebody().scrollTop;

		// Find out how close the mouse is to the corner of the window
		var winwidth = ie&&!window.opera ? ietruebody().clientWidth : window.innerWidth-20;
		var winheight = ie&&!window.opera ? ietruebody().clientHeight : window.innerHeight-20;
		
		var rightedge=ie&&!window.opera ? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX;
		var bottomedge=ie&&!window.opera ? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY;
		
		var leftedge=(offsetfromcursorX<0) ? offsetfromcursorX*(-1) : -1000;
		
		//if the horizontal distance isnt enough to accomodate the width of the context menu
		if (rightedge<tipObj.offsetWidth) {
			//move the horizontal position of the menu to the left by its width
			tipObj.style.left=curX-tipObj.offsetWidth+"px";
			nondefaultpos=true;
		}
		else if (curX<leftedge) {
			tipObj.style.left="5px";
		}
		else {
			// position the horizontal position of the menu where the mouse is positioned
			tipObj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px";
		}
		
		// same concept with the vertical position
		if (bottomedge<tipObj.offsetHeight) {
			tipObj.style.top=curY-tipObj.offsetHeight-offsetfromcursorY+"px";
			nondefaultpos=true;
		}
		else {
			tipObj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px";
		}
		tipObj.style.visibility="visible";
		
	}
}

function hideTip(node) {

	if (ns6 || ie) {

		node.onmouseout = "";
		node.onmouseover=function newmouseover() { doTip(this, 0, "", "", ""); };
	
		tto = tips[node.getAttribute("tipcount")]; 

		enabletip = false;
		tto.style.visibility = "hidden";
		//tto.style.left = "-1000px";
		tto.style.backgroundColor = "";
		tto.style.width = "";
	}
}

// --------------------------------------------------------------------




var timerID = null;
var localOffset = 0;

function displayTime() {
	nd = new Date();
	ms = nd.getTime()+(localOffset*60000);
	d = new Date(ms);

	localTimeTag = document.getElementById("localTime");
	localTimeTag.innerHTML = formatTime(d.getHours())+":"+formatTime(d.getMinutes())+":"+formatTime(d.getSeconds());
}

function initTime() {
	timerID = setInterval("displayTime()", 1000);
}


function correctTime(offset) {

	if (isNaN(offset)) {
		offset = 0;
	}
	localOffset = offset;

	displayTime();
	setUrlOffset(offset);
	changeTips();
}

function formatTime(timeval) {
	if (timeval < 10) timeval = "0"+timeval;
	return timeval;
}


// --------------------------------------------------------------------

function setUrlOffset (offset) {
	at = document.getElementsByTagName("a");
	for (var i = 0; i < at.length; i++) {
		with (at[i]) {
			if (id == "plink") {
				hasParam = !(href.indexOf("to=") == -1);
				if (hasParam) {
					href = href.replace(/&to=([\-0-9a-z]+)/i, "&to="+offset); 
				}
				else {
					href = href + "&to="+offset; 
				}
			}		
		}
	}
}


function setUrlSIds (sendung) {
	at = document.getElementsByTagName("a");
	for (var i = 0; i < at.length; i++) {
		with (at[i]) {
			if (id == "plink") {
				hasSId = !(href.indexOf("sendungsId=") == -1);
				if (hasSId) {
					if (sendung != "") {
						href = href.replace(/&sendungsId=([0-9a-z]+)/i, "&sendungsId="+sendung); 
					}
					else {
						href = href.replace(/&sendungsId=([0-9a-z]+)/i, ""); 
					}
				}
				else {
					href = href + "&sendungsId="+sendung; 
				}
			}		
		}
	}
}


var lastChange = 0;
function colorCells (sendung) {
	td = document.getElementsByTagName("td");
	for (var i = 0; i < td.length; i++) {
		with (td[i]) {
			if (lastChange != 0) {
				re = new RegExp("sl"+lastChange+"#([0-9])+");
				if (id == "s"+lastChange || re.test(id)) {
					style.backgroundColor = oldColor;
				}				
			}
			re = new RegExp("sl"+sendung+"#([0-9])+");
			if ((id == "s"+sendung || re.test(id)) && sendung != "") {
				oldColor = style.backgroundColor
				style.backgroundColor = "#d3e7fd";
			}		
		}
	}
	lastChange = sendung;	
	setUrlSIds(sendung);
}


