
//	justLeP.net script
//	(c)2005-2006 Lennart Pegel aka LeP

if (typeof window.justlep=='undefined') window.justlep = {};

//var xx = new Image();xx.src = "/img/bg.jpg";
//var xx2 = new Image();xx2.src = "/img/bg_smaller.jpg";
var addCSS = '';
if (document.all){
	addCSS += 'p.bc{width:100%}p.paging{width:100%}';
} else {
	addCSS += '#tooltip{opacity:0.9}';
}
addCSS += 'table.galleries tr.grow td{cursor:pointer}table.galleries tr.grow:hover td{background-color:#121212}';
addCSS += 'table.gallery tr td.img:hover{background-color:#101010}';
document.write('<style type="text/css">'+ addCSS.replace(/};/g,'}') +'</style>');

// -------------------------------------------------------------------------------------------------------------------------------------

/*
	tooltip adder for gallery view
	(c)2006 lennart pegel aka LeP
*/

var tooti = null;
// window.onerror = function(){};

function addTooltips() {
	document.write(	'<div id="tooltip" style="display:none;position:absolute;border:1px solid #2a2a2a;background-color:#1d1d1d;color:#aaa;padding:3px 10px">' +
					'<div style="color:#777;border-bottom:1px solid #2a2a2a;padding-bottom:2px"><!-- title --></div><small style="color:#4c4c4c"><!-- #comments --></small></div>');
	
	tooti = document.getElementById("tooltip");
	tooti.tit = tooti.childNodes[0];
	tooti.cmt = tooti.childNodes[1];
	
	tooti.DELAY = 300;
	tooti.off = false;
	tooti.vis = false;
	tooti.td = null;
	tooti.delayed = false;
	tooti.timer = 0;
	tooti.reTT = /^(.*) \((\w+ \w+)\)$/;

	tooti.enter = function(event) {
		try {
			if (tooti.off) return;
			if (event==null) event = window.event;
			var src = (event.target)? event.target : event.srcElement;
			tooti.td = (src.tagName.toLowerCase()=='img')? src.parentNode.parentNode : src;
			if (tooti.td==null || !tooti.td.tooltip) return;
	
			tooti.timer = setTimeout('try{tooti.showThumb();}catch(e){}', tooti.DELAY);
			var cmtFontSize = '10px';
			if (!tooti.reTT.test(tooti.td.tooltip)) {
				tooti.tit.innerHTML = 'whhops, unknown title format!';
				tooti.cmt.innerHTML = '---';
			} else {
				tooti.tit.innerHTML = RegExp.$1;
				tooti.cmt.innerHTML = RegExp.$2;
				if (RegExp.$2.match(/\d+/g)) cmtFontSize = '11px';
			}
			tooti.cmt.style.fontSize = cmtFontSize;
			tooti.repos(event);
			tooti.delayed = true;
		} catch (e){}
	}

	tooti.leave = function(event) {
		try {
			if (typeof tooti=='undefined') return;
			if (event==null) event = window.event;
			var to = (event.toElement)? event.toElement : event.relatedTarget;
			if (to && to.tagName.toLowerCase()=='img') return;
			clearTimeout(tooti.timer);
			tooti.vis = false;
			tooti.td = null;
			tooti.style.display = "none";		// hide preview
			tooti.delayed = false;
		} catch (e){}
	}
	
	// called delayed by enter()
	tooti.showThumb = function() {
		try {
			if (tooti.off || tooti.td==null) return;
			tooti.style.display = "block";			// display preview frame incl. image
			tooti.vis = true;
		} catch (e){}
	}
	
	// mousemove-handler, to be assigned to document/window!
	tooti.moveThumb = function(event) {
		try {
			if ((!tooti.vis && !tooti.delayed)) return;
			if (event==null) event = window.event;
			tooti.repos(event);
		} catch (e){}
	}

	tooti.repos = function(e) {
		if (typeof tooti=='undefined') return;
		var x = (document.all)? (e.clientX + document.body.scrollLeft) : e.pageX;
		var y = (document.all)? (e.clientY + document.body.scrollTop) : e.pageY;
		try {
		tooti.style.top  = y + 18 + "px";
		tooti.style.left = x - 15 + "px";
		} catch (e){};
	}

	// prevent mouseout for images (but allow for img->!td)
	tooti.stopIt = function(event) {
		if (event==null) event = window.event;
		var to = (event.toElement)? event.toElement : event.relatedTarget;
		if (!to || to.tagName.toLowerCase()!='td') return;
		if (event.stopPropagation) event.stopPropagation(); else event.cancelBubble = true;
	}
	
	// assign events on appropriate A-Tags...
	
	if (document.all) document.body.onmousemove = tooti.moveThumb;
	 else document.onmousemove = tooti.moveThumb;

	var TDs = document.getElementById("gtab").getElementsByTagName("TD");
	var td = null;
	for (var i=TDs.length-1; i>=0; i--) {
		td = TDs[i];
		if (!td.className.match(/^img/)) continue;
		var img = td.getElementsByTagName("IMG")[0];
		td.onclick = function() {top.location.href=this.childNodes[0].href};
		td.onmouseover = tooti.enter;
		td.onmouseout = tooti.leave;
		td.tooltip = img.title.replace('<','&lt;');
		td.title = '';
		try {td.style.cursor = 'pointer';} catch (e){}
		img.onmouseout = tooti.stopIt;
		img.title = '';
	}
}


