var content_breite = 0;
var content_hoehe = 0;
var hoehe_center = 0;

var preloads = new Array();

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 MM_swapImgRestore() { //v3.0
  var i, x, a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){
  	document.MM_sr[j++]=x; 
	if (!x.oSrc) x.oSrc=x.src; 
	x.src=a[i+2];
  }
}

function MM_validateForm() { //v4.0
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      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+='- '+nm+' muss eine E-Mail Adresse enthalten.\n';}
          else if (val.match(/\s/)) errors+='- '+nm+' darf keine Leerzeichen enthalten.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' muss eine Zahl sein.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' muss eine Zahl zwischen '+min+' und '+max+' sein.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' ist erforderlich.\n'; }
    } 
    return errors;
}

function zentrieren() {
	var i, obj, breite, hoehe;
	
	
 	if (window.innerWidth) {
		breite = window.innerWidth;
  	} else if (document.body && document.body.offsetWidth) {
    	breite = document.body.offsetWidth;
	} else {
    	breite = 0;
	}	

	if (window.innerHeight) {
		hoehe = window.innerHeight;
  	} else if (document.body && document.body.offsetHeight) {
    	hoehe = document.body.offsetHeight;
	} else {
    	hoehe = 0;
	}
	obj = document.getElementById("center_div");


	if (obj) {
		i = breite - content_breite;
		i = i / 2;
		if (i < 0) i = 0;

		j = hoehe - content_hoehe;
		j = j / 2;
		if (j < 0) j = 0;
	
		obj.style.left = i+"px";
		if (hoehe_center) {
			obj.style.top = j+"px";
		}
	}
}

function zentrierenBreite(breite) {
	var i, obj, tmpBreite;
	
	content_breite = breite;
	
 	if (window.innerWidth) {
		tmpBreite = window.innerWidth;
  	} else if (document.body && document.body.offsetWidth) {
    	tmpBreite = document.body.offsetWidth;
	} else {
    	tmpBreite = 0;
	}	
	
	i = tmpBreite - breite;
	i = i / 2;
	if (i < 0) i = 0;
	
	/*return i;*/
	document.write('<style type="text/css">');
	document.write('#center_div { position: absolute; left: '+i+'px; }');
	document.write('</style>');
}

function zentrierenHoehe(hoehe, vertikal) {
	var i, obj, tmpHoehe;

	content_hoehe = hoehe;
	hoehe_center = vertikal;

	if (window.innerHeight) {
		hoehe = window.innerHeight;
  	} else if (document.body && document.body.offsetHeight) {
    	hoehe = document.body.offsetHeight;
	} else {
    	hoehe = 0;
	}

	j = hoehe - content_hoehe;
	j = j / 2;
	if (j < 0) j = 0;
	
	if (!vertikal) {
		j = 0;
	} 
	
	/*return j;*/
	document.write('<style type="text/css">');
	document.write('#center_div { position: absolute; top: '+j+'px; }');
	document.write('</style>');
}

function preload() {
  var d = document; 
  if (d.images){ 
  	if (!d.MM_p) d.MM_p = new Array();
    var j = d.MM_p.length;
    for (var i = 0; i < preloads.length; i++) {
		if (preloads[i].indexOf("#") != 0) {
			d.MM_p[j] = new Image; 
			d.MM_p[j++].src = preloads[i];
		}
    }
  }
}

function getSel()
{
	var txt = '';
	var foundIn = '';
	if (window.getSelection)
	{
		txt = window.getSelection();
		foundIn = 'window.getSelection()';
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
		foundIn = 'document.getSelection()';
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
		foundIn = 'document.selection.createRange()';
	}
	else return;
	document.forms[0].selectedtext.value = 'Found in: ' + foundIn + '\n' + txt;
}

function addResizeEvent(func) {
  var old = window.onresize;
  if (typeof window.onresize != 'function') {
    window.onresize = func;
  } else {
    window.onresize = function(e) {
      old(e);
      func(e);
    }
  }
}

function addScrollEvent(func) {
  var old = window.onscroll;
  if (typeof window.onscroll != 'function') {
    window.onscroll = func;
  } else {
    window.onscroll = function(e) {
      old(e);
      func(e);
    }
  }
}

function setRightContentHeight() {
	if ($('contentLeft') && $('contentRight')) {
		var h1 = $('contentLeft').offsetHeight;
		var h2 = $('contentRightHeight').offsetHeight;
		if ((h2 - 6) < h1) {
			$('contentRightHeight').style.height = h1-7+"px";
		}
	}	
}

