// date listener is probably not necessary, but keep it for now
// refreshDetectorSite.prototype.setDateListener=function(fun){
//     this.dateListener=fun;
// };
// refreshDetectorSite.prototype.removeDateListener=function(){
//     this.dateListener=null;
// };


function detectorSite(options) {
    var formGenerator=options.formGenerator;
    this.ownerDocument=options.map.ownerDocument;
    var mapElement=options.map.div;
    var mapObject=options.map;
	
    //this.div=this.ownerDocument.createElement('div'); 
    //Element.hide(this.div);
    this.GEventListeners=[]; 
    var old_ul=null;
    var filename;
    var url;
    var baseUrl;
    var dateObj=options.dateslider;

    this.timer=null;
    this.toString=function(){
	return "current url="+url;
    }
    this.fg=new formGenerator(options);
    var self=this;

    var oshut=null;
    var cancel=false;
    this.clearOpenShut=function(){
	if(oshut){
	    oshut.DESTROY();
	    for(var i=0; i<self.GEventListeners.length;i++){
		GEvent.removeListener(self.GEventListeners[i]);
		self.GEventListeners=[];
	    }
	    oshut=null;
	}
    };
    this.setOpenShut=function(req,fun){
	if(oshut){
	    self.clearOpenShut();
	}
	oshut = new openshut(req,mapElement,mapObject,fun);
	oshut.open();
	Event.observe(oshut.getCloseButton(),"click",self.closeHandler.bind(self));
    };


    function displayCallback(req){
	if(cancel){
	    return false;
	}
	//if(_debug){_debug.log("entering displayCallback");}
	// pass on to open shut
	self.clearOpenShut();
	self.setOpenShut(req,self.fg.IFWClass.bind(self.fg));
	self.GEventListeners.push(GEvent.addListener(mapObject, "zoom",function(){oshut.refresh();}));
	self.GEventListeners.push(GEvent.addListener(mapObject, "maptypechanged",function(){oshut.refresh();}));
	self.GEventListeners.push(GEvent.addListener(mapObject, "moveend",function(){oshut.getElement.checkDisplayable();}));
	
    }

    this.fetchFile=function(req){
	if(cancel){
	    return false;
	}
	// first, check for valid session
	var jsonText=req.responseText;
	var loginRegex = new RegExp("login");
	var needToLogin=jsonText.match(loginRegex);
	if(needToLogin){
// 	    var explain=self.ownerDocument.createTextNode("The Detector Testbed thinks your session has expired.  ");

// 	    var a=self.ownerDocument.createElement("a");
// 	    a.href="http://testbed.its.uci.edu:8080/login.do";
// 	    a.target="_top";
// 	    a.appendChild(self.ownerDocument.createTextNode("Please login"));
// 	    var explain2=self.ownerDocument.createTextNode(", and then reload this page from the Testbed menus.");	    
// 	    if(old_ul){
// 		try{Element.remove(old_ul);}catch(Ex){}
// 	    }
// 	    var ulD=self.ownerDocument.createElement("ul");
// 	    ulD.className="segmentData";
// 	    var liD=self.ownerDocument.createElement("li");
// 	    liD.style.display="block";
// 	    liD.style.width="150px";
// 	    var p=self.ownerDocument.createElement('p');
// 	    p.appendChild(explain);
// 	    p.appendChild(a);
// 	    p.appendChild(explain2);
// 	    liD.appendChild(p);
// 	    ulD.appendChild(liD);
// 	    old_ul=ulD;
// 	    this.div.appendChild(ulD);
// 	    return null;
	}

	// otherwise, moving along fine

	var fetchUrl=baseUrl+_dtFileFetchAction+"?value(filename)="+filename;

	self.timer=setTimeout(function(){loadXMLDoc(fetchUrl,displayCallback,null);}, 100);	
    }    

    this.bootStrap=function(curdate){
	cancel=false;
	//if(_debug){_debug.log("entering bootStrap with date " +curdate);}
	if(self.timer){
	    clearTimeout(self.timer);
	}
	self.clearOpenShut();
	self.setOpenShut(null,self.fetchingIFWClass.bind(self));
	self.GEventListeners.push(GEvent.addListener(mapObject, "zoom",function(){oshut.refresh();}));
	self.GEventListeners.push(GEvent.addListener(mapObject, "maptypechanged",function(){oshut.refresh();}));
	self.GEventListeners.push(GEvent.addListener(mapObject, "moveend",function(){oshut.getElement.checkDisplayable();}));


	url=self.fg.url;
	baseUrl=self.fg.baseUrl;

	//if(_debug){_debug.log("calling Ajax.Request with url "+url+"?"+self.fg.getOptions(curdate)+" ");}
	loadXMLDoc(url+"?"+self.fg.getOptions(curdate),self.fetchFile.bind(self))
	filename=self.fg.computedFilename;
	if(_debug){_debug.log("called Ajax.Request");}
    };

    this.openHandler=function(event){
	self.bootStrap(dateObj.syncDate);
	dateObj.observeDate(self.bootStrap);
    };

    this.closeHandler=function(event){
	cancel=true;
	if(_debug){_debug.log("entering closeHandler");}
	dateObj.stopObservingDate(self.bootStrap);
	self.clearOpenShut();
    };

}

detectorSite.prototype.fetchingIFWClass=function(dummy,containerDiv,mapObject) {
    var ds=this;
    var ifw = new infoWindow(dummy,containerDiv,mapObject,function(){
				 var box=new boxSize(225,20); 
				 var moreText=new boxSize(0,10); 
				 var ulD=ds.ownerDocument.createElement("ul");
				 ulD.className="segmentData";
				 var title=ds.ownerDocument.createElement("li");
				 title.appendChild(ds.ownerDocument.createTextNode("working on "+ds.fg.labelName));
				 ulD.appendChild(title);
				 box.grow(moreText);
				 box.apply(ulD);
				 var pos=new coord(0,0);
				 pos.apply(ulD);
				 return {ul:ulD, 
					 box:box ,
					 lon:ds.fg.lon,
					 lat:ds.fg.lat
					 };
			     });
    return ifw;
};
