// JavaScript Document
d=document;

$(function(){
	var curSize = readCookie('currentSize');
	var curCtr  = readCookie('currentContrast');
	if(curSize){
	resize(curSize);
	}
	if((curCtr==0 || curCtr==-1)&& curCtr!=''){
		contrast(curCtr);
	}
	var curPan  = readCookie('sb2_panel');
	if(!curPan){curPan=0;}
	
	// jscript Befehle in die Controls einbinden
	$("#ctlContrast1").attr('href','javascript:contrast('+hsSelect+')');
	$("#ctlContrast0").attr('href','javascript:contrast(0)');
	$("#ctlContrast_1").attr('href','javascript:contrast(-1)');
	//*
	$('#ctlResize0').attr('href','javascript:resize(\'-\')');
	$('#ctlResize1').attr('href',"javascript:resize(1)");
	$('#ctlResize2').attr('href',"javascript:resize('+')");
	
	$(".hasSubMenu").attr({'href':'javascript:;'}).addClass('MenuBarItemSubmenu');
	// Schnick Schnack implementieren
	$('#mobileMainNav').attr('href','javascript:toggleMenuMobile()');
	$('#content').hide().show();//fadeIn('slow',function(){do_tb();});
	
	var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgRight:"/inc/SpryAssets/SpryMenuBarRightHover.gif"});
	var MenuBarMobil = new Spry.Widget.MenuBar("MenuBarMobil", {imgRight:"/inc/SpryAssets/SpryMenuBarRightHover.gif"});
	var Accordion1 = new Spry.Widget.Accordion("sidebar2",{useFixedPanelHeights:false,defaultPanel:curPan});
	// Links jscript tauglich machen
	
	
});	
window.onunload =function (){
	try{
	var panelNumber = Accordion1.getCurrentPanelIndex();
	} catch(e){
	var panelNumber=0;
	}
	
	writeCookie('sb2_panel',panelNumber,null);
}
function toggleMenuMobile(){
	$('#MenuBarMobil').toggle();
	$('#overlay').remove();
	$('body').append("<div id='overlay'>&nbsp;</div>");
	$('#overlay').width($('#container').width()).click(function(){$('#MenuBarMobil').hide();$(this).remove()})
}
function validateForm() { //v4.0
// Eingabe Format : 'ObjName','','R','Bez'
  var i,p,q,nm,test,num,min,max,bez,errors='',args=validateForm.arguments;
  for (i=0; i<(args.length-2); i+=4) { 
  	test=args[i+2];
	bez=args[i+3]; 
	val=MM_findObj(args[i]);
	objErr=false;
    if (val) { 
		nm=val.name; 
		if ((val=val.value)!="") {
      		if (test.indexOf('isEmail')!=-1) { 
				p=val.indexOf('@');
        		if (p<1 || p==(val.length-1)) {
					errors+='Das Feld '+bez+' muss eine eMail adresse sein.\n';
					objErr=true;
				}
      		} else if (test!='R') { 
				num = parseFloat(val);
        		if (isNaN(val)) {
					errors+='Das Feld '+bez+' muss eine Zahl sein.\n';
					objErr=true;
				}
        		if (test.indexOf('inRange') != -1) { 
					p=test.indexOf(':');
          			min=test.substring(8,p); 
					max=test.substring(p+1);
          			if (num<min || max<num) {
						errors+='Das Feld '+bez+' muss eine Zahl zwischen '+min+' und '+max+' sein.\n';
						objErr=true;
					}
    			} 
			} 
		} else if (test.charAt(0) == 'R') {
			errors += 'Das Feld '+bez+' darf nicht leer sein.\n';objErr=true;
		} 
	}
  doBorder(document.getElementById(nm),objErr);
  } 
  return errors;
}

function doBorder(obj,bdStat){
	
	try{obj.style.border = bdStat==true ? "dashed 0.1em red" : "solid 0.1em #00FF00";}catch(e){}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}
// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function resize(step){
	//if(rsBrowser!=1){ // wenn es sich um einen Brwoser handelt, der keinen vernüftigen Zoom kann (<=IE6 )
	var curFs =parseFloat(document.body.style.fontSize,10);
	var oldFs = curFs;
	switch(step){
	case '+':
		curFs = Math.min(curFs+0.1,6);
	break;
	case '-':
		curFs = Math.max(0.70,curFs-0.1);
	break;
	default:
		curFs = step;
	break;
	}
	var zoomFaktor = parseFloat(curFs/oldFs);
	
	document.body.style.fontSize=curFs+'em';
	document.getElementById('curSize').innerHTML='aktueller&nbsp;Wert:&nbsp;'+parseInt(curFs*100,10)+'%';
	if(!$.browser.msie){
	$('img').each(function(){
		var iW=$(this).width()
		var iH=$(this).height();
		var nW=parseInt(iW*zoomFaktor,10);
		var nH = parseInt(zoomFaktor*iH,10);
		$(this).width(nW).height(nH);
	});
	} else {
	var imgs=document.getElementById('mainContent').getElementsByTagName('img');
	for(i=0;i<imgs.length;i++){
		var iW=imgs[i].getAttribute('width');
		var iH =imgs[i].getAttribute('height');
		var nW=parseInt(iW*zoomFaktor,10);
		var nH = parseInt(zoomFaktor*iH,10);
		imgs[i].setAttribute('width',nW);
		imgs[i].setAttribute('height',nH);
	}
	
	}
	writeCookie('currentSize',curFs,9999);
	//} // END IF rsBrowser
}

