//
// 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 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 xhttpGallery ;

function ajax_getGalleryContent() {

  if ( xhttpGallery.readyState == 4 ) {
    if ( xhttpGallery.status == 200 ) {
    	jsapi('#gallery_picture').html(xhttpGallery.responseText) ;
    }
  }
}


function getGalleryContent( sURL ) {
  
  xhttpGallery = createXMLHttpRequest(xhttpGallery) ;
  xhttpGallery.open( 'GET', sURL, true ) ;
  xhttpGallery.setRequestHeader("Pragma", "no-cache");
  xhttpGallery.setRequestHeader("Cache-Control", "must-revalidate");
  xhttpGallery.setRequestHeader("If-Modified-Since", document.lastModified);

  xhttpGallery.onreadystatechange = ajax_getGalleryContent ;
  xhttpGallery.send(null) ;
  
}


function showGallery( iCatId, iPage ) {
  
  if ( iPage == undefined ) {
    iPage = 1 ;
  }
  
  if ( iCatId == "undefined" || iCatId < 1 || iCatId > 8 ) {
    return false ;
  }

  var _overlayDuration = 0.2 ;

  var _sWHWImage       = String('themes/whw/custom/weinhaus-woehler.png') ;
  var _oWHWImage       = new Image() ;

  var _sCloseImage     = String('themes/whw/custom/gallery_close.png') ;
  var _oCloseImage     = new Image() ;

  if ( iCatId > 4 ) {
  	_sCloseImage = String('themes/whw/custom/gallery_close_en.png') ;
  }

  var _iDivWidth = 890 ;
  var _iDivHeight = 500 ;
      
  _oWHWImage.src = _sWHWImage ;
  _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', 'gallery_bg' ) ;
  oOverlayBG.style.display = 'none' ;
  oOverlayBG.onclick = function() { 
  	jsapi('#gallery').hide('slow') ;   
    jsapi('#gallery_bg').hide('slow') ; 
  }
  oBody.appendChild(oOverlayBG) ;
  
  // stretch overlay to fill page and fade in
  var arrayPageSize = getPageSize();
  jsapi('#gallery_bg').width(arrayPageSize[0]);
  jsapi('#gallery_bg').height(arrayPageSize[1]);
  
  var oOverlay = document.createElement("div") ;
  oOverlay.setAttribute( 'id', 'gallery' ) ;
  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('#gallery').hide('slow') ;   
  	  jsapi('#gallery_bg').hide('slow') ;   
    }
  }
  oBody.appendChild(oOverlay) ;
  
  var oWHWImg = document.createElement("img") ;
  oWHWImg.setAttribute( 'id', 'whw_logo' ) ;
  oWHWImg.setAttribute( 'src', _sWHWImage ) ;
  oOverlay.appendChild(oWHWImg) ;
  
  var oPictureDiv = document.createElement("div") ;
  oPictureDiv.setAttribute( 'id', 'gallery_picture' ) ;
  oOverlay.appendChild(oPictureDiv) ;
  
  var oCloseA = document.createElement("a") ;
  oCloseA.setAttribute( 'id', 'gallery_close' ) ;
  oCloseA.setAttribute( 'href', '#' ) ;
  oCloseA.onclick = function () {
  	
  	jsapi('#gallery').hide('slow') ;   
    jsapi('#gallery_bg').hide('slow') ;   
  
    return false ;
  }
  oOverlay.appendChild(oCloseA) ;
  var oCloseImg = document.createElement("img") ;
  oCloseImg.setAttribute( 'border', '0' ) ;
  oCloseImg.setAttribute( 'src', _sCloseImage ) ;
  oCloseA.appendChild(oCloseImg) ;
  
  
  var oNavUL = document.createElement("ul") ;
  oNavUL.setAttribute( 'id', 'whw_nav' ) ;
  oOverlay.appendChild(oNavUL) ;
  
  var oHotelLI = document.createElement("li") ;
  oNavUL.appendChild(oHotelLI) ;
  var oRestaurantLI = document.createElement("li") ;
  oNavUL.appendChild(oRestaurantLI) ;
  var oWeinkontorLI = document.createElement("li") ;
  oNavUL.appendChild(oWeinkontorLI) ;
  var oBarLI = document.createElement("li") ;
  oNavUL.appendChild(oBarLI) ;
  
  var oHotelA = document.createElement("a") ;
  oHotelA.setAttribute( 'href', '#hotel' ) ;
  oHotelA.innerHTML = 'Hotel' ;
  oHotelA.onclick = function () { 
    oOverlay.style.background = '#004764' ;
    oHotelLI.style.background = '#4d7a8f' ;
    oRestaurantLI.style.background = '#30677e' ;
    oWeinkontorLI.style.background = oRestaurantLI.style.background ;
    oBarLI.style.background        = oRestaurantLI.style.background ;
    if ( iCatId < 5 ) {
    	getGalleryContent('index.php?id=58&clean=yes&site=' + iPage ) ;
    }
    else {
    	getGalleryContent('index.php?id=100&clean=yes&site=' + iPage ) ;
    }
    return false ;
  }
  oHotelLI.appendChild(oHotelA) ;
  
  var oRestaurantA = document.createElement("a") ;
  oRestaurantA.setAttribute( 'href', '#restaurant' ) ;
  oRestaurantA.innerHTML = 'Restaurant' ;
  oRestaurantA.onclick = function () {
    oOverlay.style.background = '#d37c0e' ;
    oRestaurantLI.style.background = '#e1a058' ;
    oHotelLI.style.background = '#dd9340' ;
    oWeinkontorLI.style.background = oHotelLI.style.background ;
    oBarLI.style.background        = oHotelLI.style.background ;
    if ( iCatId < 5 ) {
	    getGalleryContent('index.php?id=59&clean=yes&site=' + iPage ) ;
    }
    else {
    	getGalleryContent('index.php?id=101&clean=yes&site=' + iPage ) ;
    }
    return false ;
  }
  oRestaurantLI.appendChild(oRestaurantA) ;
  
  var oWeinkontorA = document.createElement("a") ;
  oWeinkontorA.setAttribute( 'href', '#Weinkontor' ) ;
  oWeinkontorA.innerHTML = 'Weinkontor' ;
  if ( iCatId > 4 ) {
	  oWeinkontorA.innerHTML = 'Wine Bar' ;
  }
  oWeinkontorA.onclick = function () {
    oOverlay.style.background = '#741417' ;
    oWeinkontorLI.style.background = '#9f575a' ;
    oRestaurantLI.style.background = '#8f4041' ;
    oHotelLI.style.background = oRestaurantLI.style.background ;
    oBarLI.style.background   = oRestaurantLI.style.background ;
    if ( iCatId < 5 ) {
	    getGalleryContent('index.php?id=60&clean=yes&site=' + iPage ) ;
    }
    else {
    	getGalleryContent('index.php?id=102&clean=yes&site=' + iPage ) ;
    }
    return false ;
  }
  oWeinkontorLI.appendChild(oWeinkontorA) ;
  
  var oBarA = document.createElement("a") ;
  oBarA.setAttribute( 'href', '#Bar' ) ;
  oBarA.innerHTML = 'Tapas-Bar' ;
  oBarA.onclick = function () {
    oOverlay.style.background = '#9f9b1a' ;
    oBarLI.style.background = '#b9b662' ;
    oRestaurantLI.style.background = '#afac4a' ;
    oHotelLI.style.background      = oRestaurantLI.style.background ;
    oWeinkontorLI.style.background = oRestaurantLI.style.background ;
    if ( iCatId < 5 ) {
	    getGalleryContent('index.php?id=61&clean=yes&site=' + iPage ) ;
    }
    else {
    	getGalleryContent('index.php?id=103&clean=yes&site=' + iPage ) ;
    }
    return false ;
  }
  oBarLI.appendChild(oBarA) ;

  if ( iCatId == 1 || iCatId == 5 ) {
    oOverlay.style.background = '#004764' ;
    oHotelLI.style.background = '#4d7a8f' ;
    oRestaurantLI.style.background = '#30677e' ;
    oWeinkontorLI.style.background = oRestaurantLI.style.background ;
    oBarLI.style.background        = oRestaurantLI.style.background ;
    if ( iCatId < 5 ) {
	    getGalleryContent('index.php?id=58&clean=yes&site=' + iPage ) ;
    }
    else {
    	getGalleryContent('index.php?id=100&clean=yes&site=' + iPage ) ;
    }
  }
  else if ( iCatId == 2 || iCatId == 6 ) {
    oOverlay.style.background = '#d37c0e' ;
    oRestaurantLI.style.background = '#e1a058' ;
    oHotelLI.style.background = '#dd9340' ;
    oWeinkontorLI.style.background = oHotelLI.style.background ;
    oBarLI.style.background        = oHotelLI.style.background ;
    if ( iCatId < 5 ) {
	    getGalleryContent('index.php?id=59&clean=yes&site=' + iPage ) ;
    }
    else {
    	getGalleryContent('index.php?id=101&clean=yes&site=' + iPage ) ;
    }
  }
  else if ( iCatId == 3 || iCatId == 7 ) {
    oOverlay.style.background = '#741417' ;
    oWeinkontorLI.style.background = '#9f575a' ;
    oRestaurantLI.style.background = '#8f4041' ;
    oHotelLI.style.background = oRestaurantLI.style.background ;
    oBarLI.style.background   = oRestaurantLI.style.background ;
    if ( iCatId < 5 ) {
	    getGalleryContent('index.php?id=60&clean=yes&site=' + iPage ) ;
    }
    else {
    	getGalleryContent('index.php?id=102&clean=yes&site=' + iPage ) ;
    }
  }
  else if ( iCatId == 4 || iCatId == 8 ) {
    oOverlay.style.background = '#9f9b1a' ;
    oBarLI.style.background = '#b9b662' ;
    oRestaurantLI.style.background = '#afac4a' ;
    oHotelLI.style.background      = oRestaurantLI.style.background ;
    oWeinkontorLI.style.background = oRestaurantLI.style.background ;
    if ( iCatId < 5 ) {
	    getGalleryContent('index.php?id=61&clean=yes&site=' + iPage ) ;
    }
    else {
    	getGalleryContent('index.php?id=103&clean=yes&site=' + iPage ) ;
    }
  }
  
  if ( iCatId < 5 ) {
		jsapi('#gallery_picture').html('<div style="padding:15px">Daten werden geladen...</div>') ;
  }
  else {
		jsapi('#gallery_picture').html('<div style="padding:15px">Loading datas...</div>') ;
  }
  

  jsapi('#gallery').css( 'top', iTop ) ;
  jsapi('#gallery').css( 'left', iLeft ) ;
  jsapi('#gallery').width( _iDivWidth + 4 ) ;
  jsapi('#gallery').height( _iDivHeight + 4 ) ;

  jsapi('#gallery_bg').fadeTo( 'slow', 0.4, function() {
		
	 	jsapi('#gallery').fadeIn('slow') ;   
  
  }) ;   
}