function showKreationImage(id) {
	
	var div = document.getElementById("imageVideo"+id);
	
	if(!window.currentImage) {
		window.currentImage = document.getElementById("imageVideo"+"mainImage")
	}
	
	window.currentImage.style.display = "none"
	div.style.display                 = "block"
	window.currentImage               = div;
}

function loadBewerbungDetails () {
	var paras = document.location.hash.replace(/#/, '').split(/;/);
	var id;
	var title = 'Initiativbewerbung';
	if (paras.length) id = paras.shift();
	if (paras.length) title = paras.join(';');
	if ($('form_jobtitle')) {
  	  $('jobtitle').innerHTML = title;
	  $('form_jobtitle').value = title;
	}
	if (id && $('form_id')) $('form_id').value = id;
}

function submitBewerbung () {
	var f = $('bewerbung_form');

	var errors = MM_validateForm('Nachname','','R','Vorname','','R','Strasse','','R','PLZ/Ort','','R','Land','','R','Telefon','','R','E-Mail','','RisEmail','Anschreiben','','R','Lebenslauf','','R');

    if (errors != "") {
      alert("Folgende Fehler traten auf:\n" + errors);
    } else {
	  f.submit();
	}
}




function xScroll () {
		if (window.innerHeight && window.scrollMaxY) {	
			return window.innerWidth + window.scrollMaxX;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			return document.body.scrollWidth;
		} else { 
			return document.body.offsetWidth;
		}
}

function yScroll () {
		if (window.innerHeight && window.scrollMaxY) {	
			return window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			return document.body.scrollHeight;
		} else { 
			return document.body.offsetHeight;
		}
}

function windowWidth () {
		if (window.innerWidth) {	// all except Explorer
			if(document.documentElement && document.documentElement.clientWidth){
				return document.documentElement.clientWidth; 
			} else {
				return window.innerWidth;
			}
		} else if (document.documentElement && document.documentElement.clientWidth) {
			return document.documentElement.clientWidth;
		} else { 
			return document.body.clientWidth;
		}	
}

function windowHeight () {
		if (window.innerHeight) {	// all except Explorer
			return window.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			return document.documentElement.clientHeight;
		} else { 
			return document.body.clientHeight;
		}	
}

function showShadow() {
  var s = $('shadow');

  if (!s) {
    s = document.createElement('div');
    s.id = 'shadow';
    document.body.insertBefore(s, document.body.childNodes[0]);
  }
	
  var breite = xScroll() < windowWidth()  ? windowWidth()  : xScroll();
  var hoehe  = yScroll() < windowHeight() ? windowHeight() : yScroll();
	
  s.style.width   = breite + "px";
  s.style.height  = hoehe  + "px";
  s.style.display = 'block';
  $('coverflow').style.visibility = 'hidden';
}

function hideShadow() {
  $('coverflow').style.visibility = 'visible';
  $('shadow').style.display = 'none';
}


function show_cf_overlay (url) {
  var c = $('coverflow_layer_container');
  if (!c) {
    c = document.createElement('div');
    c.id = 'coverflow_layer_container';
    document.body.insertBefore(c, document.body.childNodes[0]);
  }
  c.innerHTML = '';

  new Ajax.Updater(c, url, {
    method: 'get',
    onComplete: function () { handle_cf_load() }
  });

  showShadow();
  c.style.display = 'block';
}

function hide_cf_overlay () {
  $('coverflow_layer_container').style.display = 'none';
  hideShadow();
}

function handle_cf_load () {
  var x;
  if (x = $('cf_movie_url')) {
    load_player(x.innerHTML);
  }
}

function load_player (url) {
  var s1 = new SWFObject("/flash/video.swf","mediaplayer","711","419","8");
  s1.addParam("allowfullscreen","true");
  s1.addVariable("width","711");
  s1.addVariable("height","419");
  s1.addVariable("file",url);
  s1.addVariable("autostart","true");
  s1.addVariable("resizing","true");
  s1.addVariable("backcolor","0xFFFFFF")
  s1.addVariable("frontcolor","0x808080");

  s1.write("player_container");
}

function cf_image_show (s, h) {
  var o = $('cf_image_' + h);
  var n = $('cf_image_' + s);
  if (o && n) {
    o.style.display = 'none';
    n.style.display = 'block';
  }
  
  var o = $('cf_image_panel_' + h);
  var n = $('cf_image_panel_' + s);
  if (o && n) {
    o.style.display = 'none';
    n.style.display = 'block';
  }
}
