function createXMLHttpRequest(xhttp) { 
  if ( window.ActiveXObject ) {  
    try {  
     // IE 6 and higher 
     xhttp = new ActiveXObject("MSXML2.XMLHTTP") ; 
    } 
    catch (e) { 
      try { 
        // IE 5 
        xhttp = new ActiveXObject("Microsoft.XMLHTTP") ; 
      } 
      catch (e) { 
        xhttp = false ; 
      } 
    } 
  } 
  else if (window.XMLHttpRequest) { 
    try { 
      // Mozilla, Opera, Safari ... 
      xhttp = new XMLHttpRequest() ; 
    } 
    catch (e) { 
      xhttp = false ; 
    } 
  } 
  
  return xhttp ;
}

var xhttpReference ;
var xhttpJobs ;

function ajax_getJobsContent() {

  if ( xhttpJobs.readyState == 4 ) {
    if ( xhttpJobs.status == 200 ) {
    	jsapi('#jobs_picture').html(xhttpJobs.responseText) ;

    	jsapi('.job_list_item:first-child a').addClass('sel') ;
    	
      jsapi('.job_list_item a').click(function () {
      	jsapi('.job_list_item a').removeClass('sel') ;
      	jsapi(this).addClass('sel') ;
      }) ;
    	
    }
  }
}

function ajax_getJobsDetailContent() {

  if ( xhttpJobs.readyState == 4 ) {
    if ( xhttpJobs.status == 200 ) {
    	jsapi('#job_detail').html(xhttpJobs.responseText) ;    	
    }
  }
}

function ajax_getReferenceContent() {

  if ( xhttpReference.readyState == 4 ) {
    if ( xhttpReference.status == 200 ) {
    	jsapi('#reference_picture').html(xhttpReference.responseText) ;
    }
  }
}


function getJobsContent( sURL ) {
	
  xhttpJobs = createXMLHttpRequest(xhttpJobs) ;
  xhttpJobs.open( 'GET', sURL, true ) ;
  xhttpJobs.setRequestHeader("Pragma", "no-cache");
  xhttpJobs.setRequestHeader("Cache-Control", "must-revalidate");
  xhttpJobs.setRequestHeader("If-Modified-Since", document.lastModified);

  xhttpJobs.onreadystatechange = ajax_getJobsContent ;
  xhttpJobs.send(null) ;
  
}

function getJobsDetailContent( sURL ) {
	
  xhttpJobs = createXMLHttpRequest(xhttpJobs) ;
  xhttpJobs.open( 'GET', sURL, true ) ;
  xhttpJobs.setRequestHeader("Pragma", "no-cache");
  xhttpJobs.setRequestHeader("Cache-Control", "must-revalidate");
  xhttpJobs.setRequestHeader("If-Modified-Since", document.lastModified);

  xhttpJobs.onreadystatechange = ajax_getJobsDetailContent ;
  xhttpJobs.send(null) ;
  
}

