var vZone
var vSubDir
var vBody  //the page-name base

function init(){ //Written in jquery syntax
	//Used in setZone and to set subdir syntax.
	vBody = document.getElementsByTagName('body')[0].className; //the page-name base
	vZone =  $("body").attr("rel");
	vSubDir = vZone == "root" ? "" : "../";
	var vShadow = 'url(\"' + vSubDir + 'images/shadow.png\") no-repeat bottom right'; //Don't know why I have to do it this way just because I put the img in a subdir.

    $("#nav ul a").prepend("<span>&gt;&nbsp;</span>");
//    $("#nav ul a.md span").html("<img src='" + vSubDir + "images/md_icon.gif' class='md_icon' width='18' height='15' border='0'>");
//    $("#nav ul a.md2").append("<img src='" + vSubDir + "images/md_icon.gif' class='md_icon2' width='18' height='15' border='0'>");
    $("#nav ul a.md3").attr('title','Information for Doctors').append("<span class='md_icon3'>m.d.</span>");
    $("#ul_tert_nav a.md3").append("<span class='md_icon3' title='Information for Doctors'>m.d.</span>"); //Title attr is used for the lateral nav, so can't affect title here.
//	$(".test_ul_shadow").wrap("<div class='ddshadow'></div>");

	var $mainmenu=$("#nav") //reference main menu UL
	var $submenus = $mainmenu.find("li>ul")
	var $vHeight
 	$submenus.each(function(i){
		if (!($.browser.msie && $.browser.version < 7)){//Turn off the shadow for IE6
			$(this).css({'z-index':'1000','visibility' : 'visible','display' : 'none', 'background' : vShadow, 'padding' : '0 8px 9px 0', '-moz-border-radius-bottomleft' : '17px', '-moz-border-radius-topright' : '17px', '-webkit-border-top-right-radius' : '17px', '-webkit-border-bottom-left-radius' : '17px'});
		}
		$(this).find('li:last').css({'border-bottom':'1px solid #5086b5','height':'auto','opacity':'.85','filter':'alpha(opacity=85)','-moz-opacity':'0.85'});
		$(this).find('li').css({'z-index':'1000','border-left':'1px solid #5086b5','border-right':'1px solid #5086b5'});
	}); //end $submenus.each()  

//	setZone(){
		var oZone //object container
		if(vZone != "root"){//Use 'root' for homepage and any root-level pages for the value of the REL attribute in the BODY tag.
			oZone =  "#" + $("body").attr("rel") + ">a";
			$(oZone).addClass("zone_selected");
		}
//	end setZone
	
	//FOR BETA: then delete
	setNavLinkRelativity();

	var d = new Date(); //write in the footer for the copyright year.
	//	$("#c_year").text(d.getFullYear());
}

function basename(path, suffix) {
    var b = path.replace(/^.*[\/\\]/g, '');
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    return b;
}
function addEvent(elm, evType, fn, useCapture){
  if (elm.addEventListener) 
  {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent) { 
    var r = elm.attachEvent('on' + evType, fn);
    return r;
  } else {
    elm['on' + evType] = fn;
  }
}


function setNavLinkRelativity(){
	var aA = document.getElementById("contentbody").getElementsByTagName("a");
	var vHref
	for(var i = 0; i<aA.length; i++){
		vHref = basename(aA[i].href, ".php")
		if(vHref == "placeholder"){
			aA[i].onclick = function(){alert("Not linked yet or not yet built."); return false;}
		}
	}
	
/* 	var aGlobalNavA = $("#nav > li > a")
	var x = $("#nav > li > a").length;
	var oSubA, vSubAID, vCurParURL, vNewParURL, vCurSubURL, vNewSubURL
	for(var i = 0; i < x; i++){
	//Get the SUB-anchors for a particular global zone.
		oSubA = aGlobalNavA[i].parentNode;
		vSubAID = oSubA.id;
		vSubA = "#" + oSubA.id + ">ul a";
	//set relativity for this link.
		vCurParURL = basename(aGlobalNavA[i].href);
		vNewParURL = vSubDir + vSubAID + "/" + vCurParURL;
		aGlobalNavA[i].href = vNewParURL;
	//The collections of sub-anchors for this zone.
		aSubA = $(vSubA);
		for(var y = 0; y < aSubA.length; y++){
			vCurSubURL = basename(aSubA[y].href);
			vNewSubURL = vSubDir + vSubAID + "/" + vCurSubURL;
			aSubA[y].href = vNewSubURL;
		}
	} */
}

