

var AanbiedingBeheer = new Class({
	
	Implements: Options,
	options: {
		width: 400,
		project: null,
		adverteerder: 0,
		hash: 0,
		url: 'http://agenda.web-effects.nl/aanbiedingenkorting/adverteerder',
		language: 'nl'
	},
	
	initialize: function( options ) {		
		this.setOptions( options );
		
		document.write( '<div id="framecontainer"></div>' );

		// the iframe container
		this.container = $('framecontainer');
		
		var url = this.options.url + "?project=" + this.options.project + "&adverteerder=" + this.options.adverteerder + "&hash=" + this.options.hash;
		if ( this.options.language ) url += "&lang=" + encodeURIComponent( this.options.language );
		
		// the iframe
		this.iframe = new Element( 'iframe', {
			src: url,
			width: this.options.width,
			height: 1000,
			frameborder: 0,
			marginheight: 0,
			marginwidth: 0,
			scrolling: 'auto'			
		}).inject( this.container );
	}
});


var AdvertentieAanbieding = new Class({
	Implements: Options,
	options: {
		width: 400,
		height: 300,
		container: null,
		project: null,
		aanbiedingidx: 0,
		advertentieidx: 0,
		url: 'http://agenda.web-effects.nl/aanbiedingenkorting/toonaanbieding',
		language: 'nl'
	},
	
	initialize: function( options ) {
		this.setOptions( options );
		
		this.showing = false;
	},
	
	show: function() {
		
		if ( this.showing ) return;
		this.showing = true;
		
		var url = this.options.url + "?project=" + this.options.project + "&aanbiedingidx=" + this.options.aanbiedingidx + "&advertentieidx=" + this.options.advertentieidx + "&referer=" + encodeURIComponent( window.location );
		if ( this.options.language ) url += "&lang=" + encodeURIComponent( this.options.language );
		
		// the iframe
		this.iframe = new Element( 'iframe', {
			src: url,
			width: this.options.width,
			height: this.options.height,
			frameborder: 0,
			marginheight: 0,
			marginwidth: 0,			
			scrolling: 'no'			
		}).inject( this.options.container );
		
		
		new PostMessage( this.iframe, {
			domain: 'http://agenda.web-effects.nl',
			onReceive: function ( message ) {
	
				message = message.split( ':' );
				
				if ( message[0] != this.options.aanbiedingidx )
					return;
				
				var height = this.iframe.getSize().y;
				var newheight = message[1].toInt();
					
				if ( window.console ) console.log( "height: " + this.options.aanbiedingidx + " " + height );
				if ( window.console ) console.log( "newheight: " + newheight );
					
				if ( height < newheight )	
					this.iframe.set( "height", newheight ).setStyle( "height", newheight + 20 );
					
			}.bind( this )
		});
	}	
});

var AanbiedingenEnKorting = new Class({
	Implements: Options,
	options: {
		width: 400,
		container: null,
		project: null,
		url: 'http://agenda.web-effects.nl/aanbiedingenkorting/aanbiedingen',
		language: 'nl'
	},
	
	initialize: function( options ) {
		this.setOptions( options );	
			
		document.write( '<div id="framecontainer"></div>' );

		// the iframe container
		this.container = $('framecontainer');
				
		var url = this.options.url + "?project=" + this.options.project;
		if ( this.options.provincie ) url += "&provincie=" + encodeURIComponent( this.options.provincie );
		if ( this.options.filter ) url += "&filter=" + encodeURIComponent( this.options.filter ); 
		if ( this.options.language ) url += "&lang=" + encodeURIComponent( this.options.language );
		url += "&referer=" + encodeURIComponent( window.location );
		
		// the iframe
		this.iframe = new Element( 'iframe', {
			id: "aek-iframe",
			src: url,
			width: this.options.width,
			height: 3000,
			frameborder: 0,
			marginheight: 0,
			marginwidth: 0,
			scrolling: 'no'			
		}).inject( this.container );
		
		window.addEvent( "domready", function() {
			new PostMessage( document.getElementById( 'aek-iframe' ), {
				domain: 'http://agenda.web-effects.nl',
				onReceive: function ( message ) {
	
					var height = this.iframe.getSize().y;
					var newheight = message.toInt();
					
					if ( window.console ) console.log( "newheight: " + newheight );
					
					if ( height < newheight )	
						this.iframe.set( "height", newheight ).setStyle( "height", newheight + 200 );
					
				}.bind( this )
			});

		}.bind( this ));
		
	}	
	
});
