$(document).ready(function(){

	// fix PNGs for IE
	$('#page_shadow').supersleight();	
	$('#page_end').supersleight();	

/*	
	// cycle the sidebar images (uses jQuery.cycle plugin)
	$('#cycle1').cycle({ timeout: 5000, delay: -2500, speed: 2000, pause: 1});
*/
 	
	// handle the directions form
	$('#directions_show').click(function () {
  		$("#address").attr({ value: "" });
		$('#footer').animate({ height: "185px" }, 300);
		$('#directions_show').css({'cursor' : 'default'});
		return false;
    }); 
	function closeDirections() {
		$('#footer').animate({ height: "125px" }, 400);
		$('#directions_show').css({'cursor' : 'pointer'});
	}
	$('#directions_cancel').click(function () {
		closeDirections();
		return false;
	});
	$('#directions_submit').click(closeDirections);
		
	// NEEDS TO BE PERFORMED AFTER EACH AJAX PAGELOAD
	function activateContent($id) {
		switch($id) {
			//case "home": activateVideo(); break;
			case "patients": activateFancybox(); break;
			case "contact": activateForm(); break;
		}
	}
	function activateVideo() {
    	swfobject.registerObject("youtube_main", "9.0.0");
	}
	function activateFancybox() {
		$('.gallery_detail').fancybox({
			'overlayShow': true,
			'overlayOpacity': .8,
			'frameWidth': 757,
			'frameHeight': 567, //562,
			'zoomOpacity' : true,
			'hideOnContentClick': false,
			'centerOnScroll': false // better for small screens...
		});
	}

	function activateForm() {
	  	// evaluate and submit the contact form (uses jQuery.validate and jQuery.form plugins)
	  	$("#contact_submit_btn").click(function() {					   				   
			// validate form and submit
			$("#contact_form").validate({
				onkeyup: false,
				submitHandler: function(form) {
					var submitOptions = { 
				        beforeSubmit:	showRequest,	// pre-submit callback 
				        success:		showResponse,	// post-submit callback 
				        clearForm:		true			// clear all form fields after successful submit 
				    }; 
					$(form).ajaxSubmit(submitOptions);
					function showRequest() {
						// $("#contact_submit_btn").replaceWith('<img id="contact_submit_btn" src="gfx/btn_sending.png" alt="">');
						return true;
					};
					function showResponse() {
						$("#form_replace_me").slideToggle(400, function () {
							$("#contact_form").append('<label id="thanks" style="display:none">Thank you for your interest.<br />We\'ll contact you as soon as possible.</label>');
							$("#thanks").slideToggle(300);
						});
					};
					return false;
				},
				errorPlacement: function(error, element) {
					error.appendTo(element.parent("input").prev("label"));
				},
				rules: {
					name: { required: true, minlength: 2 },
					phone: { required: true, minlength: 10 },
					email: { required: true, email: true }
				},
				messages: {
					name: { required: "", minlength: "" },
					phone: { required: "", minlength: "" },
					email: { required: "", email: "" }
				}
			});
		});
	} // end of evaluate and submit the contact form

	// change the page content
	function showPage($id) {
		$('#main_container').slideUp(500, function() {
			var newContentURL = '/' + $id + '.php';
			$.get(newContentURL, function(data) {
				//remove ids: footer, legal, page_end
				$('#main_container .page').replaceWith(data);
				$('#main_container').slideDown(500);
				// console.log('PageLoad: '+textStatus);
				activateNav('#main_content');
				activateContent($id);
				pageTracker._trackPageview(newContentURL); // call to Google Analytics
			});
		});
	}
	// update the nav bar
	function updateNav($id) {
        var $nav = $('#main_nav').find('a[href$="' + $id + '"]').get(0); // go find the navigation link that has this target
        selectNav.call($nav);
	}
	// handle nav selection
	function selectNav() {
		$(this)
			.parents('ul:first')
				.find('a')
					.removeClass('nav_active')
				.end()
			.end()
			.addClass('nav_active');
	}
    
    // hit it!
    function trigger($id) {
		var $id = $id.split('?')[1];
    	showPage($id); 
    	updateNav($id); 
    }
	// activate nav elements
	function activateNav($nav_element) {
		$($nav_element + ' a[href*=?]').click(function() {
			var $link = $(this).attr('href');
			trigger($link);
		    return false;
		});
	}
	// for main page
	activateNav('#main_nav');
	activateNav('#bottom_nav');
	activateNav('#main_content');
	// check if specific nav is being requested and update the nav (content is done by PHP)
    if (window.location.search) {
		var $id = window.location.search.split('?')[1];
    	updateNav($id); 
    } else {
		var $id = "home";
    	updateNav($id); 
    }
    // for main page
	activateNav('#main_nav');
	activateNav('#bottom_nav');
	// for content
	activateNav('#main_content');
	activateContent();
});