function setlateral(){
//FOR THIS TO WORK, the classname of the BODY tag and the name of the htm file MUST BE THE SAME.
//<li><a href='drs_rc.htm' class="tert_zone drs_rc" title="Dr. Cole" onmouseover="showPortrait(this);" onmouseout="hidePortrait();">Randall E. Cole, M.D., FAAO</a></li>
//             drs_rc    and                drs_rc  = class name of Body for the respective htm file.
//Because of the special needs for diff. dr names, the TITLE attribute is required.

	var aA = document.getElementById("ul_tert_nav").getElementsByTagName("a");
	var vPrev, vPrevA, vCur, vNext, vNextA, filename;
//	var vBody = document.getElementsByTagName('body')[0].className; //the page-name base
	var vLevel = $("body").attr("level"); //the page level, used for manipulating the navigation from a tert page to a secondary  page.
	var Prev, PrevA, Next, NextA
	var aLateral = document.getElementById("nav_lateral").getElementsByTagName("a");
	var aLabels = document.getElementById("nav_lateral").getElementsByTagName("span");
	var vTitle, vCurTitle //The title of the dr to go in the lateral nav, stored in the Title attribute of the anchor.

	var aLen = aA.length;
	for(var i=0; i<aLen; i++){
		filename = basename(aA[i].href, ".php");
		if(filename == vBody){//Establish what page is the CURRENT (vCur) page.
			var a, b
			a = i>0 ? i-1 : null; //for PREV 
			b = i==aLen-1 ? null : i+1; //for NEXT
			vCurTitle = aA[i].title;
			vCur = aA[i].vTitle;
			if((a>0) || (a != null)){
				vPrevA = basename(aA[a].href);
				vTitle = aA[a].title;
				aLateral[0].href=vPrevA;
				aLabels[0].innerHTML = vTitle;
			}else{
				aLateral[0].style.display = "none"; //If there is no object for Previous.
				aLabels[1].style.display = "none"; //the separator pipe.
			}
			if(b){
				vNextA = basename(aA[b].href);
				vTitle = aA[b].title;
				aLateral[1].href=vNextA;
				aLabels[4].innerHTML = vTitle;
			}else{
				aLateral[1].style.display = "none"; //If there is no object for Next.
				aLabels[3].style.display = "none"; //the separator pipe.
			}
			break;
		}
	}
	aLabels[2].innerHTML = vCurTitle;
	return;
//NOTE: 1) The BODY class must be set to the filename base.

}

var isIE=null
var agt=navigator.userAgent.toLowerCase();
if(agt.indexOf("msie 6")!=-1){isIE=1};
var d_imgs = new Array();
function initPortraits(){
	var aImageAnchors = $('#ul_tert_nav > li > a');
	var ocontainer = $("#sm_portraitcontainer");
	aImageAnchors.each(function(i){
		var vImgName = $(this).attr("class").substr(10);//this section of classname = img and dr filename
		var str = "../images/" + vImgName + ".jpg";
		d_imgs[i] = new Image; d_imgs[i].src = str; //preload image
		var vBkgrd = "url(" + d_imgs[i].src + ") top left no-repeat";
		if(vBody == "index"){
			$('#tertiary_nav').mouseleave(function(){hidePortrait("index");});//Set the default image on mouseleave of nav area.
		}
		if(vImgName == vBody){
			ocontainer.attr("class","sm_portraitcontainer_selected").css("background",vBkgrd); //Set current image
			$('#tertiary_nav').mouseleave(function(){hidePortrait(i);});//Set the default image on mouseleave of nav area.
		}
		$(this).mouseover(function(){
			showPortrait(i);
		});
	});
}
function showPortrait (xImg){
	if (isIE){return};
	var vBkgrd = "url(" + d_imgs[xImg].src + ") top left no-repeat";
	$("#sm_portraitcontainer").css("background",vBkgrd).attr("class","sm_portraitcontainer");
}

function hidePortrait(xImg){
	if (isIE){return};
	var ocontainer = $("#sm_portraitcontainer");
	if(xImg == "index"){
		ocontainer.css("background","");
		return;
	}
//	if(d_imgs[xImg] == 'undefined' || d_imgs[xImg] == ''){ return; } //testing
	var vBkgrd = "url(" + d_imgs[xImg].src + ") top left no-repeat";
	$("#sm_portraitcontainer").css("background",vBkgrd).attr("class","sm_portraitcontainer_selected");
}


