

//************************************************************************************************************
//
// 	QuickMenu add-on object creator
//
//************************************************************************************************************


	function qmadInit () {

		var qmad = new Object();

		qmad.bvis = "";
		qmad.bhide = "";
		qmad.bhover = "";

		return qmad;
	}

	function qmenuAddonInit (args) {

		var a =	new Object();

		// Rounded Corners Add On
		if (typeof args["rcorner_size"] != "undefined") {
			a.rcorner_size = args["rcorner_size"];
		} else {
			a.rcorner_size = 4;
		}

		if (typeof args["rcorner_border_color"] != "undefined") {
			a.rcorner_border_color = args["rcorner_border_color"];
		}

		if (typeof args["rcorner_bg_color"] != "undefined") {
			a.rcorner_bg_color = args["rcorner_bg_color"];
		}

		if (typeof args["rcorner_apply_corners"] != "undefined") {
			a.rcorner_apply_corners = args["rcorner_apply_corners"];
		}


		// Item Bullets Add On
		if (typeof args["ibullets_apply_to"] != "undefined") {
			a.ibullets_apply_to = args["ibullets_apply_to"];
		} else {
			a.ibullets_apply_to = "parent";
		}


		if (typeof args["ibullets_main_image"] != "undefined") {
			if (args["ibullets_main_image"]) {
				a.ibullets_main_image = args["ibullets_main_image"];

				if (typeof args["ibullets_main_image_width"] != "undefined") {
					a.ibullets_main_image_width = args["ibullets_main_image_width"];
				}

				if (typeof args["ibullets_main_image_height"] != "undefined") {
					a.ibullets_main_image_height = args["ibullets_main_image_height"];
				}

				if (typeof args["ibullets_main_position_x"] != "undefined") {
					a.ibullets_main_position_x = args["ibullets_main_position_x"];
				} else {
					a.ibullets_main_position_x = -19;
				}

				if (typeof args["ibullets_main_position_y"] != "undefined") {
					a.ibullets_main_position_y = args["ibullets_main_position_y"];
				} else {
					a.ibullets_main_position_y = -5;
				}

				if (typeof args["ibullets_main_align_x"] != "undefined") {
					a.ibullets_main_align_x = args["ibullets_main_align_x"];
				} else {
					a.ibullets_main_align_x = "right";
				}

				if (typeof args["ibullets_main_align_y"] != "undefined") {
					a.ibullets_main_align_y = args["ibullets_main_align_y"];
				} else {
					a.ibullets_main_align_y = "middle";
				}

			}
		}





		if (typeof args["ibullets_sub_image"] != "undefined") {
			if (args["ibullets_sub_image"]) {
				a.ibullets_sub_image = args["ibullets_sub_image"];

				if (typeof args["ibullets_sub_image_width"] != "undefined") {
					a.ibullets_sub_image_width = args["ibullets_sub_image_width"];
				}

				if (typeof args["ibullets_sub_image_height"] != "undefined") {
					a.ibullets_sub_image_height = args["ibullets_sub_image_height"];
				}

				if (typeof args["ibullets_sub_position_x"] != "undefined") {
					a.ibullets_sub_position_x = args["ibullets_sub_position_x"];
				} else {
					a.ibullets_sub_position_x = -12;
				}

				if (typeof args["ibullets_sub_position_y"] != "undefined") {
					a.ibullets_sub_position_y = args["ibullets_sub_position_y"];
				} else {
					a.ibullets_sub_position_y = -2;
				}

				if (typeof args["ibullets_sub_align_x"] != "undefined") {
					a.ibullets_sub_align_x = args["ibullets_sub_align_x"];
				} else {
					a.ibullets_sub_align_x = "right";
				}

				if (typeof args["ibullets_sub_align_y"] != "undefined") {
					a.ibullets_sub_align_y = args["ibullets_sub_align_y"];
				} else {
					a.ibullets_sub_align_y = "middle";
				}

			}
		}

		return a;
	}



//************************************************************************************************************
//
// 	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;

			var newdiv = document.createElement("div"); 
			newdiv.innerHTML = newImageCaption;
			document.getElementById(captionId).innerHTML = ''; 
			document.getElementById(captionId).appendChild(newdiv); 

		}
	}


//************************************************************************************************************
//
// 	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.
//
//************************************************************************************************************

	// Attempt to replace the old adjustHeight with a more robust jQuery version. Unable to make it work because
	// the height() function in IE adds the specified number of pixels to the already existing height.
	// A better generalization would be to use two classes. one to identify all elements to resize.
	// And another that had an id to group elements. Ex: class="adjustHeight aid_row1"
	/*
	function jQ_adjustHeight() {
		$('.jQ_adjustMyHeightSubTable').each(function(index) {

    		var $row = $(this).parents("tr:first");
    		var maxHeight = 0;

    		$row.find('.jQ_adjustMyHeightSubTable').each(function(index) {
    			if ($(this).height() > maxHeight) {
    				maxHeight = $(this).height();
    			}
    		})
    		$row.find('.jQ_adjustMyHeightSubTable').each(function(index) {
    			alert($(this).height()+' < '+maxHeight);
    			if ($(this).height() < maxHeight) {
    				alert('setting height to:' + maxHeight);
    				$(this).height(maxHeight);
    				alert('has height:' + $(this).height());
    			}
    		})
 		 });

	}
	*/



	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;
	}


	// For backwards compatibility. Old systems don't have the adjustMyHeight class
	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';
						}
					}
				}
			}
		}
	}


	// If this fails because images are not loaded, then a separate wait function should be added.
	// A function that waits until all images on the page have been loaded.

	addLoadEvent(function() {
		adjustHeight();
	
		})


//************************************************************************************************************
//
// 	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;
		}

	}

