var ctModuleObject = Class.create({
	initialize: function(el){
	 	this.properties = eHA.elementToObjectLiteral(el);
	 	this.conditions = {type:"conditions"};
	 	this.treatments = {type:"treatments"};
	 	this.conditions.el = $(el);
	 	this.treatments.el = $$("div.ctModule.treatments.assetid-"+this.properties.assetid)[0];
 		this.build(this.conditions);
 		this.build(this.treatments);
 		this.buildTabs();
 		this.selectLetter(this.treatments,"A");
 		this.selectLetter(this.conditions,"A");
	},
	build: function(group){
		try{
			group.el.select(eHA.ct.selectors.alphabetLinks).each(function(el){
				el.observe("click",function(event){
					event.stop();
					this.selectLetter(group,el.innerHTML);
				}.bind(this));
			}.bind(this));
			group.title = group.el.down("h3").innerHTML.replace("&amp;","&");
			group.formInput = group.el.down("form input[type=text]");
			group.formInput.value = "Search "+group.title;
			group.formInput.observe("focus",function(evt){
				if(evt.element().value==="Search "+group.title){
					evt.element().value = "";
				}
			});
			group.formInput.observe("blur",function(evt){
				if(evt.element().value===""){
					evt.element().value = "Search "+group.title;
				}
			});
			
		}catch(err){
			eHA.log.create("failure building ct group: "+err.toString(), eHA.log._ERROR);
		}
	},
	buildTabs: function(){
		var tabsWrapper = new Element("div",{className:"ctModuleTabs"});
		var tabsHeader = new Element("h4").update("Conditions and Treatments Navigation");
		var tabsUL = new Element("ul");
		var tabsConditionsLI = new Element("li",{className:"conditions"});
		this.conditions.tabAnchor = new Element("a",{href:""}).update("Search by "+this.conditions.title);
		var tabsTreatmentsLI = new Element("li",{className:"treatments"});
		this.treatments.tabAnchor = new Element("a",{href:""}).update("Search by "+this.treatments.title);

		tabsConditionsLI.update(this.conditions.tabAnchor);
		tabsTreatmentsLI.update(this.treatments.tabAnchor);
		tabsUL.insert({bottom:tabsConditionsLI});
		tabsUL.insert({bottom:tabsTreatmentsLI});
		
		tabsWrapper.insert({bottom:tabsHeader});
		tabsWrapper.insert({bottom:tabsUL});
		
		this.conditions.tabAnchor.observe("click",function(evt){
			evt.stop();
			this.selectTab("conditions");
		}.bind(this));

		this.treatments.tabAnchor.observe("click",function(evt){
			evt.stop();
			this.selectTab("treatments");
		}.bind(this));
		
		this.ctWrapper = new Element("div",{className:"ctModuleWrapper"});
		this.conditions.el.insert({before:this.ctWrapper});
		this.ctWrapper.insert({bottom:tabsWrapper});
		this.ctWrapper.insert({bottom:this.conditions.el});
		this.ctWrapper.insert({bottom:this.treatments.el});
		
		// select conditions onload
	},
	selectTab: function(groupType){
		//SWFAddress.setValue(groupType+"-"+this.properties.assetid);
		if(typeof this[groupType].selectedLetter==="undefined"){
			this[groupType].selectedLetter = "A";
		}
		SWFAddress.setValue(this.properties.assetid+"/"+groupType+"/"+this[groupType].selectedLetter);
	},
	loadTab: function(groupType){
		try{
			eHA.log.create("selected tab "+groupType);
			this[groupType].tabAnchor.up("li").siblings().invoke("removeClassName","sel");
			this[groupType].tabAnchor.up("li").addClassName("sel");
			this.ctWrapper.select(".ctModule").invoke("removeClassName","sel").invoke("hide");
			this[groupType].el.addClassName("sel").show();
		}catch(err){
			eHA.log.create("error on selectTab. "+err.toString(),1);
		}
	},
	clearSelection: function(groupType){
		try{
			eHA.log.create("clearSelection "+groupType);
			if(this[groupType].el.select(eHA.ct.selectors.ctGroups)[0]){
				this[groupType].el.select(eHA.ct.selectors.ctGroups).invoke("hide");
			}
		}catch(err){
			eHA.log.create("failure on clearSelection "+err.toString(),eHA.log._ERROR);
		}
	},
	selectLetter: function(group,letter){
		try{
			eHA.log.create("selected letter "+letter+" in group "+group.type);
			//SWFAddress.setValue(group.type+"-"+this.properties.assetid+"-"+letter);
			SWFAddress.setValue(this.properties.assetid+"/"+group.type+"/"+letter);
		}catch(err){
			eHA.log.create("error on selectLetter. "+err.toString(),1);
		}
	},
	loadLetter: function(groupType,letter){
		this[groupType].selectedLetter = letter;
		this.clearSelection(groupType);
		this.loadTab(groupType);
		eHA.log.create("loading letter "+letter+" in group "+groupType);
		var selectedLiId = groupType+"-"+this.properties.assetid+"-"+letter;
		this.adjustWrapper(this[groupType], selectedLiId);
		$(selectedLiId).appear({duration:eHA.ct.duration});
	},
	readHash: function(pathNames){
		try{
			var alphaRegex = /[A-Z]/;
			var groupType = pathNames[1];
			if(pathNames.length===3){
				var letter = pathNames[2];
				if(alphaRegex.test(letter) && (groupType==="conditions" || groupType==="treatments")){
					this.loadLetter(groupType, letter);
				}else{
					eHA.log.create("bad letter for ctModule",2);
				}
			}
		}catch(err){
			eHA.log.create("failure readHash. "+err.toString(), eHA.log._ERROR);
		}
	},
	adjustWrapper: function(group,selectedLiId){
		var height = $(selectedLiId).getHeight();
		new Effect.Morph(group.el.down(eHA.ct.selectors.ctWrapper),{
				"style":{ "height":height+"px" },
				"duration": eHA.ct.duration
			});
	}
});

