/*
 * Modul: contextbox.js
 * Datum: 27.08.2007
 *
 * Copyright © TENSQUARE gmbh (www.tensquare.de)
 */

var elementVis = new Array();
elementVis["furtherlinks"] = false;
elementVis["snapshots"] = true;

var contextBoxVis = true;
var cookies = null;
var acceptCookies = true;
var contextBoxBodyName = 'contextbox_body';

function isArray(x) { return isObject(x) && x.constructor == Array; }
function isFunction(x) { return typeof x == 'function'; }
function isNumber(x) { return typeof x == 'number' && isFinite(x); }
function isString (x) { return typeof x == 'string' }
function isBoolean(x) { return typeof x == 'boolean'; }
function isObject(x) { return (x && typeof x == 'object') || isFunction(x); }
function isNull(x) { return typeof x == 'object' && !x; }
function isUndefined(x) { return typeof x == 'undefined'; }
function isDefined(x) { return typeof x != 'undefined'; }

function showContextBox(ForceShow)
{																									
	var i, s, img, sty, newsrc, show, box;
	box = contextBoxBodyName;
	s = (isObject(box)) ? box.id : box;
	if (!s) return; 											
	i = s.indexOf("_");
	if (i >= 0) s = s.substr(0, i);
	box = getObjectById(s + "_body");	
	img = getImageById(s + "_bt_img"); 
	if (!box || !img) return;
	sty = box.className;		
	if (sty == "show")
		show = false;
	else
		show = true;
	if (ForceShow != "")
		show = true;		
	box.className = (show) ? "show" : "hide";	
	newsrc = (show) ? "/_resources/img/arrow_up.gif" : "/_resources/img/arrow_down.gif";	
	img.src = newsrc;	
	
	if (ForceShow != "")
	{
		obj = getObjectById("tab-nav-" + ForceShow)
		obj.style.display = "block";
	}	
}

function getContextBoxStatus()
{
	var box, sty, s;
	
	box = contextBoxBodyName;	
	box = getObjectById(box);
	s = (isObject(box)) ? box.id : box;
	if (!s) 
		return false; 
	sty = box.className;
	if (sty == "show")
		return true;
	else
		return false;
}

function getObjectById(n)
{
	var o = null;
	if (document.getElementById)
		o = document.getElementById(n);
	else if (document.all)
		o = document.all[n];
	else if (document.layers)
		o = document.layers[n];
	return o;
}

function getImageById(n)
{
	var o = null;
	if (document.getElementById)
		o = document.getElementById(n);
	else if (document.all)
		o = document.all[n];
	else if (document.images)
		o = document.images[n];
	return o;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookies(force)
{	
	if (cookies && !force) return;
	cookies = new Array();
	var cl = document.cookie.split(";");
	for (var i = 0; i < cl.length; i++)
	{
		var c, j, n, v;
		c = cl[i];
		while (c.charAt(0) == " ") c = c.substring(1, c.length);
		if (c == "") continue;
		j = c.indexOf("=");
		n = (j >= 0) ? c.substring(0, j) : c;
		v = (j >= 0) ? unescape(c.substring(j + 1, c.length)) : "";
		cookies[n] = v;
	}																				
}

function readCookie(name)
{
	readCookies();
	var result = cookies[name];
	return (isUndefined(result) ? null : result);
}

function deleteCookie(name)
{
	document.cookie = name + "=; max-age=0; path=/";
}

function setContextBoxVis()
{
	var c = readCookie("contextBoxVis");
	if (c == false)
	{	
		showContextBox("");
	}	
}

function getCookiesAccepted()
{
	var c = readCookie("AcceptCookies");
	return ((c == null) || (c.toUpperCase() != "NO"));
}

function setCookiesAccepted(accept)
{
	if (accept)
		deleteCookie("AcceptCookies");
	else
		createCookie("AcceptCookies", "NO", 1095);
	readCookies(true);
}

function loadPrefs()
{	
	//readCookies();
	setContextBoxVis();
}

function savePrefs()
{
	if (!getCookiesAccepted())
		return;		
	createCookie("contextBoxVis", (getContextBoxStatus()) ? 1:0, 30);
}

function deletePrefs()
{	
	deleteCookie("contextBoxVis");
}

window.onunload = function(e)
{
	savePrefs();
}
// Ersetzt durch onDocumentReady mit JQuery in defaultdocready.js
/*
window.onload = function(f)
{
	loadPrefs();
}
*/
