
var _=function(obj){
	if(!obj) return null;
	if(document.getElementById) obj=document.getElementById(obj);
	if(obj) return obj;
	else return null;
};

document.onload=function(){
	var A=document.body.getElementsByTagName("a");
	for(var i=0; i<A.length; i++)
		if(A[i].lang=="autoload") 
			A[i].onclick();
}

$(document).ready(function(){
	openFoldsFromURL();
	openListBoxesFromURL();
});

/* Öffnet die Accordeans entsprechend der Angabe ind er URL (Hash):
Syntax: #acc[id_acc1:numm_item, id_acc2:numm_item, id_acc3:numm_item]
Beispiel: #acc[l1:2,l2:2,l3:3]
*/
function openListBoxesFromURL () {
	if (window.location.hash.indexOf('#acc[')==0) {
		var accData = window.location.hash.split('#acc[').join('').split(']').join('').split(',');
		var currentData;
		var acc;
		for (var i=0; i<accData.length; i++) {
			currentData = accData[i].split(':');
			acc = $("div#"+currentData[0]+".blocks");
			acc.find("div.item").each(function (j, n) {
				if ((j+1) == (+currentData[1])) {
					$(this).find("a[onclick]").each(function(j,k) {k.onclick()});
				}
			});
		}
	}
}

function openFoldsFromURL () {
	if (window.location.hash.indexOf('#details[')==0) {
		var foldData = window.location.hash.split('#details[').join('').split(']').join('').split(',');
		for (var i=0; i<foldData.length; i++) {
			var linkElement = document.getElementById(foldData[i]);
			if (linkElement.className == 'icon_expand') {
				linkElement.onclick();
			}
		}
	}
}

//Element styles functions
var CSS=function(obj){
	return {
		js2css:function(prop){
			return prop.replace(/([A-Z])/g,"-$1").toLowerCase();
		},
		get:function(prop){
			if(obj.currentStyle) return obj.currentStyle[prop];
			if(window.getComputedStyle) return window.getComputedStyle(obj,null).getPropertyValue(this.js2css(prop));
		}
	}
};

//simple Tabs control
var Tab=function(obj){
	return {
		act:function(tab_id){
			if(obj.parentNode.cur_tab) this.close(obj.parentNode.cur_tab);
			obj.parentNode.cur_tab=_(tab_id);
			obj.parentNode.cur_tab.sender=obj;
			this.open(obj.parentNode.cur_tab);
		},
		openclose:function(block, b){
			block.style.display=b?"block":"none";
			block.sender.className=b?"act":"";
		},
		open:function(block){this.openclose(block, 1)},
		close:function(block){this.openclose(block, 0)}
	}
};


function showhide(obj){
	var root=obj;
	while((root=root.previousSibling).nodeType!=1){}
	obj.isShow=obj.isShow?false:true;
	$(root)[obj.isShow?"show":"hide"]("fast");
	obj.className=obj.isShow?"icon_collapse":"icon_expand";
	obj.blur();
	return false;
	
}


function showhideprio(obj){
	var root=obj;
	while((root=root.previousSibling).nodeType!=1){}
	obj.isShow=obj.isShow?false:true;
	$(root)[obj.isShow?"show":"hide"]("fast");
	obj.className=obj.isShow?"icon_collapse":"icon_prioexpand";
	obj.blur();
	return false;
	
}


