/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function handleStatResponse(json)
{
    // Handle error
    if (json.error) {
        $('#error-container')
            .append($('<p>').text(json.error))
            .slideDown('fast');
        return false;
    }

    setValue = function(el, val, cls){
        // If element is hidden and value is not empty
        // display the element
        if (val != "" && el.css('display') == 'none') {
            el.show();
        }

        el.addClass(cls).html(val);
    }

    for (stat in json) {
        val = json[stat].formatted
        if (typeof(val) != 'object') {
            setValue($('.'+stat),
                     json[stat].formatted,
                     json[stat].rank);

            // Set stat's real value
            // Convert string to length and leave other types as is
            if (typeof(json[stat].value) == 'string' && !json[stat].value.match(/\d+/i))
                $('.'+stat+'-value').html(json[stat].value.length);
            else
                $('.'+stat+'-value').html(json[stat].value);
        } else {
            for (key in val) {
                setValue($('.'+stat+'.'+key),
                         json[stat].formatted[key],
                         json[stat].rank);
            }
        }
    }

    return true;
}

function requestStats(domain, fn)
{
    if (typeof(domain) == 'undefined')
        return;

    $('#loading-status').text('Loading off-page...');

    // Call for domain stats
    setTimeout(function(){
        $.ajax({
           type: 'GET',
           url: base+'/stats?d='+ domain +'&g=d',
           complete: function(xhr, status) {
                // Handle JSON header
                jsonHeader = xhr.getResponseHeader('X-JSON');
                if (jsonHeader.length > 0) {
                    json = eval(jsonHeader);
                    if (handleStatResponse(json)) {
                        $('#domain-stats').slideDown('fast');
                    }
                }
           }
        }); // end ajax
    }, 0)

    // Call for html stats
    setTimeout(function(){
        $('#loading-status').text('Loading on-page...');

        $.ajax({
           type: 'GET',
           url: base+'/stats?d='+ domain +'&g=h',
           complete: function(xhr, status) {
                // Handle JSON header
                jsonHeader = xhr.getResponseHeader('X-JSON');
                if (jsonHeader && jsonHeader.length > 0) {
                    json = eval(jsonHeader);
                    if (handleStatResponse(json)) {
                        $('#html-stats').slideDown('fast');
                    }

                    $('#loading-status').text('Loading score...');

                    // Call for final score
                    $.ajax({
                        type: 'GET',
                        url: base+'/stats?d='+ domain +'&g=s',
                        complete: function(xhr, status) {
                            // Handle JSON header
                            jsonHeader = xhr.getResponseHeader('X-JSON');
                            if (jsonHeader && jsonHeader.length > 0) {
                                json = eval(jsonHeader);
                                if (handleStatResponse(json)) {
                                    if (jQuery.browser.version < 8)
                                        $('#score-container').show();
                                    else
                                        $('#score-container').slideDown('fast');
                                    $('#loading-stats').hide();
                                }
                                
                                if (typeof(fn) != 'undefined')
                                    fn();
                            }
                        }
                    }); // end ajax
                }
           }
        }); // end ajax
    }, 600);
}
