// configurator
function setConfModel(inIndex, pgId, propId, thumbElement) {
	offset = inIndex * -300;
	Element.setStyle($('custom-framemask'),{ margin: offset+'px 0 0 0'});
	Element.setStyle($('custom-glasses'),{ margin: offset+'px 0 0 0'});
	$('pg'+pgId).value = propId;
	$$('a.model.tag.selected').each(function(item) { item.removeClassName('selected'); })
	thumbElement.addClassName('selected');
	$('custom-start').hide();
	showIfHidden('pgbox2');
	showIfHidden('custom-framemask-container');
	showIfHidden('custom-glasses-container');
}

function setConfColor(inIndex, pgId, propId, thumbElement) {
	offset = inIndex * -300;
	Element.setStyle($('custom-framemask-container'),{ background: 'url(/frontend/elements/conf_horn.jpg) 0 '+offset+'px no-repeat'});
	$('pg'+pgId).value = propId;
	$$('a.color.tag.selected').each(function(item) { item.removeClassName('selected'); })
	thumbElement.addClassName('selected');
	showIfHidden('pgbox3');
}


function setConfGlass(inFile,inOpacity, pgId, propId, thumbElement) {
	Element.setStyle($('custom-glasses'),{ opacity: inOpacity});
	$('custom-glasses').src = inFile;
	$('pg'+pgId).value = propId;
	$$('a.glass.tag.selected').each(function(item) { item.removeClassName('selected'); })
	thumbElement.addClassName('selected');
	showIfHidden('toCartButtom');
}

function showIfHidden(inId) {
	if (!$(inId).visible()) {
		$(inId).appear();
	}
}

// promotion images

function homeInit() {
	actualImageIndexNr = 0;
	resizeScreen();
	new Effect.Appear('promotionImage', { duration: 1.5 } );
	initAutoChange();
}

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

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

function resizeScreen() {
	homeImgResize('promotionImage');
}

function submitenter(searchfield,e){
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return true; 
  if (keycode == 13){
     searchfield.form.submit();
     return false;
  }else
     return true;
}

function homeImgResize(id) {
	windowHeigth = window.innerHeight;
	windowWidth = window.innerWidth;
	imgWidth = windowWidth - 240;
	imgHeight = Math.round(0.625 * imgWidth);
	if (imgHeight < windowHeigth) {	
		imgHeight = windowHeigth;
		imgWidth = Math.round(1.6 * imgHeight);
		offsetHeight = 0;
		offsetWidth = (windowWidth -240 -imgWidth) / 2 + 240;
	} else {
		offsetHeight = (windowHeigth -imgHeight) / 2;
		offsetWidth = 240;
	}
	$(id).setStyle({
			width: imgWidth+'px',
			height: imgHeight+'px',
		top: offsetHeight+'px',
		left: offsetWidth+'px'
	});
}

function changePromotionImage(imageIndexNr) {
	new Effect.Fade('promotionImage', { duration: 0.5, afterFinish: function()
		{
			$('promotionImageContainer').innerHTML = '<img style="display:none;" src="'+promotionImageList[imageIndexNr]+'" id="promotionImage" alt="sire promotion" />';
			homeImgResize('promotionImage');
			new Effect.Appear('promotionImage', { duration: 0.5 } );
		}} );
}

// product images


function updateProductImage(inIndex) {	
	fadeDuration = 0.2;
	appearDuration = 0.7;
	imageName = imageList[inIndex];
	
	$('productImage').fade({ duration: fadeDuration, afterFinish: function(){
	   $('productImage').observe("load", function(){
	  		setThumb(inIndex);
		   	$('productImage').appear({duration: appearDuration});  
	   });	
	   $('productImage').writeAttribute('src', ''); 
	   $('productImage').writeAttribute('src', imageName); 	   
	   $('productImage').setStyle({ height: "auto", width:"684px"});   
     } 
	}); 
   	imageListIndex = inIndex;
   	return false;
}


function setThumb(inID) {
	thumbId = "t"+inID;
	var ts = $$('#detailThumbs a.selected');
	ts[0].removeClassName('selected');
	$(thumbId).addClassName('selected');
}