// -------------------------------------------------------------------------------------------------------------------------------------

// galleries overview: adds onclick to TDs + set cursor to pointer
function addGSHover() {
	var trs = document.getElementById("gstab").getElementsByTagName("TR");
	for (var i=trs.length-1; i>=0; i--) {
		var tr = trs[i];
		if (!tr.className || !tr.className.match(/grow/)) continue;
		tr.onclick = function() {
			top.location.href = this.getElementsByTagName("A")[0].href;
		}
		tr.title = tr.getElementsByTagName("IMG")[0].title;
	}
}

// -------------------------------------------------------------------------------------------------------------------------------------

/*
	cookie stuff
*/

function saveUserData(f) {
	setCookie("cmtName",f["name"].value,false,"/");
	setCookie("cmtMail",f.email.value,false,"/");
}

function loadUserData() {
	var f = document.getElementById("cmtform");
	var n = getCookie("cmtName");
	var e = getCookie("cmtMail");
	if (e) f.email.value = e;
	if (n) {
		f["name"].value = n;
		f.msg.focus();
	}
}

// next 2 funcs taken from http://www.stcarchiv.de/stc2002/05_javascript.php
// modifications by LeP, 30 August 2006

function setCookie (name, value, expires, path, domain, secure) { 
	document.cookie = name + "=" + escape(value) + 
	((expires) ? "; expires=" + expires : "") + 
	((path) ? "; path=" + path : "") + 
	((domain) ? "; domain=" + domain : "") + 
	((secure) ? "; secure" : ""); 
} 

function getCookie(cName) {
	var allCookies = document.cookie;
	cookieArr = allCookies.split(";");
	for (var i=0; i<cookieArr.length; i++) {
		if (cookieArr[i].split("=")[0].match(cName)) {
			return cVal = unescape(cookieArr[i].split("=")[1] || '');
		}
	}
	return false;
}

// -------------------------------------------------------------------------------------------------------------------------------------

/*
	links -> log if someone followed a link
*/

// referer logging
function addLogRefs(_all_) {
	var allLinks = (_all_ != null);
	var scope = (allLinks)? document : document.getElementById("leplinks");	// allLinks -> to be used in not Links-page
	var as = scope.getElementsByTagName("A");
	var time = encodeURIComponent('['+Date.parse(new Date()).toString(16)+'] ');
	for (var i=as.length-1; i>=0; i--) {
		if (allLinks && as[i].href.match(/justlep\.net/)) continue;
		as[i].onclick = function(e) {
			if (e==null) e = window.event;
			var a = (e.target)? e.target : e.srcElement;
			window.LLogIMG = new Image();
			var suffix = (allLinks)? encodeURIComponent(' [' + a.href.replace("http://","").replace('/','') + ']') : '';
			window.LLogIMG.src = '/refimg/' + time + encodeURIComponent(a.innerHTML) + suffix;
		}
	}
}



// -------------------------------------------------------------------------------------------------------------------------------------

/*
	more links..
*/

// for start: enables the 'mehr links anzeigen' link
function initMoreNewsLink() {
	var lis = document.getElementById("newslist").getElementsByTagName("LI");
	for (var i=lis.length-1; i>=0; i--) {
		var l = lis[i];
		if (!l.className || l.className!='showmore') continue;
		var a = l.childNodes[0];
		a.href = 'javascript:void(0)';
		a.onclick = function(){
			this.parentNode.parentNode.className = '';
			this.style.display = 'none';
			this.parentNode.childNodes[1].style.display = 'inline';
			if (typeof $ != 'undefined') setTimeout(function(){$(a).ScrollTo(400);},250);
		};
		l.style.display = 'block';
		return;
	}
}


