window.onload = function () {
};

$(document).ready(function(){
	//Initializes main search field value
	var searchDefaultEng = $(".searchDefaultEng").html();
	var searchDefaultEs = $(".searchDefaultEs").html();
	var searchDefaultZh = $(".searchDefaultZh").html();
	var searchDefault = searchDefaultEng;

	$(".langToggleEng").click(function() {
		$(".langEsp").hide();
		$(".langZh").hide();
		$(".langEng").show();
		$("#navSub").css("width", $("#navMainItems").width());
		$("#langPref, #langPref2").val("");
		searchDefault = searchDefaultEng;
		$("#ssUserText").val(searchDefault);
	});
	$(".langToggleEsp").click(function() {
		$(".langEng").hide();
		$(".langZh").hide();
		$(".langEsp").show();
		$("#navSub").css("width", $("#navMainItems").width());
		$("#langPref, #langPref2").val("es");
		searchDefault = searchDefaultEs;
		$("#ssUserText").val(searchDefault);
	});
	$(".langToggleZh").click(function() {
		$(".langEng").hide();
		$(".langEsp").hide();
		$(".langZh").show();
		$("#navSub").css("width", $("#navMainItems").width());
		$("#langPref, #langPref2").val("zh");
		searchDefault = searchDefaultZh;
		$("#ssUserText").val(searchDefault);
	});

	if (location.href.indexOf("lang=es") > 0) {
		$(".langEng").hide();
		$(".langZh").hide();
		$(".langEsp").show();
		$("#langPref, #langPref2").val("es");
		searchDefault = searchDefaultEs;
	}
	if (location.href.indexOf("lang=zh") > 0) {
		$(".langEng").hide();
		$(".langEsp").hide();
		$(".langZh").show();
		$("#langPref, #langPref2").val("zh");
		searchDefault = searchDefaultZh;
	}

	//Adds Spanish language flag to all links contained in Spanish language content
	$(".langEsp a").each(function() {
		var tempHref = $(this).attr("href");
		if (tempHref.indexOf("?") > 0) {
			$(this).attr("href", tempHref + "&lang=es");
		} else {
			$(this).attr("href", tempHref + "?lang=es");
		}
	});
	//Adds Mandarin language flag to all links contained in Mandarin language content
	$(".langZh a").each(function() {
		var tempHref = $(this).attr("href");
		if (tempHref.indexOf("?") > 0) {
			$(this).attr("href", tempHref + "&lang=zh");
		} else {
			$(this).attr("href", tempHref + "?lang=zh");
		}
	});
	
	$("#ssUserText").val(searchDefault).focus(function() {$(this).val("");}).blur(function() {$(this).val(searchDefault);});
	
	//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 private content container
	$("#contentMainPriv").prepend("<div id=\"contentMainPrivTop\"></div>");
	$("#contentMainPriv").append("<div id=\"contentMainBottom\"></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 media boxes
	$("#homeMedia, #subMedia, #secondaryMedia, #homePoll").prepend("<div class=\"contentBoxTop\"><div class=\"IE6Fix\"></div></div>");
	$("#homeMedia, #subMedia, #secondaryMedia, #homePoll").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>");
	
	//Modifies links on home page feature videos for video player popup
	$("#homeVideos a").each(function() {
		var tempHref = $(this).attr("href");
		if (tempHref.indexOf(".flv") >= 0) {
			tempHref = ssUrlPrefix + "videoplayer/?fname=" + tempHref;
			$(this).attr({href : tempHref});
			$(this).addClass("iframe");
		}
	});
	
	
	//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>");

	//Adds top and bottom to secondary page spotlight boxes
	$("#spotlight .contentBox").prepend("<div class=\"contentBoxTop\"><div class=\"IE6Fix\"></div></div>");
	$("#spotlight .contentBox").append("<div class=\"contentBoxBottom\"><div class=\"IE6Fix\"></div></div>");

	//Adds top and bottom to physician finder sidebar boxes
	$(".detailSidebar .contentBox").prepend("<div class=\"contentBoxTop\"><div class=\"IE6Fix\"></div></div>");
	$(".detailSidebar .contentBox").append("<div class=\"contentBoxBottom\"><div class=\"IE6Fix\"></div></div>");


	//"Why I Joined" photo functionality
	$("#whyIJoinedPhotos ul li").wrapInner("<p></p>");
	$("#whyIJoinedPhotos ul li img").each(function() {
		var theImage = $(this);
		theImage.parent().parent().prepend(theImage);
	});
	$("#whyIJoinedPhotos ul li").append("<div class=\"activePhoto\"></div>");
	$("#whyIJoinedPhotos ul li").mouseover(
		function() {
			$("#whyIJoinedPhotos ul li").find(".activePhoto").hide();
			$(this).find(".activePhoto").show();
			$("#whyIJoinedCopy").html($(this).html());
			$("#whyIJoinedCopy img").remove();
			$("#whyIJoinedCopy .activePhoto").remove();
		}
	);
	
	//Selects random "Why I Joined" photo and quote to display on page load
	var whyIJoinedPhotoCount = 0;
	$("#whyIJoinedPhotos ul li").each(function() {
		whyIJoinedPhotoCount += 1;
	});
	var randomPhotoIndex = Math.ceil(Math.random()*whyIJoinedPhotoCount);  //nth-child indexes start at 1
	$("#whyIJoinedPhotos ul li:nth-child(" + randomPhotoIndex + ")").triggerHandler("mouseover");

	//MD Spotlight - IE fixes
	$("#mdSpotlight ul li").wrapInner("<p></p>");
	$("#mdSpotlight ul li img").each(function() {
		var mdSpotlightImage = $(this);
		mdSpotlightImage.parent().parent().prepend(mdSpotlightImage);
	});

	//Patient Spotlight - IE fixes
	$("#patientSpotlight p img").each(function() {
		var patientSpotlightImage = $(this);
		patientSpotlightImage.parent().parent().prepend(patientSpotlightImage);
	});
	
	//News & Events Spotlight - adds bottom line to the list
	$("#newsEventsSpotlight ul li:last-child").addClass("last");

	$("#homeVideos ul li a").wrapInner("<p></p>");
	$("#homeVideos li p img").each(function() {
		var theImage = $(this);
		theImage.parent().parent().prepend(theImage);
	});
	$("#homeVideos .langEng li p").append("<strong>View</strong>");
	$("#homeVideos .langEsp li p").append("<strong>Visualizar</strong>");
	$("#homeVideos .langZh li p").append("<strong>&#35264;&#30475;</strong>");
	

	//Adds rollover to highlight table rows
	$("#finderSearchResults tbody tr").hover(
		function() {$(this).addClass("active");}, 
		function() {$(this).removeClass("active");}
	);
	
	//PCP finder city dropdown
	$("#pcpCityFilter a").click(function () {
		var tempPCPCity = $(this).text();
		$("#pcpCity").val(tempPCPCity);
		$("#flyoutPCPCity .default").text(tempPCPCity);
		$("#flyoutPCPCity .content").hide();
		$("#flyoutPCPCity .default").show();	
	});
	$("#flyoutPCPCity .default").click(function() {
		$("#flyoutPCPCity .default").hide();
		$("#flyoutPCPCity .content").show();
	});

	//PCP finder search filters
	var pcpZipDefault = "By zip";
	var pcpNameDefault = "By physician name";
	
	if ($("#pcpZip").val() == "") $("#pcpZip").val(pcpZipDefault);
	$("#pcpZip").focus(function() {if ($(this).val() == pcpZipDefault) $(this).val("");});
	$("#pcpZip").blur(function() {if ($(this).val() == "") $(this).val(pcpZipDefault);});

	if ($("#pcpName").val() == "") $("#pcpName").val(pcpNameDefault);
	$("#pcpName").focus(function() {if ($(this).val() == pcpNameDefault) $(this).val("");});
	$("#pcpName").blur(function() {if ($(this).val() == "") $(this).val(pcpNameDefault);});

	$("#searchPCP").submit(function() {
		if ($("#pcpZip").val() == pcpZipDefault) {$("#pcpZip").val('');}
		if ($("#pcpName").val() == pcpNameDefault) {$("#pcpName").val('');}

		return true;
	});
	
	$("#btnSearchPCP").click(function() {$("#searchPCP").submit();});
	$("#pcpZip, #pcpName").keypress(function(e) {
		if (e.which == 13) $("#searchPCP").submit();
	});
	
	//Specialist finder city dropdown
	$("#specSpecialtyFilter a").click(function () {
		var tempSpecSpecialty = $(this).text();
		$("#specSpecialty").val(tempSpecSpecialty);
		$("#flyoutSpecSpecialty .default").text(tempSpecSpecialty);
		$("#flyoutSpecSpecialty .content").hide();
		$("#flyoutSpecSpecialty .default").show();	
	});
	$("#flyoutSpecSpecialty .default").click(function() {
		$("#flyoutSpecSpecialty .default").hide();
		$("#flyoutSpecSpecialty .content").show();
	});

	//Specialist finder search filters
	var specNameDefault = "By specialist name";
	
	if ($("#specName").val() == "") $("#specName").val(specNameDefault);
	$("#specName").focus(function() {if ($(this).val() == specNameDefault) $(this).val("");});
	$("#specName").blur(function() {if ($(this).val() == "") $(this).val(specNameDefault);});

	$("#searchSpecialist").submit(function() {
		if ($("#specName").val() == specNameDefault) {$("#specName").val('');}

		return true;
	});
	
	$("#btnSearchSpecialist").click(function() {$("#searchSpecialist").submit();});
	$("#specName").keypress(function(e) {
		if (e.which == 13) $("#searchSpecialist").submit();
	});

	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 numGroups = 0;
		var listGroups = "";
		$(this).find(".physGroupName").each(function () {
			if (numGroups == 0) {
				listGroups = $(this).html();
			} else {
				if (listGroups.indexOf($(this).html()) == -1) listGroups += ", " + $(this).html();
			}
			numGroups++;
		});
		$(this).find("td.physListGroups").html(listGroups);
	});
	
	$("#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});
	});
	
	//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();
}); 

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} : {}));
}
