//	YEAH, a custom class especially desigend for IE! only because normal browser interpret XML as XML ... not IE
Request.XML = new Class({
	Extends: Request,
	success: function(text, xml) {
		if (xml) {
			if(Browser.Features.xpath) {
				// some stuff
			} else {
				xml = this.createXML(xml.documentElement); // ie browsers
			}
		}
		window.xml = xml;
		this.onSuccess(text,xml);
	},
	createXML : function(xml, parent, level) {
		if(!parent) {
			parent = new Element(xml.nodeName);
			level  = 'root';
		}
		level += '>>'+xml.nodeName;
		//console.log(level);
		if(xml.childNodes.length) {
			for(var i = 0; i < xml.childNodes.length; i++) {
				var son = xml.childNodes[i];
				if(son.nodeType == 1) { // Element type
					// NodeName
					var el = new Element(son.nodeName.replace(':', ''));
					// Attributes
					if(son.attributes.length) {
						for(var j = 0; j < son.attributes.length; j++) {
							var property = son.attributes[j].nodeName;
							var value    = son.attributes[j].nodeValue;
							el.setProperty(property, value);
						}
					}
					// Value
					if (son.firstChild && son.firstChild.nodeType==3) {
					 	if (son.firstChild.nodeValue) {
							el.set({'text' : son.firstChild.nodeValue});
						}
					}
					if (son.nodeName == 'fileentryid') console.log('docId:'+el.get('text'));
					parent.grab(el);
					this.createXML(son, el, level);
				}
			}
		}
		return parent;
	}
});


function initializePrice()
{
//	sigh ... IE fires the change event when the radio looses focus (blur event) not when it changes ... more code and resources to fix ...
//	for a complete - its not a bug its a feature - list see: http://www.quirksmode.org/dom/events/change.html
	if(Browser.Engine.trident)
	{
		$$('input[type=radio]').removeEvents('click');
		$$('input[type=radio]').addEvent('click', function(e){
			this.fireEvent('change');
		});
	}
	
	var totalprice = new Class({
		Implements: Options,
		options: {},
		initialize: function(){
		//	request the product options that have to be searched after
			this.myRequest = new Request.XML({
				url: '/xml/product.php?action=options',
				method: 'get',
				onSuccess: function(responseText, responseXML) {
				//	store the XML into a local variable
					this.priceXML = responseXML;
					this.priceXML.getElements('option').each(function(ele){
					//	add change events to the form elements
						$$(document.getElementsByName(ele.get('name'))).removeEvents('change');
						$$(document.getElementsByName(ele.get('name'))).addEvent('change', function(e) {
							myprice.updateTarget('total', this);
						});
					//	intiate the targets start value!
						$$(document.getElementsByName(ele.get('name'))).each(function(ele){
						//	check the value or the checked status
						//alert(ele.value)
						//alert(ele.checked)
						//alert(ele.type)
							if(ele.type == "text" || typeof(ele.checked) == "undefined" || ele.checked == true){
								this.options[ele.name] = ele.value;
							}
						}.bind(this));
					}.bind(this));
				//	update the total
					this.updateTarget('total');
				}.bind(this)
			}).send();
		},
		updateTarget: function(target, object){
		//	update the options array with the new value of the changed option
			if(typeof(object) != "undefined") {
				this.options[object.name] = object.value;
			}
		//	request the total by sending the options as an array back to the server
			this.getTotal = new Request.XML({
				url: '/xml/product.php?action=calculate',
				data: this.options,
				method: 'get',
				onSuccess: function(responseText, responseXML) {
					if(responseXML){
//alert(responseText);
if(responseXML.getElement('sales_order').getAttribute('status')) alert(responseXML.getElement('sales_order').getAttribute('status'));
//alert(responseXML.getElement('sales_order').getAttribute('price').type)
						$(target).innerHTML = responseXML.getElement('sales_order').getAttribute('price');
						$(target).set('tween',{duration: 'long'});
						$(target).tween('background-color', ['#8FD400','#CFEA8B']);
					} else {
						alert('no response from server');
					}
				}.bind(this)
			}).send();
		}
	});
	
//	initiate the class and add click events to the required form elements
	var myprice = new totalprice();
}
window.addEvent('domready', function(){
	//initializePrice();
	var myTips = new Tips($$('.mytips'), {className: 'info'});
//	jaja luuk, heb je toch gelijk gekregen :p met een javascriptje de kolommen gelijk trekken ...........

	var bottom =	Math.max(	($('right').getPosition().y + $('right').getSize().y),
								($('extra').getPosition().y + $('extra').getSize().y)
					);
					
	$('bestellen').setStyle('height',(bottom - $('right').getPosition().y - 12));
	if($('page'))	$('page').setStyle('height',(bottom - $('page').getPosition().y - $('extra').getSize().y - 7));
	if($('sticker_types'))	$('sticker_types').setStyle('height',(bottom - $('sticker_types').getPosition().y - $('extra').getSize().y));
	
	if($('select_material').getElement('select') && $('other_material')){
		$('select_material').getElement('select').addEvent('change',function(e){
			if(this.value == '51')
				$('other_material').removeClass('hidden');
			else
				$('other_material').addClass('hidden');
		});
	}
});

