if (!this.PLINE) {
	PLINE = {};
	
	PLINE.pl = null;
	
	PLINE.render = function(pl, onProductSelectFunc, infoImageNodeId) {
		//Get available combinations
		var combinations = JSON.parse(pl);
		
		//transfer values
		PLINE.pl = new ProductLine();
		PLINE.pl.setProperties(combinations);
		
		PLINE.pl.renderAll();
		
		if (onProductSelectFunc != null) {
			PLINE.pl.onProductSelectFunc = onProductSelectFunc;
			PLINE.pl.onProductSelectFunc();
		}
		
		if (infoImageNodeId != null && PLINE.pl.groupPrice.range) {
			//Create info image node toolip, so price help can be displayed
		    var placeHolder = document.createElement("div");
		    var tt = new dijit.Tooltip({label:PLINE.pl.groupPrice.tooltiptext, connectId:[infoImageNodeId], position: ['below','above']}, placeHolder);

		}
	};
	
	PLINE.renderMiniBrowser = function(outerContainerNodeId, containerNodeId, noImageTextNode, productWidth, scrollBarProps) {
		var noImageElment = dojo.byId(noImageTextNode);
		var noImageText;
		if (typeof(noImageElement) == 'undefined')
			noImageText = 'No image available';
		else
			noImageText = noImageElement.innerHTML;
		if (PLINE.pl != null)
			PLINE.pl.renderMiniBrowser(outerContainerNodeId, containerNodeId, noImageText, productWidth, scrollBarProps);
	};
	
}

