/*OVERRIDES*/
	wps.objectShop.prototype.waiter = function(){
		waiter();
	}
	
	wps.objectShop.prototype.unWaiter = function(){
		unWaiter();
	}
	
/*\OVERRIDES*/


var vpClient = Class.create(wps.base, {
		
	initialize:function(productID){
		this.instID = 'vpClient' + parseInt(Math.random()*1000000);
		this.timeoutCounter = 0;
		this.itemsPerPageList = 3;
		if (productID) {
			this.productID = productID ;
		}else{
			this.productID = "FIRST";
		}
		this.page = 0;
		this.pages = $A();
		this.objects = $A();
		this.category = 'catalogue';
		this.view = 'list';
		this.currFilter = 'filterOngoing';
		this.deliveryInit = false;
		//register unload event for purging memory
		window.onbeforeunload = this.cleanUp.bind(this);
	},

	init : function(productID){
		if (!productID) {
			productID = this.productID;
		}
		waiter();
		this.productID = productID;
		this.ready = false;
		var wpsRPC = new wps.rpc;
		wpsRPC.debug = true;
		wpsRPC.attachWaiter(this.waiter, this);
		wpsRPC.attachUnWaiter(this.unWaiter, this);
		wpsRPC.createCall('vpClient', this.setup.bind(this), this);
		wpsRPC.call('getChildObjects', 'parentObjectID=' + productID, 'isIE=' + this.is_ie());
	},
	
	setup : function(req){
		this.DOM = req.responseXML;
		var html = req.responseText;
		if (this.productID == "FIRST"){
			// set up some extra var's for easy navigation:
			
			////var firstNode = this.DOM.getElementsByTagName('product').item(0);
			//wps.dom.debug(this.DOM);
			//build array of product-id's and their page;
			if (this.is_ie()){
				var first = this.DOM.childNodes.item(0);
			}else{
				var first = this.DOM.firstChild;
			}
			this.currProjectNode = first;
			$A(first.childNodes).each(
				(function(product){
					var productID = this.getProductProperty(product, 'product_id');
					this.objects.push(productID);
				}).bind(this)
			)
			this.projectCount = this.objects.length;
		}else{
			this.currProjectNode = this.DOM.getElementsByTagName('product').item(0);
		}
		unWaiter();
		this.initView();
	},
	
	initView : function(){
		if (this.view == 'list'){
			$('listContainer').show();
			$('detailContainer').hide();
			this.initList();
		}else if(this.view == 'store'){
			$('shopContainer').show();
			this.initShop();
		}else{
			$('listContainer').hide();
			$('detailContainer').show();
			this.initBrowser();
		}
		placeFooter();
	},
	
	// HELPERS
	
	closeContainer : function(container){
		new Effect.SlideUp(container, {duration:'0.3'});
	},

	openContainer : function(container){
		new Effect.SlideDown(container, {duration:'0.3'});
	},
	
	waiter : function(){
    	waiter();
	},
	
	unWaiter : function(){
		unWaiter();
	},
	
	getProductProperty : function(productNode, propKey){
		var returnValue;
		if (propKey == 'product_id'){
			var ret = $A(productNode.childNodes).findAll(
				(function(aChildNode){
					if (aChildNode.nodeName == 'product_id'){
						returnValue = wps.dom.getElementValue(aChildNode);
						return;
					}
				}).bind(this)
			)
		}else{
			var ret = $A(productNode.childNodes).findAll(
				(function(aChildNode){
					if (aChildNode.nodeName == 'property'){
						//console.log(wps.dom.getNodeValue(aChildNode.getElementsByTagName('key')) + ' / '  + propKey);
						if (wps.dom.getNodeValue(aChildNode.getElementsByTagName('key')) == propKey){
							
							returnValue = wps.dom.getNodeValue(aChildNode.getElementsByTagName('value'));
							return;
						}
						
					}
				}).bind(this)
			)
		}
		return returnValue;
	},
	
	getChildObjectByName : function(parentNode, key){
		var retArr = new Array();
		$A(parentNode.childNodes).each(
			(function(node){
				if (node.nodeName=='product'){
					// determine name:
					var objectName = this.getProductProperty(node, 'name');
					if (objectName == key) retArr.push(node);
				}
			}).bind(this)
		)
		return retArr;
	},
	
	hasChildObjects : function(parentNode){
		var bool = false
		$A(parentNode.childNodes).each(
			(function(node){
				if (node.nodeName=='product'){
					if (this.getProductProperty(node, 'name')  != 'Name of medium'){
						bool = true;
					}
				}
			}).bind(this)
		)
		return bool;
	},	

	cleanUp : function(){
		this.DOM = null;
		this.currProjectNode = null;
		this.template = null;
		this.projectLinks = null;
	}
	
});