function openPopup(url, width, height) {
	var win = window.open(url, 'Neunlive', 'width='+width+',height='+height);
	if (win) win.focus();
}

function openPopUpWindow( side, width, height, top, left, scrollbars )
{
    if (side && side != null)
    {
        var win = document.open(side, "_blank", "width=" + width + ",height=" + height + ",top="+ top + ",left=" + left + ",toolbar=no,status=no,location=no,menubar=no,directories=no,resizable=yes,scrollbars=" + scrollbars + ",scrolling=");
		if (win) win.focus();
    }
    return;
}

function openPopUpWindowInside( side, width, height, top, left, scrollbars )
{
    if (side && side != null)
    {
        var win = window.open(side, "Detail", "width=" + width + ",height=" + height + ",top="+ top + ",left=" + left + ",toolbar=no,status=no,location=no,menubar=no,directories=no,resizable=yes,scrollbars=" + scrollbars + ",scrolling=");
		if (win) win.focus();
    }
    return;
}

function openerLocation(url)
{
	if (window.opener) {
		window.opener.parent.location.href = url;
	}
	else {
		window.open(url);
		window.focus();
	}
	return void(0);
}

function toggleElementVisibility(el) {
	if (el) {
        if (el.style.display != 'block') {
            showElement(el);
        } else {
            hideElement(el);
        }
    }
}

function hideElement(el) {
    if (el) {
        el.style.display = 'none';
    }
}

