/**
 * Lightbox Hintergrund und Lightbox-DIV (ohne Inhalte) einblenden
 */ 

function display_lb(){
  // Position der Inneren Box
  var arrayPageSize = getPageSize();
  var arrayPageScroll = getPageScroll();
 
  //var lb_con_breite = 1233;
  var lb_con_breite = 820;
  var lb_con_hoehe = 770;//790;
  
  var left = (((arrayPageSize[0] - 20 - lb_con_breite) / 2) + '');
  var top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - lb_con_hoehe) / 2) + ''); // 7.5 nachträglich eingefügt
  
  // ---
  // Lightbox Hintergrund einblenden  
  styleContent = 'height:'+arrayPageSize[1]+'px;';  
  // Korrekte Transparenzeigenschaften für verschiedene Browser
  if (!navigator.appVersion.match(/MSIE/i)){
    styleContent += ' -moz-opacity:0.6; opacity:0.6;'; // FF; FF 2.0, Opera 9.0
  }
  else{
    styleContent += ' filter:alpha(opacity:60); KHTMLOpacity:0.6;'; // IE; Safari
  }
  
  var attNames = new Array('id','onclick','style');
  var attValues = new Array('lightbox','close_lb();',styleContent);

  createHTMLelement_v2('div', attNames, attValues, new Array('','body',0));
  
  //alert (top);
  
  // ---
  // Lightbox Innere Box einblenden     
  
  //alert(top);
  //alert(lb_con_hoehe+' - '+arrayPageSize[3]);
  // Korrektur der Lightbox-Position, wenn Höhe der Inhaltsbox größer ist als die Fenstergröße
  // oder, wenn berechneter Abstand kleiner 0
  // bzw. kleiner 10px 
  if ((lb_con_hoehe > arrayPageSize[3]) || (top.indexOf('-') != -1) || (top <= 10)){
    top = '10';
  }
   
  //alert (top);
  //alert(top.indexOf('-'));
          
  var attNames = new Array('id','style');
  var attValues = new Array('lightbox_content',"width:"+lb_con_breite+"px; height:"+(lb_con_hoehe)+"px; top:"+top+"px; left:"+left+"px;"); // background-position:center 700px;");
  
  createHTMLelement_v2('div', attNames, attValues, new Array('','body',0));
   
  // X zum Schliessen 
  activate_keyhandler();
}

/**
 * Lightbox - Inhalte
 * Standardinhalte in Lightbox einfügen
 * 
 * @param type - Typ der Lightbox (beeinflusst andere Parameter)
 *        
 *        type == 'produktbilder':
 *        @param param_1 - Name des Produktes
 *               param_2 - src für das große anzuzeigende Bild  
 */ 

function insert_lb_content(type, param_1, param_2, param_3){
  // Produktbilder-Lightbox
  if (type == 'produktbilder'){
    var href = param_2;
    var name = param_1;
    
    //alert(href+' - '+name);
    content = '<img style="position:absolute; right:0; cursor:pointer;" src="pics/icons/close.png" onclick="close_lb();" />';
    
    content += '<span style="line-height:650px; height:650px;';
    
    if (!navigator.appVersion.match(/MSIE/i)){
      content += '">&nbsp;';
    }
    else{
      content += ' #font-size:600px;">';
    }
    
    content += '<img id="big_pic" style="vertical-align:middle;" src="'+href+'" /></span>';
    
    //content += '<div style="width:798px; height:600px; border-bottom:1px solid #A8A8A8; margin:0 auto; display:block; background:url('+href+') no-repeat center center;">&nbsp;</div>'
    
    content += '<div style="width:818px; height:135px; position:absolute; bottom:0; left:0;">'; // border-top:1px solid #A8A8A8;
    content += '<span style="float:left; margin-left:10px; font-size:12px;">'+name+'</span>';
    
		// Neu: Druckansicht Produkt - Quelle
    content += '<span style="position:relative; z-index:101; width:100%; text-align:center;"><a style="font-size:10px;" id="druckansicht" href="'+href.replace(/large/, "hires")+'" target="_blank">Produktbild Druckansicht</a></span>';		
    content += '</div>';
        
    document.getElementById('lightbox_content').innerHTML = content;
    
    // IE Höhe der Lightbox korrigieren
    arrayPageSize = getPageSize();
		document.getElementById('lightbox').style.height = (arrayPageSize[1] + 'px');
		
		// AJAX-Request für Menü losschicken
		ajax_request('ajax_lightbox_navi.php','displayLBnavi');
  }
}

/**
 * Produktbilder-Lightbox - Inhalte 01
 * Bildernavigation zum Wechseln des Produktbildes
 * (AJAX-Aufruf in insert_lb_content)
 */ 

