$(document).ready( function()
{
    $('.veneer_library_categories a').live('click', function()
    {
        //return false;
    });
    
    $('a.veneer_item').live('click', function()
    {
        request_page($(this).attr('href'), false);
        return false;
    });
    
    $('.slip_match_link').live('click', function()
    {
        $('.slip_match').show();
        $('.description_link').show();
        $('.book_match').hide();
        $('.veneer_item_content').hide();
        return false;
    });
    
    $('.book_match_link').live('click', function()
    {
        $('.book_match').show();
        $('.description_link').show();
        $('.slip_match').hide();
        $('.veneer_item_content').hide();
        return false;
    });
    
    $('.description_link').live('click', function()
    {
        $('.veneer_item_content').show();
        $('.description_link').hide();
        $('.book_match').hide();
        $('.slip_match').hide();
        return false;
    });
});

//--------------------------------------------------------------------------
//  Request a page from the server
//--------------------------------------------------------------------------
function request_page(page_url, return_content)
{
    var url_string = 'ajax=1';
    //var url_string = '';
    
    if((page_url.split('&').length - 1) > 0)
    {
        url_string += page_url.substring(page_url.indexOf('&', page_url), page_url.length);
    }
    
    if((page_url.split('&').length - 1) != 0)
    {
        page_url = page_url.substring(0, page_url.indexOf('&', page_url));
    }
    
    return ajax_it(page_url, url_string, return_content);
}

//--------------------------------------------------------------------------
//  Make AJAX Request
//--------------------------------------------------------------------------
function ajax_it(page_url, url_string, return_content)
{
    var response = '';
    
    $.ajax({
        type: "POST",
        async: false,
        url: page_url,
        dataType: "text",
        data: url_string,
        beforeSend: function() {
            if(return_content === false) {
                //$('.ajx_page').fadeTo(200,.05);
            }
            //create_loading();
        },
        success: function(html) {
            if(html.length > 0)
            {
                response = jQuery.trim(html);
                
                if(return_content === false) {
                    window.setTimeout( function() {
                        // Insert retrieved content
                        //$('.ajx_page').html(response).fadeTo(200,1);
                        $('.ajx_page').html(response).show();
                    }, 200);
                }
            }
            
            return false;
        },
        complete: function (XMLHttpRequest, textStatus) {
            //destroy_loading();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            window.setTimeout( function() {
                $('body').before('<div>There was an error with you request.  Please contact customer service. '+XMLHttpRequest.responseText+'Status Text: <b>' + XMLHttpRequest.statusText + '</b> Error Thrown: <b>' + errorThrown + '</b></div>');
            }, 200);
        }
    });
    
    return response;
}
