
// Globale Variablen
var iexplore	= 0;
var ie6				= 0;
var timerwert = 0;

// Hooks
onloadHooks.push(function(){
	tx_igpageext_pi2_onload();
});

// Browser-Check
function tx_igpageext_pi2_checkBrowser(){
	var browser = navigator.userAgent.toLowerCase();
  var ganzzahl = parseInt(navigator.appVersion);
  var genauezahl = parseFloat(navigator.appVersion);
  var explorer = ((browser.indexOf("msie") != -1) && (browser.indexOf("opera") == -1));
	var explorer6 = (explorer && (ganzzahl == 4) && (browser.indexOf("msie 6.0") !=-1));
	if(explorer){
		iexplore = 1;
	}
	if(explorer6){
		ie6 = 1;
	}
}

// Load-Funktionen
function tx_igpageext_pi2_onload(){
	tx_igpageext_pi2_checkBrowser();
	
	window.addEvents({
		'click' : function(){
			reset_timer();
		},
		'keydown'	: function(Ereignis){
			if(Ereignis.key == 'esc'){
				esm_ausschalten();
			}
			reset_timer();
		},
		'scroll' : function(){
			reset_timer();
		},
		'resize' : function(){
			reset_timer();
			positionOverlays();
		}
		
	});
	
	
	$$('body').addEvents({
		'click' : function(){
			reset_timer();
		},
		'keydown'	: function(Ereignis){
			if(Ereignis.key == 'esc'){
				esm_ausschalten();
			}
			reset_timer();
		},
		'scroll' : function(){
			reset_timer();
		}
	});
	
	
	if(Cookie.read('energiesparmodus') != 'aus'){
		setTimeout('checkESM();',10000);
	}
}


function reset_timer(){
	timerwert = 0;
}


function checkESM(){
	timerwert++;
	if(timerwert > 18){
		
		if(Cookie.read('energiesparmodus') != 'aus'){
			esm_einschalten();
		}
	}else{
		setTimeout('checkESM();',10000);
	}
}


function togglerAktivieren(){
	
	var toggler = $$('.turnoffcb');
	
	for(var i = 0; i < toggler.length; i++){
		
		if(Cookie.read('energiesparmodus') == 'ein'){
			if(toggler[i].hasClass('checked')){
				toggler[i].removeClass('checked');
			}
			if(!toggler[i].hasClass('unchecked')){
				toggler[i].addClass('unchecked');
			}
		}
		
		if(Cookie.read('energiesparmodus') == 'aus'){
			if(toggler[i].hasClass('unchecked')){
				toggler[i].removeClass('unchecked');
			}
			if(!toggler[i].hasClass('checked')){
				toggler[i].addClass('checked');
			}
		}
		
		toggler[i].removeEvents('click');
		toggler[i].addEvents({
			click : function(){
				toggleEsmAktiv(this);
			}
		});
	}
	
}


function toggleEsmAktiv(elem){
	if(elem.hasClass('checked')){
		elem.removeClass('checked');
		elem.addClass('unchecked');
		Cookie.dispose('energiesparmodus');
		Cookie.write('energiesparmodus', 'ein', {
			duration: 1,
			domain	: 'reclay-group.com',
			path		: '/'
		});
	}else{
		elem.removeClass('unchecked');
		elem.addClass('checked');
		Cookie.dispose('energiesparmodus');
		Cookie.write('energiesparmodus', 'aus', {
			duration: 1,
			domain	: 'reclay-group.com',
			path		: '/'
		});
	}
}


function esm_einschalten(){
	$$('body').scrollTo(0,0);
	
	if(!$('overlay1_p') && !$('overlay2_p') && !$('overlay1') && !$('overlay2')){
		var overlay1 = new Element('div', {
			'id'					:	'overlay1',
			'styles'			:	{
				'position'		: 'absolute',
				'top'					:	'0px',
				'left'				:	'0px',
				'width'				: '1px',
				'height'			: '1px',
				//'opacity'			:	'0.7',
				'background'	: '#000000'
			}
		});
		var overlay2 = new Element('div', {
			'id'					:	'overlay2',
			'styles'			:	{
				'position'		: 'absolute',
				'top'					:	'0px',
				'left'				:	'0px',
				'width'				: '900px',
				'height'			: '500px'
			},
			'html'				:	'<div class="lightbox_outer_wrap"><div class="lightbox_top"><a class="galerie_closebtn" href="javascript:;" onclick="esm_ausschalten(); return false;">SCHLIESSEN</a></div><div class="lightbox_center" id="lightbox_center_inner_content">&nbsp;</div><div class="lightbox_bottom">&nbsp;</div></div>'
		});
		
		overlay1.inject(document.body, 'bottom');
		overlay2.inject(document.body, 'bottom');
		
		getEsmContent();
		
		positionOverlays();
		togglerAktivieren();
	}
}


