

//************************************************************************************************************
//
// 	These functions are used to rotate the display of images on a page:
//	If the cradle/base html is not present it creates a default one
//
//************************************************************************************************************

	var imageBrowserInitialized = false;

	function imageBrowserInit(cradleId, insertBeforeElement, newCradleHtml, firstImageUrl, captionId, firstImageCaption) {
		if(document.images){
			if (imageBrowserInitialized == false) {

				// Decode newCradleHtml
				newCradleHtml = newCradleHtml.replace(/&gt;/g,'>');
				newCradleHtml = newCradleHtml.replace(/&lt;/g,'<');
				newCradleHtml = newCradleHtml.replace(/&quot;/g,'"');
				newCradleHtml = newCradleHtml.replace(/&amp;/g,'&');

				// Decode firstImageCaption
				firstImageCaption = firstImageCaption.replace(/&gt;/g,'>');
				firstImageCaption = firstImageCaption.replace(/&lt;/g,'<');
				firstImageCaption = firstImageCaption.replace(/&quot;/g,'"');
				firstImageCaption = firstImageCaption.replace(/&amp;/g,'&');

				// Wait for the document to be loaded to try and locate cradle code
				addLoadEvent(function() {
					imageBrowserLocateCradle(cradleId, insertBeforeElement, newCradleHtml);
					imageBrowserSwitch(cradleId, firstImageUrl, captionId, firstImageCaption)
					})

			}
		}
	}


	function imageBrowserLocateCradle(cradleId, insertBeforeElement, newCradleHtml) {
		
		if (!(document.getElementById(cradleId))) {
			// Cradle code is missing
			// Insert code above the insertBeforeElement
			var newNode = document.createElement('div');
			newNode.innerHTML = newCradleHtml;
			insertBeforeElement.parentNode.insertBefore(newNode,insertBeforeElement);
		}
		
	}

	function imageBrowserSwitch(cradleId, newImageUrl, captionId, newImageCaption) {

		if(document.images){
			// Decode newImageCaption
			newImageCaption = newImageCaption.replace(/&gt;/g,'>');
			newImageCaption = newImageCaption.replace(/&lt;/g,'<');
			newImageCaption = newImageCaption.replace(/&quot;/g,'"');
			newImageCaption = newImageCaption.replace(/&amp;/g,'&');

			document.getElementById(cradleId).src = newImageUrl;
			document.getElementById(captionId).innerHTML = newImageCaption;
		}
	}


//************************************************************************************************************
//
// 	The way this works is relatively simple:
//	If window.onload has not already been assigned a function, the function passed to addLoadEvent
//	is simply assigned to window.onload. If window.onload has already been set, a brand new function
//	is created which first calls the original onload handler, then calls the new handler afterwards
//
//	Ex: addLoadEvent(function() {document.body.style.backgroundColor = 'yellow';})
//
//************************************************************************************************************


	function addLoadEvent(func) {
		var oldonload = window.onload;   
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}




//************************************************************************************************************
//
// 	These function are here to adjust heights of table cells in Boxed listing mode
// 	All cells with its class set to: adjustMyHeight will be set to have the same height
// 	as the tallest cell in it's row.
//
//************************************************************************************************************



	function getElementsByClass(searchClass,node,tag) {
		var classElements = new Array();
		if ( node == null )
		node = document;
		if ( tag == null )
		tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
		for (i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}

	function adjustHeight() {
		allcells = getElementsByClass("adjustMyHeight");
		for (i = 0; i < allcells.length; i++) {
			// For each cell we iterate through we find all the cells in the containing row and calculate their max height
			siblingcells = allcells[i].parentNode.childNodes;
			var maxsize = 0;
			var maxroom = 0;
			for (j = 0; j < siblingcells.length;j++) {
				if (siblingcells[j].tagName == "TD") {
					if (siblingcells[j].scrollHeight > maxsize) {
						maxsize = siblingcells[j].scrollHeight;
						// Manual compensation for padding in the parent cell. The figure 5 might need
						// Too be adjusted if the padding in the template for the product boxes is changed
						// The best would be to retrieve the padding from this cell and deduct that instead
						maxroom = maxsize - 5;
					}
				}
			}

			for (j = 0; j < siblingcells.length;j++) {
				if (siblingcells[j].tagName == "TD") {
					siblingcells[j].style.height = maxsize+'px';
					childtables = siblingcells[j].getElementsByTagName('TABLE');
					for (k = 0; k < childtables.length;k++) {
						if (childtables[k].tagName == "TABLE") {
							childtables[k].style.height = maxroom+'px';
						}
					}
				}
			}
		}
	}

    adjustHeight();
    setTimeout("adjustHeight()", 1000)
    setTimeout("adjustHeight()", 2000)
    setTimeout("adjustHeight()", 5000)
	setTimeout("adjustHeight()", 15000)

//************************************************************************************************************
//
// 	Div. code
//
//************************************************************************************************************


  	function copyValues() {
  		var variants = '';
  		var n = 0;
  		while (document.getElementById('addBasketVariant' + n) != null) {
			select = document.getElementById('addBasketVariant' + n);
			verdi = select.options[select.selectedIndex].value;
			if (n > 0) {
				variants += ';';
			}
			variants += verdi;
  			n++;
  		}
  		document.getElementById('addShoppingListVariants').value = variants;
		if (document.getElementById('addBasketQuantity') != null) {
			document.getElementById('addShoppingListQty').value = document.getElementById('addBasketQuantity').value;
		}
  	}



	// This function only returns true if all selects in the containing form have a selection other than the first
	// This allows it to abort purchase attempts with invalid selections
	// Implemented in <form> tag like this: onSubmit="if (checkForSelections(this)) {return true} else {alert('Du må foreta valg av variant først!');return false}"
  	function checkForSelections(myElement) {

		for (var n = 0; myElement.childNodes[n]; n++ ) {
			if (myElement.childNodes[n].tagName == 'SELECT') {
		  		if (myElement.childNodes[n].selectedIndex == 0) {
		  			// If a child is a wrongly selected <select> then return false 
					return false;
		  		}
		  		// otherwise just move on

			} else if (myElement.childNodes[n].hasChildNodes()) {
				// If there are children, then check the childrens children recursivly
				if (!(checkForSelections(myElement.childNodes[n]))) {
					return false;
				}
			}
		}

		return true;

  	}

	var isShown = new Array();

	// This function unhides or hides all nodes under the parent node
	function flipMode(myNode) {

		if (isShown[myNode.id]) {
			// Hides
			for( var x = 0; myNode.parentNode.childNodes[x]; x++ ) {
				if (myNode.parentNode.childNodes[x].style) {
					myNode.parentNode.childNodes[x].style.display = 'none';
				}
			}			
			isShown[myNode.id] = false;
		} else {
			// Unhides
			for( var x = 0; myNode.parentNode.childNodes[x]; x++ ) {
				if (myNode.parentNode.childNodes[x].style) {
					myNode.parentNode.childNodes[x].style.display = '';
				}
			}
			isShown[myNode.id] = true;
		}

	}