var vPage
function cyclehomepages(){
	var numbspons = 8;  //= number+1 of Drs to rotate
	var randomnumber=Math.floor(Math.random()*numbspons);
//alert(randomnumber);	
	if(randomnumber == 0){randomnumber=1;} //randomnumber=1;
	vPage = "hp_" + randomnumber + ".php";
    document.getElementById("hp_if").src = vPage;
//	document.write("<iframe frameborder='0' border='0' allowtransparency='true' src='" + vPage +"' name='iframe_rotate' id='iframe_rotate' marginheight='0' marginwidth='0' width='600' height='308' hspace='0' scrolling='No' vspace='0' style='border:none; background-color:transparent;'>&nbsp;</iframe>");
}	
//Overview Notes:
// * the BODY class is must be set to filename base for all pages except level 1.0 index pages. Those are set to "index".
// * the BODY REL attribute is always set to the name of the zone, which is the same as the ID of the zone's LI global-nav label container.
// * The tertiary navigation scheme is different for zones with actual tertiary navigation and those with only Secondary navigation presented in the left-margin. 

//FAQ page toggle script
function toggleit(x, oL, oA){ //oL = label (text of anchor), oA= answer DIV
	if ($(oA).is(":visible")==true){
		$(oL).text("[Show Answer]");
		$(oA).slideUp("slow");
	}else{
		$(oL).text("[Hide Answer]");
		$(oA).slideDown("slow");
	}
}
function initFAQ(){
	if ($.browser.mozilla){
		$("html").css("overflow","-moz-scrollbars-vertical")
	}
	$(".a_").hide();
	var aLinks = $("a.see_answer");
	aLinks.each(
		function(i){
			var y=i+1
			var jThis = $(this); //The link
			//Get # suffix for corresponding a# id.
			var vParentID = $(aLinks[i]).parents("div:eq(0)").attr("id").slice(1);
			//Create selector strings
			var vL = "#l" + y; //the label
			var iL = "l" + y;
			var vA = "#a" + y; //the visible-toggled answer DIV
			//Object Variable for Answer object
			var oA = $(vA); //the visible-toggled answer DIV
			//Create ID for each Show-Anwser link.
	 		$(aLinks[i]).attr({"id":iL});
			//Bind click event.
			jThis.click(/* function (objEvent){
					toggleit(y, vL, vA); 
					return (false);
			}); */ 
				function (){
					if (oA.is(":visible")){
						$(oA).slideUp();
						$(this).text("[Show Answer]");
					}else{
						oA.slideDown("slow");
						$(this).text("[Hide Answer]");
					}
				});  //end click
		} //end function
	); //end each
} //end initFAQ

function setPopDefinitions(vStandard){//For displaying images or definitions on select words in running text, esp. in the Eye Care/Treatment area.
	var vContainer = "." + vStandard;
	var vFind = vContainer + ":eq(0)";
	$("span.pop_label").css({"cursor":"pointer","color":"#3366FF"});
	$(".pop_container").each(function(i){
		var oSpan = $(this).find("span:eq(0)");
		var oPop = $(this).find(vFind);
		oSpan.mouseenter(function(){
			$(this).css({"color":"#65B561"});
//			$(this).css({"color":"#65B561","border-bottom":"2px dotted #65B561"});
			oPop.show("slow");
		});
		$(this).mouseleave(function(){
			oSpan.css({"color":"#3366FF"});
//			oSpan.css({"color":"#8acae9","border-bottom":"2px dotted #3180ff"});
			oPop.hide("slow");
		})
	});//End Each pop_container
}

function makeModal(LBType,isDblFade){//ex: light, light1 ... all must begin with 'light'. For having more than one LBx on a page.
	//isDblFade = true | false. If false, then fade both objects in at the same time.
//	$('#light').css('left', $(window).width() / 2 - ($('#light').width() / 2));
	if(LBType==null){LBType = "light"} //default lbx id.
	jqLightID = "#" + LBType;
	if(isDblFade==null){isDblFade=true;} //deftaul is to have double fades.
	
	var vScrollHeight = $(document).height() + "px"; //100% doesn't quite cut it.
	$('#fade').css({
		'height':vScrollHeight,
		'opacity':'0.65' //IE isn't following the CSS spec for some reason.
	})
	$("div[id^='light']").css("z-index","4000")
	if(isDblFade==false){
		var objs = "#fade," + jqLightID;
		$(objs).show("fast");
		return;
	}
	$("#fade").show("fast");
	setTimeout(function() { $(jqLightID).fadeIn("slow"); }, 250);
/* 
	document.getElementById('fade').style.display='block'
	document.getElementById('light').style.display='block'; 
*/
//	$("#light").fadeIn("normal");
	return (false);
}	
function closeModel(LBType){
	LBType = "#" + LBType; //(may need this later if more than one type of the same lightbox on one page)
	$("div[id^='light']").fadeOut("normal");
	setTimeout(function() { $("#fade").hide("slow"); }, 250);
}

