window.addEvent('domready', function(e) {

	// setup the timer variable and plade it here for scope reasons
	var timer;
	// hash to store what has been logged
	var logged = new Hash({
		'full': [],
		'phone': []
	});

	// for IE6 add a -3 margin
	if( Browser.Engine.trident4 ) $$( 'div.top' ).setStyle( 'margin-left', '-3px' );
	// add the mouseovers
	$$( 'div.listing' ).each( function( listing ) {
		// set the bid
//		var lang = listing.getElements( 'input.info' )[0];
		var bid = listing.lang.split(":")[0];
		var append = listing.lang.replace(/-AMP-/gi,"&").replace(/-equals-/gi,"=").split(":")[1];
		// add the click event to each link
		listing.getElements('a').each( function( link ) {
			// events			
			link.addEvents({
				'click': function(e) {
					if( !link.hasClass('fullad') ) {
						logstat2( '?bid=' + bid + '&type=' + this.lang + append );
					}
				},
				'mouseenter': function(e) {
					// make sure it is a full ad
					if( link.hasClass('fullad') ) {
						
						// log the stat
						if( !logged.full.contains(bid) ) {
							logstat2( '?bid=' + bid + '&type=Full' + append );
							logged.full.push(bid);
						}
						// dimmer
                       	new Element( 'div', {
                       		'id': 'shadow',
                       		'styles': {
                       			'position': 'absolute',
                       			'left': 0,
                       			'top': 0,
                       			'width': window.getScrollSize().x,
                       			'height': window.getScrollSize().y,
                       			'background-color': '#000000',
                       			'opacity': 0.7,
                       			'z-index': 20000
                       		}
                       	}).inject( document.body );
                       	// ad container
   						var contain = new Element( 'div', {
                       		'id': 'ad_contain',
                       		'styles': {
                       			'position': 'absolute',
                       			'width': 800,
                       			'left': Math.round( ( ( window.getCoordinates().width - parseFloat( 800 ) ) / 2 ), 1 ),
                       			'top': link.getCoordinates().top.toInt()-100,
                       			'z-index': 100000
                       		}
                      	}).inject( document.body );
                      	// the actual ad
                       	var ad = new Element( 'div', {
                       		'class': 'fullad',
                       		'html': link.lang.replace("::",""),
                       		'styles': {
                       			'margin-left': 'auto',
                       			'margin-right': 'auto',
                       			'text-align': 'center'
                       		}
                       	}).inject( contain, 'inside' );
                       	// add the mouseleave to the img
                       	ad.getElements('img')[0].addEvent( 'mouseleave', function(e) {
                       		this.dispose();
                       		$('ad_contain').dispose();
                       		$('shadow').dispose();
                       	});
					}
				}
			});
		});
		// add the mouse enter/move/leave events
		listing.addEvents({
			'mouseenter': function(e) {
				// the function to log a phone stat
				phoneStat = function() {
					if( !logged.phone.contains( bid ) ) {
						logstat2(  '?bid=' + bid + '&type=Phone' + append );
						logged.phone.push(bid);
					}
				}
				timer = phoneStat.delay(2000);
				// modify the bubble
				$( 'bubble' ).setStyles({
					'display': 'block',
					'left': e.client.x + 25,
					'top': this.getCoordinates().top - 120,
					'position': 'absolute',
					'z-index': 10000
				});
				// set the business name, address, phone, accredited since
				$$( 'div#bubble div.dba' )[0].set( 'html', this.getElements( 'a.dba' )[0].get( 'html' ) );
				$$( 'div#bubble div.phone' )[0].set( 'html', this.getElements( 'span.phone' )[0].get( 'html' ) );
				$$( 'div#bubble div.since' )[0].set( 'html', this.getElements( 'span.since' )[0].get( 'html' ) );
				$$( 'div#bubble div.moreinfo' )[0].set( 'html', this.getElements( 'span.locations' )[0].get( 'html' ) );
			},
			'mousemove': function(e) {
				// make sure we don't go to far to the right
				if( ( e.page.x + $('bubble').getCoordinates().width ) < window.getCoordinates().width ) $('bubble').setStyle( 'left', e.page.x+25 );
			},
			'mouseleave': function(e) {
				$clear(timer);
				$( 'bubble' ).setStyle( 'display', 'none' );
			}
		});
	});

});