function ProductLine() {
	this.selectionobjects = null;
	
	this.selectedPrice = null;
	this.selectedProduct = null;
	
	this.onProductSelectFunc = null;
	
	this.availableCombinations = null;
	
	this.groupPrice = null;
	
	this.setProperties = function(c) {
		this.selectionobjects = c.selectionids;
		this.availableCombinations = c.combinations;
		this.groupPrice = new Object();
		this.groupPrice.price = c.groupprice.price;
		if (c.groupprice.range == 'true') {
			this.groupPrice.range = true;
			this.groupPrice.tooltiptext = c.groupprice.tooltiptext;
		} else {
			this.groupPrice.range = false;
			this.groupPrice.tooltiptext = null;
		}
	}
	
	this.renderAll = function() {
		//Check if combos exist already?
		if (this.selectionobjects != null) {
			for (var soIdx = 0; soIdx<this.selectionobjects.length; soIdx++) {
				//check if dom element exists?
				var cbElement = dojo.byId(this.selectionobjects[soIdx].id);
				if (cbElement == null) {
					//Create it
					//Get containerNode
					var containerNode = dojo.byId(this.selectionobjects[soIdx].id+'Container');
					if (containerNode != null) {
						cbElement = document.createElement('select');
						cbElement.setAttribute('id', this.selectionobjects[soIdx].id);
						cbElement.setAttribute('class', 'gwTextBox');
						cbElement.onchange=function(){PLINE.pl.onchange(this)};
						
						//Add the selectiontext
						var optionElement = document.createElement('option');
						optionElement.setAttribute('value','');
						var textNode = document.createTextNode(this.selectionobjects[soIdx].selectiontext);
						optionElement.appendChild(textNode);
						optionElement.setAttribute('class', 'gwOption');
						
						cbElement.appendChild(optionElement);
						
						containerNode.appendChild(cbElement);
					}
				}
				
				if (cbElement != null) {
					this.rewriteSelectOptions(cbElement)
				}
			}
		}
		
		var resetElement = dojo.byId('comboAndBuyReset');
		if (resetElement != null) {
			this._addEvent(resetElement, 'mousedown', PLINE.pl.resetAllSelections);
		}
	}
	
	this.onchange = function(cbElement) {
		//rewrite others if necessary as there might be selections that are not available
		for (var soIdx = 0; soIdx<this.selectionobjects.length; soIdx++) {
			if (cbElement.id != this.selectionobjects[soIdx].id) {
				var otherCBElement = dojo.byId(this.selectionobjects[soIdx].id);
				if (otherCBElement != null)
					this.rewriteSelectOptions(otherCBElement);
			}
		}
		
		this.setSelectedProductAndPrice();
		
		if (this.onProductSelectFunc != null) {
			this.onProductSelectFunc();
		}
	}
	
	this.rewriteSelectOptions = function(cbElement) {
		//Get selection of other objects
		var selections = new Object();
		for (var i = 0; i<this.selectionobjects.length; i++) {
			if (this.selectionobjects[i].id != cbElement.id) {
				var otherCBElement = dojo.byId(this.selectionobjects[i].id);
				if (otherCBElement != null && otherCBElement.options != null) {
					selections[this.selectionobjects[i].id] = otherCBElement.options[otherCBElement.selectedIndex].value;
				} else
					selections[this.selectionobjects[i].id] = '';
			}
		}
		if (cbElement.options != null)
			selections[cbElement.id] = cbElement.options[cbElement.selectedIndex].value;
		else
			selections[cbElement.id] = '';
		
		//At this point selections holds the actual selections, iterate through combinations and get the available options for this value
		//Also if first time add the non-selection value
		for (var i=cbElement.options.length-1;i>0;i--) {
			cbElement.remove(i);
		}
		
		//now scan combinations and add all available values
		var selectedIndex = -1;
		var addedIndex = 0;
		for (var i = 0; i<this.availableCombinations.length; i++) {
			var c = this.availableCombinations[i];
			
			//Check if value needs to be filtered
			var actualValue = c[cbElement.id];
			var includeActualValue = true;
			for (var key in selections) {
				if (includeActualValue) {
					if (key != cbElement.id && selections[key] != '') {
						includeActualValue = (c[key] == selections[key]);
					}
				}
			}
			if (includeActualValue) {
				//Check if cbElement already has this value
				for (var j=0; includeActualValue && j<cbElement.options.length; j++) {
					if (cbElement.options[j].value == actualValue)
						includeActualValue = false;
				}
				if (includeActualValue) {
					var optionElement = document.createElement('option');
					optionElement.setAttribute('value',actualValue);
					var textNode = document.createTextNode(actualValue);
					optionElement.appendChild(textNode);
					optionElement.setAttribute('class', 'gwOption');
					
					cbElement.appendChild(optionElement);
					addedIndex++;
					
					if (selections[cbElement.id] == actualValue)
						selectedIndex = addedIndex;
				}
			}
			
			if (selectedIndex >= 0)
				cbElement.selectedIndex = selectedIndex;
		}
		
		this.setSelectedProductAndPrice = function() {
			this.selectedPrice = null;
			this.selectedProduct = null;		
		
			var selections = new Object();
			for (var i = 0; i<this.selectionobjects.length; i++) {
				var cbElement = dojo.byId(this.selectionobjects[i].id);
				if (cbElement != null && cbElement.options != null) {
					selections[this.selectionobjects[i].id] = cbElement.options[cbElement.selectedIndex].value;
				} else
					selections[this.selectionobjects[i].id] = '';
			}
			
			//find combination record
			for (var i = 0; i<this.availableCombinations.length && this.selectedProduct == null; i++) {
				var c = this.availableCombinations[i];
				
				//Check if value needs to be filtered
				var recordFound = true;
				for (var key in selections) {
					if (recordFound) {
						recordFound = (c[key] == selections[key]);
					}
				}
				
				if (recordFound) {
					this.selectedProduct = c.product;
					this.selectedPrice = c.price;
				}
			}
		}
	}
	
	this.getCombinationOfProduct = function(productId) {
		var combination = null;
		
		for (var i = 0; i<this.availableCombinations.length && combination == null; i++) {
			var c = this.availableCombinations[i];
			if (c.product.productid == productId)
				combination = c;
		}
		
		return combination;
	}
	
	this.renderMiniBrowser = function(outerContainerNodeId, containerNodeId, noImageText, productWidth, scrollBarProps) {
		var mbContainerElement = dojo.byId(containerNodeId);
		var classProp;
		if (IE)
			classProp = 'className';
		else
			classProp = 'class';
		
		var bar = document.createElement('div');
		var actualWidth = productWidth * this.availableCombinations.length;
		if (actualWidth<mbContainerElement.offsetWidth)
			actualWidth = mbContainerElement.offsetWidth;
		bar.style.width = actualWidth + 'px';
		bar.style.height = mbContainerElement.style.height
		mbContainerElement.appendChild(bar);
		
		var eventElements = new Array();
		
		for (var i = 0; i<this.availableCombinations.length; i++) {
			var p = this.availableCombinations[i].product;
			var wrapper = document.createElement('div');
			wrapper.setAttribute(classProp,'productWrapper');

			var imageDiv = document.createElement('div');
			if (p.thumbimage == '') {
				imageDiv.setAttribute(classProp,'productImageNoImage link');
				var noImageTextDiv = document.createElement('div');
				noImageTextDiv.setAttribute('id','mbImageTxt_'+p.productid);
				noImageTextDiv.setAttribute(classProp,'productImageNoImageText');
				
				var noImageTextNode = document.createTextNode(noImageText); 
				noImageTextDiv.appendChild(noImageTextNode);
				imageDiv.appendChild(noImageTextDiv);
			} else {
				imageDiv.setAttribute(classProp,'productImage link');
				imageDiv.style.backgroundImage = 'url(/getfile.html?fileId='+p.thumbimage+')';
			}
			
			wrapper.appendChild(imageDiv);
			imageDiv.setAttribute('id','mbImage_'+p.productid);
			eventElements.push(imageDiv);

			var nameDiv = document.createElement('div');
			nameDiv.setAttribute(classProp,'productName link');
			nameDiv.setAttribute('id','mbName_'+p.productid);
			var nameTextNode = document.createTextNode(p.name); 
			nameDiv.appendChild(nameTextNode);
			wrapper.appendChild(nameDiv);
			eventElements.push(nameDiv);
			
			bar.appendChild(wrapper);
		}
		
		jsScrollbar(outerContainerNodeId, scrollBarProps);
		
		//Here for some reason dojo.byId has to be called again
		this._addEvent(dojo.byId(containerNodeId), 'mousedown', PLINE.pl.setCombinationSelectionDojo);

	}
	
	this.setCombinationSelectionDojo = function(e) {
		if (!e) e = window.event
		var elementId = (e.target || e.srcElement).id;
		if (elementId != null && elementId.match(/^(mbImage|mbName|mbImageTxt)_[0-9]+$/) != null) {
			var idParts = elementId.split('_');
			if (idParts != null && idParts.length>1) {
				PLINE.pl.setCombinationSelection(idParts[1], true);
			}
		}
	}
	
	this.setCombinationSelection = function(productId, setProductId) {
		var c = this.getCombinationOfProduct(productId);
		if (c != null) {
			//At current selection it is possible that we cannot select the actual element, so first reset all combo boxes
			var cbElement = null;
			for (var i = 0; i<this.selectionobjects.length; i++) {
				cbElement = dojo.byId(this.selectionobjects[i].id);
				cbElement.selectedIndex = 0;
			}
			for (var i = 0; i<this.selectionobjects.length; i++) {
				cbElement = dojo.byId(this.selectionobjects[i].id);
				this.rewriteSelectOptions(cbElement);
			}
			for (var i = 0; i<this.selectionobjects.length; i++) {
				cbElement = dojo.byId(this.selectionobjects[i].id);
				var selectionIndex = -1;
				for (var j = 0; selectionIndex==-1 && j<cbElement.options.length; j++) {
					if (c[cbElement.id]==cbElement.options[j].value)
						selectionIndex = j;
				}
				if (selectionIndex>=0)
					cbElement.selectedIndex = selectionIndex;
			}
			
			if (setProductId) {
				this.setSelectedProductAndPrice();
				
				if (this.onProductSelectFunc != null) {
					this.onProductSelectFunc();
				}
			}
		}
	}
	
	this.resetAllSelections = function(e) {
		if (PLINE.pl.selectionobjects != null) {
			var cbFirstElement = null;
			
			for (var soIdx = 0; soIdx<PLINE.pl.selectionobjects.length; soIdx++) {
				//check if dom element exists?
				var cbElement = dojo.byId(PLINE.pl.selectionobjects[soIdx].id);
				if (cbElement != null) {
					if (cbFirstElement == null)
						cbFirstElement = cbElement;
					cbElement.selectedIndex = 0;
				}
			}
			
			for (var soIdx = 0; soIdx<PLINE.pl.selectionobjects.length; soIdx++) {
				//check if dom element exists?
				var cbElement = dojo.byId(PLINE.pl.selectionobjects[soIdx].id);
				PLINE.pl.rewriteSelectOptions(cbElement);
			}

			PLINE.pl.setSelectedProductAndPrice();
			
			if (PLINE.pl.onProductSelectFunc != null) {
				PLINE.pl.onProductSelectFunc();
			}
		}
	}
	
	this._addEvent = function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else
			obj.attachEvent( 'on'+type, fn );
	}	
}