// run the code below when page load
$(document).ready(function(){
    
    $("#nav img").mouseover(function(){
        $(this).addClass("mouse_over");
    }).mouseout(function(){
        $(this).removeClass("mouse_over");
    });

    $("#top img").mouseover(function(){
        $(this).addClass("mouse_over");
    }).mouseout(function(){
        $(this).removeClass("mouse_over");
    });

    $("#top img").click(function (){
        loadPage(this.id);
    });
    
    $("#nav img").click(function (){
        //alert(this.id);
        loadPage(this.id);
    });

    // button control center
    $("#body button").live("click", function(){
        //alert(this.id);
        // comments send/reset button
        if (this.id == "comment_reset"){
            $("#fname").val('');
            $("#lname").val('');
            $("#email").val('');
            $("#phone").val('');
            $("#comments").val('');
        }
        if (this.id == "comment_submit"){
          submitComments( $("#fname").val(), $("#lname").val(), $("#email").val(), $("#phone").val(), $("#comments").val());
        }
        if (this.id == "new_comments"){
            loadPage("storynew");
            }
        if (this.id == "comments_active"){
            activeComments($("#sid").val(), $("#comments").val());
            loadPage("story");
            }
        if (this.id == "deactivate_comments"){
            deactivateComments($("#sid").val());
            //loadPage("story");
            }

    });
    
    // policy agree check box
    $("#body input[id=policyagreement]").live("click", function(){
        $("#agreeformdownload").toggle();
    });
    
    // read policy link on new patient page
    $("#body p[id=readpolicy]").live("click", function(){
        loadPage("policy");
    });
    
    // online book on home page
    $("#body p[id=onlinebook]").live("click", function(){
        loadPage("appointment");
    });
});


//----------------------------------------------------------------------------
  // load page 
  function loadPage(pagename){
    $.ajax({
         type: "POST",
         url: "./" + pagename + ".php",
         success: function(html){
           $("#body").html(html);
         },
         error: function(){
            loadUnderConstruction(); 
         }
        });
  }

  // load under contruction
  function loadUnderConstruction(){
    $.ajax({
         type: "POST",
         url: "./under_construction.php",
         success: function(html){
           $("#body").html(html);
         }
        });
  }

  //store comments in database and send email for review/approve
  function submitComments(fname, lname, email, phone, comments) {
    $.ajax({
         type: "POST",
         url: "./utils/utils.php",
         data: "action=new_comments&fname=" + fname + "&lname=" + lname + "&email=" + email + "&phone=" + phone + "&comments=" + comments,
         success: function(html){
           $("#body").html(html);
         }
        });
  }

  //active comments in database
  function activeComments(sid, comments) {
    $.ajax({
         type: "POST",
         url: "./utils/utils.php",
         data: "action=active_comments&sid=" + sid + "&comments=" + comments,
         success: function(html){
           $("#body").html(html);
         }
        });
  }

  //delete comments in database
  function deactivateComments(sid) {
    $.ajax({
         type: "POST",
         url: "./utils/utils.php",
         data: "action=deactivate_comments&sid=" + sid,
         success: function(html){
           $("#body").html(html);
         }
        });
  }


