$(document).ready(function(){
	
	// fix "external" links to open in new window									 
	$('a[rel="external"]').click(function(){
		window.open(this.href);
		return false;
	}).attr('title', 'Opens in New Window or Tab');
	
	// show slideshow on homepage
	$("#slideshow").cycle({
		speed:2000,
		timeout:5000
	});
	
	// control search box in header
	var active_color = '#000';
	var inactive_color = '#666';
	$("input.default-value").css("color", inactive_color);
	var default_values = new Array();
	$("input.default-value").focus(function() {
		if (!default_values[this.id]) {
		  default_values[this.id] = this.value;
		}
		if (this.value == default_values[this.id]) {
		  this.value = '';
		  this.style.color = active_color;
		}
		$(this).blur(function() {
		  if (this.value == '') {
			this.style.color = inactive_color;
			this.value = default_values[this.id];
		  }
		});
	});
	
	// show 'forgot password' box 
	$('a.hider').click(function () {
		$('div.hider').slideToggle("fast");
		return false;
	});
	
	$("#contact-footer-form").validate();
	$("#contact-page-form").validate();
	$("#register-form").validate();
	$("#login-form").validate();
	
	// show Doctor Search box
	if($('#searchbox').length) {
		$('#searchbox').load('/ws1.php', function() {
            $("#searchbox-loading").hide("fast");
		    $("#searchbox").show("fast");
		    
		    show_physician_search_box();
		    
		});
		$("#searchbox a.reload").click(function(){
			$('#searchbox').load('/ws1.php',show_physician_search_box);
			return false;
		});
	} // end if $('#searchbox').length
	
	
	if($(".doctor-plan-lookup-link").length) {
		$('.doctor-plan-lookup-link').colorbox({width:'640px',height:'400px'});
	}
	
	
});

// show physician search box
function show_physician_search_box() {
	// AJAX bind the form that /ws1.php returns
	$('#searchform').bind('submit', function() {
		$("#ajax-message").show("fast");
		$("#searchresults").hide("fast");
		$(this).ajaxSubmit({
			target: '#searchresults',
			success: function(){
				$("#ajax-message").hide("fast");
				$("#searchresults").show("fast");
				
				// sort the table from /ws2.php
				$("table.tablesorter").tablesorter();
			}
		});
		return false;
	});
}


// used in cleaning forms
	function stripEOLN(formElement) {
	   for(i=0; i<formElement.elements.length; i++) {
		  formElement.elements[i].value = formElement.elements[i].value.replace(/[\r\n]/g," ");
	   }
	}
