map2AnalyticusLogger_instances = {};

// Static variable accessor to be used by page to pass in product id for these supported products.
var getGoldProductID = 	function() {return {method:function() {return 11;}}}();
var getFreeProductID = 	function() {return {method:function() {return 9;}}}();
var getDesktopProductID = function() {return {method:function() {return 13;}}}();

map2AnalyticusLogger = function(productID, sessionID, zoomLevel, argAnimationFlag, layerType, wxLayer, alsm, alat, alon){

	this.id = sessionID;	
	map2AnalyticusLogger_instances[this.id] = this;

	var canLog = true; 
	this.stopLogging = function(){log(26);/* Expired session */ canLog = false;}
	this.startLogging = function(){canLog = true; this.logTimeAction();}
	
	
	/**** 	Need to create a url of the following formate: 
	* 	protocol + domain + product + action_id + ? + cb=randomNumber + &basePageParms + &anyOtherTrackingStrings
	****/
    


	var poi=""
	var protocol = "http://";
	var domain = 'x.weather.com/maps/';
	var productID = (getGoldProductID.method() == productID || getFreeProductID.method() == productID || getDesktopProductID.method() == productID )? productID: "productIDNotSupported";
     	var cacheBuster = "?cb=" + new Date().getTime();
	var baseURLQryString = (window.location.search.length > 0) ? "&[" + window.location.search.substring(1) + "]": "";	
	
	// initialize actions attributes
	var zoomLvl = (zoomLevel==null || zoomLevel=="")?"incorrectInitializationOfzoomLevel":zoomLevel;
	var animationFlag = (argAnimationFlag == "true" || argAnimationFlag == "false")? argAnimationFlag:"argAnimationFlagIsNotSupported";
	var wxLayer = (wxLayer==null || wxLayer=="")?"incorrectInitializationOfwxLayer":wxLayer;
	var wxLayerType = (layerType==null || layerType=="")?"incorrectInitializationOflayerType":layerType;
	var sessionID = sessionID;	
	var timeBaseUsage = "T";//indicates whether user has been interacting with map (T/F)
	var actionTimeSpan = 5000;// PM configed in ms
	
	/***
	* The following attributes are currently not completly implemented on the server, but still are being thrown here. So will have to
	* revisit the initialization of these vars
	***/
	var lat= alat;
	var lon= alon;
	var country="";
	var state="";
	var dma="";
	var city="";
	var zip="";
	var lsm=alsm;
	
	this.setCoordinates = function(latitude, longitude){lat = latitude; lon = longitude;}
	this.getActionTimeSpan = function(){return actionTimeSpan;}
	
	// Session timer
	this.sessionTimerID = null;
	var sessionTimeSpan = 900000;// PM configed in ms
	this.setSessionTimerID = function(){
		this.sessionTimerID = setTimeout('map2AnalyticusLogger_instances[\'' + this.id + '\'].onSessionExpire()', sessionTimeSpan);		
		
	}	
	this.refreshSessionTimerID = function(){
		if(this.sessionTimerID != null) clearTimeout(this.sessionTimerID);
		this.setSessionTimerID();
	}
	this.setSessionTimerID();
	
					
	// Time based action
	var timeBasedAction = 25;
	this.logTimeAction = function(){
		//this.refreshSessionTimerID();
		log(timeBasedAction);
		setTimeout('map2AnalyticusLogger_instances[\'' + this.id + '\'].logTimeAction()', 30000);
		timeBaseUsage = "F";
	}	
	

	function log(argAction) {
		/*if(argAction != 0) {
			try {
				reloadSponsAd();
			} catch(Exception) {}
		}*/
		if(productID == "" || domain == "" || !canLog) return;		
		actionAttributes = 	"&zoom=" + zoomLvl + "&animation=" + animationFlag + "&datalayer=" + wxLayer + 
					"&basemap=" + baseMap + "&id=" + sessionID + "&usage=" + timeBaseUsage +
					"&lat=" + lat + "&lon=" + lon + "&country=" + country + "&state=" + state +
					"&dma=" + dma + "&city=" + city + "&zip=" + zip + "&lsm=" + lsm + "&poi=" + poi;
		cacheBuster = "?cb=" + new Date().getTime();
		throwBeacon(protocol + domain + productID + "/" + argAction + cacheBuster + baseURLQryString + actionAttributes);
		
		
	}
	
//fixed dual calls for beacon issue in IE6
	function throwBeacon(url) {
		var analyticusDiv= document.createElement('div');
		analyticusDiv.id="analyticusImg";
		analyticusDiv.style.display="none";
		var imgTag            = '<IMG width="1px" height="1px" border="0px" style="position: absolute; top: 0px; left: 0px; zIndex:0;" src="' + url + '">';	
		if (!document.getElementById("analyticusImg")){
			document.body.insertBefore(analyticusDiv, document.body.firstChild);	
		}
		document.getElementById("analyticusImg").innerHTML=imgTag;
		
	}	
	
	// Time based actions
	// Zooming
	var zoomAction = 3;
	this.zoomingCurrentState = 0;	
	this.logZoomAction = function(){		
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		log(zoomAction);
		this.zoomingCurrentState--;
	}
	
	this.setPoi=function(poiLayer){
				poi=poiLayer;
		};
			
		this.getPoi=function(){
			return poi;
		};
	
	
	this.setZoomLevel = function(level){zoomLvl=(level==null || level=="")?zoomLvl:level;}
	
	
	// Panning
	var panAction = 2;
	this.panCurrentState = 0;	
	this.logPanAction = function(){
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		log(panAction);
		this.panCurrentState--;
	}
	
	
	// Layer	
	var aerialButtonClickedAction = 6;
	var roadButtonClickedAction = 7;
	var radarButtonSelectedAction = 8;
	var satButtonSelectedAction = 9;
	var satradButtonSelectedAction = 10;
	var baseMap="hybrid";
	var rTransparencyAction = 14; // radar
	var sTransparencyAction = 15; // satellite
	var srTransparencyAction = 16; // satrad composite
	var poiLayerSelectedAction=17;
	var arbitraryPlotAction =18;
	this.getWxLayerType=function(){return wxLayerType;};this.setWxLayerType=function(type){wxLayerType=(type==null || type=="")?wxLayerType:type;};
	this.setWxLayer=function(argwxLayer){wxLayer=(argwxLayer==null || argwxLayer=="")?wxLayer:argwxLayer;};this.getWxLayer=function(){return wxLayer;};
	this.setBaseMap=function(argwxLayer){
		baseMap=(argwxLayer==null || argwxLayer=="")?"":argwxLayer;
	};
	this.getBaseMap=function(){
		return baseMap;
	};
        this.getAnimationFlag=function(){
			return animationFlag;
	};
	this.setAnimationFlag=function(flag){
				animationFlag=flag;
	};
	this.layerCurrentState = 0;
	this.logTransparencySliderAction = function(){
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		if(wxLayer == "radar"){log(rTransparencyAction);}
		else if(wxLayer == "sat"){log(sTransparencyAction);}
		else if(wxLayer == "satrad"){log(srTransparencyAction);}
		else{log("errorlogTransparencySliderAction(wxLayer is not radar, sat, satrad")}
		this.layerCurrentState--;
	}
	this.logLayerAction = function(){
			this.refreshSessionTimerID();		
			timeBaseUsage = "T";
			if(wxLayerType == "baseMap" && baseMap == "hybrid") log(aerialButtonClickedAction);
			else if(wxLayerType == "baseMap" && baseMap == "road") log(roadButtonClickedAction);
			else if(wxLayerType == "wxLayer" && wxLayer == "radar") log(radarButtonSelectedAction);
			else if(wxLayerType == "wxLayer" && wxLayer == "sat") log(satButtonSelectedAction);
			else if(wxLayerType == "wxLayer" && wxLayer == "satrad") log(satradButtonSelectedAction);
			else {log("somethingGoneWrongWithLayerAction");}//something has gone wrong	
			
		}
		
	this.logPoiLayerAction = function(poiLayer){
				this.setPoi(poiLayer);
				this.refreshSessionTimerID();		
				timeBaseUsage = "T";
				log(poiLayerSelectedAction);	

	}


	this.onPlotArbitraryLayer = function(){
				this.refreshSessionTimerID();		
				timeBaseUsage = "T";
				log(arbitraryPlotAction);	

	}
	
	
	
	// Flash Dialog
	var flashDialogDisplayAction = 4;
	this.logflashDialogDisplayAction = function(){
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		log(flashDialogDisplayAction);
	}
        // Flash not installed or version 8 not installed
	var flashPluginIssueAction = 5;
	this.logFlashPluginIssueAction = function(){
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		// Need to allow logging before throwing this beacon
		canLog = true;
		log(flashPluginIssueAction);
		canLog = false;
	}
	
	
	// Animation
	var radarAnimatedAction = 11;
	var satAnimatedAction = 12;
	var satradAnimatedAction = 13;
	this.logAnimationStartAction = function(){
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		log(19); // animation started
		if(/*wxLayerType == "wxLayer" &&*/ wxLayer == "radar") log(radarAnimatedAction);
		else if(/*wxLayerType == "wxLayer" &&*/ wxLayer == "sat") log(satAnimatedAction);
		else if(/*wxLayerType == "wxLayer" &&*/ wxLayer == "satrad") log(satradAnimatedAction);
		/* 	This was commneted out due to the fact that the flash is sending road and hybrid as wxlayer
			else {log("errorInLogAnimationStartAction");}
		*/
	}
	
	// Animate Step Backward
	var animateStepBackwardAction = 22;
	this.animateStepBackwardCurrentState = 0;	
	this.logAnimateStepBackwardAction = function(){
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		log(animateStepBackwardAction);
		this.animateStepBackwardCurrentState--;
	}
	
	// Animate Step Forward
	var animateStepForwardAction = 21;
	this.animateStepForwardCurrentState = 0;	
	this.logAnimateStepForwardAction = function(){
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		log(animateStepForwardAction);
		this.animateStepForwardCurrentState--;
	}
	
	// Animate Stop
	var animateStopAction = 20;	
	this.logAnimateStopAction = function(){
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		log(animateStopAction);
	}
	
	// animateOnToStart
	var animateOnToStartAction = 23;	
	this.logAnimateOnToStartAction = function(){
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		log(animateOnToStartAction);
	}
	
	// animateOnToEnd
	var animateOnToEndAction = 24;	
	this.logAnimateOnToEndAction = function(){
		this.refreshSessionTimerID();
		timeBaseUsage = "T";
		log(animateOnToEndAction);
	}
	
	
	// Log page beacon
	log(0);
	canLog = false;// force them to call the start logging function.

	EventBroadcaster.initialize(this);
};

