

/*----------------------------------------------------------------------------------
		  Class  for dynamically controlling the display of the Advertisement's iframe 
		  
		  CONSTANTS:  kDefaultAdvertFrequency,kInfiniteShow,kDefaultMaxShows
		  INSTANCE VARS:adSrc
*/
	function onAdClick(){
		widget.openURL(this.src);
	}
	

	var Advert = Class.create(
	{
		
		initialize:  function(inParams)
		{
			this.kDefaultAdvertFrequency = 10;
			this.kDefaultVersion = 1.0;
			if (inParams === undefined)
			{
				inParams = {};
			}
			this.adsShown			=0;
			this.eventsFired		=0;
			this.adCfgFile			= inParams.adCfgFile || null;
			this.adSrc				= inParams.adSrc 	 || 'http://www.space.com/snserver/ads/gadgetAds.html';
			this.version			= inParams.version	 || this.kDefaultVersion;
			this.frequency			= inParams.frequency || this.kDefaultAdvertFrequency;
			this.startOnFrame		= inParams.startOnFrame || 0;
			this.maxShows			= inParams.maxShows || 0;
			this.beforeAdShow 		= inParams.beforeAdShow.bind(this) || null;
			this.afterAdShow		= inParams.afterAdShow.bind(this) || null;
			this.beforeAdHide		= inParams.beforeAdHide.bind(this) || null;
			this.afterAdHide 		= inParams.afterAdHide.bind(this) || null;
			this.showAdMilliseconds = inParams.showAdMilliseconds || 4000;
			this.adWidth			= inParams.width	 || '300px';
			this.adHeight			= inParams.height	 || '250px';
			this.bgCSS				= inParams.bgCSS	 || 'black';
			this.firstRefresh = true;
			this.advertHTML = "";
			
			if (this.adCfgFile)
			{
				this.requestAdvertCfg();
			}
			else
			{			
		
			}
			
		},
		
		//if calling this independently, make sure that
		//	refreshAdvert() is called
		requestAdvertCfg: function()
		{
			var boundSuccessCallback = this.setCfgFromJSONResponse.bind(this);
			if(this.beforeCfgReq){
				this.beforeCfgReq();			
			}
			new Ajax.Request (this.adCfgFile,
			{
				asynchronous: false,
				method: 'get',
				evalJSON: 'force',
				onSuccess: boundSuccessCallback,
				onFailure: function(e) {;}
			});	
		},
		
		setCfgFromJSONResponse : function(transport)
		{
			var advertCfg = transport.responseJSON;

			if (advertCfg === null) //something went horribly wrong
				return;
			
			this.adsEnabled = advertCfg.adsEnabled;
			
			if (advertCfg.adsEnabled)
			{
				this.adSrc = advertCfg.adSrc;			
				this.startOnFrame = advertCfg.startOnFrame;
				this.frequency = advertCfg.frequency || this.kDefaultAdvertFrequency; //frequency cannot be 0
				this.version   = advertCfg.version || this.kDefaultVersion;
				this.maxShows = advertCfg.maxShows;
				this.showAdMilliseconds = advertCfg.duration;
				this.adHeight = advertCfg.adHeight;
				this.adWidth = advertCfg.adWidth;
				this.firstRefresh = true;
				$('adFrame').src = this.adSrc;

				//reset counters since now we have a new ad config
				this.eventsFired = 0;
				this.adsShown = 0;
				//setTimeout(this.refreshAdvert.bind(this), 0);
				setTimeout(this.showAds.bind(this), 500);
				if(this.afterCfgReq){
					this.afterCfgReq();
				}
								 
			}
		},
		
		getHTML : function()
		{
			return this.advertHTML;
		},
		
		getFrequency : function()
		{
			return this.frequency;
		},
		
		getStartOnFrame : function()
		{
			return this.startOnFrame;
		},
		
		getMaxShows : function()
		{
			return this.maxShows;
		},
		showAds :function (){
			
				if (
					(this.adsEnabled == true) &&(this.eventsFired >= this.getStartOnFrame()) && 
					( (this.getMaxShows() == 0) || (this.getMaxShows() > this.adsShown) ) &&
					( ((this.eventsFired - this.getStartOnFrame()) % this.getFrequency()) == 0 )
				)
				{
				
					//$("GLContent").setOpacity(0.0);
					if(GEEKLOGIK){
						GEEKLOGIK.trackAction({ action: GEEKLOGIK.consts.kTrack_adDisplayed, isError: false });
					}			
					this.showAdvert();
					this.adsShown += 1;
					this.eventsFired += 1;
					
				}
				else{
					
					if(this.adsShown < (this.maxShows+1)){ 
						this.eventsFired += 1;
					}
					if((this.adsEnabled == false) || this.eventsFired > 1){
						this.afterAdHide();
					}
					
				}
				
		
		},	
		showAdvert : function()
		{
			var appearParams = {duration:1}
		
			if (this.beforeAdShow) {
				appearParams.beforeStart = this.beforeAdShow;
			}
			if(this.afterAdShow){
			
				appearParams.afterFinish=this.afterAdShow;
				
			}
			
			//this.beforeAdShow();
			Effect.Appear("advert", appearParams);
			

			setTimeout(this.hideAdvert.bind(this), this.showAdMilliseconds);
		
		},

		hideAdvert : function()
		{
			
				var fadeParams = {duration:1}
		
		
				if (this.beforeAdShow) {
					fadeParams.beforeStart = this.beforeAdHide;
				}
				if(this.afterAdHide){
				
					fadeParams.afterFinish=this.afterAdHide;
					
				}
								
				
		
				Effect.Fade("advert",fadeParams);
			//this.afterAdHide();
			
				setTimeout(this.refreshAdvert.bind(this), 3500); //switch ad after hiding so that it doesn't do the annoying swap on show
			
				
			//}
		},
		refreshAdvert : function()
		{
			//if (this.advertHTML)
			//{
				//$('adFrame').src = null;
				//$('adFrame').src= this.adSrc;
				/*if(this.firstRefresh){
					if(this.startOnFrame == 0){
						this.showAdvert();
					}
						this.firstRefresh = false;
				}*/
				if($('adFrame')){
					var theWindow = $('adFrame').contentWindow;
					if (false === (!!theWindow))
					{
						theWindow = $('adFrame').contentDocument;
					}
					
					if (theWindow)
					{
						//
						theWindow.location.replace(this.adSrc); 
						//alert(theWindow.location);
					}
					
				}
			
		
		}
		
	});