function displayLBnavi(){
  if (req.readyState==4){
    if (req.status==200){
       var xmldoc=req.responseXML;
       
       //document.getElementById('lightbox_content').style.backgroundImage = 'none';
       
       lbdata = xmldoc.getElementsByTagName('lightbox')[0];
       
       navi_con = '<span style="float:right; margin-right:10px; font-size:12px; line-height:17px; cursor:pointer;" onclick="close_lb();">'+get_xmltag_value('x',lbdata)+'</span>';
       
       navi_con += document.getElementById('lightbox_content').getElementsByTagName('div')[0].innerHTML;  
      
       navi_con += '<div id="lb_bildnavi">';
       
       navi_con += '<span>&nbsp;';
       // Produktbilder
       for (i=1; i<5; i++){
         if (get_xmltag_value('bild_'+i,lbdata)){
           navi_con += '<img id="bild_'+i+'" onclick="lb_picchanger(this.id, this.src);" src="'+get_xmltag_value('bild_'+i,lbdata)+'"';
           if (i == 1){
             navi_con += ' style="margin-left:0;"';
           }
           
           navi_con += ' />';
           //navi_con += '<a href="" onclick="return false;" style="background-position:center center; background-repeat:no-repeat; background-image:url('+get_xmltag_value('bild_'+i,lbdata)+');">&nbsp;</a>';
         }
       }
       
       // Verpackungsbild
       if (get_xmltag_value('bild_verpackung',lbdata)){
        // navi_con += '<li>';         
        //navi_con += '<a href="" onclick="return false;" style="display:block; cursor:pointer; height:80px; width:80px; background-position:center center; background-repeat:no-repeat; background-image:url('+get_xmltag_value('bild_verpackung',lbdata)+');">&nbsp;</a>';
        navi_con += '<img id="bild_verpackung" onclick="lb_picchanger(this.id, this.src);" src="'+get_xmltag_value('bild_verpackung',lbdata)+'" />';
        // navi_con += '</li>';
       }
         
       navi_con += '</span>';
       navi_con += '</div>'; 
       
       //alert (document.getElementById('lightbox_content').getElementsByTagName('div')[0].innerHTML);
       //alert(navi_con);
       document.getElementById('lightbox_content').getElementsByTagName('div')[0].innerHTML = navi_con;  
    }
  }
}

/**
 * Produktbilder-Lightbox - Inhalte 02
 * Bildwechsel-Funktion 
 * 
 * @param id          - ID des kleinen Bildes auf das geklickt wurde
 *        new_pic_src - src-Attribut des kleinen Bildes 
 */  

function lb_picchanger(id, new_pic_src){
  // Source für großes Bild anpassen
  src = new_pic_src.replace(/thumb/, "large");

  // Source des großen Bildes ändern
  document.getElementById('big_pic').src = src;

	// Neu: Link der Druckanischt ändern
	var ref = src.replace(/large/, "hires")
	document.getElementById('druckansicht').href = ref;
}

/**
 * Lightbox schliessen (Element komplett entfernen)
 */ 

function close_lb(){
  var lb = document.getElementById("lightbox");
  var lbc = document.getElementById("lightbox_content");
  document.getElementsByTagName("body")[0].removeChild(lbc);
  document.getElementsByTagName("body")[0].removeChild(lb);
  
  // X zum Schliessen entfernen
  deactivate_keyhandler();
  
  // events erneut zuweisen (gehen in creatHTMLelement durch innerHTML verloren)
  // andere Elemente als body haben zu geringe Breite - lightbox im IE zu klein !!
  eventLoader();
}

// -----------------------------------------------------------------------------
/**
 * Allgemeine Funktionen für das Tastaturhandling
 */ 

/**
 * Je nach dezimaler Nummer der aktuell gedrückten Taste wird event ausgelöst
 */ 
function keyhandler (eventParameter) {
  if (!eventParameter){ // im IE muss das event noch zugewiesen werden
    eventParameter = window.event;
  }
  
  var last_key = eventParameter.keyCode // im FF wird auf die eventParameter zugegriffen
  
  //alert(last_key);
  
  if (last_key == 88){ // X
    close_lb();
  }
}

/**
 * Speicherung der ursprünglichen Events
 */ 
var oldonkeyup = document.onkeyup;

/**
 * Tastaturabfragen aktivieren / deaktivieren
 */ 

function activate_keyhandler(){
  document.onkeyup = keyhandler;
}

function deactivate_keyhandler(){
  document.onkeyup = oldonkeyup;
}

// -----------------------------------------------------------------------------
/**
 * Hilfsfunktionen aus der originalen Lightbox
 */ 

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	var arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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;
	if (self.innerHeight) {	// all except Explorer
		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){
		var pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		var pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	
	var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


