$(document).ready(function(){

	// fix PNGs for IE
	$('#logo_bubble').supersleight();	
	
	// handle 'element bubbles'
	if($.browser.name == "msie" && $.browser.versionNumber > 6) {var $fixmode = true;}
	else { var $fixmode = false; }
	
	function showBubble() {
		// reset
		$("#logo_bubble div").hide();
		$("#logo_bubble").stop({"clearQueue": true})
		if($("#logo_bubble").css("top") != "85px") {
			$("#logo_bubble").css({ top: "95px" });
		}
		// show requested
		var $id = $(this).parent().attr('id');
		$("#bubble_"+$id).show();
		if($fixmode) { $("#logo_bubble").show().animate({ top: "95px" }, 250 ); }
		else { $("#logo_bubble").animate({ top: "95px", opacity: 1 }, 250 ); }
	}
	
	function hideBubble() {
		var $id = $(this).parent().attr('id');
		if($fixmode) { $("#logo_bubble").animate({ top: "115px" }, 500, function() { $("#logo_bubble").hide().css({ top: "85px" }); }); }
		else { $("#logo_bubble").animate({ top: "115px", opacity: 0 }, 500, function() { $("#logo_bubble").css({ top: "85px" }); }); }
	}
	$("#logo_elements a").hover(showBubble, hideBubble);

	// cycle the sidebar images (uses jQuery.cycle plugin)
	$('#side_bar_images').cycle({ fx: 'fade', timeout: 8000, speed: 2000, random: 1 });

	// cycle the ticker images (uses jQuery.cycle plugin) // fix the container to work with scrollLeft!
	$('#ticker_content').cycle({ fx: 'scrollLeft', timeout: 4000, speed: 1000, random: 1 });
 	
	// handle the directions form
	$('#directions_show').click(function () {
  		$("#address").attr({ value: "" });
		$('#left_overlay').animate({ top: "445px", height: "224px" }, 500);
		$('#directions_show').css({'cursor' : 'default'});
		return false;
    }); 
	function closeLeftOverlay() {
		$('#left_overlay').animate({ top: "525px", height: "144px" }, 500);
		$('#directions_show').css({'cursor' : 'pointer'});
	}
	$('#directions_cancel').click(closeLeftOverlay);
	$('#directions_submit').click(closeLeftOverlay);

	// NEEDS TO BE PERFORMED AFTER EACH AJAX PAGELOAD
	function activateContent() {

		// handle the text toggle
		$('a.read_more').click(function(){
			var list = $(this).attr('rel');
			switch(list) {
				case "#pricelist_massage": $(list).slideToggle(800); break;    
				case "#pricelist_aesthetics": $(list).slideToggle(800); break;
				case "#pricelist_chiropractic": $(list).slideToggle(600); break;
				case "#pricelist_classes": $(list).slideToggle(300); break;
				case "": break; // no forward relation set
				default:
					$(list+"_show").toggle();
					$(list).slideToggle(800);
					break;
			}
			return false; // no hash-jumping
		});
		
	  	// evaluate and submit the mailing-list form (uses jQuery.validate and jQuery.form plugins)
	  	$("#email_input").attr({value: "email address", style: "color:#b48b68"}); // start state
	  	
	  	$("#email_input").focus( function() {
	  		if($("#email_input").attr("value") == "email address") {
	  			$("#email_input").attr({value: "", style: "color:#603813"});
	  		}
	  	});
	  	$("#email_input").blur( function() {
	  		if($("#email_input").attr("value") == "") {
	  			$("#email_input").attr({value: "email address", style: "color:#b48b68"});
	  		}
	  	});
	
	  	$("#email_ok_button").click(function(){					   				   
			// validate form and submit
			$("#mailing_list").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() {
						$("#email_ok_button").replaceWith('<img id="email_ok_button" src="gfx/btn_spinner.gif" alt="">');
						return true;
					};
					function showResponse() {
						$("#email_replace_me").slideToggle(500, function () {
							$("#email_replace_me").replaceWith('<div id="email_replacement" style="display:none"><p>Thank you for subscribing to our mailing-list.</p><p><em>We\'ll be in touch! ;-)</em></p><div>');
							$("#email_replacement").slideToggle(300);
						});
					};
					return false;
				},
				errorPlacement: function(error, element) {
					error.appendTo(element.parent().parent());
				},
				rules: { email: { required: true, email: true } },
				messages: { email: { required: "", email: "" } }
			});
		});
		// end of evaluate and submit the mailing-list form
	
	  	// 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() {
						$("#contact_submit_btn").slideToggle(200, function () {
							$("#contact_form").append('<label id="thanks" style="display:none">Thank you so much 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
		
	} // function activateContent
	
	// change the page content
	function showPage($id) {
		$('#main_container').fadeOut(300, function() {
			//$(this).before('<img id="spinner_pageload" src=gfx/btn_spinner_lrg.gif>').fadeIn(200);
			var newContentURL = './' + $id + '.php';
			$(this).load(newContentURL, function(responseText, textStatus, XMLHttpRequest) {
				//$('#spinner_pageload').fadeOut(200).remove();
				$(this).fadeIn(300);
				// console.log('PageLoad: '+textStatus);
				activateContentNav();
				activateContent();
				pageTracker._trackPageview(newContentURL); // call to Google Analytics
			});
		});
		
	}
	// update the nav bar
	function updateNav($id) {
        var $nav = $('#nav_bar').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 bar
	$('#nav_bar').find('a[href*=?]').click(function() {
		var $link = $(this).attr('href');
		trigger($link);
	    return false;
	});
	// activate any other hrefs in the content
	function activateContentNav() {
		$('#main_container a[href*=?]').click(function() {
			var $link = $(this).attr('href');
			trigger($link);
		    return false;
		});
	}
	// 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); 
    }
	activateContentNav();
	activateContent();
});
