var search = function(account_id, search_term, resources){
    $('div.profiles').hide();
    $('#account').empty();
    $('#anonymous').empty();
    $('#common').empty().show();
    $('div.results').show();
            
    $.ajax({
        url:'app/profiler/search/'+account_id+'/'+search_term,
        dataType: 'json',
        success:function(result){
            resources.profile
                .clone()
                .templateReplace({
                    picture: result.account.picture,
                    name: result.account.name,
                    search_domain: result.account.search_domain,
                    search_term: search_term
                })
                .appendTo('#account');
               
                if(result.account.hits.length > 0){
                    $('<h3>exclusive search results for ' + result.account.name + '</h3>').appendTo('#account');
                    resources.results.clone().templateRepeat(result.account.hits, 'div.hit', function(i, item, node){
                        node.templateReplace({
                            href: item.href,
                            title: $('<span></span>').append(item.title),
                            source: item.source ? $('<div></div>').append(item.source) : ''
                        }).addClass(item.style);
                    }).appendTo('#account');
                }
                else{
                    $('<div class="results">sorry, no exclusive search results this time</div>').appendTo('#account');
                }

            resources.profile
                .clone()
                .templateReplace({
                    picture: 'anonymous.png',
                    name: 'anonymous',
                    search_domain: result.anonymous.search_domain,
                    search_term: search_term
                })
                .appendTo('#anonymous');

                if(result.anonymous.hits.length > 0){
                    $('<h3>exclusive search results for anonymous</h3>').appendTo('#anonymous');
                    resources.results.clone().templateRepeat(result.anonymous.hits, 'div.hit', function(i, item, node){
                        node.templateReplace({
                            href: item.href,
                            title: $('<span></span>').append(item.title),
                            source: item.source ? $('<div></div>').append(item.source) : ''
                        }).addClass(item.style);
                    }).appendTo('#anonymous');
                }
                else{
                    $('<div class="results">sorry, no exclusive search results this time</div>').appendTo('#anonymous');
                }
               
            $('<h3>shared search results, different ranking for ' + result.account.name + '</h3>').appendTo('#common');
            resources.results.clone().templateRepeat(result.position_missmatch, 'div.hit', function(i, item, node){
                node.templateReplace({
                    href: item.href,
                    title: $('<span></span>').append(item.title),
                    source: item.source ? $('<div></div>').append(item.source) : ''
                }).addClass(item.style);
            }).appendTo('#common');
            $('<h3>shared search results, identical ranking</h3>').appendTo('#common');
            resources.results.clone().templateRepeat(result.match, 'div.hit', function(i, item, node){
                node.templateReplace({
                    href: item.href,
                    title: $('<span></span>').append(item.title),
                    source: item.source ? $('<div></div>').append(item.source) : ''
                }).addClass(item.style);
            }).appendTo('#common');
        }
    });

};

var profiles = function(resources){
    $('div.results').hide();
    $('#common').hide();
    $('div.profiles').show();
};

var show = function(hash, resources){
    var path = hash.split('/');
    var command = path[0];
    $(document).scrollTop(0);
    if(command == 'profiles'){
        profiles(resources);
    }
    else if(command == 'search'){
        var account_id = Number(path[1]);
        var search_term = path[2];

        search(account_id, search_term, resources);
    }
};

var main = function(resources){
    $.history(function(hash){
        if(!hash){
            hash = 'profiles';
        }
        show(hash, resources);
    });
};

$(document).ready(function(){
    $.loadResources({
        persons         : 'app/persons.json',
        domains         : 'app/domains.json',
        profile         : 'templates/profile.html',
        results         : 'templates/results.html',
        profiles        : 'templates/profiles.html'
    }, {
        load:function(resources){
            resources.profiles
                .templateRepeat(resources.persons, 'div.group', function(i, group, node){
                    node.templateRepeat(group, 'div.profile', function(i, person, profile){
                        profile.templateReplace(person);
                        var input = profile.find('input');
                        var account = profile.find('select');
                        input.suggest(person.suggest);
                        // profile.find('option').repeatFor(person.accounts);
                        profile.find('option').repeat(person.accounts, function(item, node){
                            if(item.search_domain == 'google.co.uk'){
                                node.templateReplace(item);
                            }
                            else{
                                node.remove();
                            }
                        })
                        profile.find('button').click(function(){
                            if(!input.warn_empty_input()){
                                var account_id = Number(account.val());
                                document.location.hash = 'search/' + account_id + '/' + input.val();
                            }
                        });
                    });
                })
                .replaceAll('div.profiles')
            main(resources);
        }
    });
});
