document.observe("dom:loaded", function(){
	if($$('body')[0].hasClassName('HomePage') == true) {SpotLight.init()}
	
	if(document.getElementById('listed_item') != null)
	{
		if($('listed_item').hasClassName('listed_physician') == true)
		{
			Affiliation.init();
		}	
	}
	
	eHA.loadMeta();

	NavigationSelect.setSelectedOpen("div#content_body_left div.page_list",eHA.assetid);
	NavigationSelect.setParents("#content_body_left .page_list ul li");
	NavigationSelect.setActive("div#content_body_left div.page_list",eHA.assetid);
	//if left navigation create navigation selectors
	if($$('div#content_body_left div.page_list')){	NavigationSelect.setFirst("div#content_body_left div.page_list ul li");}
	NavigationSelect.setSelected("div#primary_navigation",eHA.assetid);
	NavigationSelect.setSelected("div#action_toolbar",eHA.assetid);
	NavigationSelect.setSelected("div#global_navigation",eHA.assetid);
	NavigationSelect.setSelected("div#portal_navigation",eHA.assetid);

	if(eHA.assetid !== eHA.categoryid){
		NavigationSelect.setSelected("div#primary_navigation",eHA.categoryid);
		NavigationSelect.setSelected("div#action_toolbar",eHA.categoryid);
		NavigationSelect.setSelected("div#global_navigation",eHA.categoryid);
	}

	MoreButtons.divsToHideSelectors.push(".category_general_body");
	MoreButtons.divsToHideSelectors.push("#make_an_appointment");
	if($('content_right_rail')){MoreButtons.createMoreButtons();}
	mediaDirectory.usePanels = true;
	mediaDirectory.assignMediaEvents();
});

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;

/**
 * rewrites prototype's String#unescapeHTML so that it further decodes &apos; and &quot;
 * @function
 * @return {String} decoded string
 */
String.prototype.unescapeHTML = function(){
	return this.stripTags().replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&').replace(/&apos;/g,'\'').replace(/&quot;/g,"\"");
}
var TabRollOvers = {
	setRollovers: function(selector){
		this.hoverSelector = selector;
		$$(this.hoverSelector).each(function(element){
			element.observe('mouseover',function(event){
				element.className=element.className+' hover';
				element.addClassName(' hover');
			});
			element.observe('mouseout',function(event){
				element.removeClassName('hover');
				element.className=element.className.replace(' hover','');
			});
			element.observe('click',function(event){
				window.location=this.down('a').getAttribute('href')
			})
		});
	},
	focusIndex:0,
	setTabFocus: function(selector){
		$$(selector).each(function(element,i){
			this.focusIndex++;
			element.down('a').setAttribute('tabindex',-1);
			element.setAttribute('tabindex',this.focusIndex);
			element.observe('keydown',function(event){if(event.keyCode==Event.KEY_RETURN){window.location=Event.element(event).down('a').href;}})
		}.bind(this));
	}
};

var MoreButtons = {
	relatedDivSelectors:new Array(".smart_navigation","#clinical_trials_smart_navigation","#page_related_links","#page_related_documents","#category_related_links","#category_related_documents"),
	divsToHideSelectors:new Array(".media_wrapper","#content_body_right .display_media",".category_general_body",".content_category_information",".page_title",".service_description",".contact_information",".release_date",".city_state","#media_contacts","#listed_item","#listed_location","#location_tab_navigation","#tabs_content",".page_general_body"),
	//divsToHideSelectors:new Array(".media_wrapper",".category_general_body",".content_category_information",".page_title",".service_description",".contact_information",".release_date",".city_state","#media_contacts","#make_an_appointment"),
	createMoreButtons: function(){
		this.relatedDivSelectors.each(function(selector){
			$$(selector).each(function(element){
			if(element.select('ul.sub li').length > 0){var liArray=element.select('ul.sub li');}
			else{var liArray=element.select('ul li');}
			liArray.each(function(element,i){if(i>2){element.hide()}});
			if(liArray.length>2){
				var moreLink=new Element('a',{href:'javascript:void(0)'}).addClassName('see_more_related').update('more >>');
				element.appendChild(moreLink);
				moreLink.observe('click',function(event){
					var divCounter=0;
					var afterFinishFunction=function(){return false}
					this.divsToHideSelectors.each(function(divSelect){$$('#content_body '+divSelect).each(function(element,i){
						if(divCounter==0){afterFinishFunction=function(){MoreButtons.showMore(moreLink.up('div'))}}
						Effect.Fade(element,{ duration: .3,afterFinish:afterFinishFunction});
						divCounter++;
					})});
					if(divCounter==0){MoreButtons.showMore(moreLink.up('div'))}
				}.bind(this));
			}
			else if(liArray.length==0){element.hide()}
			}.bind(this));
		}.bind(this))
	},
	showMore:function(element){
		var newElement=new Element('div').addClassName('more_related').insert(element.cloneNode(true)).setStyle({display:'none'});
		newElement.select('.see_more_related')[0].remove();
		newElement.insert(new Element('a',{href:"javascript:void(0)"}).addClassName('remove').update('<<<'));
		newElement.select('.remove')[0].observe('click',function(event){Effect.Fade(newElement,{afterFinish:MoreButtons.showOriginal,duration: .3})}.bind(this));
		newElement.select('li').invoke('show');
		if(newElement.down('ul').select('ul').length>0){
			newElement.down('ul').addClassName('no_list_style');
		}
		if($$('#content_body .more_related')[0]){$$('#content_body .more_related')[0].remove();}
		$('content_body').appendChild(newElement);
		Effect.Appear(newElement,{ duration: .3});
		//Effect.SlideDown(newElement);
	},
	showOriginal:function(){
		$$('#content_body .more_related').invoke('remove');
		MoreButtons.divsToHideSelectors.each(function(divSelect,i){$$('#content_body '+divSelect).each(function(element){
			Effect.Appear(element,{ duration: .3});
		})});
	}
};


