var location_data = {
    '73_330' : {
        x_pos: 73,
        y_pos: 330,
        label: 'Teton Valley, Idaho',
        anchor: 'teton_valley',
        employees: [
            {
                name: 'Jon Jensen',
                position: 'Chief Technical Officer'
            }
        ]
    },
    '69_329' : {
        x_pos: 69,
        y_pos: 329,
        label: 'Idaho Falls, Idaho',
        anchor: 'idaho_falls',
        employees: [
            {
                name: 'Mike Farmer',
                position: 'Web application developer'
            }
        ]
    },
    '68_285' : {
        x_pos: 68,
        y_pos: 285,
        label: 'Salt Lake City, Utah',
        anchor: 'salt_lake_city',
        employees: [
            {
                name: 'Brian Gadoury',
                position: 'Web application developer'
            }
        ]
    },
    '64_266' : {
        x_pos: 64,
        y_pos: 266,
        label: 'Hatton, Utah',
        anchor: 'hatton',
        employees: [
            {
                name: 'Josh Tolley',
                position: 'Database specialist'
            }
        ]
    },
    '268_310' : {
        x_pos: 268,
        y_pos: 310,
        label: 'Tappan, New York',
        anchor: 'tappan',
        employees: [
            {
                name: 'Benjamin Goldstein',
                position: 'President'
            }
        ]
    },
    '207_207' : {
        x_pos: 207,
        y_pos: 207,
        label: 'Brentwood, Tennessee',
        anchor: 'brentwood',
        employees: [
            {
                name: 'Brian Miller',
                position: 'Web application architect'
            }
        ]
    },
    '252_215' : {
        x_pos: 252,
        y_pos: 215,
        label: 'Research Triangle, North Carolina',
        anchor: 'research_triangle',
        employees: [
            {
                name: 'Carl Bailey',
                position: 'Ecommerce specialist'
            }
        ]
    },
    '243_218' : {
        x_pos: 243,
        y_pos: 218,
        label: 'Winston-Salem, North Carolina',
        anchor: 'winston-salem',
        employees: [
            {
                name: 'Steph Skardal',
                position: 'Web application developer'
            }
        ]
    },
    '145_235' : {
        x_pos: 145,
        y_pos: 235,
        label: 'Wichita, Kansas',
        anchor: 'wichita',
        employees: [
            {
                name: 'David Christensen',
                position: 'Cross-platform developer'
            }
        ]
    },
    '263_290' : {
        x_pos: 263,
        y_pos: 290,
        label: 'Philadelphia, Pennsylvania',
        anchor: 'philadelphia',
        employees: [
            {
                name: 'Greg Sabino Mullane',
                position: 'Database specialist'
            }
        ]
    },
    '205_309' : {
        x_pos: 205,
        y_pos: 309,
        label: 'St. Joseph, Michigan',
        anchor: 'st_joseph',
        employees: [
            {
                name: 'Jeff Boes',
                position: 'Ecommerce developer &amp; database architect'
            }
        ]
    },
    '254_268' : {
        x_pos: 254,
        y_pos: 268,
        label: 'Herndon, Virginia',
        anchor: 'herndon',
        employees: [
            {
                name: 'Mark Johnson',
                position: 'Ecommerce developer &amp; database architect'
            }
        ]
    },
    '180_116' : {
        x_pos: 180,
        y_pos: 116,
        label: 'Lafayette, Louisiana',
        anchor: 'lafayette',
        employees: [
            {
                name: 'Richard Templet',
                position: 'Ecommerce developer'
            }
        ]
    },
    '20_214' : {
        x_pos: 20,
        y_pos: 214,
        label: 'Santa Clarita, California',
        anchor: 'santa_clarita',
        employees: [
            {
                name: 'Ron Phipps',
                position: 'Director of Team Management'
            }
        ]
    },
    '40_440' : {
        x_pos: 40,
        y_pos: 440,
        label: 'Salmon Arm, British Columbia, Canada',
        anchor: 'salmon_arm',
        employees: [
            {
                name: 'Greg Davidson',
                position: 'Web application developer'
            }
        ]
    },
    '241_90' : {
        x_pos: 241,
        y_pos: 90,
        label: 'Valrico, Florida',
        anchor: 'valrico',
        employees: [
            {
                name: 'Josh Williams',
                position: 'Database specialist'
            }
        ]
    },
    '246_95' : {
        x_pos: 246,
        y_pos: 95,
        label: 'Orlando, Florida',
        anchor: 'orlando',
        employees: [
            {
                name: 'Nick Alexander',
                position: 'Web developer'
            }
        ]
    },
    '218_309' : {
        x_pos: 218,
        y_pos: 309,
        label: 'Taylor, Michigan',
        anchor: 'taylor',
        employees: [
            {
                name: 'Gerard Drazba',
                position: 'Liquid Galaxy Project Manager'
            }
        ]
    },
    '280_40' : {
        x_pos: 280,
        y_pos: 40,
        label: "Cruz Bay, U.S. Virgin Islands",
        anchor: "cruz_bay",
        employees: [
            {
                name: 'Brian Buchalter',
                position: 'Web developer &amp; system administrator'
            }
        ]
    },
    '229_207' : {
        x_pos: 229,
        y_pos: 207,
        label: "Asheville, North Carolina",
        anchor: "asheville",
        employees: [
            {
                name: 'Matt Vollrath',
                position: 'Liquid Galaxy Tech'
            }
        ]
    },
   '268_300' : {
        x_pos: 268,
        y_pos: 300,
        label: "New York, New York",
        anchor: "new_york",
        employees: [
            {
                name: "Adam Vollrath",
                position: "Systems &amp; network specialist"
            },
            {
                name: "Kiel Christofferson",
                position: "Systems &amp; network specialist"
            },
            {
                name: "Rick Peltzman",
                position: "Chief Executive Officer"
            }
        ]
    },
    '9000_9000' : {
        x_pos: 9000,
        y_pos: 9000,
        label: "Łódź, Poland",
        anchor: "lodz",
        employees: [
            {
                name: "Szymon Guz",
                position: "Web application developer"
            }
        ]
    },

    '25_200' : {
        x_pos: 25,
        y_pos: 200,
        label: "Los Angeles, California",
        anchor: "los_angeles",
        employees: [
            {
                name: "Marina Lohova",
                position: "Ruby on Rails developer"
            }
        ]
    }

};
var timeline = [[1950, 0], [1963,0], [1976, 0], [1995, 0], [1997, 0], [1998, 0], [2000,0], [2002, 0], [2004, 0], [2006, 0], [2007, 0], [2008, 0], [2009, 0], [2010, 0], [2011, 0], [2012, 0], [3000, 0]];

