﻿$(document).ready(function()
{
    //var cachedImage = new Image();
    //cachedImage.src = "images/loading.gif";
    var objLoading = document.createElement("img");
    objLoading.setAttribute("src","/images/loading.gif");
    objLoading.setAttribute("alt","loading...");

    $("a[name^=appointment_]").click(function(event)
    {
        //the specific anchor
        var obj = $(this);
        
        //check if this anchor has been clicked before
        if(!obj.data("container"))
        {
            //extract the id from the name-attribute of the anchor
            var appointmentID = obj.attr("name").substring(12);            
            var objContainer = document.createElement("div");            
            obj.after(objContainer);            
            objContainer = $(objContainer);
            //save the jQuery object which references the container div as data of the specific anchor
            obj.data("container", objContainer);
            objContainer.hide().addClass("tt_infoContainer");
            //check if the same information have been requested by another anchor before
            if($.jCache.getItem(appointmentID))
            {
                //display the cached info
                objContainer.html($.jCache.getItem(appointmentID));
                objContainer.hide().slideDown("normal");
            }
            else
            {           
                /*
                $.get("ajax_functions.aspx",{"func":"getAppointmentInfo","id":appointmentID},function(data)
                {
                    //meanwhile a request for the same info could be finished and already cached
                    if(!$.jCache.getItem(appointmentID))
                        $.jCache.setItem(appointmentID,data);//cache the requested info for future requests
                        
                    objContainer.html(data).slideDown("slow",function()
                    {
                        $(objLoading).remove();
                    });
                });*/  
                obj.after(objLoading);          
                
                $.post("/ajax_functions.aspx",{"func":"getAppointmentInfo","id":appointmentID},function(data)
                {
                    //meanwhile a request for the same info could be finished and already cached
                    if(!$.jCache.getItem(appointmentID))
                        $.jCache.setItem(appointmentID,data);//cache the requested info for future requests     
                                           
                    objContainer.html(data).slideDown("slow",function()
                    {
                        $(objLoading).remove();
                    },"html");
                });
                /*$(objContainer).load("ajax_functions.aspx","func=getAppointmentInfo&id="+appointmentID,function()
                {
                    //meanwhile a request for the same info could be finished and already cached
                    if(!$.jCache.getItem(appointmentID))
                        $.jCache.setItem(appointmentID,$(objContainer).html());//cache the requested info for future requests       
                                  
                    objContainer.slideDown("slow",function()
                    {
                        $(objLoading).remove();
                    });
                });*/               
            }
        }
        else
        {
            //data is already there, just show it
            obj.data("container").slideToggle("normal");
        }
        
        return false;
    });
});
