/*global configuratorArray*/
configuratorArray = [];

function initialiseConfigurator(){
	var configuratorWrapper = $('collectionForm');
	if (configuratorWrapper)  {		
		$$('.collection-arrow-left').each(function(item){
			item.setStyle({cursor: 'pointer'});
			item.observe('click', configuratorItemsLess);
		});		
		$$('.collection-arrow-right').each(function(item){
			item.setStyle({cursor: 'pointer'});
			item.observe('click', configuratorItemsMore);
		});		
		$$('.collection-item-img').each(function(item){
			item.setStyle({cursor: 'pointer'});
			item.observe('click', selectItem);
		});		
		$$('.collection-chosen-remove').each(function(item){
			item.setStyle({cursor: 'pointer'});
			item.observe('click', removeItem);
		});
		
		$$('.collection-summary-item-image').each(function(item){
			item.setStyle({cursor: 'pointer'});
			item.observe('click', selectSummaryItem);
		});
		
		var summaryLeftArrow = $('collection-summary-items-left');
		if (summaryLeftArrow){
			summaryLeftArrow.setStyle({cursor: 'pointer'});
			summaryLeftArrow.observe('click', configuratorSummaryItemsLess);
		}
		
		var summaryRightArrow = $('collection-summary-items-right');
		if (summaryRightArrow){
			summaryRightArrow.setStyle({cursor: 'pointer'});
			summaryRightArrow.observe('click', configuratorSummaryItemsMore);
		}
		
		var summaryPreviewLeft = $('collection-summary-preview-left');
		if (summaryPreviewLeft){
			var summaryPreviewLeftSpan = summaryPreviewLeft.select('span')[0];
			if (summaryPreviewLeftSpan){
				summaryPreviewLeftSpan.setStyle({cursor: 'pointer'});
				summaryPreviewLeftSpan.observe('click', configuratorSummaryImgPrev);
			}
		}
		
		var summaryPreviewRight = $('collection-summary-action-lower');
		if (summaryPreviewRight){
			var summaryPreviewRightSpan = summaryPreviewRight.select('span')[0];
			if (summaryPreviewRightSpan){
				summaryPreviewRightSpan.setStyle({cursor: 'pointer'});
				summaryPreviewRightSpan.observe('click', configuratorSummaryImgNext);
			}
		}
		
		var summaryCta = $('collection-sa-upper-total');
		if (summaryCta){
			summaryCta.setStyle({cursor: 'pointer'});
			summaryCta.observe('click', summaryDoSubmit);
		}
	}
}
function selectItem(event)
{
	var ele = Event.element(event).up();
	var keyArr = ele.select('span.collection-hidden');
	var sourceImage = ele.select('img.collection-item-img')[0];
	var sourceName;
	var sourcePrice;
	var sourceProdcat;
	var sourceMatnr;
	var sourceImg = '';
	var sourceDims = '';
	
	keyArr.each(function(item){
		switch (item.title){
		case 'item-price':
			sourcePrice = item.lastChild.nodeValue;
			break;
		case 'item-name':
			sourceName = item.lastChild.nodeValue;
			break;
		case 'item-prodcat':
			sourceProdcat = item.lastChild.nodeValue;
			break;
		case 'item-code':
			sourceMatnr = item.lastChild.nodeValue;
			break;
		case 'item-image':
			sourceImg = item.lastChild.nodeValue;
			break;
		case 'item-dimensions':
			sourceDims = item.lastChild.nodeValue;
			break;
		}
	});
	
	try {	
		var destination = $(('collection-right|' + sourceProdcat));
		var sourceImgFix = sourceImg + '?height=135&width=185';
		var previewImg = Builder.node('img', {cssClass: 'collection-chosen-img', src: sourceImgFix, alt: sourceName});
		destination.select('input')[0].value = sourceMatnr;
		destination.select('div.collection-chosen-inner')[0].update(previewImg);
		destination.select('h3 span')[0].show();
		destination.select('div.collection-chosen-title h4')[0].lastChild.nodeValue = sourceName;
		destination.select('span.collection-chosen-price')[0].lastChild.nodeValue = sourcePrice;
		destination.select('a.collection-enlarge-img')[0].href = sourceImg;
		destination.select('a.collection-enlarge-dim')[0].href = sourceDims;	
	} catch (exception) {}
	
	calcSubtotal();
}
function removeItem(event)
{
	try {
		var destination = Event.element(event).up('.collection-right');
		var previewSpan = Builder.node('span', 'CHOOSE IF REQUIRED');
		destination.select('input')[0].value = '';
		destination.select('div.collection-chosen-inner')[0].update(previewSpan);
		destination.select('h3 span')[0].hide();
		destination.select('div.collection-chosen-title h4')[0].lastChild.nodeValue = '';
		destination.select('span.collection-chosen-price')[0].lastChild.nodeValue = '0';
		destination.select('a.collection-enlarge-img')[0].href = '';
		destination.select('a.collection-enlarge-dim')[0].href = '';
	} catch (exception) {}
	
	calcSubtotal();	
}
function configuratorItemsLess(event){
	var ele = Event.element(event).up();
	var items = ele.select('.collection-item-wrapper');
	if (items){
		var itemToHide = null;
		var itemToShow = null;
				
		items.each(function(item){
			if (!itemToHide && item.hasClassName('collection-item-hidden')){
					itemToShow = item;			
			}
			if (!item.hasClassName('collection-item-hidden')){
				itemToHide = item;
			}
		}); 
		
		if (itemToShow){
			itemToShow.removeClassName('collection-item-hidden');
			itemToHide.addClassName('collection-item-hidden');  
		}	
	}
}
function configuratorItemsMore(event){
	var ele = Event.element(event).up();
	var items = ele.select('.collection-item-wrapper');
	if (items){			
		var itemToHide = null;
		var itemToShow = null;
		
		items.each(function(item){
			if (!itemToHide && !item.hasClassName('collection-item-hidden')){
				itemToHide = item;
			}
			if (itemToHide && item.hasClassName('collection-item-hidden')){
				itemToShow = item;
				throw $break;
			}		
		});				
		if (itemToShow){
			itemToHide.addClassName('collection-item-hidden');
			itemToShow.removeClassName('collection-item-hidden');
		}
	}
}
function selectSummaryItem(event){
	var ele = Event.element(event).up();
	var newSrc = ele.select('span.collection-img-src')[0].lastChild.nodeValue;
	var newAlt = ele.select('span.collection-img-alt')[0].lastChild.nodeValue;
	var newPos = parseInt(ele.select('span.collection-img-position')[0].lastChild.nodeValue);
	newSrc += '?height=385&width=530';
	var newImg = Builder.node('img', {src: newSrc, alt: newAlt});
	$('collection-summary-preview-center').update(newImg);
	$('summaryImageCurrent').value = newPos;
}
function configuratorSummaryItemsLess(event){
	var ele = Event.element(event).up();
	var items = ele.select('.collection-summary-item');
	if (items){
		var itemToHide = null;
		var itemToShow = null;
				
		items.each(function(item){
			if (!itemToHide && item.hasClassName('collection-item-hidden')){
					itemToShow = item;			
			}
			if (!item.hasClassName('collection-item-hidden')){
				itemToHide = item;
			}
		}); 
		
		if (itemToShow){
			itemToShow.removeClassName('collection-item-hidden');
			itemToHide.addClassName('collection-item-hidden');  
		}	
	}
}
function configuratorSummaryItemsMore(event){
	var ele = Event.element(event).up();
	var items = ele.select('.collection-summary-item');
	if (items){			
		var itemToHide = null;
		var itemToShow = null;
		
		items.each(function(item){
			if (!itemToHide && !item.hasClassName('collection-item-hidden')){
				itemToHide = item;
			}
			if (itemToHide && item.hasClassName('collection-item-hidden')){
				itemToShow = item;
				throw $break;
			}		
		});				
		if (itemToShow){
			itemToHide.addClassName('collection-item-hidden');
			itemToShow.removeClassName('collection-item-hidden');
		}
	}
}
function configuratorSummaryImgPrev(event){
	var currentNode = $('summaryImageCurrent');
	var currentIdx = parseInt(currentNode.value);
	if (currentIdx > 0)
	{
		currentIdx--;
		var newSrc = $$('span.collection-img-src')[currentIdx].lastChild.nodeValue;
		var newAlt = $$('span.collection-img-alt')[currentIdx].lastChild.nodeValue;
		newSrc += '?height=385&width=530';
		var newImg = Builder.node('img', {src: newSrc, alt: newAlt});
		$('collection-summary-preview-center').update(newImg);
		currentNode.value = currentIdx;
	}
}
function configuratorSummaryImgNext(event){
	var currentNode = $('summaryImageCurrent');
	var currentIdx = parseInt(currentNode.value);
	currentIdx++;
	var srcArr = $$('span.collection-img-src');
	var altArr = $$('span.collection-img-alt');
	if (currentIdx < srcArr.size())
	{		
		var newSrc = srcArr[currentIdx].lastChild.nodeValue;
		var newAlt = altArr[currentIdx].lastChild.nodeValue;
		newSrc += '?height=385&width=530';
		var newImg = Builder.node('img', {src: newSrc, alt: newAlt});
		$('collection-summary-preview-center').update(newImg);
		currentNode.value = currentIdx;
	}
}
function summaryDoSubmit(event){
	$('collection-add-bag').click();
}
function calcSubtotal(){
	try {
		var subTot = $('coll-subtotal-val');
		var subTot1 = 0.00;
		$$('span.collection-chosen-price').each(function(item){
			subTot1 += parseFloat(item.lastChild.nodeValue);
		});
		var subTotPre = $('coll-subtotal-pre');
		if (subTotPre){subTot1 += parseFloat(subTotPre.value);}
		subTot.lastChild.nodeValue = subTot1.toFixed(2).toString();
	} catch (exception) {}
}
function getPageSize(){
	var xScroll;var yScroll;var windowWidth;var windowHeight;	
	if (window.innerHeight && window.scrollMaxY){	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	}  else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}  else{
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	if (self.innerHeight){
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		}  else{
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	}  else if (document.documentElement && document.documentElement.clientHeight){
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}  else if (document.body){
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}		
	if(yScroll < windowHeight){pageHeight = windowHeight;} 
	else{pageHeight = yScroll;}	
	if(xScroll < windowWidth){pageWidth = xScroll;}
	else{pageWidth = windowWidth;}	
	return {width:pageWidth,height:pageHeight};
}