var previous_point = [0,0];
var previous_history_point = 0;
var ep = new Array;
var location_map = {}; 
var info_mapper = {};

$(function() {

    /* Data setup! */
    var a = 0;
    $.each(location_data, function(i, j) {
        info_mapper[j.anchor] = { type: 'location', datapoint: [j.x_pos, j.y_pos], a_pos: a };
        ep.push([j.x_pos, j.y_pos]);
        $.each(j.employees, function(x, y) {
            var eanchor = y.name.replace(' ', '_').toLowerCase();
            $('a.' + eanchor).addClass(i);
            info_mapper[eanchor] = { type: 'employee' };
            location_map[y.name] = {
                key: i,
                employee: y,
                a_pos: a
            };
        });
        j.a_pos = a;
        a++;
    });

    var plot = $.plot($('div#plot'),
        [ { data: ep } ],
        {
            lines: { show: false },
            points: { show: true, radius: 3 },
            bars: { show: false },
            xaxis: { mode: null, min: 0, max: 300, ticks: 0 },
            yaxis: { min: 0, max: 440, ticks: 0 },
            grid: { borderWidth: 0, hoverable: true },
            colors: ['#195065']
        });

    var history_plot = $.plot($('div#history_plot'),
        [ { data: timeline } ],
        {
            lines: { show: true },
            points: { show: true, radius: 3 },
            bars: { show: false },
            xaxis: { min: 1960, max: 2015},
            yaxis: { min: -0.5, max: 0.5, ticks: 0, color: '#FFF'},
            grid: { borderWidth: 0, hoverable: true },
            colors: ['#195065'],
            shadowSize: 0
        });

    $('div#plot').bind('plothover', function(event, pos, item) { if(item) { do_hover(item, plot); }});
    $('div#history_plot').bind('plothover', function(event, pos, item) { if(item) { history_do_hover(item, history_plot); }});
    $('div#headshots a').hover(function() { highlight_point(this, plot); });

    if(document.location.hash != '') {
        var hash = document.location.hash.replace('#' ,'');
        if(hash.match('[0-9][0-9][0-9][0-9]')) {
            var item = { 'datapoint': [hash, 0] };
            history_do_hover(item, history_plot);
        } else if(info_mapper[hash]) {
            if(info_mapper[hash].type == 'employee') {
                highlight_point($('a.' + hash), plot, 1);
            } else if(info_mapper[hash].type == 'location') {
                do_hover(info_mapper[hash], plot);
            }
        }
    }
})
var highlight_point = function(obj, plot, on_page_load) {
    var anchor = '#' + $(obj).attr('href').split('/').pop();
    if(document.location.hash != anchor || on_page_load) {
        var lmap = location_map[$(obj).attr('title')];
        if(lmap) {
            var key = lmap.key;
            plot.unhighlight();
            plot.highlight(0, lmap.a_pos);
            $('div#headshots a').removeClass('outlined');
            var employees = new Array(lmap.employee);
            $(obj).addClass('outlined');
            show_bios(key, employees);
            document.location.href = document.location.href.split('#')[0] + anchor;
            if($('input#in_production').length > 0) {
                pageTracker._trackEvent('Team Page Interaction', 'Bio Hover', anchor.replace('#', ''));
            } 
        }
    }
    return false;
};
var do_hover = function(item, plot) {
    if(item && !(previous_point[0] == item.datapoint[0] && previous_point[1] == item.datapoint[1])) {
        previous_point = item.datapoint;
        $('div#headshots a').removeClass('outlined');
        var key = item.datapoint[0] + '_' + item.datapoint[1];
        $('div#headshots a.' + key).addClass('outlined');
        plot.unhighlight();
        plot.highlight(0, location_data[key].a_pos);
        show_bios(key, location_data[key].employees);
        document.location.href = document.location.href.split('#')[0] + '#' + location_data[key].anchor
        if($('input#in_production').length > 0) {
            pageTracker._trackEvent('Team Page Interaction', 'Map Hover', anchor);
        } 
    } 
};