/**
 * 
 * 
 * @namespace
 */
var NavigationSelect = {
	selectedClass: " sel",
	openClass: " open",
	parentClass:" parent",
	activeClass:" active",
	activeParentClass:" activeParent",
	firstClass:" first",
	/**
	 * selects the nav appropriately
	 * @function
	 * @param {String} selector CSS selector for DIV of nav
	 * @requires eHA.assetid
	 */
	setSelected: function(selector,assetid){
		if(typeof assetid !== "undefined"){
			if($$(selector)[0] && $$(selector)[0].select(".assetid-"+assetid)[0]){
				$$(selector)[0].select(".assetid-"+assetid).each(function(element){
					if($(element).up("li") && !$(element).up("li").hasClassName("sel")){
						$(element).up("li").className += this.selectedClass;
						$(element).up("li").addClassName(this.selectedClass);
					}else{
						if(!$(element).hasClassName("sel")){
							$(element).className += this.selectedClass;
							$(element).addClassName(this.selectedClass);
						}
					}
				}.bind(this));
			}
		}

	},
	setFirst: function(selector){
		if($$(selector)[0]) $$(selector)[0].addClassName(this.firstClass);
		if($$(selector)[0]) $$(selector)[0].down("span a").addClassName(this.firstClass);
	},

	setOpen: function(selector,assetid){
		if(typeof assetid !== "undefined"){
			if($$(selector)[0] && $$(selector)[0].select(".assetid-"+assetid)[0]){
				$$(selector)[0].select(".assetid-"+assetid).each(function(element){
					if($(element).up("ul"))
						$(element).up("ul").addClassName(this.openClass);
					if($(element).down("ul"))
						$(element).addClassName(this.parentClass);
					if($(element).up("li"))
						$(element).up("li").addClassName('test');
				}.bind(this)); 
			}
		}
	},
	setActive: function(selector,assetid){
			if(typeof assetid !== "undefined"){
			if($$(selector)[0] && $$(selector)[0].select(".assetid-"+assetid)[0]){
				$$(selector)[0].select(".assetid-"+assetid).each(function(element){
						$(element).className += this.activeClass;
						if($(element).up("li").up("ul").up("li")) $(element).up("li").up("ul").up("li").addClassName(this.activeParentClass);


				}.bind(this));
			}
		}
	},
	setParents: function(parentSelector){
			$$(parentSelector).each(function(element){
			if($(element).down("ul")) $(element).addClassName('parent');
		  })

	},
	setSelectedOpen: function(selector,assetid){
		if(typeof assetid !== "undefined"){
			if($$(selector)[0] && $$(selector)[0].select(".assetid-"+assetid)[0]){
				$$(selector)[0].select(".assetid-"+assetid).each(function(element){
					if($(element).up("ul"))
						$(element).up("ul").addClassName(this.openClass);
					if($(element).up("ul").up("li"))
						$(element).up("ul").up("li").addClassName(this.openClass);
					if($(element).up("li") && !$(element).up("li").hasClassName("sel")){
						$(element).up("li").className += this.selectedClass;
						$(element).up("li").addClassName(this.selectedClass);
					}else{
						if(!$(element).hasClassName("sel")){
							$(element).className += this.selectedClass;
							$(element).addClassName(this.selectedClass);
						}
					}
				}.bind(this));
			}
		}
	}
};

