// bg images



////
function initBg() {
	actualImageIndexNr = 0;
	resizeBgScreen();
	new Effect.Appear('bgImage', { duration: 1.5 } );
	initAutoChange();
}

function resizeBgScreen() {
	imgResize('bgImage');
}

function initAutoChange() {		
	setTimeout("autoChange()",6000);
}	

function autoChange() {
	if (bgImageList.length > 1) {
		++actualImageIndexNr;
		if (actualImageIndexNr > bgImageList.length - 1) {
			actualImageIndexNr = 0;
		}
		changeBgImage(actualImageIndexNr);
		initAutoChange();
	}
}

function changeBgImage(imageIndexNr) {
	new Effect.Fade('bgImage', { duration: 0.5, afterFinish: function() {
		$('bgImageContainer').innerHTML = '<img id="bgImage" src="'+bgImageList[imageIndexNr]+'" alt="" style="display:none;" />';
		resizeBgScreen();
		new Effect.Appear('bgImage', { duration: 0.5 } );
	}});
}


// product images

function initProduct() {
	actualImageIndexNr = 0;
	resizeProductScreen();
	new Effect.Appear('productImage', { duration: 1.5 } );  
	imageCapt = productImageCaptList[actualImageIndexNr];
	if(imageCapt != ''){
    $('imageCaption').innerHTML = imageCapt;
    $('imageCaption').style.display = "";
	}
	setTimeout('initProductChange()',4000);
  
}

function initProductChange(){
  if(actualImageIndexNr==0){
	  changeProductImage(1);
	}	
}

function resizeProductScreen() {
	imgResize('productImage');
	Scroller.updateAll();
	$('scroll-innerBox-1').scrollTop = 0;
}

function imgResize(id) {

	var windowWidth = 800, windowHeigth = 600;
	if (document.body && document.body.offsetWidth) {
	 windowWidth = document.body.offsetWidth;
	 windowHeigth = document.body.offsetHeight;
	}
	if (document.compatMode=='CSS1Compat' &&
	    document.documentElement &&
	    document.documentElement.offsetWidth ) {
	 windowWidth = document.documentElement.offsetWidth;
	 windowHeigth = document.documentElement.offsetHeight;
	}
	if (window.innerWidth && window.innerHeight) {
	 windowWidth = window.innerWidth;
	 windowHeigth = window.innerHeight;
	}

	Element.setStyle($(id),{ width: 'auto', height: 'auto'});

	imgDimensions = $(id).getDimensions();	
	imgRatio = imgDimensions.height / imgDimensions.width;
	imgHeight = windowHeigth;
	imgWidth = Math.round(imgHeight / imgRatio);	

	// wenn quadritisch mittig platzieren, sonst formatfÃ¼llend
	if ((imgRatio < 1.05) && (imgRatio > 0.95)) {
		offsetHeight = 0 ;
		offsetWidth = (windowWidth - imgWidth) / 2;	
		if (((windowWidth - imgWidth) / 2) < 300 ) {
			offsetWidth = (windowWidth - 300 - imgWidth) / 2 + 300;
		}
	} else {
		if (imgWidth < windowWidth) {
			imgWidth = windowWidth;
			imgHeight = Math.round(imgWidth * imgRatio);
		}	
		offsetHeight = (windowHeigth - imgHeight) / 2 ;
		offsetWidth = (windowWidth - imgWidth) / 2;
	}
		
	newWidth = imgWidth+'px';
	newHeight = imgHeight+'px';
	newOffsetH = offsetHeight+'px';
	newOffsetW = offsetWidth+'px';
	Element.setStyle($(id),{
			width: newWidth,
			height: newHeight,
		top: newOffsetH,
		left: newOffsetW
	});
}
function changeProductImage(inIndex) {	
   	console.log(inIndex, actualImageIndexNr);
  if (!(actualImageIndexNr == inIndex)) {	
	fadeDuration = 0.2;
	appearDuration = 0.7;
	imageName = productImageList[inIndex];
	imageCapt = productImageCaptList[inIndex];
	$('productImage').fade({ duration: fadeDuration, queue: 'end' , afterFinish: function(){
	   $('productImage').observe("load", function(){
		   imgResize('productImage');
	  		setImageNr(inIndex+1);
		   	$('productImage').appear({duration: appearDuration, queue: 'end' });  
		   	$('productImage').stopObserving('load');

	   });	
	   $('productImage').writeAttribute('src', imageName); 	   
	   if(imageCapt != ''){
  	   	$('imageCaption').innerHTML = imageCapt;
  	   	$('imageCaption').style.display = "";
	   }else{
       $('imageCaption').style.display = "none";
     }
     } 
	}); 
   	actualImageIndexNr = inIndex;
   	return false;
  }
}


function nextProductImage() {
	if(actualImageIndexNr == productImageList.length-1) {
		imageIndexNr = 0; }
	else {
		imageIndexNr = actualImageIndexNr+1; }
	changeProductImage(imageIndexNr);
	return false;
}

function prevProductImage() {
	if(actualImageIndexNr == 0) {
		imageIndexNr = productImageList.length-1; }
	else {
		imageIndexNr = actualImageIndexNr-1; }
	changeProductImage(imageIndexNr);
	return false;
}