function showContentImage(inArray,inIndex) {
	bigImageList = inArray;
	imageListIndex = inIndex;
	bigImageActive = 1;
	$('bigImage').setStyle({display:'none'});
	$('overlay').appear({duration: 0.3, afterFinish: function(){
		$('bigImageContainer').appear({duration: 0.3});  
 		}
	});  
	updateBigImage();
}

function hideContentImage() {
	bigImageActive = 0;
	$('bigImageContainer').fade({duration: 0.3, afterFinish: function(){
		$('overlay').fade({duration: 0.3});  
	}
	});  
}

function showBigImage() {
	bigImageActive = 1;
	$('bigImage').setStyle({display:'none'});
	$('overlay').appear({duration: 0.3, afterFinish: function(){
		$('bigImageContainer').appear({duration: 0.3});  
 		}
	});  
	updateBigImage();
}

function hideBigImage() {
	bigImageActive = 0;
	setThumb(imageListIndex);
    $('productImage').writeAttribute('src', imageList[imageListIndex]); 	   
    $('productImage').setStyle({ height: "auto", width:"684px"});   
	$('bigImageContainer').fade({duration: 0.3, afterFinish: function(){
		$('overlay').fade({duration: 0.3});  
	}
	});  
}

function updateBigImage() {	
	fadeDuration = 0.3;
	appearDuration = 0.3;
	bigImageName = bigImageList[imageListIndex];
	$('bigImage').fade({ duration: fadeDuration, afterFinish: function(){
	   $('bigImage').observe("load", function(){
		   	$('bigImage').appear({duration: appearDuration});  
		   	bigImgResize();  
	   });	
	   $('bigImage').writeAttribute('src', ''); 
	   $('bigImage').writeAttribute('src', bigImageName); 	   

     } 
	}); 
}

function resizeBigImageScreen() {
	if (bigImageActive) {
		bigImgResize();
	}
}

function bigImgResize() {
	windowHeigth = window.innerHeight;
	windowWidth = window.innerWidth;
	imgDimensions = $('bigImage').getDimensions();	
	imgRatio = imgDimensions.height / imgDimensions.width;

	imgWidth = windowWidth * 0.9;
	imgHeight = Math.round(imgWidth * imgRatio);
	
	if (imgHeight > (windowHeigth * 0.9)) {
		imgHeight = windowHeigth * 0.9;
		imgWidth = Math.round(imgHeight / imgRatio);
	}

	offsetHeight = (windowHeigth - imgHeight) / 2 ;
	offsetWidth = (windowWidth - imgWidth) / 2;
	
	$('bigImageContainer').setStyle({
		top: offsetHeight+'px',
		left: offsetWidth+'px',
		width: imgWidth+'px',
		height: imgHeight+'px'
	});
	$('bigImage').setStyle({
		width: imgWidth+'px',
		height: imgHeight+'px'
	});

}


function nextBigImage() {
	if(imageListIndex == bigImageList.length-1) {
		imageListIndex = 0; }
	else {
		imageListIndex++; }
	updateBigImage(imageListIndex);
	return false;
}

function prevBigImage() {
	if(imageListIndex == 0) {
		imageListIndex = bigImageList.length-1; }
	else {
		imageListIndex--; }
	updateBigImage(imageListIndex);
	return false;
}





/*
function preloader(imageArray) {
	alert('js > preloader active')
	var i = 0;
	imageObj = new Image();
	var l = imageArray.length - 1;
	for(i=0; i<=l; i++) {
		imageObj.src=imageArray[i];
	}
}




function nextImage() {
	if(imageListIndex == imageList.length-1) {
		imageListIndex = 0; }
	else {
		imageListIndex++; }
	updateBigImage(imageListIndex);
	return false;
}

function hideImage(inEntryID) {
	new Effect.Fade(inEntryID, {duration:.4, queue: { position: 'end', scope: inEntryID, limit:2 }});
}

function showImage(inEntryID) {
	setTimeout(function(){new Effect.Appear(inEntryID, {duration:.6, queue: { position: 'end', scope: inEntryID, limit:1 }})},700);
}
*/


/*
function hideImage(inEntryID) {
	$(inEntryID).fade({duration: 0.7, queue: { position: 'end', scope: inEntryID, limit:1 }});  
}

function showImage(inEntryID) {
	$(inEntryID).appear({duration: 0.7, queue: { position: 'end', scope: inEntryID, limit:1 }});  
}
*/

