(function(){

	window.__FUBRAD__ = new function() {

		var w = window, d = document, l = w.location, b = {}, u, t = this;

		t.S = function() {

			if ( w.fubra_ad_num && w.fubra_ad_num == 3 ) { return; }
			if ( !w.fubra_ad_num ) { w.fubra_ad_num = 1; } else { w.fubra_ad_num++; }
			if ( !w.fubra_google_skip ) { w.fubra_google_skip = 0; }
			if ( !w.fubra_ad_skip ) { w.fubra_ad_skip = 0; }
			if ( !w.fubra_ad_user && ( u = t.C( 'fubra_ad_user' ) ) ) { w.fubra_ad_user = u; }
			if ( !w.fubra_flash_test ) { t.F(); }

			b.id = '__fad__' + t.M();
			t.D( 'partner', w.fubra_ad_partner );
			t.D( 'width', w.fubra_ad_width );
			t.D( 'height', w.fubra_ad_height );
			t.D( 'type', w.fubra_ad_type );
			t.D( 'max', w.fubra_ad_max );
			t.D( 'kw', w.fubra_ad_kw );
			t.D( 'style', w.fubra_ad_style );
			t.D( 'style_border', w.fubra_ad_border );
			t.D( 'style_background', w.fubra_ad_background );
			t.D( 'style_text', w.fubra_ad_text );
			t.D( 'style_title', w.fubra_ad_title );
			t.D( 'style_url', w.fubra_ad_url );
			t.D( 'style_provider', w.fubra_ad_provider );
			t.D( 'channel', w.fubra_ad_channel );
			t.D( 'zone', w.fubra_ad_zone );
			t.D( 'test', w.fubra_ad_test );
			t.D( 'skip', w.fubra_ad_skip );
			t.D( 'google_skip', w.fubra_google_skip );
			t.D( 'user', w.fubra_ad_user );
			t.D( 'flash', w.fubra_flash_enabled );
			t.D( 'flash_version', w.fubra_flash_version );
			t.D( 'os', navigator.platform );
			t.D( 'url', l.href );
			t.D( 'host', l.hostname );
			t.D( 'block', w.fubra_ad_num );
			t.D( 'adslot', w.fubra_ad_adslot );
			t.R();
			t.W();

			return;

		};

		t.W = function() {
			
			d.write( '<' + 'script id="' + b.id + '_fetch" type="text/javascript" src="http://a.fubra.com/advertiser/publish/serve_ads.html' + t.U( b ) + '"></' + 'script>\n' );
		
		};

		t.M = function() {

			return Math.floor( Math.random() * Math.pow( 10, 10 ) ).toString() + '__' + w.fubra_ad_num;

		};

		t.D = function( name, value ) {

			if ( value && typeof value != 'undefined' ) {
				b[name] = value;
			}

		};

		t.U = function( a ) {

			var u = '';

			for ( var i in a ) {
				u += ( u.length == 0 ? '?' : '&' ) + t.N( i ) + '=' + t.N( a[i] );
			}

			return u;
		
		};

		t.N = function( val ) {
		
			return encodeURIComponent( val );

		};

		t.C = function( n ) {

			var cs = document.cookie.split( ';' );

			for ( var i = 0; i < cs.length; i++ ) {
				var c = cs[i];
				while ( c.substring( 0, 1 ) == ' ' ) {
					c = c.substring( 1, c.length );
				}
				var pos = c.indexOf( '=' );
				if ( n == c.substring( 0, pos ) ) {
					return ( c.substring( pos + 1, c.length ) );
				}
			}

			return false;

		};

		t.F = function() {

			var p, f = false, v = false, n = navigator;

			if ( n.plugins && n.plugins.length ) {
				p = n.plugins['Shockwave Flash'];
				if ( p ) {
					f = true;
					if ( p.description ) {
						n = p.description;
						v = n.charAt( n.indexOf( '.' ) - 1 );
					}
				}
			} else if ( n.mimeTypes && n.mimeTypes.length ) {
				p = n.mimeTypes['application/x-shockwave-flash'];
				if ( p && p.enabledPlugin ) {
					f = true;
				}
			} else {
				for ( var ver = 9; ver > 0; ver-- ) {
					try {
						p = new ActiveXObject( 'ShockwaveFlash.ShockwaveFlash.' + ver );
						f = true;
						v = ver;
						break;
					} catch( e ) {}
				}
			}			

			if ( f ) {
				w.fubra_flash_enabled = true;
				if ( v ) { w.fubra_flash_version = v; }
			}

			w.fubra_flash_test = true;
			
		};

		t.R = function() {

			var n = null;

			w.fubra_ad_partner = n;
			w.fubra_ad_width = n;
			w.fubra_ad_height = n;
			w.fubra_ad_type = n;
			w.fubra_ad_max = n;
			w.fubra_ad_kw = n;
			w.fubra_ad_style = n;
			w.fubra_ad_channel = n;
			w.fubra_ad_zone = n;
			w.fubra_ad_border = n;
			w.fubra_ad_background = n;
			w.fubra_ad_text = n;
			w.fubra_ad_title = n;
			w.fubra_ad_url = n;
			w.fubra_ad_provider = n;
			w.fubra_ad_adslot = n;

		};

		t.O = function( b, a, l, s, f ) {
		
			var e = d.getElementById( b + '_fetch' );
			var p = e.parentNode;

			if ( a.length == 0 ) { return; }

			if ( typeof fubra_ad_custom == 'function' ) {

				for ( var j in l ) {
					if ( l[j].ads == a.length ) {
						l = l[j]; break;
					}
				}

				for ( var i = 0; i < a.length; i++ ) {
					/* if ( a[i].bidtype && a[i].bidtype == 'CPC' ) { */
						window.fubra_google_skip++;
					/* } */
				}

				var B = d.getElementById( b );
					B.parentNode.removeChild( B );

				var c = d.createElement( 'DIV' );
					c.id = b + '_content';
				
				p.insertBefore( c, e );

				return fubra_ad_custom( c, a, l, s, f );
			
			}

			var o = '<html><head><title>Fubra Advertiser</title></head><body>';

			if ( a[0].type == 'flash' ) {

				o += 	'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
						' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + a[0].image_width + '" height="' + a[0].image_height + '">' +
						'<param name="movie" value="' + a[0].image_url + '">' +
						'<param name="quality" value="high">' +
						'<param name="AllowScriptAccess" value="never">' +
						'<embed src="' + a[0].image_url + '" width="' + a[0].image_width + '" height="' + a[0].image_height + '" type="application/x-shockwave-flash"' +
						' AllowScriptAccess="never" ' +
						' pluginspace="http://www.macromedia.com/go/getflashplayer"></embed></object>';
			
			} else if ( a[0].type == 'image' ) {

				o +=	'<a href="' + a[0].url + '" target="_top" title="Go to: ' + a[0].visible_url + '" onmouseover="window.status=\'' + a[0].visible_url + '\'" onmouseout="window.status=\'\'">' +
						'<img style="border: 0;" src="' + a[0].image_url + '" width="' + a[0].image_width + '" height="' + a[0].image_height + '" />' +
						'</a>';

			} else if ( a[0].type == 'html' ) {

				o += a[0].snippet;

			} else {
			
				o +=	'<div style="position: relative; width: ' + s.width + 'px; height: ' + s.height + 'px; overflow: hidden;">' +
						'<table style="table-layout: fixed; font: 12px arial, verdana, sans-serif; border: 1px solid ' + t.P( s.border ) + '; width: ' + s.width + 'px; height: ' + s.height + 'px; background: ' + t.P( s.background ) + ';"><tr>';

				for ( var i = 0; i < a.length; i++ ) {

					for ( var j in l ) {
						if ( l[j].ads == a.length ) { break; }
					}

					window.fubra_google_skip++;

					o +=	'<td style="text-align: ' + l[j].align + '; padding: 2px; vertical-align: ' + l[j].vertical + ';">' + 
							'<a style="color: ' + t.P( s.title ) + '; font-size: ' + l[j].title + '; text-decoration: underline;" target="_top" href="' + a[i].url  + '" title="Go to: ' + a[i].visible_url + '" onmouseover="window.status=\'' + a[i].visible_url + '\'" onmouseout="window.status=\'\'">' + a[i].line1 + '</a><br />' +
							'<span style="color: ' + t.P( s.desc ) + '; font-size: ' + l[j].desc + '; line-height: ' + l[j].line_height + 'px;">'  + a[i].line2 + ' ' + a[i].line3 + '<br /></span>';

					if ( l[j].show_url === true ) {
						o += '<a style="color:' + t.P( s.url ) + '; font-size: ' + l[j].url + ';" target="_top" href="' + a[i].url  + '" title="Go to: ' + a[i].visible_url + '" onmouseover="window.status=\'' + a[i].visible_url + '\'" onmouseout="window.status=\'\'">' + a[i].visible_url + '</a>';
					}

					o +=	'</td>';

					if ( s.format == 'vertical' ) {
						o += '</tr></tr>';
					}
				
				}

				o +=	'</tr></table>' +
						'<a href="' + f.url + '" target="top" style="font: 11px arial, verdana; text-decoration: none; position: absolute; bottom: 1px; right: 4px; border-bottom: 1px solid ' + t.P( s.background ) + '; color: ' + t.P( s.provider ) + '; padding: 0 5px; line-height: 10px">' + f.label + '</a>' +
						'</div>';

			}

			o += '</body></html>';

			t.I( b, o );

			p.removeChild( e );

			return;

		};

		t.P = function( c ) {

			return c ? c.substring( 0, 1 ) != '#' ? '#' + c : c : null;

		};

		t.I = function( b, c ) {

			var f = document.getElementById( b ).contentWindow.document;
				f.open( 'text/html', 'replace' );
				f.write( c );
				f.close();

		};

		t.T = function( i ) {

			if ( !i ) { return; };

			var d = new Date();
				d.setDate( d.getDate() + 1 );

			document.cookie = 'fubra_ad_user=' + encodeURIComponent( i ) + ';expires=' + d.toGMTString() + ';path=/';

			return;

		};

		t.S();

	}

})();