eHA.ct = {
		init: function(){
			this.selectors = {};
			this.selectors.module = "div.ctModule.conditions";
			this.selectors.alphabetLinks = "ul.alphabet_list a";
			this.selectors.ctWrapper = ".ct_links";
			this.selectors.ctGroups = this.selectors.ctWrapper+">ul>li";
			this.duration = 0.3;
			this.ctModuleObjects = {};
			
			if($$(this.selectors.module).size()>0){
				$$(this.selectors.module).each(function(element){
					var ctModuleID = eHA.elementToObjectLiteral(element).assetid;
					this.ctModuleObjects[ctModuleID] = new ctModuleObject(element);
				}.bind(this));
			}
			this.addListeners();
		},
		addListeners: function(){
			 // adding SWFaddress listeners
			 // EXTERNAL_CHANGE is used for back button
			 SWFAddress.addEventListener(SWFAddressEvent.EXTERNAL_CHANGE, function(event){
					eHA.log.create("SWFAddress event: "+Object.toJSON(event), 3);
					if(typeof event.pathNames!=="undefined"){
						if(typeof this.ctModuleObjects[event.pathNames[0]]!=="undefined" && typeof event.parameters!=="undefined"){
							this.ctModuleObjects[event.pathNames[0]].readHash(event.pathNames);
						}
					}
				 }.bind(this));
			
			 SWFAddress.addEventListener(SWFAddressEvent.INTERNAL_CHANGE, function(event){
					eHA.log.create("SWFAddress event: "+Object.toJSON(event), 3);
					if(typeof event.pathNames!=="undefined"){
						if(typeof this.ctModuleObjects[event.pathNames[0]]!=="undefined" && typeof event.parameters!=="undefined"){
							this.ctModuleObjects[event.pathNames[0]].readHash(event.pathNames);
						}
					}
				 }.bind(this));
			
			 if(typeof SWFAddress.getPathNames()!=="undefined" && typeof SWFAddress.getPathNames()[0]!=="undefined"){
				 // explicitly listen for INIT event (this is not necessary if the hash is empty onload)
				 SWFAddress.addEventListener(SWFAddressEvent.INIT, function(event){
						eHA.log.create("SWFAddress event: "+Object.toJSON(event), 3);
						if(typeof event.pathNames!=="undefined"){
							if(typeof this.ctModuleObjects[event.pathNames[0]]!=="undefined" && typeof event.parameters!=="undefined"){
								this.ctModuleObjects[event.pathNames[0]].readHash(event.pathNames);
							}
						}
					 }.bind(this));
			 }
		}

	};
