﻿//****************************************************************
// ON PAGE LOAD
//****************************************************************
$(function () {
	StyleTooltipBoxes();
	FixFloats();
	InitializeSearchFlyouts();
	InitializePopupLinks();
	InitializeTreatmentNav();
});



//****************************************************************
// StyleTooltipBoxes
//****************************************************************
function StyleTooltipBoxes() {
	try {
		$(".help_box").prepend("<span class=\"help_box_pointer\"></span>");
		$(".practice_tooltip").prepend("<span class=\"tooltip_pointer\"></span>");
	}
	catch (error) {
		alert("StyleTooltipBoxes: " + error.description, false, true);
	}
}



//****************************************************************
// InitializeSearchFlyouts
//****************************************************************
function InitializeSearchFlyouts() {
	try {
		$(".flyout_search_dropdown").append("<a class=\"btn_close\" href=\"#\"><span>x</span>close </a>")
		$(".flyout_search_btn").bind("click", function () { $(this).siblings(".flyout_search_dropdown").slideToggle("fast") });
		$(".flyout_search_dropdown .btn_close").bind("click", function () { $(this).parent().slideToggle("fast") });
	}
	catch (error) {
		alert("InitializeSearchFlyouts: " + error.description, false, true);
	}
}



//****************************************************************
// InitializePopupLinks
// REQUIRED: jquery.js & jquery.DOMWindow.js
//****************************************************************
function InitializePopupLinks() {
	var popupLinks = $("a[class*='popup']");
	try {

		popupLinks.each(function () {
			//default popup size 
			var size = "650x500";
			// if popup size passed trough the class it will replace the default.
			// Correct format for passing size is popup_#x# where # could be any number
			// First number is width, second number is height
			var classWithSize = $(this).attr("class").match(/[popup_]+[0-9]+[x][0-9]+/);
			if (classWithSize) {
				size = classWithSize.toString().split("_")[1];
			}
			var width = size.split("x")[0];
			var height = size.split("x")[1];

			$(this).openDOMWindow({
				width: width,
				height: height,
				eventType: 'click',
				windowSource: 'iframe',
				windowPadding: '20px 0 0 0',
				windowColor: '#FFF',
				borderColor: '#CCC',
				borderSize: '4'
			});
			$(this).click(function () {
				$('#DOMWindow').prepend('<div style="position:absolute;top:0;left:0;width:100%;height:20px;background:#CCCCCC;"><a class="closePopup btn_close" style="position:absolute;top:0;right:0;color:black;" href="#"><span>x</span>close</a></div>');
				$('.closePopup').closeDOMWindow({ eventType: 'click' });
			});
		});

	}
	catch (error) {
		alert("InitializePopupLinks: " + error.description, false, true);
	}
}



//****************************************************************
// OpenSearchFlyout
//****************************************************************
function OpenSearchFlyout(element) {
	try {
		$(element).siblings(".flyout_search_dropdown").slideDown();
	}
	catch (error) {
		alert("OpenSearchFlyout: " + error.description, false, true);
	}
}



//****************************************************************
// CloseSearchFlyout
//****************************************************************
function CloseSearchFlyout() {
	try {
		$(".flyout_search_dropdown").slideUp();
	}
	catch (error) {
		alert("CloseSearchFlyout: " + error.description, false, true);
	}
}



//****************************************************************
// FixFloats
//****************************************************************
function FixFloats() {
	try {
		// fix IE6 and IE7 float clearing
		if ($.browser.msie && $.browser.version < 8) {
			$(".result_item").append("<br class=\"clearAll\" />");
			$("<br class=\"clearAll\" />").insertAfter("br.clearAll");
		}
	}
	catch (error) {
		alert("FixFloats: " + error.description, false, true);
	}
}



//****************************************************************
// ToggleCheckboxBlock
//****************************************************************
function ToggleCheckboxBlock(button, display, animate) {
	try {
		var divToToggle = $("#" + $(button).attr("id").replace("_btn", "_body"));
		var buttonText = $(button).text();

		if (display == "hide") {
			if (animate) {
				divToToggle.slideUp("fast", function () {
					$(button).text(buttonText.replace("hide", "show")).removeClass("btn_hide").addClass("btn_show");
				});
			}
			else {
				divToToggle.hide();
				$(button).text(buttonText.replace("hide", "show")).removeClass("btn_hide").addClass("btn_show");
			}
		}
		else if (display == "show") {
			if (animate) {
				divToToggle.slideDown("fast", function () {
					$(button).text(buttonText.replace("show", "hide")).removeClass("btn_show").addClass("btn_hide");
				});
			}
			else {
				divToToggle.show();
				$(button).text(buttonText.replace("show", "hide")).removeClass("btn_show").addClass("btn_hide");
			}
		}
		else {
			divToToggle.slideToggle("fast", function () {
				if (buttonText.match(/hide/)) {
					$(button).text(buttonText.replace("hide", "show")).removeClass("btn_hide").addClass("btn_show");
				}
				else {
					$(button).text(buttonText.replace("show", "hide")).removeClass("btn_show").addClass("btn_hide");
				}
			});
		}
	}
	catch (error) {
		alert("ToggleCheckboxBlock: " + error.description, false, true);
	}
}



//****************************************************************
// ToggleCheckboxesVisibility
//****************************************************************
function ToggleCheckboxesVisibility() {
	try {

		if ($("#practices_body input[type='checkbox']:checked").length > 0) {
			ToggleCheckboxBlock($("#practices_btn"), "show", false);
		} else {
			ToggleCheckboxBlock($("#practices_btn"), "hide", false);
		}

		if ($("#specialties_body input[type='checkbox']:checked").length > 0) {
			ToggleCheckboxBlock($("#specialties_btn"), "show", false);
		} else {
			ToggleCheckboxBlock($("#specialties_btn"), "hide", false);
		}

	}
	catch (error) {
		alert("ToggleCheckboxesVisibility: " + error.description, false, true);
	}
}


function InitializeTreatmentNav() {
	try {
		$(".nav_treatment ul a").each( function() {
			$(this).text( $(this).text().replace(" Program", "") );
			} );
		$(".nav_treatment a:first").attr("href","#").click( function(event){
			event.preventDefault();
			$(this).toggleClass("treatment_opened");
			
			// force style with z-index only when the dropdown is active to avoid conflict with the practices rollovers
			if ($.browser.msie && $.browser.version < 8) {
				$(".page_header").toggleClass("page_header_with_dropdown");
				}
				
			$(".nav_treatment ul").slideToggle(300);
			} );
	}
	catch( error ) {
		window.status = ( "InitializeTreatmentNav: " + error.description );
		}
}