//simple Lists control
var List=function(obj){
	return {
		init:function(root){
			root.items=[];
			var autoHeight=(root.getAttribute("height")=="auto");
			var d=root.getElementsByTagName("div");
			var h=0;
			for(var i=0; i<d.length; i++){
				if(d[i].className=="item"){
					root.items.push(d[i]);
					d[i].num=i;
					root.items[i].minHeight=20;
					if(autoHeight){
						d[i].style.height="auto";
						root.items[i].itemHeight=d[i].offsetHeight;
						if(d[i].offsetHeight>h) h=d[i].offsetHeight;
						d[i].style.height=root.items[i].minHeight+"px";
					}
				}
			}
			d[0].style.height=h?h+"px":"auto";
			//if not autoHeight
			if(!autoHeight){
				var h=parseInt(CSS(root).get("height"))-((root.getElementsByTagName("h2").length-1)*root.items[0].minHeight);
				for(var i=0; i<root.items.length; i++){
					root.items[i].itemHeight=h;
				}
			}
		},
		act:function(){
			var root=obj.parentNode.parentNode;
			if(!root.cur_item) this.init(root);
			else this.close(root.cur_item);
			root.cur_item=obj;
			this.open(root.cur_item);
			if(root.getAttribute("myonactivate")){
				(new Function("i", root.getAttribute("myonactivate")))(obj.parentNode.num);
			}
		},
		openclose:function(obj, b){
			this.toogle(obj.parentNode, b?1:-1);
			obj.className=b?"act":"";
		},
		toogle:function(item, d){
			if(!item.curPos) item.curPos=20;
			item.speed=(item.itemHeight-item.minHeight)/5; //firstly increment value (calculate real scrollStep)
			this.play(item, d); //start scrolling
		},
		play:function(item, d){
			var _this=this;
			if(item.tm) clearTimeout(item.tm);
			item.speed=Math.round(item.speed-item.speed/6); //increment
			item.curPos+=d*item.speed;
			item.style.height=item.curPos+"px";	
			if(item.offsetHeight<=item.minHeight || item.offsetHeight>=item.itemHeight){
				item.curPos=d>0?item.itemHeight:item.minHeight;
				item.style.height=item.curPos+"px";	
				return false;
			}
			item.tm=setTimeout(function(){_this.play(item, d)},10);
		},
		open:function(block){this.openclose(block, 1)},
		close:function(block){this.openclose(block, 0)}
	}
};

function inputDefault(obj){
	if(!obj.def_value){
		obj.def_value=obj.value=obj.defaultValue=obj.getAttribute("value") || obj.value;
		obj.onblur=function(){if(!this.value) this.value=this.def_value;};
	}
	if(obj.def_value==obj.value) obj.value=obj.def_text?obj.def_text:"";
	
};

function set_border(obj) {
	//store a name of a first class if not stored
	if (!obj.first_class) {
		obj.first_class = obj.className;
	}
	
	obj.className = obj.first_class + " selected";
	return false;
}

function unset_border(obj) {
	obj.className = "";
	obj.className = obj.first_class; 
	return false;
}


function init_map(id){
	var root=_(id);
	root.items=[];
	var area=root.getElementsByTagName("area");
	root.cur=1;
	root.img=_(id+"_img");
	for (var i=0; i<area.length; i++){
		root.items.push(area[i]);
		area[i].num=i+1;
		area[i].img=root.img;
		area[i].size=parseInt(CSS(root.img).get("width"));
		area[i].onclick=function(external){
			this.img.style.backgroundPosition="-"+(this.num*this.size)+"px 0px";
			if (!external) {
				external = window.event;
			}
			if(external.constructor!=Number){
				if(root.id=="map1"){
					_("info_"+root.cur).style.display="none";
					_("info_"+this.num).style.display="block";
				}
				else{
					var itm=_("accord_map_list").items[this.num-1];
					if(itm) itm.getElementsByTagName("a")[0].onclick();
				}
			}
			root.cur=this.num;
		}
	}
}

$(document).ready(function () {
	var fields = ['passwordField', 'loginField', 'searchField'];
	for (var i=0; i<fields.length; i++) {
		$('input.'+fields[i]).each(function () {
			this.onfocus = getFocus;
			this.onblur = loseFocus;
			this.onblur();
		});
	}
});

function getFocus () {
	this.className = this.className.split('_')[0];
}
function loseFocus () {
	if (this.value=="" && this.className.indexOf('_') < 0) {
		this.className += '_empty';
	}
}