// -------------------------------------------------------------------------------------------------------------------------------------

/*
	random image stuff
*/

var randImgFrame = null;
// to be called from start.php; creates an iframe used to update the random images
function initRandImg() {
	document.write('<iframe id="randimgif" style="display:none" src="/randomimg"></iframe>');
	randImgFrame = document.getElementById("randimgif");
}

var __updRandON = true;
// to be loaded as iframe (include/randomImage.php) -> loads 2 new random images into start page
function updRandImg() {
	if (!randImgFrame) return;
	try {
		var ifDoc = (typeof randImgFrame.contentWindow!='undefined')? randImgFrame.contentWindow.document : randImgFrame.contentDocument;
		var ifLoc = (typeof randImgFrame.contentWindow!='undefined')? randImgFrame.contentWindow.location : ifDoc;
		if (!ifDoc) return;	// IE50 fix
		var as = ifDoc.getElementsByTagName("A");
		var tds = document.getElementById("randimg").getElementsByTagName("TD");
		for (var i=0; i<2; i++) {
			if (!document.all) {
				tds[i].replaceChild(as[i].cloneNode(true), tds[i].childNodes[0]);
			} else {
				tds[i].innerHTML = as[i].outerHTML;
			}
		}
		ifLoc.reload();
	} catch (e) {
		__updRandON = false;
		SWISH_ENABLED = false;
		// alert(e);
	}
}

var SWISH_IMG = new Image(); SWISH_IMG.src = '/img/swish.jpg';
var SWISH_TAB = null,
	SWISH_ENABLED = true,
	SWISH_WIDTH = 230,				// width of the table
	SWISH_STEPS = 30,				// number of steps for the "animation"
	SWISH_TOTAL_TIME = 900,			// duration of an entire animation cycle
	SWISH_DELAY = Math.round(SWISH_TOTAL_TIME / SWISH_STEPS),
	SWISH_STEPWIDTH = Math.round((2*SWISH_WIDTH)/SWISH_STEPS);

// to be started from include/randomImage.php
// perfoms a swishing light animation of the randimg-table before the images are updated
function prepRandImg(step) {
	if (!SWISH_ENABLED) return;
	if (SWISH_TAB==null) {
		SWISH_TAB = document.getElementById("randimg").getElementsByTagName("TABLE")[0];
		SWISH_TAB.style.background = 'transparent url('+SWISH_IMG.src+') -'+SWISH_WIDTH+'px 0 no-repeat';
	}
	if (!SWISH_TAB) alert('no tab?!');
	if (step==null) step = 0;
	SWISH_TAB.style.backgroundPosition = -SWISH_WIDTH + (step * SWISH_STEPWIDTH);
	if (++step <= SWISH_STEPS) setTimeout('prepRandImg('+step+')',SWISH_DELAY);
}

// -------------------------------------------------------------------------------------------------------------------------------------

justlep.navi = {

	initGalleryNav: function() {
		if (typeof $ == 'undefined' || document.all) return;
		$( function() {
			justlep.navi.jg = $('#allGalleries');
			if (justlep.navi.jg.length) 
				justlep.navi.__initGalleryNav();
		});
	},

	ngIsHidden: 1,
	ngHide: function() {
		if (justlep.navi.ngIsHidden) return;
		justlep.navi.jg.hide();
		justlep.navi.ngIsHidden = 1;
		$(document.body).unbind(justlep.navi.ngHide);
	},

	__initGalleryNav: function() {
		var jng = $('#ng');		// bilderbuch link
			
		
		$('h5', this.jg).remove();
		this.jg.css({
			position:'absolute'
		}).bind('mouseover', function(){return false});

		$('#navi a, #content .bc a').not('#ng').bind('mouseover', justlep.navi.ngHide);
		$('#content .bc, #navi').bind('mouseover', function(){return false});

		this.jg.bind('mouseout', function(e) {
			if (e.target!==justlep.navi.jg[0]) return;
			justlep.navi.ngHide();
		});

		jng.bind('mouseover', function() {
			var offs = jng.offset();
			justlep.navi.jg.css({
				top: 20+(offs.top || 3) + 'px',
				left: (offs.left || 127) + 'px'
			}).show();
			justlep.navi.ngIsHidden = 0;
			$(document.body).bind('mouseover', justlep.navi.ngHide);
			return false;
		}).addClass('ngArr');
	}

}