function getReferenceContent( sURL ) {
	
  xhttpReference = createXMLHttpRequest(xhttpReference) ;
  xhttpReference.open( 'GET', sURL, true ) ;
  xhttpReference.setRequestHeader("Pragma", "no-cache");
  xhttpReference.setRequestHeader("Cache-Control", "must-revalidate");
  xhttpReference.setRequestHeader("If-Modified-Since", document.lastModified);

  xhttpReference.onreadystatechange = ajax_getReferenceContent ;
  xhttpReference.send(null) ;
  
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function showJobs() {
  
  var _overlayDuration = 0.2 ;

  var _sWiBauImage       = String('themes/wibau/custom/logo_jobs.gif') ;
  var _oWiBauImage       = new Image() ;

  var _sCloseImage     = String('themes/wibau/custom/close_jobs.gif') ;
  var _oCloseImage     = new Image() ;

  var _iDivWidth = 770 ;
  var _iDivHeight = 548 ;
      
  _oWiBauImage.src = _sWiBauImage ;
  _oCloseImage.src = _sCloseImage ;
  
  IE  = document.all && ! window.opera ;
      
  var iLeft = ( ( IE ? document.body.clientWidth : window.innerWidth ) / 2 ) - ( ( _iDivWidth + 24 ) / 2 ) ;
  var iTop  = ( ( IE ? document.body.clientHeight : window.innerHeight ) / 2 ) - ( ( _iDivHeight + 24 ) / 2 ) ;   

  var oBody = document.getElementsByTagName("body").item(0);
      
  var oOverlayBG = document.createElement("div") ;
  oOverlayBG.setAttribute( 'id', 'jobs_bg' ) ;
  oOverlayBG.style.display = 'none' ;
  oOverlayBG.onclick = function() { 
  	jsapi('#jobs').hide('slow') ;   
    jsapi('#jobs_bg').hide('slow') ; 
  }
  oBody.appendChild(oOverlayBG) ;
  
  // stretch overlay to fill page and fade in
  var arrayPageSize = getPageSize();
  jsapi('#jobs_bg').width(arrayPageSize[0]);
  jsapi('#jobs_bg').height(arrayPageSize[1]);
  
  var oOverlay = document.createElement("div") ;
  oOverlay.setAttribute( 'id', 'jobs' ) ;
  oOverlay.style.display = 'none' ;
  oOverlay.onkeydown = function ( oEvent ) {
    
    var iKey = 0 ;
    
    if ( ! oEvent) {
      oEvent = window.event ;
    }
    if ( oEvent.which ) {
      iKey = oEvent.which ;
    } 
    else if ( oEvent.keyCode ) {
      iKey = oEvent.keyCode ;
    }
    
    if ( iKey == 27 ) {
	  	jsapi('#jobs').hide('slow') ;   
  	  jsapi('#jobs_bg').hide('slow') ;   
    }
  }
  oBody.appendChild(oOverlay) ;
  
  var oWiBauImg = document.createElement("img") ;
  oWiBauImg.setAttribute( 'id', 'wibau_logo' ) ;
  oWiBauImg.setAttribute( 'src', _sWiBauImage ) ;
  oOverlay.appendChild(oWiBauImg) ;
  
  var oPictureDiv = document.createElement("div") ;
  oPictureDiv.setAttribute( 'id', 'jobs_picture' ) ;
  oOverlay.appendChild(oPictureDiv) ;
  
  jsapi('#jobs').append('<div id="jobs_navi"><ul><li id="stellen"><div class="item"><a href="#" onclick="getJobsContent(\'index.php?id=10&clean=yes\') ; return false ;">Stellenangebote</a></div></li><li id="ausbildung"><div class="item"><a href="#" onclick="getJobsContent(\'index.php?id=14&clean=yes\') ; return false ;">Duales Studium</a></div></li><li id="ausbildung"><div class="item"><a href="#" onclick="getJobsContent(\'index.php?id=11&clean=yes\') ; return false ;">Ausbildung</a></div></li><li id="praktika"><div class="item"><a href="#" onclick="getJobsContent(\'index.php?id=12&clean=yes\') ; return false ;">Praktika</a></div></li></ul></div>') ;
  
  jsapi('#jobs_navi li a').click(function () {
  	jsapi('#jobs_navi li').removeClass('sel') ;
  	jsapi(this).parent().parent().addClass('sel') ;
  }) ;

  jsapi('#jobs').append('<div id="jobs_info_text">Wir sind immer auf der Suche nach engagierten und kompetenten Mitarbeitern, die unser Team bereichern. Wenn Sie Interesse an unserem Unternehmen haben, senden Sie Ihre Initiativbewerbung an: <a href="mailto:j.hauschild@wibau-haustechnik.de">j.hauschild@wibau-haustechnik.de</a>.</div>') ;

  var oCloseA = document.createElement("a") ;
  oCloseA.setAttribute( 'id', 'jobs_close' ) ;
  oCloseA.setAttribute( 'href', '#' ) ;
  oCloseA.onclick = function () {
  	
  	jsapi('#jobs').hide('slow') ;   
    jsapi('#jobs_bg').hide('slow') ;   
  
    return false ;
  }
  oOverlay.appendChild(oCloseA) ;
  var oCloseImg = document.createElement("img") ;
  oCloseImg.setAttribute( 'border', '0' ) ;
  oCloseImg.setAttribute( 'src', _sCloseImage ) ;
  oCloseA.appendChild(oCloseImg) ;
   
	jsapi('#jobs_picture').html('<div style="padding:15px">Daten werden geladen...</div>') ;
  
  getJobsContent('index.php?id=10&clean=yes') ;
  jsapi('#jobs_navi li:first-child').addClass('sel') ;

  jsapi('#jobs').css( 'top', iTop ) ;
  jsapi('#jobs').css( 'left', iLeft ) ;
  jsapi('#jobs').width( _iDivWidth + 4 ) ;
  jsapi('#jobs').height( _iDivHeight + 4 ) ;

  jsapi('#jobs_bg').fadeTo( 'slow', 0.4, function() {
		
	 	jsapi('#jobs').fadeIn('slow') ;   
  
  }) ;   
}

function showReference( iPage ) {
  
  if ( iPage == undefined ) {
    iPage = 1 ;
  }
  
  var _overlayDuration = 0.2 ;

  var _sWiBauImage       = String('themes/wibau/custom/logo_referenz.gif') ;
  var _oWiBauImage       = new Image() ;

  var _sCloseImage     = String('themes/wibau/custom/close_refernce.gif') ;
  var _oCloseImage     = new Image() ;

  var _iDivWidth = 779 ;
  var _iDivHeight = 555 ;
      
  _oWiBauImage.src = _sWiBauImage ;
  _oCloseImage.src = _sCloseImage ;
  
  IE  = document.all && ! window.opera ;
      
  var iLeft = ( ( IE ? document.body.clientWidth : window.innerWidth ) / 2 ) - ( ( _iDivWidth + 24 ) / 2 ) ;
  var iTop  = ( ( IE ? document.body.clientHeight : window.innerHeight ) / 2 ) - ( ( _iDivHeight + 24 ) / 2 ) ;   

  var oBody = document.getElementsByTagName("body").item(0);
      
  var oOverlayBG = document.createElement("div") ;
  oOverlayBG.setAttribute( 'id', 'reference_bg' ) ;
  oOverlayBG.style.display = 'none' ;
  oOverlayBG.onclick = function() { 
  	jsapi('#reference').hide('slow') ;   
    jsapi('#reference_bg').hide('slow') ; 
  }
  oBody.appendChild(oOverlayBG) ;
  
  // stretch overlay to fill page and fade in
  var arrayPageSize = getPageSize();
  jsapi('#reference_bg').width(arrayPageSize[0]);
  jsapi('#reference_bg').height(arrayPageSize[1]);
  
  var oOverlay = document.createElement("div") ;
  oOverlay.setAttribute( 'id', 'reference' ) ;
  oOverlay.style.display = 'none' ;
  oOverlay.onkeydown = function ( oEvent ) {
    
    var iKey = 0 ;
    
    if ( ! oEvent) {
      oEvent = window.event ;
    }
    if ( oEvent.which ) {
      iKey = oEvent.which ;
    } 
    else if ( oEvent.keyCode ) {
      iKey = oEvent.keyCode ;
    }
    
    if ( iKey == 27 ) {
	  	jsapi('#reference').hide('slow') ;   
  	  jsapi('#reference_bg').hide('slow') ;   
    }
  }
  oBody.appendChild(oOverlay) ;
  
  var oWiBauImg = document.createElement("img") ;
  oWiBauImg.setAttribute( 'id', 'wibau_logo' ) ;
  oWiBauImg.setAttribute( 'src', _sWiBauImage ) ;
  oOverlay.appendChild(oWiBauImg) ;
  
  var oPictureDiv = document.createElement("div") ;
  oPictureDiv.setAttribute( 'id', 'reference_picture' ) ;
  oOverlay.appendChild(oPictureDiv) ;
  
  var oCloseA = document.createElement("a") ;
  oCloseA.setAttribute( 'id', 'reference_close' ) ;
  oCloseA.setAttribute( 'href', '#' ) ;
  oCloseA.onclick = function () {
  	
  	jsapi('#reference').hide('slow') ;   
    jsapi('#reference_bg').hide('slow') ;   
  
    return false ;
  }
  oOverlay.appendChild(oCloseA) ;
  var oCloseImg = document.createElement("img") ;
  oCloseImg.setAttribute( 'border', '0' ) ;
  oCloseImg.setAttribute( 'src', _sCloseImage ) ;
  oCloseA.appendChild(oCloseImg) ;
  
  if ( iPage == 2 ) {
    getReferenceContent('index.php?id=16&clean=yes&site=1' ) ;
  }
  else {
  	getReferenceContent('index.php?id=9&clean=yes&site=1' ) ;
  }
  
	jsapi('#reference_picture').html('<div style="padding:15px">Daten werden geladen...</div>') ;
  

  jsapi('#reference').css( 'top', iTop ) ;
  jsapi('#reference').css( 'left', iLeft ) ;
  jsapi('#reference').width( _iDivWidth + 4 ) ;
  jsapi('#reference').height( _iDivHeight + 4 ) ;

  jsapi('#reference_bg').fadeTo( 'slow', 0.4, function() {
		
	 	jsapi('#reference').fadeIn('slow') ;   
  
  }) ;   
}