function setImageNr(inImageNr) {
	$('imageNrField').innerHTML = inImageNr;
}





// basic



function toggleFilter(inID) {
	var ff = $$('#content ul.subfilter.open');
	if (ff[0]) { 
		if (ff[0].id == inID) {
			ff[0].removeClassName('open'); 
			Effect.BlindUp(ff[0], { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });
		} else {
			ff[0].removeClassName('open'); 
			Effect.BlindUp(ff[0], { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });
			$(inID).addClassName('open');
			Effect.BlindDown(inID, { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });			
		}
	} else {
		$(inID).addClassName('open');
		Effect.BlindDown(inID, { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });			
	}
	setTimeout("Scroller.updateAll()", 400);
	$('scroll-innerBox-1').scrollTop = 0;
}	

function toggleContent(inID) {
	var ff = $$('#content div.open');
	if (ff[0]) { 
		if (ff[0].id == inID) {
			ff[0].removeClassName('open'); 
			Effect.BlindUp(ff[0], { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });
		} else {
			ff[0].removeClassName('open'); 
			Effect.BlindUp(ff[0], { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });
			$(inID).addClassName('open');
			Effect.BlindDown(inID, { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });			
		}
	} else {
		$(inID).addClassName('open');
		Effect.BlindDown(inID, { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });			
	}
	setTimeout("Scroller.updateAll()", 400);
	$('scroll-innerBox-1').scrollTop = 0;
}

function toggleProgress(inID) {
	var ff = $$('#content dd.open');
	if (ff[0]) { 
		if (ff[0].id == inID) {
			ff[0].removeClassName('open'); 
			Effect.BlindUp(ff[0], { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });
		} else {
			ff[0].removeClassName('open'); 
			Effect.BlindUp(ff[0], { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });
			$(inID).addClassName('open');
			Effect.BlindDown(inID, { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });			
		}
	} else {
		$(inID).addClassName('open');
		Effect.BlindDown(inID, { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });			
	}
	setTimeout("Scroller.updateAll()", 400);
	$('scroll-innerBox-1').scrollTop = 0;
}	

function toggleCountry(inID) {
	var ff = $$('#content ul.countries.open');
	if (ff[0]) { 
		if (ff[0].id == inID) {
			ff[0].removeClassName('open'); 
			Effect.BlindUp(ff[0], { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });
		} else {
			ff[0].removeClassName('open'); 
			Effect.BlindUp(ff[0], { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });
			$(inID).addClassName('open');
			Effect.BlindDown(inID, { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });			
		}
	} else {
		$(inID).addClassName('open');
		Effect.BlindDown(inID, { duration: 0.2, queue: {position: 'end', scope: 'menuxscope'} });			
	}
	setTimeout("Scroller.updateAll()", 400);
	$('scroll-innerBox-1').scrollTop = 0;
}	

function toggleDetails() {
	Effect.toggle('detailSpecs', 'blind', { duration: 0.3 });
	toggleClass('detailsLink','active');
	setTimeout("Scroller.updateAll()", 400);
	$('scroll-innerBox-1').scrollTop = 0;
}


function toggleMetnav(toggleBoxId,linkId) {
	var ll = $$('#metanav ul li.open');	
	var bb = $$('#metanav div.metacontent.active');
	
	if (ll[0]){
		if (ll[0].id == linkId) {	
			ll[0].removeClassName('open');
			if (bb[0]) {
				Effect.BlindUp(bb[0], { duration: 0.2, queue: {position: 'end', scope: 'metanav'} });
				bb[0].removeClassName('active');
			}
		} else {
			ll[0].removeClassName('open');
			$(linkId).addClassName('open');
			if (bb[0]) {
				Effect.BlindUp(bb[0], { duration: 0.2, queue: {position: 'end', scope: 'metanav'} });
				bb[0].removeClassName('active');
				Effect.BlindDown(toggleBoxId, { duration: 0.2, queue: {position: 'end', scope: 'metanav'} });
				$(toggleBoxId).addClassName('active');
			}
		}		
	} else {
		$(linkId).addClassName('open');	
		Effect.BlindDown(toggleBoxId, { duration: 0.2, queue: {position: 'end', scope: 'metanav'} });
		$(toggleBoxId).addClassName('active');
	}
}

function changeOffer(inIndex,stopTimeout) {
	    	offerIndex = inIndex;
	    	if (stopTimeout) {
	    		clearTimeout(offerTimout);
	    	}
			elementOffset = $("a"+inIndex).positionedOffset().toArray();
			new Effect.Move('container', { x: (1-elementOffset[0]), y: 0, mode: 'absolute', duration: 0.5 });	
			var ts = $$('.FRONT_NEW ul.tabs li a.selected');
			ts[0].removeClassName('selected');
			$("t"+inIndex).addClassName('selected');
		}



function toggleClass(inID,inClass) {
	if ($(inID).hasClassName(inClass)) {	
		$(inID).removeClassName(inClass);
	} else {
		$(inID).addClassName(inClass);
	}	
}