function contrast(val){
	
	switch(val){
		case 0:
		str='kontrast';
		break;
		case -1:
		str='text';
		break;
		default:
		str='standard';
		break;
	}
	
	cls = val == 0 ? 'headerLogo invers'+hsSelect : 'headerLogo';
	writeCookie('currentContrast',val,99999);
	
	
	if(val==-1){
		// wenn farblos alle css links unbrauchbar machen
		document.getElementById('cssColors').href= '/inc/css/textonly.css';
		document.getElementById('cssMain').href= '/inc/css/textonly.css';
		document.getElementById('cssMenu').href= '/inc/css/textonly.css';
		document.getElementById('cssSidebar').href= '/inc/css/textonly.css';
		// Das SpryMenü unbrauchbar machen
		
		imgs = document.getElementsByTagName('img');
		for(i=0;i<imgs.length;i++){
			span=document.createElement('span');
			imgs[i].style.display='none';
			spanText=document.createTextNode('Bildbeschreibung: '+imgs[i].getAttribute('alt'));
			span.appendChild(spanText);
			span.setAttribute('class','altPic');
			imgs[i].parentNode.appendChild(span);
		}
	} else {
		// wenn normal oder kontrast=hoch
		document.getElementById('cssColors').href= '/inc/css/'+val+'.css';
		document.getElementById('cssMain').href= '/inc/css/main.css';
		document.getElementById('cssMenu').href= '/inc/SpryAssets/SpryMenuBarVertical.css';
		document.getElementById('cssSidebar').href= '/inc/SpryAssets/SpryAccordion.css';

		imgs = document.getElementsByTagName('img');
		for(i=0;i<imgs.length;i++){
			imgs[i].style.display='block';
		}
		objs=document.getElementsByTagName('*');
		
		for(i=0;i<objs.length;i++){
			if(objs[i].getAttribute('class')=="altPic"){
				objs[i].parentNode.removeChild(objs[i]);
			}
		}
	}
	if(isIE){
		document.getElementById('logo').setAttribute('className',cls);
	} else {
		document.getElementById('logo').setAttribute('class',cls);
	}
	document.getElementById('curCtr').innerHTML = 'aktueller&nbsp;Wert:&nbsp;'+str;
}


function openWindow(theURL,winName,cx,cy,w,h) { 
  var args = openWindow.arguments;
  var feat = args[6]!='' ? args[6] : 'left=9999, top=9999';
  ref=open("",winName,feat);
  switch(cx){
	case 'center':
		cx = (screen.availWidth-w)/2;
	break;
	case 'mouse':
		cx =window.event.screenX;
	break;
	default:
	
  }
  switch(cy){
	case 'center':
		cy=(screen.availHeight-h)/2 ;
	break;
	case 'mouse':
		cy =window.event.screenY;
	break;
	default:
	  
  }
  if(ref.closed !=true){ref.close();}
  features="width="+w+",height="+h+",left="+cx+",top="+cy+','+feat;
  win=window.open(theURL,winName,features);
  if(win){win.focus();}else{alert('Falls Sie einen Popup Blocker verwenden, sollten Sie diesen für '+window.location.host+' abschalten');}
  return false;
}

//Datei FindeFlash.js

function getFlash (flash) {
	if (document.all) {
	  if (document.all[flash]) {
		return document.all[flash];
	  }
	  if (window.opera) {
		var movie = eval(window.document + flash);
		if (movie.SetVariable) {
		  return movie;
		}
	  }
	  return;
	}
	if(document.layers) {
	  if(document.embeds) {
		var movie = document.embeds[flash];
		if (movie.SetVariable) {
		  return movie;
		}
	  }
	  return;
	}
	if (!document.getElementById) {
	  return;
	}
	var movie = document.getElementById(flash);
	if (movie.SetVariable) {
	  return movie;
	}
	var movies = movie.getElementsByTagName('embed');
	if (!movies || !movies.length) {
	  return;
	}
	movie = movies[0];
	if (movie.SetVariable) {
	  return movie;
	}
	return;
}/*END*/
