// Norman Affiliate Webshop
// This script will transform an XML-feed
// Author: Torgeir Natvig, Norman Development Department
// Updated by: Eirik Rusten, Norman Corporate IT Department

var webshop = {
	init: function() {
		webshop.parseArray();

	},

	parseArray: function() {

		var index = $('products');
		index.innerHTML = '';
		for(var i = 0; i < productgroup.length; i++) {

			var productID = productgroup[i]["id"];
			
			if (self[productID] == 0){
				continue;
			}


			// Create all necessary div tags
			var div_main = C('div');
			var div_productGroup = C('div');
			var div_box = C('div');
			var div_image = C('div');
			var div_right = C('div');
			var div_info = C('div');
			var div_additional = C('div');
			var div_license = C('div');

			// Set the name of the class for each tag. This is uses in the CSS file to style them
			div_main.className = 'main';
			div_productGroup.className = 'productgroup';
			div_box.className = 'box';
			div_image.className = 'image';
			div_right.className = 'right';
			div_info.className = 'info';
			div_additional.className = 'additional';
			div_license.clasName = 'license';

			// The below will build the div structure as described in the documentation
			div_main.appendChild(div_productGroup);
			div_main.appendChild(div_box);
			div_box.appendChild(div_image);
			div_box.appendChild(div_right);
			div_right.appendChild(div_info);
			div_right.appendChild(div_additional);
			div_right.appendChild(div_license);
			index.appendChild(div_main);

			// Add the productgroup name to the correct div (productgroup)
			div_productGroup.innerHTML = productgroup[i]["name"];

			// Create the product image
			var image = C('img');
			image.src = productgroup[i]["productimage"];
			div_image.appendChild(image);

			// Add the productinfo value to the correct div (info)
			div_info.innerHTML = productgroup[i]["productinfo"];

			// Create the link to the detailed product description and add it to the additionalinfo tag
			var link = C('a');
			link.href = productgroup[i]["additionalinfo"];
			link.innerHTML = productgroup["text"]["prodinfo"];
			link.title = productgroup["text"]["prodinfo"];
			div_additional.appendChild(link);

			// Loop through each available license
			var licenses = new Array();
			licenses = productgroup[i]["license"];
			for(var y = 0; y < licenses.length; y++) {
				var p = C('p');
				var span = C('span');
				var link = C('a');
				span.innerHTML = '>> ';
				// Create the link into element cart page
				link.href = licenses[y]["link"] + '&currencies=' + currency.toUpperCase() + ',all';
				// Set what text will be displayed for the link, ie 1 Year License /49.00 EUR
				link.innerHTML = licenses[y]["name"];
				link.innerHTML += ' / ' + licenses[y][currency.toLowerCase()];
				link.innerHTML += ' ' + currency.toUpperCase();
				// Add a title attribute (tooltip)
				link.title = productgroup["text"]["addtocart"];
				if (window.newpage){
					link.target = '_blank';
				}
				p.appendChild(span);
				p.appendChild(link);
				div_license.appendChild(p);
			}

			//div_image.style.height = div_right.offsetHeight+'px';
		}

	},

	addEvent: function(elm, evType, fn, useCapture) {
		if (elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	}


}
function $() {
	var elements = new Array();

	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
		element = document.getElementById(element);

		if (arguments.length == 1)
		return element;

		elements.push(element);
	}

	return elements;
}

function C(e) {
	var element = document.createElement(e);
	return element;
}

webshop.addEvent(window, 'load', webshop.init, false);
