//Initialize variable to cache AJAx request results
var ajaxCache = {};

//Retrives content via Ajax or pulls from cache
function getAjaxContent(url, callback)
{
    if (!ajaxCache[url])
    {
        $.ajax({
            'url': url,
            type: 'POST',
            dataType: 'json',
            data: {
                'ajax': true
            },
            success: function(data) {
                ajaxCache[url] = data;
                if (callback) {callback();}
            }
        });
        return false;
    } else {
        if (callback) {callback();}
        return ajaxCache[url];
    }
}

//Updates the banner area with the appropriate featured project
function showFeaturedProject(slug)
{
    if (slug)
    {
        url = '/featured-project-'+slug;
    } else {
        url = '/';
    }
    if (pm_feature_ticker)
    {
        clearInterval(pm_feature_ticker);
    }
    getAjaxContent(url, function(){
        json = getAjaxContent(url);
        $('#pm_breadcrumbs').html(json['crumbs']);
        $('#pm_banner').html(json['content']);
        if (json['timeout']) { pm_feature_timer_interval = json['timeout'] } else { pm_feature_timer_interval = 15000; }
        pm_feature_start_ticker()
        pm_feature_current = slug;
        $('.feature_tab').removeClass('active');
        $('#pm_fttb_'+slug).addClass('active');
        //_gaq.push(['_trackPageview', url]);
    });
}

//Update the two column layout appropriately
function showTwoColumn(url)
{
    getAjaxContent(url, function(){
        json = getAjaxContent(url);
        $('#pm_breadcrumbs').html(json['crumbs']);
        $('#pm_content').html(json['content']);
        $('#pm_sidebar').html(json['sidebar']);
        _gaq.push(['_trackPageview', url]);
    });
}

//Submit the contact form via ajax
function submitContactForm(url)
{
    $('input[title!=""]').unhint();
    $.ajax({
        'url': url,
        type: 'post',
        dataType: 'json',
        data: {
            'ajax': true,
            'contact_form_submitted': true,
            'your_name': $('#pm_form_contact_your_name').first().val(),
            'your_company': $('#pm_form_contact_your_company').first().val(),
            'your_role': $('#pm_form_contact_your_role').first().val(),
            'your_email': $('#pm_form_contact_your_email').first().val(),
            'your_phone': $('#pm_form_contact_your_phone').first().val(),
            'message': $('#pm_form_contact_message').first().val()
        },
        success: function(data){
            $('#pm_contact_message').html(data['content']);
            $("#pm_content_copy").hide();
            $("#pm_contact_form").hide();
             $('html, body').animate({scrollTop:0}, 'slow');
            _gaq.push(['/contact-us/#thank-you', url]);
        }
    });
}

//Submit the newsletter form via ajax
function submitNewsletterForm()
{
    $.ajax({
        url: '/',
        type: 'post',
        dataType: 'json',
        data: {
            'ajax': true,
            'newsletter_submitted': true,
            'email': $('#pm_form_newsletter_email').first().val()
        },
        success: function(data){
            $('#pm_form_newsletter').html(data['content']);
            _gaq.push(['/#newsletter-submitted', url]);
        }
    });
}