map2AnalyticusLogger.prototype = {
	broadcastMessage:function() {},
	addListener:function() {},
	removeListener:function() {},
	
	onZoom:function(level)  {
		//this.setZoomLevel(level); this will cause inaccurate reporting of zoom level at time of beacon throwing.
		switch(this.zoomingCurrentState) {			
			case 0: // initial state is to allow throwing of beacon
				this.setZoomLevel(level);
				this.zoomingCurrentState++;
				setTimeout('map2AnalyticusLogger_instances[\'' + this.id + '\'].logZoomAction()', this.getActionTimeSpan());
				break;
			case 1: // waiting for the end of beacon throwing.
				break;			
		}
	},	
	
	onPan:function(latitude, longitude) {
		this.setCoordinates(latitude, longitude);
		switch(this.panCurrentState) {			
			case 0: // initial state is to allow throwing of beacon
				this.panCurrentState++;
				setTimeout('map2AnalyticusLogger_instances[\'' + this.id + '\'].logPanAction()', this.getActionTimeSpan());
				break;
			case 1: // waiting for the end of beacon throwing.
				break;			
		}
	},	
	
	onSetTransparency:function(transparency) {
		//if(this.getWxLayerType() == "wxLayer"){
			// Handle the transparency slider action
			switch(this.layerCurrentState) {			
				case 0: // initial state is to allow throwing of beacon					
					this.layerCurrentState++;
					setTimeout('map2AnalyticusLogger_instances[\'' + this.id + '\'].logTransparencySliderAction()', this.getActionTimeSpan());
					break;
				case 1: // waiting for the end of beacon throwing.
					break;			
			}
		//}
	},
	onSetLayer:function(type, layer){
		//while(this.layerCurrentState == 1){/*waiting for transparency beacon to be thrown*/};
		if (type == "baseMap"){
			this.setBaseMap(layer);		
		}else{
			this.setWxLayer(layer);
// when switching wxlayers, animation is not maintained
                        this.setAnimationFlag(false);
		}
		this.setWxLayerType(type);
		this.logLayerAction();				
	},
	
	onPopUp:function(message){		
		this.logflashDialogDisplayAction();	
	},
	
	onAnimate:function() {
        this.setAnimationFlag(true);
         this.logAnimationStartAction();
	
	},
	
	onSessionExpire:function() {
		this.stopLogging();		
	      	this.broadcastMessage("onSessionExpire");
	},
	stopAnimate:function() {
this.setAnimationFlag(false);
       this.logAnimateStopAction();
		
	},
	animateStepBackward:function() {
		switch(this.animateStepBackwardCurrentState) {			
			case 0: // initial state is to allow throwing of beacon
				this.animateStepBackwardCurrentState++;
				setTimeout('map2AnalyticusLogger_instances[\'' + this.id + '\'].logAnimateStepBackwardAction()', this.getActionTimeSpan());
				break;
			case 1: // waiting for the end of beacon throwing.
				break;			
		}	
	},
	animateStepForward:function() {
		switch(this.animateStepForwardCurrentState) {			
			case 0: // initial state is to allow throwing of beacon
				this.animateStepForwardCurrentState++;
				setTimeout('map2AnalyticusLogger_instances[\'' + this.id + '\'].logAnimateStepForwardAction()', this.getActionTimeSpan());
				break;
			case 1: // waiting for the end of beacon throwing.
				break;			
		}	
	},
	animateOnToStart:function() {
		this.logAnimateOnToStartAction();
	},
	animateOnToEnd:function() {
		this.logAnimateOnToEndAction();
	},
	
	onSetPoiLayer:function(poi){
			this.logPoiLayerAction(poi);				
	},
		
	onPlotArbitraryLayer:function(){
			this.logArbitraryLayerAction();
	}
};
