/******
* Author: Kris Watson, Lumous Media, LLC
* Web: http://www.lumousmedia.com
* Created: August 2008
* Requires: JQuery v1.2.6 or greater, LiveQuery v1.0.2 (Copyright to their respective authors)
*******/
$(document).ready(function() {  
  //set initial background image
  $(".back_pic").css("background", "url(images/back/home.gif) no-repeat");
  
  //grab home page
  $.ajax({
    url: "index.php/page_right/home",
    success: function(html) {
      $(".right").empty()
      			 .hide()
      			 .append(html);       
      $(".right").fadeIn(750);
    }
  });  
  //initialize menu
  init();
  //initialize home links
  homelinks();  
  
  //preload images
  $.preloadImages("./images/back/about.gif", "./images/back/contactus.gif", "./images/back/joinourpract.gif", "./images/back/ourphys.gif");
});
////
//Initialize buttons
function init() {  
  //Home
  $(".logo").livequery('click', function() {  
    nextpage('home','home');
    updatemenu('home');
        
  });
  //About Us
  $(".nav_about").livequery('click', function() {    
    nextpage('about','about');
    updatemenu('nav_about');
    
  });
  //Our Physicians
  $(".nav_ourphysicians").livequery('click', function() {    
    nextpage('ourphys','ourphysicians');
    updatemenu('nav_ourphysicians');
    initpics();
  });
  //Join Our Practice
  $(".nav_joinourpractice").livequery('click', function() {    
    nextpage('joinourpract','joinourpractice');
    updatemenu('nav_joinourpractice');
    return false;
  });
  //Contact Us
  $(".nav_contactus").livequery('click', function() {    
    nextpage('contactus','contactus');
    updatemenu('nav_contactus');
    contact();
  });   
} 
////
//Load New Page
function nextpage(pic,page) {
  //change background image
  var image = "url(images/back/" + pic + ".gif) no-repeat";
  var npage = "includes/" + page + ".html";
  $(".back_pic").css("background", image);
  
  //create url
  var theURL = "index.php/page/" + page;
  
  //update left side with new text
  $.ajax({
    url: theURL,
    success: function(html) {
      $(".grey_left_back").empty()
                          .append(html);
    }
  });
  
  //create right side URL
  var theRURL = "index.php/page_right/" + page;
  
  //Update Right Side with HTML
  $.ajax({
    url: theRURL,
    success: function(html) {
      $(".right").fadeOut(300, function() {
						$(".right").empty()
										   .append(html)
					             .fadeIn(1000, function() {
					             	
					             	//Google maps for Contact page
												if (page == "contactus") {
													if (GBrowserIsCompatible()) {
														var map = new GMap2(document.getElementById("map"));
														map.setCenter(new GLatLng(45.526697,-122.613215), 15);
														var address = "545 NE 47th Ave, Portland OR 97213";
														map.addControl(new GSmallMapControl());
														var geocoder = null;
														geocoder = new GClientGeocoder();
														geocoder.getLatLng(address, function(point) {
															if (!point) {
																alert(address + " was not found!");
															} else {
																map.setCenter(point, 15);
																var marker = new GMarker(point);
																map.addOverlay(marker);
															}
														});	
													}
												} else {
													//Unload google Maps
													GUnload();
												} //End Google Map    
					             
					             });
			});
    }
  });
} 
////
//Update Menu
function updatemenu(selected) {
	
	//reset to un_selected	
	$(".nav_about_sel").attr("class","nav_about");
	$(".nav_ourphysicians_sel").attr("class", "nav_ourphysicians");
	$(".nav_joinourpractice_sel").attr("class", "nav_joinourpractice");
	$(".nav_contactus_sel").attr("class", "nav_contactus");
	
	//set selected, only if not home
	if (selected != "home") {
		var setClass = "." + selected;
		var cssClass = selected + "_sel";
		$(setClass).attr("class", cssClass);
	}
}
////
//enable pics as links to get doc profiles
function initpics() {

	$(".ourphy_docs").livequery('click', function() {
			var docname = "#" + $(this).attr('id');
			//change background
			var backurl = "url(images/back/" + $(this).attr('id') + ".gif) no-repeat";
			$(".back_pic").css("background", backurl);
			//get doc page
			var getdocurl = "index.php/doc/" + $(this).attr('id');
			$.ajax({
				url: getdocurl,
				success: function(html) {
					$(".right").fadeOut(300, function() {
						$(".right").empty()
										   .append(html)
					             .fadeIn(1000);
					  $(docname).attr("class", "doc_links_sel");
						initdoclinks();
					});									 
				}
			});
	});

}
////
//enable doc links
function initdoclinks() {
	$(".doc_links").livequery('click', function() {
			var docname = "#" + $(this).attr('id');
			//change background
			var backurl = "url(images/back/" + $(this).attr('id') + ".gif) no-repeat";
			$(".back_pic").css("background", backurl);
			//get doc page
			var getdocurl = "index.php/doc/" + $(this).attr('id');
			$.ajax({
				url: getdocurl,
				success: function(html) {
					$(".right").fadeOut(300, function() {
						$(".right").empty()
										   .append(html)
					             .fadeIn(1000);
					  $(docname).attr("class", "doc_links_sel");
					});
					
				}
			});
	});
	//Back to Physicians
	$(".doc_back").livequery('click', function() {
		nextpage('ourphys','ourphysicians');
    updatemenu('nav_ourphysicians');
    initpics();	
	});
}
////
//Create Home Logo Links
function homelinks() {
	//logo links
	$(".home_link").livequery('click', function() {
		var url = null;
		var name = $(this).attr('id');
		var ext = null;
		var pre = "http://www.";		
		if (name == "providence") {
			ext = ".org";
		} else {
			ext = ".com";
		}		
		url = pre + name + ext;		
		window.open(url);		
	});
}
////
//Contact Form
function contact() {
  
  $("#contact_form_sendbtn").livequery('click', function() {
    $("#contact_form_btnbar").hide();
    var theLoader = "<img src='./images/circleLoad.gif' width='16' height='16'>";		
		$("#loader").empty()
								.append(theLoader);
								
		var valid = false;
		var msg = null;
		var error = null;
		
		//check form fields
		if ($("#name").val() == "") {
			msg = "Please provide your name.";
		} else if ($("#email").val() == "") {
			msg = "Please provide your email address.";
		} else if ($("#subject").val() == "") {
			msg = "Please provide a subject.";
		} else {
			valid = true;
		}
		
		//if true, send, if false, alert user
		if (valid) {
			
			var FormData = $("form#Contact").serialize();
			$.ajax({
				type: "POST",
				url: "index.php/home/contact",
				data: FormData,
				success: function(html) {
					if (html == "email") {
						//email invalid
						error = "Invalid Email!";
						$("#msg").empty()
										 .append(error);
						$("#loader").empty();
						$("#contact_form_btnbar").show();
						//get new captcha
						$("#contact_form_code_input").val("");
						$("#contact_form_captcha_input").load('index.php/home/newimg');
						$("#email").css("border-color", "red");
						$("#contact_form_code_input").css("border-color", "black");
					} else if (html == "code") {
						//captcha code invalid
						error = "Invalid Validation Code!";
						$("#msg").empty()
										 .append(error);
						$("#loader").empty();
						$("#contact_form_btnbar").show();
						//get new captcha
						$("#contact_form_code_input").val("");
						$("#contact_form_captcha_input").load('index.php/home/newimg');
						$("#email").css("border-color", "black");
						$("#contact_form_code_input").css("border-color", "red");
					} else {
						//success!
						$(".contact_content_right").empty();
						$(".contact_content_right").append(html);
					}
				}
			});
		
		} else {
		
			$("#msg").empty()
							 .append(msg);
			$("#loader").empty();
			$("#contact_form_btnbar").show();			
		}				    
  });  
  //stop form submission,only permit AJAX form submit
  $("#Contact").livequery('submit', function() {
  	return false;
  });
}