/* -------------------------------------------------------------------
	Class Product
	-----
	Container with one product
 ------------------------------------------------------------------ */
Product = Class.create({
	initialize: function(pc, pcik, name, beautifyName, price, isDiscount, isFromPrice, strokeprice, discount, image, thumbnail, availability, availabilityText, rating, bvrating, bvratingAlt, description, highlights, productlink, addtocartlink,net){
		this.pc = pc;
		this.pcik = pcik;
		this.name = name;
		this.beautifyName = beautifyName;
		this.price = price;
		this.isDiscount = isDiscount;
		this.isFromPrice = isFromPrice;
		this.strokeprice = strokeprice
		this.discount = discount;
		this.image = image;
		this.availability = availability;
		this.availabilityText = availabilityText;
		this.rating = rating;
		this.bvrating = bvrating;
		this.bvratingAlt = bvratingAlt;
		this.description = description;
		this.thumbnail = thumbnail;
		this.highlights = highlights;
		this.productlink = productlink;
		this.addtocartlink = addtocartlink;
		this.net = net;
	}
});

/* -------------------------------------------------------------------
	Class Products
	----
	Container with different Products
 ------------------------------------------------------------------ */
Products = Class.create({
	
	initialize: function(productXml,products) {
		this.showCaseXml = parseXml(productXml);
		this.items = new Array();
		this.buttonSrc = null;
		this.allDataSrc = null;		
	},
	
	/* Load an XML with ProductData */
	load: function() {
		var curtab = (showCase) ? showCase.current : firstTab;
				
		//var params = new Hash();
		//params.set("curtab", curtab);
		//params.set("categorycode", aktuelleSeite);

		//new Ajax.Request(configuration.actionShowCase1, {
		//	parameters: params,
		//	method: 'get',
		//	asynchronous: false, /* false */
		//	onComplete: this.processXml.bind(this)
		//});

		this.processXml(curtab);	
	},

	/* Processes the XML and create Items ( Objects ) */
	processXml: function(curtab) {
		var curtabElem = this.showCaseXml.getElementsByTagName(curtab);
		var items = curtabElem[0].firstChild.getElementsByTagName('product');
		
		this.items = new Array();
		var properties = new Hash();		
		for(var i=0; i<items.length; i++) {
			properties.set('pc', this.getPropertyValue(items[i].getElementsByTagName('productcode')[0]));
			properties.set('pcik', this.getPropertyValue(items[i].getElementsByTagName('productcodeIK')[0]));
			properties.set('name', this.getPropertyValue(items[i].getElementsByTagName('name')[0]));
			properties.set('beautifyName', this.getPropertyValue(items[i].getElementsByTagName('beautifyName')[0]));
			properties.set('image', this.getPropertyValue(items[i].getElementsByTagName('productimage')[0]));
			properties.set('thumbnail', this.getPropertyValue(items[i].getElementsByTagName('thumbnailimage')[0]));
			properties.set('price', this.getPropertyValue(items[i].getElementsByTagName('price')[0]));
			
			/* Check if Discount exists */
			if (items[i].getElementsByTagName('price')[0].getAttribute ('isDiscount') != null ) {
				properties.set('isDiscount', true);
				properties.set('strokeprice', this.getPropertyValue(items[i].getElementsByTagName('strokeprice')[0]));
				properties.set('discount', this.getPropertyValue(items[i].getElementsByTagName('discount')[0]));
			}
			else {
				properties.set('isDiscount', false);
			}
			/* Check if FromPrice exists */
			if (items[i].getElementsByTagName('price')[0].getAttribute ('fromPrice') != null ) {
				properties.set('isFromPrice', true);
			}
			
			properties.set('availability', this.getPropertyValue(items[i].getElementsByTagName('availability')[0]));
			properties.set('availabilityText', this.getPropertyValue(items[i].getElementsByTagName('availabilityText')[0]));
			properties.set('rating', this.getPropertyValue(items[i].getElementsByTagName('rating')[0]));
			properties.set('bvrating', this.getPropertyValue(items[i].getElementsByTagName('bvrating')[0]));
			properties.set('bvratingAlt', this.getPropertyValue(items[i].getElementsByTagName('bvratingAlt')[0]));
			properties.set('description', this.getPropertyValue(items[i].getElementsByTagName('description')[0]));
			properties.set('highlights', this.getPropertyValue(items[i].getElementsByTagName('highlights')[0]));
			productlink = configuration.cestorePrefix + "/" + configuration.language + "/product/" + properties.get('pc') + "/" + properties.get('beautifyName');
			if(!document.cookie && !baseUrl.indexOf(";jsessionid") > 0)
				productlink = productlink + ";jsessionid=" + sessionId;
			properties.set('productlink', productlink);
			//properties.set('productlink', this.getPropertyValue(items[i].getElementsByTagName('productlink')[0]));
			//properties.set('addtocartlink', this.getPropertyValue(items[i].getElementsByTagName('addtocartlink')[0]));
			properties.set('net', this.getPropertyValue(items[i].getElementsByTagName('net')[0]));
			
			

			var f = new Product(
				properties.get('pc'), 
				properties.get('pcik'),
				properties.get('name'), 
				properties.get('beautifyName'),				
				properties.get('price'), 
				properties.get('isDiscount'),
				properties.get('isFromPrice'),
				properties.get('strokeprice'), 
				properties.get('discount'), 
				properties.get('image'), 
				properties.get('thumbnail'), 
				properties.get('availability'),
				properties.get('availabilityText'),
				properties.get('rating'),
				properties.get('bvrating'),
				properties.get('bvratingAlt'),
				properties.get('description'),
				properties.get('highlights'),
				properties.get('productlink'),
				properties.get('addtocartlink'),
				properties.get('net')
			);
			this.items.push(f);
			
		}
		
		var additionalElem = curtabElem[0].getElementsByTagName('additionalData')[0];
		this.buttonSrc = additionalElem.getElementsByTagName('button')[0].firstChild.nodeValue;
		if (	additionalElem.getElementsByTagName('allData') 
			&&	additionalElem.getElementsByTagName('allData')[0]
			&&	additionalElem.getElementsByTagName('allData')[0].firstChild
			) { 
			this.allDataSrc = additionalElem.getElementsByTagName('allData')[0].firstChild.nodeValue;
		}
		else {
			this.allDataSrc = "";
		}
		if (	additionalElem.getElementsByTagName('allDataLink') 
			&&	additionalElem.getElementsByTagName('allDataLink')[0]
			&&	additionalElem.getElementsByTagName('allDataLink')[0].firstChild
			) { 
			var allDataLink = additionalElem.getElementsByTagName('allDataLink')[0].firstChild.nodeValue;
			if(allDataLink.indexOf("type=") >= 0) {
				baseUrl = configuration.promotionAction;
			}
			else { 
				baseUrl = configuration.categoryProductsAction;
			}
			if(!document.cookie && !baseUrl.indexOf(";jsessionid") > 0)
				baseUrl = baseUrl + ";jsessionid=" + sessionId;
			this.allDataHref = baseUrl + "?" + allDataLink;
		}
		else {
			this.allDataHref = "";
		}
	},
	
	// iterator method required by Enuberable interface
	_each: function(iterator) {
		for(var i=0, len=this.items.length; i<len; ++i) {
			iterator(this.items[i]);
		}
	},

	getPropertyValue: function (property) {
		//alert((property.firstChild));
		return (property.firstChild) ? property.firstChild.nodeValue : "";  
	}
});


