/**
 *
 * ad loading, refreshing... this is the object that will be the layer between the Billboard
 * framework and the Ad Serving vendor. Currently that vendor is CheckM8 and we're using their
 * Ajax Smart Tag solution
 *
 */
billboard.ads = new ( function($)
{
	var me = this;

	me.init = function() 
	{
		billboard.info("Ads.init()");
		
		// set the default values at the window level
		window.CM8Server = "web.checkm8.com";
		window.CM8Cat = "vnu_music_bb.Homepage";

		// listen for page changes to refresh the ads
		billboard.broadcaster.addListener( "pageLoaded", function() { me.onPageLoaded() } );
	}

	/**
	 *
	 */
	me.onPageLoaded = function()
	{
		billboard.info( "Ads.onPageLoaded()" );
		me.updateAdInfo();
		me.refreshAllAds(); 
	}

	/**
	 * 
	 */
	me.updateAdInfo = function()
	{
		billboard.info( "Ads.onPageLoaded()" );
		// update ad serving properties
		// locate the new values, from div, or script and 
		var prof = $("#cm8 .profile").text();
		var cat = $("#cm8 .category").text();

		// update the window level variables used by CheckM8
		window.CM8Server = "web.checkm8.com";
		window.CM8Cat = cat.length>0?cat:"vnu_music_bb.Homepage";
		window.CM8Profile = prof.length>0?prof:"";
	}

	/**
	 * refreshAllAds()
	 *
	 * search the newly loaded page for any element with the 'ajax-ad' class
	 * pull its title attribute and use that as a load format for the cm8 ad 
	 * ajax call
	 */
	me.refreshAllAds = function()
	{
		billboard.info( "Ads.refreshAllAds()" );
		var formats = new Array();
		$(".ajax-ad").each(function() {
			var ajaxTitle = $(this).attr('title');
			formats.push(ajaxTitle);
		});

		if ( formats.length > 0 ) {
			var loadFormats = formats.join(",");
			CM8AjaxRefresh( loadFormats, window.CM8Profile, function( ud, id, ads ) {
				// hide containing divs, 'sponsored by' text for all ads that are not filled
				for ( var i=0;i<ads.length;i++ ) {
					billboard.info(ads[i]);
					
					// add the title attribute value back, since CM8 seems to strip it after loading an ad
					$(ads[i].element).attr("title", ads[i].format );
					

					// wallpaper gets a special treatment
					if ( ads[i].format.toLowerCase().indexOf("wallpaper") >= 0) {
						if ( ads[i].ad_id != null  ) { 
							var source = $(ads[i].element).find("img").attr("src");
							$("body").css( { 'background-image': 'url(' + source +')',
												 'background-repeat': 'no-repeat',
												 'background-position': 'top center'
												 }
										   );
							$(ads[i].element).find("img").css({'display': 'none'});
						} 
						else {
							// #D8D6D7 url(/images/backgrounds/body.gif) repeat-x scroll left top
							$("body").css( { 'backgroundImage': 'url(/images/backgrounds/body.gif)',
												 'backgroundRepeat': 'repeat-x',
												 'backgroundPosition': 'top left'
												 }
										   );
						}
					}
					
					else if(ads[i].format.toLowerCase().indexOf("hp_top")>=0 || ads[i].format.toLowerCase().indexOf("buzz")>=0 || ads[i].format.toLowerCase().indexOf("feed") >=0){
						if ( ads[i].ad_id == null || $(ads[i].element).find("a img").attr("height") == "1" ) { 
								billboard.log("removing"+ads[i].format);
								$(ads[i].element).parent().hide();								
							}
							else {
								billboard.log("showing: "+ads[i].ad_id+", format: "+ads[i].format );
								$(ads[i].element).parent().show();
							}
					
					
					}	
					// check the top (leaderboard) and the anchor ad
					else if (ads[i].format.toLowerCase().indexOf("anchor") >= 0 || ads[i].format.toLowerCase().indexOf("top") >=0 || ads[i].format.toLowerCase().indexOf("floating") >=0) {
						if ( ads[i].ad_id == null  || $(ads[i].element).find("a img").attr("height") == "1" ) { 
							billboard.log("removing"+ads[i].format);
							$(ads[i].element).parent().hide();								
						}
						else {
							billboard.log("showing: "+ads[i].ad_id+", format: "+ads[i].format );
							$(ads[i].element).parent().show();
							if(ads[i].format.toLowerCase().indexOf("top") >=0 && billboard.section == "charts"){
								$(ads[i].element).parent().css({'margin-bottom' : '-14px', 'margin-top' : '2px'});
							}
						}
					} 
					// check if an ad was not filled in the rest of the formats
					else if ( ads[i].ad_id == null  || $(ads[i].element).find("a img").attr("height") == "1" ) { 
						// check the chart item sponsor
						if (ads[i].format.toLowerCase().indexOf("chartsponsor") >= 0 ) {
							billboard.log("removing chartsponsor");
							$(ads[i].element).parents(".chart-item-ad-container").hide();	
						} 
						// check for any sponsorships on the page
						else if (ads[i].format.toLowerCase().indexOf("sponsor") >= 0 ) {
							billboard.log("removing sponsors");
							$(ads[i].element).parent().find(".sponsored-text").hide();								
						}
						// for middle ad in homepage
						else if (ads[i].format.toLowerCase() == 'middle' && $("#section").text()=='home' ) {
							billboard.log("removing"+ads[i].format);
							$(ads[i].element).parents(".module").hide();
							$("#noise-maker-module").css({"paddingTop":"282px"});
						}
						// check any other ad that is placed in a 'module' container
						else {
							billboard.log("removing"+ads[i].format);
							$(ads[i].element).parents(".module").hide();
						}
					}
				}
			});
		}
	}
})(jQuery);
