
var searchOpen = false;

// function showExtendedInfo(deceased_id, org_id) {
//     //Displays extended info panel for deceased_id in organizations with org_id
//     closeAllControls();
//     // stopAnimation();
//     $("#extendedInfo").load(
//        'ajax/extended_info.php',
//        {org_id: org_id, deceased_id: deceased_id, page_name: ''}
//     );
//     $("#extendedInfo").fadeIn();
//     // setOverlayTimeout();
//     // alert("Coming Soon (" + deceased_id + ", "  + org_id + ")");
// }

function showExtendedInfo(deceased_id, org_id) {
// Displays extended info panel for deceased_id in organizations with org_id
    closeAllControls();
    // stopAnimation();
    $.getJSON(
        'ajax/extended_info.php',
        {org_id: org_id, deceased_id: deceased_id, page_name: 0, memorial_id: 0, page_num: 0},
        function(jsondata) {
            $('#extendedInfo').html(jsondata.html);
            // Create list of memorials
            for(j = 0; j < jsondata.memorial_ids.length; j++) {
                // Within each memorial, create a list of links to the bio, 
                // the different memorial pages, and its images
                bio = "<li><a class='bioLink' id='b" + jsondata.memorial_ids[j] + "'>Biography" + "</a></li>";
                mem = "";
                for(k = 0; k < jsondata.memorial_page_counts[j]; k++) {
                   // Create a link for each memorial page
                   mem += "<li><a class='memorialLink' id='m" + jsondata.memorial_ids[j] + "_" + (k+1) + "'>Page " + (k+1) + "</a></li>"; 
                }
                img = "<li><a class='imagesLink' id='i" + jsondata.memorial_ids[j] + "'>Images" + "</a></li>"
                li = "<li id='mh" + jsondata.memorial_ids[j] + "'><a>Memorial " + (j+1) + "</a><ul>" + bio + mem + img + "</ul></li>"
                $("#memorialLinks").append(li);
            }
            
            $("#memorialLinks li a").click(function() {
                $(this).siblings("ul").slideToggle();
            })
            
            $("#memorialLinks li a.memorialLink").click(function() {
                // If a memorial link is clicked, parse the memorial ID and the page #
                // from the links id attribute
                $(this).attr('id').match(/m([0-9]+)_([0-9]+)/);
                changeContent('memorial', deceased_id, org_id, RegExp.$1, RegExp.$2);
            });

            $("#memorialLinks li a.bioLink").click(function() {
                changeContent('bio', deceased_id, org_id, $(this).attr('id').substring(1), 0);
            });
            
            $("#memorialLinks li a.imagesLink").click(function() {
                changeContent('images', deceased_id, org_id, $(this).attr('id').substring(1), 0);
            });
            
            $("#memorialLinks li ul").hide();
        }
    );
    $("memorialLinks li ul").hide();
    $("#extendedInfo").fadeIn();

}

function closeAllControls() {
// Closes all controls (plus extended info), restarts animation, and
// clears the overlay timeout.
    $(".controls").fadeOut();
    searchOpen = false;
    // sortOpen = false;
    // jumpOpen = false;
    // aboutUsOpen = false;
    // $("#navigation ul li a").removeClass("activated");
    $("#extendedInfo").fadeOut();
    // startAnimation();
    // clearTimeout(overlayTimeout);
}

function changeContent(pageName, deceased_id, org_id, memorial_id, page_num)
// Changes content (between main, bio, memorial, etc.) in the 
// extended info box
{
    // Change which link in the list is highlighted
    $("#links li a").removeClass('active');
    if (pageName == 'info') {
        $('#infoLink').addClass('active'); 
    }
    else if (pageName == 'relatives') {
        $('#relativesLink').addClass('active');
    }
    else if (pageName == 'bio') {
        $('#memorialLinks #b' + memorial_id).addClass('active');
    }
    else if (pageName == 'memorial') {
        $('#memorialLinks #m' + memorial_id + '_' + page_num).addClass('active'); 
    }
    else if (pageName == 'images') {
        $('#memorialLinks #i' + memorial_id).addClass('active'); 
    }


    // Load the correct page content
    $.getJSON(
       'ajax/extended_info.php',
       {org_id: org_id, deceased_id: deceased_id, page_name: pageName, memorial_id: memorial_id, page_num: page_num},
       function(data, status) {
           $('#extendedInfo #content').html(data.html);           
       }
    );
    
    $('#extendedInfo #content')[0].focus();
    
    return false;
}

$(document).ready(function(){

    $("#extendedInfo").hide();
    $(".controls").hide();
        
    $('#sponsorScroller').cycle({ 
        fx:      'scrollRight', 
        timeout:  permanentSponsorDelay * 1000
    });
    $('#generalSponsors .items').cycle({ 
        fx:      'fade', 
        timeout:  generalSponsorDelay * 1000
    });
    $('#orgSponsors .items').cycle({ 
        fx:      'fade', 
        timeout:  orgSponsorDelay * 1000
    });
    $('#currentPlaqueWrapper .items').cycle({ 
        fx:      'fade', 
        timeout:  currentPlaqueDelay * 1000
    });
    $('#notice1 .items').cycle({ 
        fx:      'fade', 
        timeout:  noticeDelay * 1000
    });
    $('#notice2 .items').cycle({ 
        fx:      'fade', 
        timeout:  noticeDelay * 1000
    })
    start_cycle();
    
    $("#sort_by").change(function() {
        data_url = 'include/data2000.php?org_id=' + org_id + '&board=' + board_num + "&sort=" + $(this).val();
        restart();
        return false;
    });
    
    $(".closeButton").click(function() {
        closeAllControls();
    });

    $("#searchButton").click(function() {
        if(searchOpen) {
            $("#search").fadeOut();
            searchOpen = false;
            // startAnimation();
            $(this).removeClass("activated");
        }
        else {
            closeAllControls();
            $("#search").fadeIn();
            searchOpen = true;
            $("#firstname").focus();
            // stopAnimation();
            // $(this).addClass("activated");
            // setOverlayTimeout();
        }
        return false;
    });
    
    // Search
    $("#searchResults").hide();
    $("#searchSubmit").click(function() {
        var fname = $('#firstname').val();
        var lname = $('#lastname').val();
        $("#searchResults ul").load(
            'ajax/deceased_search.php',
            {org_id: org_id, first_name: fname, last_name: lname}
        );
        $("#searchResults").show();
        return false;
    });
    
    // Clear search inputs
    $("#searchClear").click(function() {
        $('#firstname').val(""); 
        $('#lastname').val(""); 
        return false;
    });
    
    
    
  
});