var show_bios = function(key, people) {
    var content = '';
    $.each(people, function(i, j) {
        var link = j.name.replace(/ /g, '_').toLowerCase();
        content += '<a href="/team/' + link + '">' + j.name + '</a><p class="skills">' + j.position + '</p>';
    });
    content += '<p class="location">' + location_data[key].label + '</p>';
    $('div#tooltip').html(content);
};

var history_do_hover = function(item, plot) {
    if(item && previous_history_point != item.datapoint[0]) {
        previous_history_point = item.datapoint[0];
        var bg_pos = (item.datapoint[0] - 1960)*13.48-11;
        $('div#history_guide').css('background-position', bg_pos + 'px 0px').show();
        $('div#history_details').css('margin-top', '0px');
        $('div#history_details div').hide();
        $('div#time_' + item.datapoint[0]).show();
        document.location.href = document.location.href.split('#')[0] + '#' + item.datapoint[0];
        plot.unhighlight();
        for(i = 0; i<timeline.length; i++) { if(timeline[i] == item.datapoint[0] + ',' + item.datapoint[1]) { break; } }
        plot.highlight(0, i);      
        if($('input#in_production').length > 0) {
            pageTracker._trackEvent('Team Page Interaction', 'History Hover', item.datapoint[0]);
        } 
    }
};