var SearchPageExpandCollapse={
	childrenContainerSelector:'',
	childrenSelector:'',
	init:function(selector){
		$$('#'+selector+' '+this.childrenContainerSelector).each(function(element){
			var elementChildren=element.select(this.childrenSelector);
			elementChildren.each(function(result,i){
				if(i>=3){result.hide();}
			});	
			if(elementChildren.length>3){
				element.setAttribute('aria-expanded','false');
				element.insert(new Element('a',{href:'javascript:void(0)'}).update('More Results from '+element.down('h2').innerHTML).addClassName('see_more').observe('click',this.handleClick.bindAsEventListener(this)))
			}
		}.bind(this));
	},
	handleClick:function(event){
		var parentDiv=Event.element(event).up(this.childrenContainerSelector);
		if(parentDiv.getAttribute('aria-expanded')=='false'){
			Event.element(event).update(Event.element(event).innerHTML.replace('More','Less'));
			Event.element(event).addClassName('open');
			parentDiv.setAttribute('aria-expanded','true');
			parentDiv.select(this.childrenSelector).invoke('show');
		}
		else{
			Event.element(event).update(Event.element(event).innerHTML.replace('Less','More'));
			Event.element(event).removeClassName('open');
			parentDiv.setAttribute('aria-expanded','false');
			parentDiv.select(this.childrenSelector).each(function(result,i){
				if(i>=3){result.hide();}
			});
		}
	}
}




// add remove text in search bar field
function clearField(theField, defaultText) {
	if (theField.value == defaultText) {
		theField.value = "";
	}
}

function revertField(theField, defaultText) {
	if (theField.value == "") {
		theField.value = defaultText;
	}
}