function AddLightbox(cClose,LBType,LBOffset){//Need to do it this way because of using an INClude for the closing of the page.
  	LBOffset = 120; //for IE setting of LBx relative to contentbody
	if(LBType==null){LBType = "light"}
	var jqLightID = "#" + LBType;
	
	var lightOnClick = '\
	<div id="light" class="white_content"></div>\
	';
	if(cClose){
		lightOnClick = '\
	<div id="light" class="white_content" onclick="closeModel(\'light\')"></div>\
	';
	}
	if($('#fade').length < 1){
		$("#wrapper").append('\
	<div id="fade" class="black_overlay" onclick="closeModel(\'light\')"></div>\
		');
	}
	$("#contentbody").append(lightOnClick);
	//set the lb relative to contentbocy
	if($.browser.mozilla || $.browser.safari){
		$("#contentbody").css("position","relative"); //to contain the lightbox so that it moves on window resize.
	}
	if($.browser.msie){
		fn={
			reset: function(){
				var locContentbody = $("#contentbody").offset().left;
				$(jqLightID).css('left',(locContentbody+LBOffset)+"px");
			}
		}
		var locContentbody = $("#contentbody").offset().left;
		$(jqLightID).css('left',(locContentbody+120)+"px");//120 specific to the History page. May need to change.
		IEResizeEvent(fn.reset);
	}
	// end set relative to contentbody
	
	//Fill div#light with content of div#testimony
	if($("#testimony").length>0){
		var vData = $("#testimony").html();
		$(jqLightID).html(vData);
	}
}
function IEResizeEvent(fn){
	$(window).resize(fn);
}
/* Lightbox for Products in OS area */
function getText(vText){//Gets the FIRST word of a multiword anchor text.
	vText = vText.toLowerCase();
	var isSpace = vText.indexOf(" ");
	if(isSpace > 0){
		vText = vText.substring(0, isSpace);
	}
	return vText;
}

var t_imgs = new Array();
function initProds(imgPrfx){
	//args: img prefix, 
	$("head").append('<link href="../lb_prods.css" rel="stylesheet" type="text/css" />');
	var aProds = $("#prod_list").find("p>a");
	aProds.each(function(i){
		var vText = getText($(this).text());//img name must be first word of anchor text.
		$(this).attr("class",vText); //To change the "selected" state.
		var svImgString = "../images/" + imgPrfx + "_"
		svImgString += vText + ".jpg"
		t_imgs[i] = new Image; t_imgs[i].src = svImgString; //preload image

		$(this).click(function(){
			 showProd2(this,i,t_imgs[i],vText);
		});
	});	
}

function showProd2(oA, vX, vImg, vClass){
	$("#prod_list").removeClass().addClass(vClass).click(function(){
		$("prod_list").attr("class",vClass); //To change the "selected" state.
		$("#img_prod").attr("src",t_imgs[vX].src);
	});
}

function AddLightboxProducts(imgPfx, incRoot,LBType){
	//Requires the jquery.inc-5.js in the main webpage head.
	//args: imgPREFIX, root of incl file
	if(LBType==null){LBType = "light2"} //might need this later down the line when more than three lbx on a page.
	incRoot = incRoot + ".php";
	$("#contentbody").append('\
	<div id="light2" class="white_content" style="display:none;"></div>\
	');
	if($('#fade').length < 1){
	$("#wrapper").append('\
	<div id="fade" class="black_overlay" onclick="closeModel(\'light2\')"></div>\
	');
	}
	$("#light2").inc(incRoot);
	//initProds(imgPfx); //Needs to be placed in the inc file in because inc'd content not being recognized as objects.
}

function setRtMargingTestimonyBug(){//Picture is NOT a background.
	var htRtMarging = $("#right_margin_content_portrait2").height();
	var vHt = (htRtMarging - 70) + "px"; //65 = height of testimonial bug block
	$("#testim_trigger").css({"top":vHt,"width":"238px"});
}
