Banners_engine = Class.create({
	parentContainer: document,
	bannerContainers: null,
	arrPosBanners: [],
	argsBanner: {
			Abmodel	 	: null,
			Aposition 	: null,
			Acounter	: null,
			Aclass		: null,
			Anr			: null,
			Amaxheight	: null,
			Amaxwidth	: null,
			Alimit		: null,
			Apage		: null,
			Asquare		: null,
			Abrand		: null,
			Amodel		: null,
			buildString	: function(){
				var arrGets = [];
		  		if(typeof(this.Abmodel)!=='undefined' && this.Abmodel!=null)		arrGets.push('bmodel='+this.Abmodel);
		  		if(typeof(this.Aposition)!=='undefined' && this.Aposition!=null)	arrGets.push('position='+this.Aposition);
		  		if(typeof(this.Acounter)!=='undefined' && this.Acounter!=null)		arrGets.push('counter='+this.Acounter);
		  		if(typeof(this.Aclass)!=='undefined' && this.Aclass!=null)			arrGets.push('class='+this.Aclass);
		  		if(typeof(this.Anr)!=='undefined' && this.Anr!=null)				arrGets.push('nr='+this.Anr);
		  		if(typeof(this.Amaxheight)!=='undefined' && this.Amaxheight!=null)	arrGets.push('maxheight='+this.Amaxheight);
		  		if(typeof(this.Amaxwidth)!=='undefined' && this.Amaxwidth!=null)	arrGets.push('maxwidth='+this.Amaxwidth);
		  		if(typeof(this.Alimit)!=='undefined' && this.Alimit!=null)			arrGets.push('limit='+this.Alimit);
		  		if(typeof(this.Apage)!=='undefined' && this.Apage!=null)			arrGets.push('page='+this.Apage);
		  		if(typeof(this.Asquare)!=='undefined' && this.Asquare!=null)		arrGets.push('square='+this.Asquare);
		  		if(typeof(this.Abrand)!=='undefined' && this.Abrand!=null)			arrGets.push('brand='+this.Abrand);
		  		if(typeof(this.Amodel)!=='undefined' && this.Amodel!=null)			arrGets.push('model='+this.Amodel);
				return escape(arrGets.join(';'));
			}
	},
	
	initialize: function(idContainer){
		try{
			if(typeof(idContainer)!=='undefined' && $(idContainer)){
				this.parentContainer = $(idContainer);
			}
			this.bannerContainers = Element.select(this.parentContainer, 'ins[type="banner"]');
			this.bannerContainers.each(this.loadBanners.bind(this));
			this.requestBanners(this.arrPosBanners);
		}catch(e){
			alert('Banners_engine::initialize: ' + e);
		}
	},
	loadBanners: function(node){
		try{
			$(node).setStyle({'width':'auto', 'height':'auto', 'display':'block'});
			var urlArgs = {};
			Object.extend(urlArgs, this.argsBanner);
			urlArgs.Abmodel	 	= $(node).readAttribute('bmodel');
			urlArgs.Aposition 	= $(node).readAttribute('position');
			urlArgs.Acounter	= $(node).readAttribute('counter');
			urlArgs.Aclass		= $(node).readAttribute('class');
			urlArgs.Anr			= $(node).readAttribute('nr');
			urlArgs.Amaxheight	= $(node).readAttribute('maxheight');
			urlArgs.Amaxwidth	= $(node).readAttribute('maxwidth');
			urlArgs.Alimit		= $(node).readAttribute('limit');
			urlArgs.Apage		= $(node).readAttribute('page');
			urlArgs.Asquare		= $(node).readAttribute('square');
			urlArgs.Abrand		= $(node).readAttribute('brand');
			urlArgs.Amodel		= $(node).readAttribute('model');
			var nodeId			= $(node).identify().replace(/bnr_/i, '');
			//
			if(urlArgs.Amaxwidth > 0){
				urlArgs.Amaxwidth	= Math.max(urlArgs.Amaxwidth, node.getWidth());
			}
			//
			this.arrPosBanners[this.arrPosBanners.length] = [nodeId, urlArgs.buildString()];
		}catch(e){
			alert('Banners_engine::loadBanners: ' + e);
		}
	},
	
	requestBanners: function(data){
		try{
			var postData = [];
			for(var i=0; i<data.length; i++){
				postData.push('data['+data[i][0]+']='+ data[i][1]);
			}
			var BR = new Ajax.Request('http://www.doepunt.nl/banners/display/',
							 {	method:'get',
								parameters:postData.join('&'),
								onSuccess:this.bnrSuccess,
								onFailure:this.bnrFailure
							});
		}catch(e){
			alert('Banners_engine::requestBanners: ' + e);
		}
	},
	bnrSuccess: function(transport){
		try{
			var xmldoc		= transport.responseXML;
			if(xmldoc){
				var xmlroot 	= xmldoc.getElementsByTagName('banners').item(0);
				if(xmlroot){
					//Event.observe(window, 'load', function(){
					for(var iNode=0; iNode<xmlroot.childNodes.length; iNode++){
						var xmlnode = xmlroot.childNodes.item(iNode);
						if(xmlnode){
							var xmlsibl = xmlnode.childNodes.item(0);
							var bannerId = xmlnode.nodeName;
							if(xmlsibl && (node = $('bnr_'+bannerId))){
								$(node).update(xmlsibl.nodeValue);
								$(node).setStyle({'width':'auto', 'height':'auto', 'display':'block', 'clear':'both', 'textAlign':'center'});
							}
						}
					}
					//});
				}
			}
		}catch(e){
			alert('loadBanners::onSuccess: ' + e);
		}
	},
	bnrFailure: function(transport){
	},
	resizeIFrame: function(event){
		var element = event.element();
		var el = null;
		if(element.contentWindow){
			el = element.contentWindow.document;
		}else if (element.contentDocument){
			el = element.contentDocument;
		}
		var xheight = el.body.scrollHeight + 'px';
		element.setStyle({'height':xheight});
	}
});
document.observe('dom:loaded', function(){window.Banners = new Banners_engine();});
