//
// just another Freizeit Bastelei von Stefan Zwischenbrugger
//
function toggleX(id)
{

	$('#'+id).toggle();
	return;

	try{
		var el = document.getElementById(id);
		if (el.className=='tab_close'){
			el.className='tab_open';
		}else if (el.className=='tab_open'){
			el.className='tab_close';
		} else{
			if (el.style.display.toLowerCase()=='none'){
				if (el.tagName.toLowerCase() == 'table'){
					el.style.display='block';
				}else{
					el.style.display='';
				}
			} else {
				el.style.display='none';
			}
		}
	}catch(e){}
}
function toggle(id)
{
	$('#'+id).toggle();
	return;
	try{
		var el = document.getElementById(id);
		if (el.style.display=='none'){
			el.style.display='block';
		} else {
			el.style.display='none';
		}
	}catch(e){}
}
function toggle2(id)
{
	try{
		var el = document.getElementById(id);
		if (el.style.display=='block'){
			el.style.display='none';
		} else {
			el.style.display='block';
		}
	}catch(e){}
}
var img_grow_counter    = new Array();
var img_grow_plus_minus = new Array();

function img_grow_shrink(img,id,url){

	if (typeof window.img_grow_counter[id]=='undefined'){
		window.img_grow_counter[id]=0;
	}

	if (window.img_grow_plus_minus[id]=='-'){
		if (window.img_grow_counter[id] > 0){
			window.img_grow_counter[id]--;
			img.height--;
			img.width--;
			setTimeout(function(){img_grow_shrink(img,id,url);},150);
		}
	}else{
		if (window.img_grow_counter[id] < 25){
			window.img_grow_counter[id]++;
			img.height++;
			img.width++;
			setTimeout(function(){img_grow_shrink(img,id,url);},window.img_grow_counter[id]*3);
			if (window.img_grow_counter[id]==20){
				if (url=='undefined'){
					// nix
				}else{
					img.src=url;
				}
			}
		}
	}
}
function img_grow(img,id,url){
	//	var el = document.getElementById('preload_'+id);

	//   el.innerHTML = img.height +'x'+img.width;
	//   el.style.color='white';

	window.img_grow_plus_minus[id]='+';
	if (typeof window.img_grow_counter[id]=='undefined'){
		window.img_grow_counter[id]=0;
	}
	setTimeout(function(){img_grow_shrink(img,id,url);},10);

}
function img_shrink(img,id){

	window.img_grow_plus_minus[id]='-';

	setTimeout(function(){img_grow_shrink(img,id,'undefined');},10);
}
function switchImages(img,server,name,dim1,dim2,dim3){

	try{
		url = server+'/ztools/images/thumb/'+dim1+'_'+name;
		url1 = server+'/ztools/images/thumb/'+dim1+'_'+name;
		url2 = server+'/ztools/images/thumb/'+dim2+'_'+name;
		url3 = server+'/ztools/images/thumb/'+dim3+'_'+name;
		//		var width = img.width;
		//		var height = img.height;
		img.removeAttribute('width');
		img.removeAttribute('height');
		var width = img.naturalWidth || img.width;
		var height = img.naturalHeight || img.height;


		if (img.src==url1){
			url=url2;
			xy = dim2.split("x");
		}else{
			if (img.src==url2){
				url=url3;
				xy = dim3.split("x");
			}else{
				url=url1;
				xy = dim1.split("x");
			}
		}
		var newx = xy[0];
		var newy = xy[1];

		if ( width >= height ) {
			// landscape
			var tmpy = height*newx/width;
			if ( tmpy <= newy ){
				newy = tmpy;
			}else{
				newx = width*newy/height;
			}
		} else {
			// portrait
			tmpx = width*newy/height;
			if ( tmpx <= newx ){
				newx = tmpx;
			}else{
				newy = height*newx/width;
			}
		}
		img.width = newx;
		img.height = newy;

		img.src = url;
	}catch(e){}
}
function Set_Cookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires )
	{
		// in days
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );

	c = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );

	//	alert(document.cookie+" set_cookie "+c);

}
function doGetCaretPosition (ctrl) {

	var CaretPos = 0;
	// IE Support
	if (document.selection) {

		ctrl.focus ();
		var Sel = document.selection.createRange ();

		Sel.moveStart ('character', -ctrl.value.length);

		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
	CaretPos = ctrl.selectionStart;

	return (CaretPos);

}
function insertAtCursor(obj, text) {
	if(document.selection) {
		obj.focus();
		var orig = obj.value.replace(/\r\n/g, "\n");
		var range = document.selection.createRange();

		if(range.parentElement() != obj) {
			return false;
		}

		range.text = text;

		var actual = tmp = obj.value.replace(/\r\n/g, "\n");

		for(var diff = 0; diff < orig.length; diff++) {
			if(orig.charAt(diff) != actual.charAt(diff)) break;
		}

		for(var index = 0, start = 0;
		tmp.match(text)
		&& (tmp = tmp.replace(text, ""))
		&& index <= diff;
		index = start + text.length
		) {
			start = actual.indexOf(text, index);
		}
	} else if(obj.selectionStart) {
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;

		obj.value = obj.value.substr(0, start)
		+ text
		+ obj.value.substr(end, obj.value.length);
	}

	if(start != null) {
		setCaretTo(obj, start + text.length);
	} else {
		obj.value += text;
	}
}

function setCaretTo(obj, pos) {
	if(obj.createTextRange) {
		var range = obj.createTextRange();
		range.move('character', pos);
		range.select();
	} else if(obj.selectionStart) {
		obj.focus();
		obj.setSelectionRange(pos, pos);
	}
}
// this function gets the cookie, if it exists
function Get_Cookie( name ) {

	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// addevent
function addEvent( obj, type, fn ) {
	if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	} else
	obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
	if ( obj.detachEvent ) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	} else
	obj.removeEventListener( type, fn, false );
}
function unbox(el){
	var elb = document.getElementById('xbox');
	elb.style.display='none';
}
function box(el){

	if (el.value == ''){
		elb.style.display='none';
		return;
	}

	var e=el;
	var y=el.scrollHeight;
	var x=0;

	do {
		y+=e.offsetTop;
		x+=e.offsetLeft;

		e=e.offsetParent;
	} while(e != null);

	var elb = document.getElementById('xbox');

	elb.style.display='';
	elb.style.top = y;
	elb.style.left = x;

}