/*
* Copyright (c) 2007 Andriy Bidochko.  
* http://www.mapbuilder.net
*
*/

// mbAdsManager - class responsible for mapbuilder ads rendering
var mbAdsManager = function (map, opt) {
	// Constructor
	this.init(map, opt);
}
      
mbAdsManager.prototype = {
	map:null,
	adContainer: null, 
	//lat49AdContainer ad container
	lat49AdContainer: null,
	adsRegistry: [], 
	adDomNode: null, 
	// Register 3rd party ad systems
	adSystems: {lat49: true},
       
	init:function(map, opt){
		// Register system ads
		this.registerAds();

		this.map = map;
		var isLat49Loaded = (typeof Lat49 !== 'undefined');
		opt = opt || {
			lat49: {enabled : true} 	
		}
		this.adSystems.lat49 = ((isLat49Loaded && opt.lat49.enabled) || false);
		
		/* Create ad container */
		this.adContainer=document.createElement('div');
		this.adContainer.id='adcontainer';
		this.adContainer.style.position='absolute';
		this.adContainer.style.verticalAlign='bottom';
		this.adContainer.style.right='5px';
		this.adContainer.style.bottom='25px';
		this.adContainer.style.width='242px';
		this.adContainer.style.height='133px';
		this.adContainer.style.zIndex=99999;
		this.adContainer.style.backgroundColor='transparent';

		this.map.getContainer().appendChild(this.adContainer);

		if (this.isAdSystemAvailable('lat49')) {
			/* Create lat49 ad container */
			this.lat49AdContainer=document.createElement('div');
			this.lat49AdContainer.id='lat49AdContainer';
			this.lat49AdContainer.setAttribute('lat49adposition', 'bottom-right');
			this.lat49AdContainer.style.position='absolute';
			this.lat49AdContainer.style.verticalAlign='bottom';
			this.lat49AdContainer.style.right='5px';
			this.lat49AdContainer.style.bottom='158px';
			/*I would suggest is that you remove the
			* height & width paramaters from the div you have set up for the lat49 ads. If
			* you leave the height & width parameters in there, you'll find that in FF, if
			* you mouse over the ad area when there is no ad, you will have some strange
			* behavior where the mouse doesn't behave as expected in that region. 
			*
			* this.lat49AdContainer.style.width='242px';
			* this.lat49AdContainer.style.height='133px';
			*/
			this.lat49AdContainer.style.marginBottom='8px';
			this.lat49AdContainer.style.zIndex=99999;
			this.lat49AdContainer.style.backgroundColor='transparent';
			this.map.getContainer().appendChild(this.lat49AdContainer);
			
			Lat49.initAds(3);

			// Register a helper function to check is ad is available
			Lat49.AdHelper.prototype.isAdAvailable = function(divId) {
				var adDiv = document.getElementById(divId);
				if (adDiv == null)
					return false;
				var available = false;
				var child = adDiv.firstChild;
				while (child) {
					var childID = child.id;
					if (childID && (childID.substring(0,7) == "lat49ad")) {
						// Check if ad is available. Lat49 puts ads inside div with id like 
						// <div id="adcontainer" lat49adposition="bottom-right" style="position: absolute; vertical-align: bottom; right: 5px; bottom: 25px; width: 234px; height: 60px; z-index: 99999; background-color: transparent;">
						if (child.innerHTML != "") {
							available = true;
							break;
						}
					}
					child = child.nextSibling;
				}
				return available;
			}

		}
		// This event is fired when the change of the map view ends.
		GEvent.bind(this.map, "moveend", this, this.adListener);

		// Fire showAd event to display the Mapbuilder Ad for initial map state
		this.showAd(true);
	},

	adListener: function(){
		this.showAd(false);
	},

	showAd:function(forceInternal){
		var center = this.map.getCenter();
		var lat = center.lat();
		var lon = center.lng();
		var zoomlevel = this.map.getZoom();

		// Get ad object
		var ad = this.getAdObject();

		// Remove previous ad
		if (this.adDomNode != null) {
			this.adContainer.removeChild(this.adDomNode);
			this.adDomNode = null;
		}

		// Flag which controld output of MapBuilder ads
		var internalAd = true;

		if (internalAd || forceInternal){

			this.adDomNode = ad.getDomNode();

			// Get ad width and height	
			this.adContainer.style.width=this.adDomNode.style.width;
			this.adContainer.style.height=this.adDomNode.style.height;

			// Append ad node
			this.adContainer.appendChild(this.adDomNode);

			// Chenge lat 49 position
			if (this.isAdSystemAvailable('lat49') && (zoomlevel > 4)) {
				this.lat49AdContainer.style.bottom = (parseInt(this.adContainer.style.height) + 25) + 'px';
			}
		}

		// Check lat49 ad availability
		if (this.isAdSystemAvailable('lat49') && (zoomlevel > 4)) {
			var zoomlevel = Lat49.Tile.convertGMap2Zoom(this.map.getZoom());

			Lat49.updateAdByLatLon(this.lat49AdContainer.id, lat, lon, zoomlevel);


			/*
			* // Check if lat49 is available (hack)
			* var available = Lat49.getAdHelper().isAdAvailable(this.lat49AdContainer.id);
			* 
			* if (available) {
			* 	internalAd = false;
			* }
			*/
		}

	},


	getContainer:function(){
		return this.adContainer;
	},

	registerAds:function(){
		this.adsRegistry.push( new mbAdsManager.Ad(234, 60, "http://re4web.com", "http://media.re4web.com/ads/re4web-234x60-white.png", "Real Estate Software for your web 2.0 domain"));
		this.adsRegistry.push( new mbAdsManager.Ad(125, 125, "http://re4web.com", "http://media.re4web.com/ads/re4web-125x125-blue.png", "Real Estate Software for your web 2.0 domain"));
		this.adsRegistry.push( new mbAdsManager.Ad(234, 50, "http://mashuptechnologies.com/re/main/content/Services", "", "Mashup Technologies - web and mashups development. Affordable rates and superior quality!"));
		this.adsRegistry.push( new mbAdsManager.Ad(234, 50, "http://www.mapbuilder.net/Business.php", "", "Professional Google Maps mashups development or integration. Send request now!"));
		// BlueHost
		//this.adsRegistry.push( new mbAdsManager.Ad(234, 60, "http://www.bluehost.com/track/bidochko/ADS-CODE17", "http://img.bluehost.com/234x60/2.gif", "Best hosting ever.Proved! SIGNUP FOR BLUEHOST.COM NOW!"));
		this.adsRegistry.push( new mbAdsManager.Ad(234, 60, "http://www.bluehost.com/track/bidochko/ADS-CODE18", "http://img.bluehost.com/234x60/1.gif", "Best hosting ever.Proved! SIGNUP FOR BLUEHOST.COM NOW!"));
		this.adsRegistry.push( new mbAdsManager.Ad(125, 125, "http://www.bluehost.com/track/bidochko/ADS-CODE5", "http://img.bluehost.com/125x125/2.gif", "Best hosting ever.Proved! SIGNUP FOR BLUEHOST.COM NOW!"));
		//this.adsRegistry.push( new mbAdsManager.Ad(125, 125, "http://www.bluehost.com/track/bidochko/ADS-CODE6", "http://img.bluehost.com/125x125/1.gif", "Best hosting ever.Proved! SIGNUP FOR BLUEHOST.COM NOW!"));
		this.adsRegistry.push( new mbAdsManager.Ad(120, 120, "http://www.bluehost.com/track/bidochko/ADS-CODE37", "http://img.bluehost.com/120x120/1.gif", "Best hosting ever.Proved! SIGNUP FOR BLUEHOST.COM NOW!"));
		// Spot Crime
		this.adsRegistry.push( new mbAdsManager.Ad(234, 60, "http://crimebaltimore.com", "http://crimebaltimore.com/media/logo/crimebaltimore.com-234x60.png", " The most comprehensive online source of crime information. Local maps, crime reports and classifications, real-time interactive maps."));
		this.adsRegistry.push( new mbAdsManager.Ad(234, 60, "http://crimebaltimore.com", "http://crimebaltimore.com/media/logo/crimebaltimore.com-234x60.png", " The most comprehensive online source of crime information. Local maps, crime reports and classifications, real-time interactive maps."));
		this.adsRegistry.push( new mbAdsManager.Ad(234, 60, "http://spotcrime.com", "http://spotcrime.com/media/logo/spotcrime.com-234x60.png", " The most comprehensive online source of crime information. Local maps, crime reports and classifications, real-time interactive maps."));
		this.adsRegistry.push( new mbAdsManager.Ad(234, 60, "http://spotcrime.com", "http://spotcrime.com/media/logo/spotcrime.com-234x60.png", " The most comprehensive online source of crime information. Local maps, crime reports and classifications, real-time interactive maps."));
		this.adsRegistry.push( new mbAdsManager.Ad(234, 60, "http://spotcrime.com", "http://spotcrime.com/media/logo/spotcrime.com-234x60.png", " The most comprehensive online source of crime information. Local maps, crime reports and classifications, real-time interactive maps."));

		this.adsRegistry.push( new mbAdsManager.Ad(234, 62, "http://www.nileguide.com", "http://www.mapbuilder.net/img/ads/nileguide_com-234x62.jpg", "NileGuide's Trip Planner and Travel Guides"));
		this.adsRegistry.push( new mbAdsManager.Ad(234, 62, "http://www.nileguide.com", "http://www.mapbuilder.net/img/ads/nileguide_com-234x62.jpg", "NileGuide's Trip Planner and Travel Guides"));
		this.adsRegistry.push( new mbAdsManager.Ad(234, 62, "http://www.nileguide.com", "http://www.mapbuilder.net/img/ads/nileguide_com-234x62.jpg", "NileGuide's Trip Planner and Travel Guides"));
		this.adsRegistry.push( new mbAdsManager.Ad(234, 62, "http://www.nileguide.com", "http://www.mapbuilder.net/img/ads/nileguide_com-234x62.jpg", "NileGuide's Trip Planner and Travel Guides"));
		/*
		// GoDaddy
		this.adsRegistry.push( new mbAdsManager.Ad(125, 125, "http://www.jdoqocy.com/click-2623398-10379076", "http://www.lduhtrp.net/image-2623398-10379076", "Go Daddy $1.99 Domains"));
		this.adsRegistry.push( new mbAdsManager.Ad(120, 60,  "http://www.jdoqocy.com/click-2623398-10497118", "http://www.lduhtrp.net/image-2623398-10497118", "Go Daddy $1.99 Domains"));
		this.adsRegistry.push( new mbAdsManager.Ad(125, 125, "http://www.tkqlhce.com/click-2623398-10386908", "http://www.awltovhc.com/image-2623398-10386908", "GoDaddy.com Hosting & Servers"));
		// Beyond.com 
		this.adsRegistry.push( new mbAdsManager.Ad(125, 125, "http://www.kqzyfj.com/click-2623398-10386734", "http://www.ftjcfx.com/image-2623398-10386734", "Beyond.com - Free Tools for Job Seekers"));
		*/
		/* depricated 
		// TigerGPS.com 
		this.adsRegistry.push( new mbAdsManager.Ad(234, 50, "http://www.jdoqocy.com/click-2623398-10446499", "", "Free Shipping on orders over $250 at TigerGPS.com"));
		this.adsRegistry.push( new mbAdsManager.Ad(120, 240, "http://www.tkqlhce.com/click-2623398-10443480", "http://www.ftjcfx.com/image-2623398-10443480", "Looking for the best GPS deals on the net? Check out what's on sale this week at Tiger GPS!"));
		this.adsRegistry.push( new mbAdsManager.Ad(120, 240, "http://www.dpbolvw.net/click-2623398-10443482", "http://www.ftjcfx.com/image-2623398-10443482", "Looking for the best GPS deals on the net? Check out what's on sale this week at Tiger GPS!"));
		this.adsRegistry.push( new mbAdsManager.Ad(120, 240, "http://www.jdoqocy.com/click-2623398-10443481", "http://www.ftjcfx.com/image-2623398-10443481", "Looking for the best GPS deals on the net? Check out what's on sale this week at Tiger GPS!"));
		//Gift Card Lab
		this.adsRegistry.push( new mbAdsManager.Ad(120, 240, "http://www.kqzyfj.com/click-2623398-10505267", "http://www.ftjcfx.com/image-2623398-10505267", "Gift Card Lab - Create Photo Gift Cards Online"));
		*/
	},

	getAdObject:function(){
		var randomNum = Math.floor(Math.random() * (this.adsRegistry.length));
		return this.adsRegistry[randomNum];
	},

	/* Check if ad system is available */
	isAdSystemAvailable: function (system) {
		return (this.adSystems[system]) ? true : false;
	}

}

