// DOM ready
$(document).ready(function(e){
	
	// Assign css width/height to specially classed divs
	$("div[class*='jq-width-']").each(function(){
		var dim_data = $(this).attr('class').match(/jq\-width\-(\d+)/);
		$(this).css('width',dim_data[1]+'px');
	});

	// Assign css width/height to specially classed divs
	$("div[class*='jq-height-']").each(function(){
		var dim_data = $(this).attr('class').match(/jq\-height\-(\d+)/);
		$(this).css('height',dim_data[1]+'px');
	});
	
	// Set up form validation
	$('.req').siblings('label').append(" <span style='color:red'>*</span>");
	$('.form-validate').submit(validate_form);
	
	// Links that open in new window
	$("a.new-win").click(function(e){
		window.open($(this).attr('href'));
		return false;
	});
	
	// Services jump menu
	$("#services-jump-menu").change(function(e){
		$("#services-jump-menu option:selected").each(function(e){
			if($(this).val() != "NULL") {
				if($(this).hasClass("new-win"))
				{
					window.open($(this).val());
				}
				else
				{
					$(location).attr('href',$(this).val());
				}
			}
		});
		return false;
	});
	
	// Build select-by-name jump menu
	$("#find-a-physician #content h4").each(function(){
		var physician_id = $(this).attr('id');
		var physician_name = $(this).html();
		
		$("#select-by-name").append("<option value='" + physician_id + "'>" + physician_name + "</option>\n");
	});
		
	// Build select-by-specialty jump menu
	$("#find-a-physician #content h3").each(function(){
		var specialty_id = $(this).attr("id");
		var specialty_name = $(this).html();
		
		$("#select-by-specialty").append("<option value='" + specialty_id + "'>" + specialty_name + "</option>\n");
		
	});
	
	// Click event for select-by-name and select-by-specialty jump menu
	$("#select-by-name, #select-by-specialty").change(function(e){
		if($(this).val() != "NULL") {
			$(location).attr('href',"#" + $(this).val());
			// window.open($(this).val());
		}
	});
	
	// Place picture beside physicians who have them
	// h4 id attribute has to match filename of picture.  e.g. fap-id_of_h4_tag.jpg
	$('.has-picture').each(function(i){
		var physician_id = $(this).children('h4').attr('id');
		$(this).addClass('clearing');
		$(this).children().wrapAll("<div style='float:left;width:200px;'></div>");
		$(this).append("<div style='float:right; width:514px'><img src='i/fap-" + physician_id + ".jpg' alt='' /></div>");
	});
	

	// Back to top menu for find a physician page
	$('#find-a-physician').each(function(i){
		var top_menu = "#back-to-top-menu";
		var menuYloc = parseInt($(top_menu).css("top").substring(0,$(top_menu).css("top").indexOf("px")));
		
		$(window).scroll(function () {
			offset = menuYloc+$(document).scrollTop()+"px";
			$(top_menu).animate({top:offset},{duration:500,queue:false});
		});
	});
	
	// News events browser
	$('.news-items').each(function(){
		$('#content h3').each(function(i){
			$(this).attr('id','news-item-' + i);
			$("div.news-items").append("<p><a href='#news-item-" + i + "'>" + $(this).html() + "</a></p>");
		});
	});
		
});

// onload
$(window).load(function(e){
	$("#left-inner,#right-inner").equalHeights();
});

function validate_form(e)
{
	var message = "";
	$('.req').each(function(){
		if($(this).val() == "") { message += "Please enter a " + $(this).attr('name') + "\n";}
	});
	if(message != '')
	{
		alert(message);
		return false;
	}
	return true;
}