function showElement(el) {
    if (el) {
        el.style.display = 'block';
        el.style.visibility = 'visible';
    }
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// attrib_name may be
// id|class|readOnly
// or a style property name like 'display'
function setAttributeForElement(id, attrib_name, attrib_value)
{
	var element = document.getElementById(id);
	if (!element) return;
	
	if (attrib_name == 'class') {
		element.className = attrib_value;
	}
	else if (attrib_name == 'id') {
		element.id = attrib_value;
	}
	else if (attrib_name == 'readOnly') {
		element.readOnly = attrib_value;
	}
	else {
		element.style[attrib_name] = attrib_value;
	}
	return 1;
}


/** Magic Dropdowns **/

var magicdropdowns = new Array();
var magicdropdownButtonHandled = false;

function magicdropdownButtonClick(tag) {
	magicdropdownButtonHandled = true;
	// TODO: hide other dropdowns

	// show requested dropdown
	el = magicdropdowns[tag];
	toggleElementVisibility(el);
}

function magicdropdownOptionClick(tag, optionValue, optionText) {
	// change text
	var textEl = document.getElementById('magicdropdown-text-'+tag);
	if (textEl) {
        textEl.innerHTML = optionText;
        textEl.style.color = '#000000';
    }

	// change hidden form value to optionValue
    var valueEl = document.getElementById('magicdropdown-value-'+tag);
    if (valueEl) {
        valueEl.value = optionValue;
    }

	// hide dropdown
	hideElement(magicdropdowns[tag]);
}


function magicdropdownClickHandler(event) {
	if (!magicdropdownButtonHandled) {
		// TODO: hide all dropdowns
	}

	magicdropdownButtonHandled = false;
}

function magicdropdownInit() {
	document.onclick = magicdropdownClickHandler;
	magicdropdowns = new Array();
}

function magicdropdownRegister(tag) {
	magicdropdowns[tag] = document.getElementById('magicdropdown-dropdown-'+tag);
}



var bikky = document.cookie;

function getCookie(name) { // use: getCookie("name");
    var index = bikky.indexOf(name + "=");
    if (index == -1) return null;
    index = bikky.indexOf("=", index) + 1;
    var endstr = bikky.indexOf(";", index);
    if (endstr == -1) endstr = bikky.length;
    return unescape(bikky.substring(index, endstr));
}

var today = new Date();
var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days

function setCookie(name, value) { // use: setCookie("name", value);
    if (value != null && value != "")
      document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString()+"; path=/";
    bikky= document.cookie; // update bikky
}

function testCookie() {
    var yourName = getCookie ("yourName");
    if (yourName == null) yourName = prompt ("What is your name?", "");
    alert ("Hello, " + yourName + "\nWelcome to my Website");
    setCookie ("yourName", yourName);
}

function doTeatri(directory)
{
	if (directory && window.teatri != '') {
		document.write('<img src="https://ps.9live.de/cgi-bin/9lps.cgi?pfad=' + directory
			+ window.teatri + '" width="1" height="1" align="right">');
	}
}

/* Dynabox and Combox */

function dynaboxToggle(id, max_id, reiter_raw_id, content_raw_id, module_id) {
	var reiter_buttons = document.getElementById(reiter_raw_id + '-' + module_id);
	if (reiter_buttons) {
		reiter_buttons.className = reiter_raw_id + ' ebene' + id;
	}

	for (var i = 1; i <= max_id; i++) {
		var reiter  = document.getElementById(reiter_raw_id  + i + '-' + module_id);
		var content = document.getElementById(content_raw_id + i + '-' + module_id);
		
		if (i == id) {
			if (reiter) reiter.className = 'active';
			if (content) showElement(content);
		}
		else {
			if (reiter) reiter.className = 'inactive';
			if (content) hideElement(content);
		}
	}
	
	return void(0);
}

/* Channel Navi Highlighting */

// Make sure that all compared paths end on a slash.
// It does not cause a problem here if a slash is appended to a filename.

// Needed for neuntv
function mainChannelNaviHighlight()
{
	var channelnavi = document.getElementById('id_channelnavi');
	if (!channelnavi) return;
	
	var pathname = window.location.pathname;

	pathname = pathname.replace(/^\/neuntv/, '');
	if (! pathname.match('/$')) pathname += '/';

	var channel_links = channelnavi.getElementsByTagName('a');
	
	for (var i = 0; i < channel_links.length; ++i) {
		var link_path = channel_links[i].pathname;
		link_path = link_path.replace(/^(\/)?neuntv/, '');
		if (! link_path.match('^/')) link_path = '/' + link_path;
		if (! link_path.match('/$')) link_path += '/';
		
		if (link_path == '/') continue;

		if (link_path && new RegExp('^' + link_path).test(pathname)) {
			var link_class = channel_links[i].className;
			if (link_class) channel_links[i].className = link_class + '-highlighted';
			return;
		}
	}
	
	// Fallback
	channel_links[0].className += '-highlighted';
}

function channelNaviHighlight(urls, ids, aufklapp)
{
	var pathname = window.location.pathname;

	if (window.location.host.match('^www\.neuntv\.de')) pathname = '/neuntv' + pathname; 	
	if (! pathname.match('/$')) pathname += '/';

	for (var i = 0; i < urls.length; i++) {
		var url = urls[i];
		if (! url.match('/$')) url += '/';

		if (url && new RegExp('^' + url).test(pathname)) {
			var anchor = document.getElementById('c' + ids[i]);
			if (anchor) anchor.className = 'highlighted';
			var aufklapp = aufklapp[i];
			if (aufklapp) {
				var ul = document.getElementById('channel-more' + aufklapp);
				if (ul) ul.style.display = 'block';
			}
			break;
		}
	}
}

/* Format Navi Highlighting */

function formatNaviHighlight(urls, ids)
{
	var pathname = window.location.pathname;

	if (window.location.host.match('^www\.neuntv\.de')) pathname = '/neuntv' + pathname; 	
	if (! pathname.match('/$')) pathname += '/';

	for (var i = 0; i < urls.length; i++) {
		var url = urls[i];
		if (! url.match('/$')) url += '/';

		if (url && new RegExp('^' + url).test(pathname)) {
			var anchor = document.getElementById('f' + ids[i]);
			if (anchor) anchor.className = 'highlighted';
			break;
		}
	}
}

function wochengirl(url)
{
	var clack=window.open(url,'wochengirl','width=840,height=696,resizable=1,scrollbars=yes');
	if (clack) clack.focus();
	return;
}

function tagesgirl(day)
{
	switch (day) {
		case 1:
		day = "heute";
		break;
		
		case 2:
		day = "gestern";
		break;
		
		case 3:
		day = "vorgestern";
		break;
		
		default:
		day = "heute";
		break;	
	}
	var clack=window.open('http://www.sexgroup.tv/webs/lanotte/start/tagesgirl.php?day='+day,'tagesgirl','width=420,height=533,resizable=1,scrollbars=yes');
	if (clack) clack.focus();
	return;
}