/* Ad class */
mbAdsManager.Ad = function (width, height, url, src, text) {
	// Constructor
	this.init(width, height, url, src, text);
}
mbAdsManager.Ad.prototype = {
	width:null,
	height:null,
	src:null,
	url:null,
	text:null,
	adContainer: null,
        
	init:function(width, height, url, src, text){
		this.width = width;
		this.height = height;
		this.src = src;
		this.url = url;
		this.text = text;
	},

	getDomNode: function(){
		/* Create ad container */
		this.adContainer=document.createElement('div');
		this.adContainer.style.width= this.width.toString() + 'px';
		this.adContainer.style.height= this.height.toString() + 'px';
		// Do we deal with text or image ad?
		if (this.src == "") {
			this.adContainer.innerHTML = '<a target="_blank" style="text-decoration: none; " title="' + this.text + '" href="' + this.url + '">' + this.text + '</a>';
			this.adContainer.style.backgroundColor='#FFFFFF';
			this.adContainer.style.border='1px solid black';
			this.adContainer.style.padding='3px';
			this.adContainer.style.font='12px arial,helvetica,clean,sans-serif';
		}
		else {
			this.adContainer.innerHTML = '<a target="_blank" title="' + this.text + '" href="' + this.url + '"><img alt="' + this.text + '" src="' + this.src + '" style="border:0px;"></a>';
		}
		return this.adContainer;
	}
}