function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


function updateNavIE(li){
	if(navigator.userAgent.toLowerCase().indexOf('msie 6.') != -1 || navigator.userAgent.toLowerCase().indexOf('msie 5.') != -1){
		var as, tab_lefts = $('.tab-left', li);
		if(li.parentNode && tab_lefts.length > 0){
			if(li.parentNode.nodeName.toLowerCase() == 'ul' && li.parentNode.className){
				if(li.parentNode.className == "sub"){
					as = li.getElementsByTagName('a');
					if(as.length > 0){
						tab_lefts[0].style.width = as[0].offsetWidth + 'px';
					}
				}
			}
		}
	}
}


sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	var divs;
	
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			updateNavIE(this);
			this.className+=" sfhover";
			if(this.parentNode){
				if(this.parentNode.className){
					if(this.parentNode.className == "sub"){
						divs = this.parentNode.getElementsByTagName("div");
						for(var j=0; j<divs.length; j++){
							if(divs[j].className){
								if(divs[j].className == "sub"){
									divs[j].style.visibility = "visible";
								}
							}
						}
					}
				}
			}
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			if(this.parentNode){
				if(this.parentNode.className){
					if(this.parentNode.className == "sub"){
						divs = this.parentNode.getElementsByTagName("div");
						for(var j=0; j<divs.length; j++){
							if(divs[j].className){
								if(divs[j].className == "sub"){
									divs[j].style.visibility = "hidden";
								}
							}
						}
					}
				}
			}
		}
	}	
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


/* DROP DOWNS */

if(document.styleSheets){
	if(document.styleSheets.length > 0){
		if(document.styleSheets[0].href){
			var root = document.styleSheets[0].href.substring(0, document.styleSheets[0].href.indexOf('?css='));
			document.write('<link href="' + root + '/js/js.css" rel="stylesheet" type="text/css" media="screen" />');
		}
	}
}

function initExtraNavDivs(){
	if($('nav')){
		$('#nav ul ul ul').each(function(ind){
			$(this).wrap('<div class="sub" />');
		});
	}
}

$(document).ready(function(){
	$('.drug-info').each(function(ind1) {
		$(this).click(function() {
			$('.drop-down', this).each(function(ind2) {
				if($(this).hasClass('drop-down-open')){
					$(this).removeClass('drop-down-open');
				} else {
					$(this).addClass('drop-down-open');
				}
			});
		});
	});
	initRollovers();
	initResearchDropdowns();
	initExtraNavDivs();
	initImgsWithinLinks();
});



function refPngFix(){
	if(navigator.userAgent.toLowerCase().indexOf('msie 6.') != -1 || navigator.userAgent.toLowerCase().indexOf('msie 5.') != -1){
		if(IEPNGFix){
			clearTimeout(IEPNGFix.update.timer);
			IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
		}
	}
}



function initImgsWithinLinks(){
	$('a.rImg img').mouseover(function(){
		$(this).attr('src', $(this).attr('src').replace(/.png/, '_over.png'));
		if(this.currentStyle.filter){
			this.style.filter = this.currentStyle.filter.replace(/.png/, '_over.png');
		}
	}),
		
	$('a.rImg img').mouseout(function(){
		$(this).attr('src', $(this).attr('src').replace(/_over.png/, '.png'));
		if(this.currentStyle.filter){
			this.style.filter = this.currentStyle.filter.replace(/_over.png/, '.png');
		}
	});
}



function initRollovers(el) {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	
	if (el) var inputs = el.getElementsByTagName('input');
	else var inputs = document.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i++) {		
		if (inputs[i].type == 'image') {
			var src = inputs[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_over'+ftype);
			
			if(navigator.userAgent.toLowerCase().indexOf('msie 6.') != -1 || navigator.userAgent.toLowerCase().indexOf('msie 5.') != -1){
				if(inputs[i].currentStyle.filter){
					hsrc = inputs[i].currentStyle.filter.substring(inputs[i].currentStyle.filter.indexOf('src="')+5, inputs[i].currentStyle.filter.indexOf('",sizing'));
				}
			}

			inputs[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;						
			
			inputs[i].onmouseover = function() {
				if(navigator.userAgent.toLowerCase().indexOf('msie 6.') != -1 || navigator.userAgent.toLowerCase().indexOf('msie 5.') != -1){
					if(this.currentStyle.filter){
						this.style.filter = this.currentStyle.filter.replace('.png', '_over.png');
					} else {
						sTempSrc = this.getAttribute('src');
						this.setAttribute('src', this.getAttribute('hsrc'));
					}
					
				} else {
					sTempSrc = this.getAttribute('src');
					this.setAttribute('src', this.getAttribute('hsrc'));
				}
			}	

			inputs[i].onmouseout = function() {
				if(navigator.userAgent.toLowerCase().indexOf('msie 6.') != -1 || navigator.userAgent.toLowerCase().indexOf('msie 5.') != -1){
					if(this.currentStyle.filter){
						this.style.filter = this.currentStyle.filter.replace('_over.png', '.png');
					} else {
						if (!sTempSrc) {
							sTempSrc = this.getAttribute('src').replace('_over'+ftype, ftype);
						}
						this.setAttribute('src', sTempSrc);
					}

				} else {
					if (!sTempSrc) {
						sTempSrc = this.getAttribute('src').replace('_over'+ftype, ftype);
					}
					this.setAttribute('src', sTempSrc);
				}
			}
		}
	}
}


function initResearchDropdowns(){
	if($('research-dropdowns')){
		var drug_infos = $('#research-dropdowns .drug-info');
		var years = $('#research-dropdowns .details-year strong');
		if(drug_infos.length > 0 && drug_infos.length == years.length){			
			$('#research-dropdowns').prepend('<p>Please select: <select></select></p>');
			var i, option, select = $('#research-dropdowns select')[0];
			for(i=0; i<years.length; i++){
				if(i == 0){
					option = new Option(years[i].innerHTML, years[i].innerHTML, true, true);
				} else {
					option = new Option(years[i].innerHTML, years[i].innerHTML);
				}
				
				if ($.browser.msie) { select.add(option); }
                else { select.add(option, null); }
			}
			
			select.id = 'research-dropdowns-select';
			
			jQuery('#research-dropdowns-select').change(function () {
				$('#research-dropdowns-select option:selected').each(function () {                	
					drug_infos.css('display','none');
					if($(this).text() != ''){
						for(i=0; i<years.length; i++){
							if(years[i].innerHTML == $(this).text()){
								jQuery(drug_infos[i]).css('display','inline');
							}
						}
					}
              	});
			}).trigger('change');
			
		}
	}
}
