var whichad=1;
function RotateAds() {
	var i=0;
	$$( 'div.top-banner-rotate' ).each( function( ad ) {
		ad.setStyle( 'display', (i++==whichad?'block':'none') );
	});
	if(whichad>=(i-1)) whichad=0; else whichad++;
	
}
window.addEvent('domready', function(e) {

	var style6 = $$('div.listingAddress6').getStyle('color');
	string = style6.toString();	
	string = string.split(",");
	color = string[0].replace('#','');
	color = color.toUpperCase();
	$$('a.alist6.video').setStyle('background-image', "url('http://hurdman.app.bbb.org/images/videolinkicon-"+color+".png')");
	$$('a.alist6.video').setStyle('color', "#"+color);
	
	var style5 = $$('div.listingAddress5').getStyle('color');
	string = style5.toString();	
	string = string.split(",");
	color = string[0].replace('#','');
	color = color.toUpperCase();
	$$('a.alist5.video').setStyle('background-image', "url('http://hurdman.app.bbb.org/images/videolinkicon-"+color+".png')");
	$$('a.alist5.video').setStyle('color', "#"+color);
	
	/*var style4 = $$('a.alist4.video').getStyle('color');
	string = style4.toString();	
	string = string.split(",");
	color = string[0].replace('#','');
	color = color.toUpperCase();
	*/
	color = "0D7C89";
	$$('a.alist4.video').setStyle('background-image', "url('http://hurdman.app.bbb.org/images/videolinkicon-"+color+".png')");
	$$('a.alist4.video').setStyle('color', "#"+color);
	
	
	
	
	
	var tob = '';
	if( document.getElements('input[name=tob]').length > 0 ) {
		tob = document.getElements('input[name=tob]')[0].get('value').substr(0,5);
	}
	
	if( $('close-roster-video-player' ) ) {
		$('close-roster-video-player').addEvent( 'click', function(e) {
			e.stop();
			$('roster-video-player').setStyle( 'display', 'none' );
			$('videodata').set('html', '' );
		});
	}

	$$('a.alist.video').addEvent( 'click', function(e) {
		e.stop();
		$('videodata').set('html', '' );
		var filename = this.id.replace(/^ /,"");
		var listingbox = this.getParent().getParent();
		var url = this.href;
		new Request({
			url: url + '?inline=1',
			onComplete: function(response) {
				$('videodata').set( 'html', response );
				$('roster-video-player').setStyles({
					'display': 'block',
					'top': listingbox.getCoordinates().bottom,
					'left': listingbox.getCoordinates().left
				});
			}
		}).get();
		//var html = videostr.replace( /filename/gi, escape(filename) );

		
	});
	
	new Request({
    	url: CORE.path + '/ext/platypusid.php',
    	/*update: $('video-ad'),*/
    	onComplete: function(response) {
    		var parts = response.split("-->");
    		$('video-ad').set('html', parts[1]);
			new Asset.javascript(parts[0].replace("<!--",""));
    	}
    }).post({
    	bbbid: $('bbbid-js').get('value'),
    	platypus: $('setup5301').get('value'),
    	tob: tob,
    	campaignid: $('bbbvideocampaign').get('value')
    });


	// 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
	var curloc=new String(window.location.href);
//	alert(curloc);
	$$( 'span.createlink' ).each( function( link ) {
		if(link.lang.match("accredited-business-directory")) link.lang=curloc.replace(/\/accredited-business-directory.*$/g, link.lang);
//		if(link.get('html')=="Map") link.lang="http://maps.yahoo.com/py/maps.py?"+link.lang;
		var addr=link.lang.replace(/addr=/g, "");
		addr=addr.replace(/&zip=/g, ", ");
		if(link.get('html')=="Map") link.lang="http://maps.google.com/?q="+addr;
		var newlink = new Element('a', {
			'class': link.get('class'), 
			lang: (link.get('html')=="Map"?"Map":""),
			target: (link.get('html')=="Map"?"_blank":""), 
			href: link.lang, 
			html: link.get('html')
		});
		newlink.inject(link, 'before');
	});
	var rotateads=false;
	$$( 'div.top-banner-rotate' ).each( function( ad ) {
		ad.setStyle( 'display', (rotateads?'none':'block') );
		rotateads=true;
	});
	if(rotateads) {
		(function() { RotateAds() }).periodical(7000);
	}
	$$( '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 ) {
//			if(link.get('lang')=='Map') {
//				link.set('href',link.getChildren('div.maplink')[0].lang);
//			}
			// 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': '#333',
                       			//'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': window.getScroll().y+100,//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' );
			}
		});
	});
	// set maximu height
	/*var maxheight = 115;
	$$( 'div.banner' ).each( function(div,i) {
		if (div.getCoordinates().height>maxheight) {
			maxheight = div.getCoordinates().height;
		}
	});
	$$( 'div.info' ).setStyle('height', maxheight+'px');*/
});
