window.onload = function () {
};

$(document).ready(function(){
	//Initializes main search field value
	$("#ssUserText").val("Search");
	//Clears search field upon focus
	$("#ssUserText").focus(function() {$(this).val("");});
	//Reinitializes main search field value on blur
	$("#ssUserText").blur(function() {$(this).val("Search");});
	
	//Adds event handler for "Find a Doctor" search button
	$("#submitSearchSub").click(function() { $.ajax({ 
			type: 'post',
			url: ('findadoctor.html?ts=' + new Date().getTime()),
			data: $("#searchSub").serialize()
		});
	});
						   
	//Adds top and bottom endcaps onto main nav flyouts
	$(".navMainBubble ul li:first-child").addClass("first");
	$(".navMainBubble ul li:last-child").addClass("last");
	
	//Adjusts width of subnav to match main nav
	$("#navSub").css("width", $("#navMainItems").width());

	//Adds hover event listener to main nav items to show/hide flyouts
	$("#navMainItems li").hover(
		function() {$(this).find(".navMainBubble").show();}, 
		function() {$(this).find(".navMainBubble").hide();}
	);
	
	//Adds right side of tab to active main nav section
	$("#navMainItems li.active").prepend("<div id=\"navMainTab\"></div>");
	
	//Adds vertical separator to copyright line items in footer
	$("#copyright ul.left li:first-child, #copyright ul.right li:first-child").addClass("first");
	
	
	//Adds top and bottom to main content container
	$("#contentMain").prepend("<div id=\"contentMainTop\"></div>");
	$("#contentMain").append("<div id=\"contentMainBottom\"></div>");
	
	//Adds top and bottom to alternate content container
	$("#contentMainAlt").prepend("<div id=\"contentMainAltTop\"></div>");
	$("#contentMainAlt").append("<div id=\"contentMainAltBottom\"></div>");
	
	//Adds top and bottom to sidebar container
	$(".sidebar").prepend("<div class=\"sidebarTop\"></div>");
	$(".sidebar").append("<div class=\"sidebarBottom\"></div>");
	
	//Adds top and bottom to search tips sidebar
	$("#searchTips .contentBox").prepend("<div class=\"contentBoxTop\"><div class=\"IE6Fix\"></div></div>");
	$("#searchTips .contentBox").append("<div class=\"contentBoxBottom\"><div class=\"IE6Fix\"></div></div>");
	
	//Adds top and bottom to search tips sidebar
	$("#currents .contentBox").prepend("<div class=\"contentBoxTop\"><div class=\"IE6Fix\"></div></div>");
	$("#currents .contentBox").append("<div class=\"contentBoxBottom\"><div class=\"IE6Fix\"></div></div>");	
	
	//Adds top and bottom to intranet media box
	$("#homeMedia").prepend("<div class=\"contentBoxTop\"><div class=\"IE6Fix\"></div></div>");
	$("#homeMedia").append("<div class=\"contentBoxBottom\"><div class=\"IE6Fix\"></div></div>");
	
	$(".alertContent p").append("<span class=\"arrowMore\">&nbsp;</span>");
	
	//Adds top and bottom to content box (sub page - right column)
	$("#col2 .contentBox").prepend("<div class=\"contentBoxTop\"><div class=\"IE6Fix\"></div></div>");
	$("#col2 .contentBox").append("<div class=\"contentBoxBottom\"><div class=\"IE6Fix\"></div></div>");
	
	//Hides content box if empty
	//$("#col2 .contentBox .contentBoxBody").each(function (i) {
	//	if ($(this).text().trim() == "") $(this).parent().hide();
	//});

	//Adds top and bottom to content box (sub page - right column)
	$("#subHeadCol2 .contentBox").prepend("<div class=\"contentBoxTop\"><div class=\"IE6Fix\"></div></div>");
	$("#subHeadCol2 .contentBox").append("<div class=\"contentBoxBottom\"><div class=\"IE6Fix\"></div></div>");


	//Adds top and bottom to article container
	$(".articleContainer").prepend("<div class=\"top\"><div class=\"IE6Fix\"></div></div>");
	$(".articleContainer").append("<div class=\"bottom\"><div class=\"IE6Fix\"></div></div>");

	//Adds top and bottom to article container
	$(".topicContainer").prepend("<div class=\"top\"><div class=\"IE6Fix\"></div></div>");
	$(".topicContainer").append("<div class=\"bottom\"><div class=\"IE6Fix\"></div></div>");
	
	//Adds top and bottom to content box (sub page - right column)
	$("#wrapMapDirections").prepend("<div class=\"mapDirectionsTop\"><div class=\"IE6Fix\"></div></div>");
	$("#wrapMapDirections").append("<div class=\"mapDirectionsBottom\"><div class=\"IE6Fix\"></div></div>");
	
	

	$("#awards li p img").each(function() {
		var theImage = $(this);
		theImage.parent().parent().prepend(theImage);
	});

	//Toggles English and Spanish language content
	var blnLangToggle = false;
	
	$(".langToggle").click(function() {
		if (!blnLangToggle) {
			$(".langEng").hide();
			$(".langEsp").show();
			blnLangToggle = true;
		} else {
			$(".langEng").show();
			$(".langEsp").hide();
			blnLangToggle = false;
		}
	});

	if (location.href.indexOf("lang=esp") > 0) {
		$(".langEng").hide();
		$(".langEsp").show();
		blnLangToggle = true;
	}

	//Adds Spanish language flag to all links contained in Spanish language content
	$("#navSecondary .langEsp a, #subHeadCol1Alt .langEsp a, .topicBody .langEsp a").attr("href", function() {
		return this + "?lang=esp";
	});

	
	//Toggles Visibility of Barton-Schmidt Topic Sections
	$(".linkCauses").click(function() {toggleTopicVis("#topicCauses");});
	$(".linkCallDoctor").click(function() {toggleTopicVis("#topicCallDoctor");});
	$(".linkHomeCareAdvice").click(function() {toggleTopicVis("#topicHomeCareAdvice");});
	$(".linkAdditionalInfo").click(function() {toggleTopicVis("#topicAdditionalInfo");});
	
	$(".linkDosageTable").click(function() {toggleTopicVis("#topicDosageTable");});
	$(".linkNotes").click(function() {toggleTopicVis("#topicNotes");});
	
	$(".linkInstructions").click(function() {toggleTopicVis("#topicInstructions");});
	$(".linkReviewers").click(function() {toggleTopicVis("#topicReviewers");});
	$(".linkCopyright").click(function() {toggleTopicVis("#topicCopyright");});
	
	//Adds rollover to highlight table rows
	$("#resultList tr").hover(
		function() {$(this).addClass("active");}, 
		function() {$(this).removeClass("active");}
	);
	
	//Fixes height issue in spotlight box with IE6
	$("#spotlightBottom").append("<div id=\"spotlightBottomIE6\"></div>");
	
		//Adds rollover to highlight table rows
	$("#finderSearchResults tbody tr").hover(
		function() {$(this).addClass("active");}, 
		function() {$(this).removeClass("active");}
	);
	
	//PCP finder city dropdown
	$("#physCityFilter a").click(function () {
		var tempPhysCity = $(this).text();
		$("#physCity").val(tempPhysCity);
		$("#flyoutPhysCity .default").text(tempPhysCity);
		$("#flyoutPhysCity .content").hide();
		$("#flyoutPhysCity .default").show();	
	});
	$("#flyoutPhysCity .default").click(function() {
		$("#flyoutPhysCity .default").hide();
		$("#flyoutPhysCity .content").show();
	});

	
	$("#btnSearchPhys").click(function() {$("#searchPhysDir").submit();});
	$("#physName").keypress(function(e) {
		if (e.which == 13) $("#searchPhysDir").submit();
	});
	
	//Physician finder specialty dropdown
	$("#physSpecialtyFilter a").click(function () {
		var tempPhysSpecialty = $(this).text();
		$("#physSpecialty").val(tempPhysSpecialty);
		$("#flyoutPhysSpecialty .default").text(tempPhysSpecialty);
		$("#flyoutPhysSpecialty .content").hide();
		$("#flyoutPhysSpecialty .default").show();	
	});
	$("#flyoutPhysSpecialty .default").click(function() {
		$("#flyoutPhysSpecialty .default").hide();
		$("#flyoutPhysSpecialty .content").show();
	});

	//Physician finder search filters
	var physNameDefault = "Search by name";
	
	if ($("#physName").val() == "") $("#physName").val(physNameDefault);
	$("#physName").focus(function() {if ($(this).val() == physNameDefault) $(this).val("");});
	$("#physName").blur(function() {if ($(this).val() == "") $(this).val(physNameDefault);});

	$("#searchPhysDir").submit(function() {
		if ($("#physName").val() == physNameDefault) {$("#physName").val('');}
		return true;
	});

	var tempFinderDetail = "";
	$("#finderSearchResults tbody tr").click(function() {
		tempFinderDetail = $(this).find(".itemDetail").html();
		$("#finderDetail").html(tempFinderDetail);
		$("#finderDetail .physPhone").prepend("Phone: ");
		$("#finderDetail .physFax").prepend("Fax: ");
		
		var destination = $("#finderDetail").offset().top;
		$("html:not(:animated),body:not(:animated)").animate({scrollTop : destination-20}, 500);
	});
	
	$("#finderSearchResults tbody tr").each(function() {
		var numCities = 0;
		var listCities = "";
		$(this).find(".physCity").each(function () {
			if (numCities == 0) {
				listCities = $(this).html();
			} else {
				if (listCities.indexOf($(this).html()) == -1) listCities += ", " + $(this).html();
			}
			numCities++;
		});
		$(this).find("td.physListLocations").html(listCities);
		
		var numPhones = 0;
		var listPhones = "";
		$(this).find(".physPhone").each(function () {
			if (numPhones == 0) {
				listPhones = $(this).html();
			} else {
				if (listPhones.indexOf($(this).html()) == -1) listPhones += ", " + $(this).html();
			}
			numPhones++;
		});
		$(this).find("td.physListPhone").html(listPhones);
		
		var numFaxes = 0;
		var listFaxes = "";
		$(this).find(".physFax").each(function () {
			if (numFaxes == 0) {
				listFaxes = $(this).html();
			} else {
				if (listFaxes.indexOf($(this).html()) == -1) listFaxes += ", " + $(this).html();
			}
			numFaxes++;
		});
		$(this).find("td.physListFax").html(listFaxes);
	});
	
	$("#finderDetail .physPhone").prepend("Phone: ");
	$("#finderDetail .physFax").prepend("Fax: ");
	
	$(".detailMain li").wrapInner("<p></p>");
	$(".detailMain li p strong").each(function() {
		var headerText = $(this);
		headerText.parent().parent().prepend(headerText);
	});
	
	//Links Location IDs to physician address "Get Directions" link
	$("#finderSearchResults span[class^='loc']").each(function() {
		var tempHref = $(this).parent().attr("href");
		tempHref += "?locID=" + $(this).attr("class");
		$(this).parent().attr({href : tempHref});
	});

	//Modifies links on home page feature items for video player popup
	$("#homeAlerts .alertContent a").each(function() {
		var tempHref = $(this).attr("href");
		if (tempHref.indexOf(".flv") >= 0) {
			tempHref = g_HttpRelativeWebRoot + "groups/public/@everyone/documents/assets/a002284.jsp?fname=" + tempHref;
			$(this).attr({href : tempHref});
			$(this).addClass("iframe");
		}
	});

	
	
	//Adds hover event listener to flyouts
	$(".flyoutShort, .flyoutMed, .flyoutWide, .flyoutLocSvcs").hover(
		function() {
			if (!$(this).find(".default").hasClass("noFly")) {
				$(this).find(".default").hide();
				$(this).find(".content").show();
			}
		}, 
		function() {
			if (!$(this).find(".default").hasClass("noFly")) {
				$(this).find(".content").hide();
				$(this).find(".default").show();
			}
		}
	);
	
	$('.scroll-pane').jScrollPane({showArrows: true, scrollbarWidth: 20});
	$(".flyoutWide .content, .flyoutMed .content, .flyoutShort .content, .flyoutLocSvcs .content").hide();
	
	$('input:checkbox').checkbox({cls:'jquery-safari-checkbox'});
	$('input:radio').checkbox({cls:'jquery-safari-checkbox'});
	
	//Initializes Colorbox elements
	$(".colorbox").colorbox();
	$(".iframe").colorbox({width:"690", height:"565", iframe:true});
	
	//Preloads all images referenced in CSS
	//$.preloadCssImages();
}); 

toggleTopicVis = function (elementId) {
	if ($(elementId + " .topicToggleOn").css("display") != "none") {
		$(elementId + " .topicToggleOn").hide();
		$(elementId + " .topicToggleOff").show();
		$(elementId + " .topicContent").show();
	} else {
		$(elementId + " .topicToggleOn").show();
		$(elementId + " .topicToggleOff").hide();
		$(elementId + " .topicContent").hide();
	}
}

displayForm = function (elementId) {
	var content = [];
	$('#' + elementId + ' input').each(function() {
		var el = $(this);
		if ( (el.attr('type').toLowerCase() == 'radio')) {
		if ( this.checked )
			content.push([
			'"', el.attr('name'), '": ',
			'value="', ( this.value ), '"',
			( this.disabled ? ', disabled' : '' )
			].join(''));
		} else
			content.push([
			'"', el.attr('name'), '": ',
			( this.checked ? 'checked' : 'not checked' ), 
			( this.disabled ? ', disabled' : '' )
			].join(''));
	});
	alert(content.join('\n'));
}

changeStyle = function(skin) {
	jQuery('#myform :checkbox').checkbox((skin ? {cls: skin} : {}));
}