var eHA = {
	Version: '3.3.5',
	Site: 'demo',
	requiredFlashVersion: '9.0.0',
	includeScripts: "media,panels,swfobject,qtobject", // possible scripts to include
	loadMeta: function(){
		if(this.meta("Asset_Id")){
			this.assetid = this.meta("Asset_Id");
		}
		if(this.meta("eHA_Category_Id")){
			this.categoryid = this.meta("eHA_Category_Id");
		}
	},
	require: function(libraryName) {
		// inserting via DOM fails in Safari 2.0, so brute force approach
		document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
	},
	REQUIRED_PROTOTYPE: '1.6.1',
	load: function() {
		function convertVersionString(versionString){
			var r = versionString.split('.');
			return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
		}
	
		if((typeof Prototype=='undefined') || 
				(typeof Element == 'undefined') || 
				(typeof Element.Methods=='undefined') ||
				(convertVersionString(Prototype.Version) < 
						convertVersionString(eHA.REQUIRED_PROTOTYPE)))
			throw("eHA requires the Prototype JavaScript framework >= " +
					eHA.REQUIRED_PROTOTYPE);
	
		$A(document.getElementsByTagName("script")).findAll( function(s) {
			return (s.src && s.src.match(/eHA\.js(\?.*)?$/))
		}).each( function(s) {
			var path = s.src.replace(/eHA\.js(\?.*)?$/,'');
			var includes = s.src.match(/\?.*load=([a-z,]*)/);
			(includes ? includes[1] : eHA.includeScripts).split(',').each(
					function(include) { eHA.require(path+include+'.js') });
		});
	},
	/**
	 * returns "content" attribute of requested meta tag
	 * @function
	 * @param {String} metaName
	 * @example alert(eHA.meta("Asset_Id")); // will output "1264128919400" (or something)
	 * @returns {String} value of meta's "content" or false if metaName can't be found
	 */
	meta: function(metaName){
		if($$("meta[name="+metaName+"]")[0]){
			return $$("meta[name="+metaName+"]")[0].readAttribute("content");
		}else{
			return false;
		}
	},
	log: {
		// there are 4 log levels, 0 = off, 1 = error, 2 = warning, 3 = debug
		verbosity_level: 0,
		messages_total: 0, // number of total messages
		messages_1: 0, // number of error messages
		messages_2: 0, // number of warning messages
		messages_3: 0, // number of debug messages
		create: function(log_string,log_level_int){
			if(log_level_int<4 && log_level_int>0){
				this.messages_total++;
				eval("this.messages_"+log_level_int+"++;");
				if(this.verbosity_level>=log_level_int){
					// this message should be displayed, prepare outputString
					eval("var message_number = this.messages_"+log_level_int);
					var message_name = "ERROR MESSAGE";
					if(log_level_int==2) message_name = "WARNING MESSAGE";
					if(log_level_int==3) message_name = "DEBUG MESSAGE";
					var outputString = message_name+" "+new Date()+" "+message_number+": "+log_string;
					this.output(outputString);
				}
			}
		},
		output: function(outputString){
			// placeholder function.  redefine this function in order to see output
		}
	},
	loadValuefromClass: function(el, requestedName){
		var requestedValue = false;
		var classNames = el.className.split(" ");
		for(var cni=0;cni<classNames.length;cni++){
			if(classNames[cni].indexOf('-')!=-1){
				classNameSplit = classNames[cni].split('-');
				if(classNameSplit[0]==requestedName){
					requestedValue = classNameSplit[1];
				}
			}
		}
		return requestedValue;
	},
	elementAttributeArray: ["class","id","title","style","dir","lang","cite","classid","type","href","src","alt","name","value","for","action","method","target","enctype","accept","accepty-charset","longdesc","height","width","disabled","selected","checked","profile","size","multiple","label","xmlns","maxlength","cols","rows"],
	
	/**
	 * turns an HTML element into a javascript object
	 * uses all attributes of HTML element (from above array)
	 * as well as any name-value pairs in "className" to set properties
	 * NOTE: className property will also continue to contain all
	 * classes
	 * 
	 * @function
	 * @param {Element} el
	 * @returns {Object}
	 */
	elementToObjectLiteral: function(el){
		var objectLiteral = {};
		for (var i=0; i<eHA.elementAttributeArray.length; i++){
			var thisAttrName = eHA.elementAttributeArray[i];
			if(thisAttrName.indexOf("class")!=-1) thisAttrName = "className"; // this is necessary for IE
			var thisAttrValue = $(el).readAttribute(thisAttrName);
			if(thisAttrValue!==null){
				try{
					eval("objectLiteral."+thisAttrName+"='"+thisAttrValue+"'");
				}catch(err){
					eHA.log.create(err.name+": "+err.message+" -- objectLiteral."+thisAttrName+"='"+thisAttrValue+"'", 1);
				}
			}
		}
		if(el.className){
			var classNames = el.className.split(" ");
			for(var cni=0;cni<classNames.length;cni++){
				if(classNames[cni].indexOf('-')!=-1){
					classNameSplit = classNames[cni].split('-');
					eval("objectLiteral."+classNameSplit[0]+"='"+classNameSplit[1]+"'");
				}
			}
		}
		return objectLiteral;
	},
	addChild: function(ob, childName, childOb){
	   ob[childName] = childOb;
	   childOb.parent = ob;
	}
}
var Affiliation= {
		init: function(){
				var list_selector1="North Shore University<br />Hospital Manhasset, NY";
				var list_selector2="LIJ Medical Center<br />New Hyde Park, NY";
				var message="Please Select Primary Hospital Affiliation ";
				this.navigation = new Element('div',{'id':"affiliation_navigation"});
				this.navigation_message = new Element('div',{'id':"affiliation_message"});
				this.navigation_ul = new Element('ul',{'id':"affiliation_navigation_wrapper"});
				this.navigation_li1 = new Element('li',{'id':"affiliation_NS"});
				this.navigation_li2 = new Element('li',{'id':"affiliation_LIJ"});
				this.navigation_li_a1= new Element('a',{'id':'NS', 'href':'javascript:void(0)'})
				this.navigation_li_a2= new Element('a',{'id':'LIJ','href':'javascript:void(0)'})
				$("listed_item").insert({'top':this.navigation});
				$("affiliation_navigation").insert({'bottom':this.navigation_ul});
				$("affiliation_navigation_wrapper").insert({'top':this.navigation_message});
				$("affiliation_navigation_wrapper").insert({'bottom':this.navigation_li1});
				$("affiliation_NS").insert({'top':this.navigation_li_a1});
				$("affiliation_navigation_wrapper").insert({'bottom':this.navigation_li2});
				$("affiliation_LIJ").insert({'top':this.navigation_li_a2});
				$("NS").innerHTML=list_selector1;
				$("LIJ").innerHTML=list_selector2;
				$("affiliation_message").innerHTML=message;
				if($('listed_item').select('div.left').length>0){
					$('listed_item').select('div.left')[0].addClassName("LIJ").hide();
				}
				if($('listed_item').select('div.right').length>0){
					$('listed_item').select('div.right')[0].addClassName("NS").hide();
				}
				$$("#affiliation_navigation ul li").each(function(element){
					element.down("a").observe('click',function(event){
					Affiliation.show(element.down("a").getAttribute('id'))
						//alert(element.down("a").getAttribute('id'))
					})
				});

		},
		show: function(affiliation){
		$$("#listed_item .listed_item_wrapper .list").each(function(element){
				element.setStyle({
						display: "none"
					});
		
		});	
		$$("#listed_item .listed_item_wrapper ."+affiliation).each(function(element){
				element.setStyle({
						display: "block"
					});
		
		});	
		}
	};