/* -------------------------------------------------------------------
Class ProductsSC2
----
Container with different Products for Showcase2 (Artikelliste)
(verwendet den gleichen Pager, der für Showcase1 eingesetzt wird) 
------------------------------------------------------------------ */
ProductsSC2 = Class.create({

initialize: function(productXml) {
	this.items = new Array();
	this.showCaseXml = parseXml(productXml);
},

/* Load an XML with ProductData */
load: function(productcode) {

	//var params = new Hash();
	//params.set("productcode", productcode);

	//new Ajax.Request(configuration.actionShowCase2ProductPager, {
	//	parameters: params,
	//	method: 'GET',
	//	asynchronous: false, /* false */
	//	onComplete: this.processXml.bind(this)
	//});
	this.processXml(productcode);
},

/* Processes the XML and create Items ( Objects ) */
processXml: function(productCode) {
	
	var items = this.showCaseXml.getElementsByTagName('product');

	var properties = new Hash();
	for(var i=0; i<items.length; i++) {
		if(items[i].getElementsByTagName('productcode')[0].firstChild.nodeValue != productCode) 
			continue;
		properties.set('pc', this.getPropertyValue(items[i].getElementsByTagName('productcode')[0]));
		properties.set('pcik', this.getPropertyValue(items[i].getElementsByTagName('productcodeIK')[0]));
		properties.set('name', this.getPropertyValue(items[i].getElementsByTagName('name')[0]));
		properties.set('beautifyName', this.getPropertyValue(items[i].getElementsByTagName('beautifyName')[0]));
		properties.set('image', this.getPropertyValue(items[i].getElementsByTagName('productimage')[0]));
		properties.set('thumbnail', this.getPropertyValue(items[i].getElementsByTagName('thumbnailimage')[0]));
		properties.set('price', this.getPropertyValue(items[i].getElementsByTagName('price')[0]));
		
		/* Check if Discount exists */
		if (items[i].getElementsByTagName('price')[0].getAttribute ('isDiscount') != null ) {
			properties.set('isDiscount', true);
			properties.set('strokeprice', this.getPropertyValue(items[i].getElementsByTagName('strokeprice')[0]));
			properties.set('discount', this.getPropertyValue(items[i].getElementsByTagName('discount')[0]));
		}
		else {
			properties.set('isDiscount', false);
		}
		/* Check if FromPrice exists */
		if (items[i].getElementsByTagName('price')[0].getAttribute ('fromPrice') != null ) {
			properties.set('isFromPrice', true);
		}
		
		properties.set('availability', this.getPropertyValue(items[i].getElementsByTagName('availability')[0]));
		properties.set('availabilityText', this.getPropertyValue(items[i].getElementsByTagName('availabilityText')[0]));
		properties.set('rating', this.getPropertyValue(items[i].getElementsByTagName('rating')[0]));
		properties.set('bvrating', this.getPropertyValue(items[i].getElementsByTagName('bvrating')[0]));
		properties.set('bvratingAlt', this.getPropertyValue(items[i].getElementsByTagName('bvratingAlt')[0]));
		properties.set('description', this.getPropertyValue(items[i].getElementsByTagName('description')[0]));
		properties.set('highlights', this.getPropertyValue(items[i].getElementsByTagName('highlights')[0]));
		productlink = configuration.cestorePrefix + "/" + configuration.language + "/product/" + properties.get('pc') + "/" + properties.get('beautifyName');
		if(!document.cookie && !baseUrl.indexOf(";jsessionid") > 0)
			productlink = productlink + ";jsessionid=" + sessionId;
		properties.set('productlink', productlink);
		//properties.set('addtocartlink', this.getPropertyValue(items[i].getElementsByTagName('addtocartlink')[0]));
		properties.set('net', this.getPropertyValue(items[i].getElementsByTagName('net')[0]));
		
		

		var f = new Product(
			properties.get('pc'), 
			properties.get('pcik'),
			properties.get('name'), 
			properties.get('beautifyName'),
			properties.get('price'), 
			properties.get('isDiscount'),
			properties.get('isFromPrice'),
			properties.get('strokeprice'), 
			properties.get('discount'), 
			properties.get('image'), 
			properties.get('thumbnail'), 
			properties.get('availability'),
			properties.get('availabilityText'),
			properties.get('rating'),
			properties.get('bvrating'),
			properties.get('bvratingAlt'),
			properties.get('description'),
			properties.get('highlights'),
			properties.get('productlink'),
			properties.get('addtocartlink'),
			properties.get('net')
		);
		this.items = new Array(f);		
	}
},

// iterator method required by Enuberable interface
_each: function(iterator) {
	for(var i=0, len=this.items.length; i<len; ++i) {
		iterator(this.items[i]);
	}
},

getPropertyValue: function (property) {
	//alert((property.firstChild));
	return (property.firstChild) ? property.firstChild.nodeValue : "";  
}
});


function parseXml(xmlString) {
	//for IE
	if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.loadXML(xmlString);
		return xmlDoc;
	}
	//for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		var parser = new DOMParser();
		return parser.parseFromString(xmlString,"text/xml");
	}
}

