( function() {
	var isAutorotationActive = true;
	var batchSize = 1;
	var pauseTime = 0;

	function getWideBanners() {
		jQuery.getJSON( '/json/banners/wide/' + batchSize + '/?' + new Date().getTime(), function( banners ) {
			if ( banners == undefined ) {
				if ( console != undefined ) {
					console.warn( 'No wide banners defined.' );
				}
				return;
			}
			jQuery( '#horizontal-banners' ).empty();
			jQuery.each( banners, function( i, banner) {
				if ( banner == undefined || banner.extension == undefined || banner.extension == '' ) {
					if ( console != undefined ) {
						console.warn( 'Invalid wide banner definition.' );
					}
					return;
				}
				if ( banner.extension == 'swf' ) {
					var flashvars = {
						clickTAG: '/banner/click/' + banner.banner_id + '/'
					};
					var params = {};
					var attributes = {
						styleclass: 'horizontal-banner',
						style: 'margin-bottom: 10px'
					};

					var container = jQuery( '<div/>' ).attr( 'name', 'horizontal-banner-' + banner.banner_id ).attr( 'id',
						'horizontal-banner-' + banner.banner_id ).attr( 'class', 'horizontal-banner' );
					container.appendTo( '#horizontal-banners' );
					swfobject.embedSWF( '/banners/' + banner.filename + '.' + banner.extension, 'horizontal-banner-' +
						banner.banner_id, banner.width, banner.height, '9.0.0','/swf/expressInstall.swf', flashvars, params,
						attributes );
				}
				else {
					var anchor = jQuery( '<a/>' ).attr( 'href', '/banner/click/' + banner.banner_id + '/' ).attr( 'target',
						'_blank' ).attr( 'class', 'horizontal-banner' );
					jQuery( '<img/>' ).attr( 'src', '/banners/' + banner.filename + '.' + banner.extension ).attr( 'style',
						'margin-bottom: 10px' ).appendTo( anchor );
					anchor.appendTo( '#horizontal-banners' );
				}
			} );
			jQuery( '#horizontal-banners' ).css( 'display', 'block' );
		} );
	}

	jQuery( document ).ready( function() {
		jQuery.getJSON( '/json/autorotation-configuration/', function( configuration ) {
			isAutorotationActive = ( configuration.isAutorotationActive == '1' ) ? true : false;
			batchSize = parseInt( configuration.batchSize );
			pauseTime = parseInt( configuration.pauseTimeInSeconds );
			getWideBanners();
			if ( isAutorotationActive && batchSize > 0 && pauseTime > 0 ) {
				setInterval( getWideBanners, pauseTime * 1000 );
			}
			else {
				if ( console != undefined ) {
					console.warn( 'Invalid autorotation configuration.' );
					console.warn( configuration );
				}
			}
		} );
	} );
} )();