function getEsmContent(){
	var esm = $('energiesparmodus_layer').getChildren()[0].clone();
	esm.inject($('lightbox_center_inner_content'), 'top');
}


function esm_ausschalten(){
	if($('overlay1') && $('overlay2')){
		$('overlay1').destroy();
		$('overlay2').destroy();
	}
	timerwert = 0;
	setTimeout('checkESM();',1000);
}



function positionOverlays(){
	
	
	if($('overlay1') && $('overlay2')){
		if (window.innerHeight){
			theHeight=window.innerHeight;
		}else if (document.documentElement && document.documentElement.clientHeight){
			theHeight=document.documentElement.clientHeight;
		}else if (document.body){
			theHeight=document.body.clientHeight;
		} 
		
		theScrollHeight = window.getScrollHeight();
				
		if (window.innerWidth){
			theWidth=window.innerWidth;
		}else if (document.documentElement && document.documentElement.clientWidth){
			theWidth=document.documentElement.clientWidth;
		}else if (document.body){
			theWidth=document.body.clientWidth;
		}
		
		if (window.pageYOffset){
			theHeightOffset=window.pageYOffset;
		}else if (document.documentElement && document.documentElement.scrollTop){
			theHeightOffset=document.documentElement.scrollTop;
		}else if (document.body.scrollTop){
			theHeightOffset=document.body.scrollTop;
		}else{
			theHeightOffset=0;
		}
		
		if(!iexplore){
			if(theScrollHeight > theHeight){
				theWidth = theWidth - 17;
			}
		}
		
		$('overlay1').setStyle('width',(theWidth-1));
		$('overlay1').setStyle('height',(theScrollHeight-1));
		
		
		if(theWidth > 900){
			ovleft = Math.floor((theWidth-900)/2);
		}else{
			ovleft = 0;
		}
		$('overlay2').setStyle('left',ovleft);
		
		ovHeight = $('overlay2').getChildren()[0].getChildren()[1].getStyle('height').toInt() + 30;
		
		if(theScrollHeight > ovHeight){
			var reltop = Math.floor((theHeight-ovHeight)/2);
			//if(reltop < 0)
			ovtop = reltop + theHeightOffset;
		}else{
			ovtop = 0;
		}
		
		if(ovtop < 50){
			ovtop = 50;
		}
		
		
		// Ganz oben positionieren:
		ovtop = 0;
		
		$('overlay2').setStyle('top',(ovtop));
		
		theScrollHeight = window.getScrollHeight();
		
		$('overlay1').setStyle('width',(theWidth-1));
		$('overlay1').setStyle('height',(theScrollHeight-1));
	}
}

/*

function positionOverlays(){
	if($('overlay1') && $('overlay2')){
		if (window.innerHeight){
			theHeight=window.innerHeight;
		}else if (document.documentElement && document.documentElement.clientHeight){
			theHeight=document.documentElement.clientHeight;
		}else if (document.body){
			theHeight=document.body.clientHeight;
		} 
		
		theScrollHeight = window.getScrollHeight();
				
		if (window.innerWidth){
			theWidth=window.innerWidth;
		}else if (document.documentElement && document.documentElement.clientWidth){
			theWidth=document.documentElement.clientWidth;
		}else if (document.body){
			theWidth=document.body.clientWidth;
		}
		
		if (window.pageYOffset){
			theHeightOffset=window.pageYOffset;
		}else if (document.documentElement && document.documentElement.scrollTop){
			theHeightOffset=document.documentElement.scrollTop;
		}else if (document.body.scrollTop){
			theHeightOffset=document.body.scrollTop;
		}else{
			theHeightOffset=0;
		}
		
		if(!iexplore){
			if(theScrollHeight > theHeight){
				theWidth = theWidth - 17;
			}
		}
		
		$('overlay1').setStyle('width',(theWidth-1));
		$('overlay1').setStyle('height',(theScrollHeight-1));
		
		
		if(theWidth > 864){
			ovleft = Math.floor((theWidth-864)/2);
		}else{
			ovleft = 0;
		}
		$('overlay2').setStyle('left',ovleft);
		
		ovHeight = $('overlay2').getChildren()[0].getChildren()[1].getStyle('height').toInt() + 30;
		
		if(theScrollHeight > ovHeight){
			var reltop = Math.floor((theHeight-ovHeight)/2);
			//if(reltop < 0)
			ovtop = reltop + theHeightOffset;
		}else{
			ovtop = 0;
		}
		
		if(ovtop < 50){
			ovtop = 50;
		}
		
		
		$('overlay2').setStyle('top',(ovtop));
		
		theScrollHeight = window.getScrollHeight();
		
		$('overlay1').setStyle('width',(theWidth-1));
		$('overlay1').setStyle('height',(theScrollHeight-1));
	}
}

*/
