var MSIE_Browser   = /MSIE/.test(navigator.userAgent) && !window.opera;
var MSIE6_Browser   = /MSIE 6/.test(navigator.userAgent) && !window.opera;
var Safari_Browser = /Safari/.test(navigator.userAgent) && !window.opera;
var Gecko_Browser = /Gecko/.test(navigator.userAgent) && !window.opera;

var height_diff = 250;
Browser_height_correction = 0;
if (MSIE_Browser) Browser_height_correction = -14;

var cur_ext = '';
var cur_lang = 'en';
if (location.pathname.match(/\.de\.html/)) {
	cur_ext='.de';
	cur_lang='de';
}

var default_interval_length = 20;
if (MSIE_Browser) default_interval_length = 30;

function inner_height() {
	if (window && (typeof(window.innerWidth)=='number') && window.innerHeight) {
		return window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
	return document.getElementById('body').offsetHeight;
}

function scrolledXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

var Zoomables = {
	'left_side': {
		'document': parent.document,
		'left_align_id': 'main',
		'left_add': 0,
		'top_align_id': 'left_content',
		'top_add': 0,
		'width_align_id': 'left_content',
		'width_add': -16,
		'height_align_id': 'left_content',
		'height_add': 0,
		'div_id': 'zoom_left_div',
		'img_id': 'zoom_left_img',
		'img_height_add': -26,
		'img_width_add': 0,
		'closer_id': 'zoom_left_close'
	},
	'right_side': {
		'document': document,
		'left_align_id': 'right_content',
		'left_add': 0,
		'top_align_id': 'right_content',
		'top_add': 0,
		'width_align_id': 'right_content',
		'width_add': 0,
		'height_align_id': 'Zusatzinfo',
		'height_add': 0,
		'div_id': 'zoom_right_div',
		'img_id': 'zoom_right_img',
		'img_height_add': -24,
		'img_width_add': 0,
		'closer_id': 'zoom_right_close'
	}
};
Zoomables['zoom_left_div']   =Zoomables.left_side;
Zoomables['zoom_left_img']   =Zoomables.left_side;
Zoomables['zoom_left_close'] =Zoomables.left_side;
Zoomables['zoom_right_div']  =Zoomables.right_side;
Zoomables['zoom_right_img']  =Zoomables.right_side;
Zoomables['zoom_right_close']=Zoomables.right_side;

function zoom_left() {zoom('left_side',arguments[0]);}
function zoom_right() {zoom('right_side',arguments[0]);}

var IDCounter=0;
var zoom_running = false;

function zoom(side) {
	if (zoom_running) return;
	if (effect.abort_signaled) return;
	zoom_running = true;
	var eventObj = arguments[1];
	if( eventObj == null ) {var eventObj = window.event}
	var obj = ( eventObj.srcElement ) ? eventObj.srcElement : eventObj.originalTarget;
	if (obj) {
		var id = obj.id;
		if (!id) {id = 'zoomable'+(IDCounter++);	obj.setAttribute('id',id);}
		Zoomables[id] = Zoomables[side];
		var ref = obj.src;
		var ref = ref.replace(/\.th\./,'.');
		var img = Zoomables[id].document.getElementById(Zoomables[id].img_id);
		var div = Zoomables[id].document.getElementById(Zoomables[id].div_id);
		Zoomables[id].img          = img;
		Zoomables[id].div          = div;
		Zoomables[id].top_align    = Zoomables[id].document.getElementById(Zoomables[id].top_align_id);
		Zoomables[id].closer       = Zoomables[id].document.getElementById(Zoomables[id].closer_id);
		Zoomables[id].left_align   = Zoomables[id].document.getElementById(Zoomables[id].left_align_id);
		Zoomables[id].width_align  = Zoomables[id].document.getElementById(Zoomables[id].width_align_id);
		Zoomables[id].height_align = Zoomables[id].document.getElementById(Zoomables[id].height_align_id);
		var nimg = new Image();
		nimg.src = ref;
		if (div.style.display!='none') {
			if ((!Safari_Browser) && nimg.complete) {
				blending_down_effect(img,{'src': ref});
			} else {
				blending_down_effect(img,{'after_effect': zoom_blend_down, 'ref': ref});
			}
		}	else {
			zoom_grow_up(div,ref,id);
		}
	} else  {alert("object not found");}
}

function MSIE6_max_dim_workaround(nimg,id) {
  if (MSIE6_Browser) {
		var img = new Image();
		img.src = nimg.src;
		var doc = Zoomables[id].document;
		var zimg = doc.getElementById(Zoomables[id].img_id);
  	var wscaling = 1;
  	var hscaling = 1;
  	var max_width = doc.getElementById(Zoomables[id].width_align_id).offsetWidth+Zoomables[id].width_add;
  	var max_height = doc.getElementById(Zoomables[id].height_align_id).offsetHeight+Zoomables[id].height_add+Zoomables[id].img_height_add;
  	if (img.width > max_width) {
  		wscaling = (max_width)/img.width;
  	}
  	if (img.height > max_height) {
  		wscaling = (max_height)/img.height;
  	}
  	var scaling = hscaling;
  	if (wscaling<hscaling) scaling=wscaling;
  	zimg.src = img.src;
		zimg.style.height = (img.height*scaling)+'px';
		zimg.style.width = (img.width*scaling)+'px';
  }
}

function zoom_blend_down(img,ref,id) {
	var nimg = new Image();
	nimg.src = ref;
	if (Safari_Browser || nimg.complete) {
		if (MSIE6_Browser) MSIE6_max_dim_workaround(nimg,id);
		if (MSIE_Browser) {
			img.style.filter = 'alpha(opacity=0)';
		} else {
			img.style.mozOpacity = 0.0;
			img.style.opacity = 0.0;
		}
		img.src = ref;
		effect.running = false;
		blending_up_effect(img);
	} else {
		effect.pre_load_img = nimg;
		effect.loading_interval = window.setInterval("when_loaded('"+img.id+"')", 100);
	}
}
function when_loaded(img_id) {
	if (effect.abort_signaled) return abort_handling();
	if (effect.pre_load_img.complete) {
		var img = Zoomables[img_id].img;
		window.clearInterval(effect.loading_interval);
		if (MSIE6_Browser) MSIE6_max_dim_workaround(effect.pre_load_img,img_id);
		if (MSIE_Browser) {
			img.style.filter = 'alpha(opacity=0)';
		} else {
			img.style.mozOpacity = 0.0;
			img.style.opacity = 0.0;
		}
		img.src = effect.pre_load_img.src;
		if (effect.abort_signaled) return abort_handling();
		effect.running = false;
		blending_up_effect(img);
	}
}
function zoom_grow_up(div,ref) {
	Zoomables[div.id].img.src = ref;
	grow_effect( div,
	 Zoomables[div.id].img,
	 Zoomables[div.id].closer,
	 abs_left(Zoomables[div.id].left_align),
	 ((Zoomables[div.id].top_align) ? abs_top(Zoomables[div.id].top_align) : Zoomables[div.id].top_add),
	 Zoomables[div.id].width_align.offsetWidth+Zoomables[div.id].width_add,
	 Zoomables[div.id].height_align.offsetHeight + Zoomables[div.id].height_add
	);
}
var effect = {
	'running':false,
	'abort_signaled':false
};
var abort_interval;
function abort_effects() {
	effect.abort_signaled=true;
	abort_interval = window.setInterval("release_semaphores()", 100);
}
function abort_handling() {
	if (effect.abort_signaled && !abort_interval) {
		if (effect.interval) {
			window.clearInterval(effect.interval);
			effect.interval=null;
		}
		abort_interval = window.setInterval("release_semaphores()", 50);
	}
}
function release_semaphores() {
	if (abort_interval) {
		window.clearInterval(abort_interval);
		abort_interval=null;
		effect.abort_signaled=false;
		effect.running=false;
		zoom_running=false;
	}
}
function blending_down_effect(img) {
	if (effect.running) return;
	effect.running = true;
	effect.img = img;
	effect.options = arguments[1];
	effect.step = 1;
	effect.max = 5;
	if (effect.options && effect.options.src && img.tagName=='IMG') effect.max = 3;
	effect.interval = window.setInterval("blending_down_effect_step()", default_interval_length);
}
function blending_down_effect_step() {
	if (effect.abort_signaled) return abort_handling();
	var step = effect.step;
	var img = effect.img;
	if (step>effect.max) {
		window.clearInterval(effect.interval);
		effect.interval = null;
		if (effect.options && effect.options.src) {
			effect.step = 3;
			img.src = effect.options.src;
			if (MSIE6_Browser) MSIE6_max_dim_workaround(img,img.getAttribute('id'));
			effect.step = 2;
			effect.interval = window.setInterval("blending_up_effect_step()", default_interval_length);
		} else if (effect.options && effect.options.after_effect) {
			effect.running = false;
			effect.options.after_effect(img,effect.options.ref);
		} else effect.running = false;
		return;
	}
	if (MSIE_Browser) {
		img.style.filter = 'alpha(opacity='+(100-100/5*step)+')';
	} else {
		img.style.mozOpacity = 1/5*(5-step);
		img.style.opacity = 1/5*(5-step);
	}
	effect.step++;
}
function blending_up_effect(img) {
	if (effect.abort_signaled) return abort_handling();
	if (effect.running) return;
	effect.running = true;
	effect.img = img;
	effect.step = 1;
	effect.interval = window.setInterval("blending_up_effect_step()", default_interval_length);
}
function blending_up_effect_step() {
	if (effect.abort_signaled) return abort_handling();
	var step = effect.step;
	if (step>5) {
		effect.running = false;
		zoom_running = false;
		window.clearInterval(effect.interval);
		effect.interval = null;
		effect.abort_signaled=false;
		return;
	}
	var img = effect.img;
	if (MSIE_Browser) {
		img.style.filter = 'alpha(opacity='+(100/5*step)+')';
	} else {
		img.style.mozOpacity = 1/5*step
		img.style.opacity = 1/5*step;
	}
	effect.step++;
}
function grow_effect(div,img,closer,left,top,width,height) {
	if (effect.abort_signaled) return abort_handling();
	if (effect.running) return;
	effect.running = true;
	effect.closer = closer;
	closer.style.display = 'none';
	div.style.border = '1px solid #CCCCCC';
	div.style.backgroundColor = '#EEEEEE';
	effect.div = div;
	effect.img = img;
	img.style.display = 'none';
	effect.left = left;
	effect.top = top;
	effect.width = width;
	effect.height = height;
	effect.step = 1;
	effect.img_completed=false;
	effect.Zoomable = Zoomables[div.id];
	effect.interval = window.setInterval("grow_effect_step()", default_interval_length);
}
function grow_effect_step() {
	if (effect.abort_signaled) return abort_handling();
	var step = effect.step;
	var div = effect.div;
	if (step==9) {
  	effect.closer.style.display = '';
  	effect.closer.innerHTML = 'LOADING';
	}
	if (step>8) {
		if ((Safari_Browser || effect.img.complete) && effect.interval)
			grow_effect_finalize();
		return;
	}
	var img = effect.img;
	
	div.style.left = (effect.left + effect.width/16*(8-step))+'px';
	div.style.top = (effect.top + effect.height/16*(8-step))+'px';
	div.style.width = (effect.width/8*step)+'px';
	div.style.height = (effect.height/8*step)+'px';
	div.style.display = '';
	effect.step++;
}
function grow_effect_finalize() {
	if (effect.abort_signaled) return abort_handling();
	var div = effect.div;
	var img = effect.img;
	div.style.display = '';
	div.style.border = '0px solid white';
	div.style.left = effect.left+'px';
	div.style.top = effect.top+'px';
	div.style.width = effect.width+'px';
	div.style.height = effect.height+'px';
	if (!MSIE6_Browser) {
  	img.style.maxHeight = (effect.height+effect.Zoomable.img_height_add)+'px';
  	img.style.maxWidth = effect.width+'px';
	}
 	effect.closer.style.display = '';
	if (cur_ext=='')
		effect.closer.innerHTML = 'Close';
	else
		effect.closer.innerHTML = 'Schließen';
	div.style.backgroundColor = 'white';
	window.clearInterval(effect.interval);
	effect.interval = null;
	effect.running = false;
	if (img.style.display=='none') {
		if (MSIE_Browser) {
			img.style.filter = 'alpha(opacity=0)';
		} else {
			img.style.mozOpacity = 0.0;
			img.style.opacity = 0.0;
		}
		if (MSIE6_Browser) MSIE6_max_dim_workaround(img,div.id);
		img.style.display = '';
		blending_up_effect(img);
	}
}
function zoom_left_out() {
	if (zoom_running) abort_effects();
	effect.abort_signaled=true;
	document.getElementById('zoom_left_div').style['display'] = 'none';
	document.getElementById('zoom_left_img').src='../img/blank.gif';
}
function zoom_right_out() {
	if (zoom_running) abort_effects();
	document.getElementById('zoom_right_div').style['display'] = 'none';
	document.getElementById('zoom_right_img').src='../img/blank.gif';
}
function make_left_zoomable(img) {
	img.onclick = zoom_left;
	if (cur_lang=='en') img.title = 'Zoom';
	else img.title = 'Vergrößern';
  delayed_load_image(img.src.replace(/\.th\./,'.'));
}
function make_right_zoomable(img) {
	img.onclick = zoom_right;
	if (cur_lang=='en') img.title = 'Zoom';
	else img.title = 'Vergrößern';
  delayed_load_image(img.src.replace(/\.th\./,'.'));
}
function zoom_right_resize() {
 var new_height = inner_height()-height_diff+Browser_height_correction;
 var div = document.getElementById('zoom_right_div');
 div.style.left = abs_left(document.getElementById('Zusatzinfo'))+'px';
 div.style.width = document.getElementById('Zusatzinfo').offsetWidth+'px';
 if (MSIE6_Browser) {
		MSIE6_max_dim_workaround(document.getElementById('zoom_right_img'),'right_side');
 } else
 document.getElementById('zoom_right_img').style.maxHeight = (new_height-22)+'px';
 div.style.height =  (new_height)+'px';
}

function zoom_left_resize() {
 var new_height = inner_height()-height_diff+Browser_height_correction;
 var div = document.getElementById('zoom_left_div');
 var mn = document.getElementById('main');
 div.style.left = abs_left(mn)+'px';
 if (MSIE6_Browser) {
		MSIE6_max_dim_workaround(document.getElementById('zoom_left_img'),'left_side');
 } else
 document.getElementById('zoom_left_img').style.maxHeight = (new_height-28)+'px';
 div.style.height =  (new_height)+'px';
}

function abs_left(e) {
	var t=0; do {t+=e.offsetLeft || 0; e=e.offsetParent;} while (e); return t;
}
function abs_top(e) {
	var t=0; do {t+=e.offsetTop || 0; e=e.offsetParent;} while (e); return t;
}
function previous_tags(el,tagName) {
	var els = [];
	var last_el = el;
	while (el) {
		while (el) {
			if (el.nodeType == 1 && el.tagName==tagName) els.push(el);
			last_el = el; el = el.previousSibling;
		}
		el = last_el.parentNode;
		last_el = el;
	}
	return els;
}


var delayed_loading_image = null;
var delayed_load_images_interval = null;
var delayed_preloaded_images = new Array();
var delayed_cached_images = new Array();

function delayed_load_image(path) {
	delayed_preloaded_images.push(path);
	if (delayed_load_images_interval==null)
		delayed_load_images_interval = window.setInterval("delayed_load_images_step()", 500);
}
function delayed_load_images_step() {
	if (zoom_running) return;
	if (delayed_preloaded_images.length==0) {
		window.clearInterval(delayed_load_images_interval);
		var doc = parent.document || document;
		doc.getElementById('zoom_left_div').style.backgroundImage = 'url(img/blank.gif)';
		doc.getElementById('zoom_right_div').style.backgroundImage = 'url(img/blank.gif)';
		return;
	}
	while (delayed_preloaded_images.length>0 && (Safari_Browser || delayed_loading_image===null || delayed_loading_image.complete)) {
		if (Safari_Browser && delayed_loading_image) {delayed_loading_image=null; return;}
		var img = new Image(); img.src = delayed_preloaded_images.shift();
		delayed_loading_image = img;
		delayed_cached_images.push(img);
		if (Safari_Browser) return;
	}
}

