(function($) {
    $('ul.commands li').live('click', function() {
        var div = $(this).parent().parent().parent();
        var build_order = div.data('options').build + $($(this).children()[0]).attr('class').match(/.$/)[0];
        var options = {
            build : build_order
        }
        div.sc2build(options);
        window.location.hash = '#' + build_order;
    });
    $('#raceselect li').live('click', function(){
        window.location.hash = '#' + $(this).attr('class');
        $('#sc2build').sc2build({build : $(this).attr('class')});
    });
    $('td.delete span').live('click', function() {
        var div = $(this).parent().parent().parent().parent().parent().parent();
        var build = div.data('options').build.split('');
        build.splice(Number($(this).attr('class'))+1, 1);
        var build_order = build.join('');
        var options = {
            build : build_order
        }
        div.sc2build(options);
        window.location.hash = '#' + build_order;
    });
    $.fn.sc2build = function(opts) {
        var options = $.extend({}, $.fn.sc2build.defaults, opts);
        var s2boc = new sc2build(options);
        this.html('');
        this.html(s2boc.render());
        $(this).data('options', options);
    }
    $.fn.sc2build.defaults = {
        'build' : 'p'
    }
    function sc2build(opts) {
        this.race = new race(opts.build[0]);
        var build_order = opts.build.substr(1).split('');
        this.build = [];
        for(var i in build_order) {
            this.race.cost += this.race.units[build_order[i]].cost;
            this.build.push([
                this.race.cost,
                this.race.units[build_order[i]].name,
                build_order[i]
            ]);
        }
    }
    sc2build.prototype.render = function() {
        var render = '<div id="order"><table><thead><tr><th>Supply</th><th>Action</th><th>Tools</th></thead><tbody>';
        
        for(var i in this.build) {
            var supply = '';
            
            if (typeof(this.build[i-1]) == 'undefined' || this.build[i][0] != this.build[i-1][0]) {
                supply = this.build[i][0];
            }
            render += '<tr><td>'+supply+'</td><td class="sprite-container"><span class="sprite r'+this.race.id+' '+this.build[i][2]+'"></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+this.build[i][1]+'</td><td class="delete"><span  class="'+i+'">Delete</span></td></tr>';
        }
        
        render += '</tbody></table></div><div id="tools"><ul class="commands">';
        
        for(var i in this.race.units) {
            if(typeof(this.race.units[i].name) == 'undefined') continue;
            if(i == 'a') render += '</ul><ul class="commands">'
            render += '<li class="sprite-container"><span class="sprite r'+this.race.id+' '+i+'"></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+this.race.units[i].name+'</li>';
            
        }
        
        render += '</ul></div>';
        
        return render;
    }
    sc2build.prototype.build = [];
    function race(id) {
        this.id = id;
        switch(id) {
            case 'p' : this.units = this.protoss; break;
            case 't' : this.units = this.terran;  break;
            case 'z' : this.units = this.zerg;    break;
        }
        this.cost = this.units.cost;
    }
    race.prototype.units = {};
    race.prototype.protoss = {
        A : { cost : 0, name : 'Nexus' },
        B : { cost : 0, name : 'Pylon' },
        C : { cost : 0, name : 'Assimilator' },
        D : { cost : 0, name : 'Gateway' },
        E : { cost : 0, name : 'Warpgate' },
        F : { cost : 0, name : 'Forge' },
        G : { cost : 0, name : 'Cybernetics Core' },
        H : { cost : 0, name : 'Photon Cannon' },
        I : { cost : 0, name : 'Twilight Council' },
        J : { cost : 0, name : 'Stargate' },
        K : { cost : 0, name : 'Robotics Facility' },
        L : { cost : 0, name : 'Templar Archives' },
        M : { cost : 0, name : 'Fleet Beacon' },
        N : { cost : 0, name : 'Robotics Bay' },
        O : { cost : 0, name : 'Dark Shrine' },
        a : { cost : 1, name : 'Probe' },
        b : { cost : 2, name : 'Zealot' },
        c : { cost : 2, name : 'Sentry' },
        d : { cost : 2, name : 'Stalker' },
        e : { cost : 2, name : 'High Templar' },
        f : { cost : 2, name : 'Dark Templar' },
        g : { cost : 1, name : 'Observer' },
        h : { cost : 2, name : 'Warp Prism' },
        i : { cost : 4, name : 'Immortal' },
        j : { cost : 6, name : 'Colossus' },
        k : { cost : 2, name : 'Phoenix' },
        l : { cost : 3, name : 'Void Ray' },
        m : { cost : 6, name : 'Carrier' },
        n : { cost : 8, name : 'Mothership' },
        cost : 6
    };
    race.prototype.terran = {
        A : { cost : 0, name : 'Command Center'      },
        B : { cost : 0, name : 'Supply Depot'        },
        C : { cost : 0, name : 'Refinery'            },
        D : { cost : 0, name : 'Barracks'            },
        C : { cost : 0, name : 'Orbital Command'     },
        E : { cost : 0, name : 'Planetary Fortress'  },
        F : { cost : 0, name : 'Engineering Bay'     },
        G : { cost : 0, name : 'Bunker'              },
        H : { cost : 0, name : 'Missile Turret'      },
        I : { cost : 0, name : 'Sensor Tower'        },
        J : { cost : 0, name : 'Factory'             },
        K : { cost : 0, name : 'Ghost Academy'       },
        L : { cost : 0, name : 'Armory'              },
        M : { cost : 0, name : 'Starport'            },
        N : { cost : 0, name : 'Fusion Core'         },
        O : { cost : 0, name : 'Tech Lab'            },
        P : { cost : 0, name : 'Reactor'             },
        a : { cost : 1, name : 'SCV'                 },
        b : { cost : 0, name : 'MULE'                },
        c : { cost : 1, name : 'Marine'              },
        d : { cost : 2, name : 'Marauder'            },
        e : { cost : 1, name : 'Reaper'              },
        f : { cost : 2, name : 'Ghost'               },
        g : { cost : 2, name : 'Hellion'             },
        h : { cost : 3, name : 'Siege Tank'          },
        i : { cost : 6, name : 'Thor'                },
        j : { cost : 2, name : 'Viking'              },
        k : { cost : 2, name : 'Medivac'             },
        l : { cost : 2, name : 'Raven'               },
        m : { cost : 3, name : 'Banshee'             },
        n : { cost : 6, name : 'Battlecruiser'       },
        o : { cost : 0, name : 'Auto-Turret'         },
        p : { cost : 0, name : 'Point Defense Drone' },
        cost : 6
    };
    race.prototype.zerg = {
        A : { cost : 0, name : 'Hatchery'          },
        B : { cost : 0, name : 'Extractor'         },
        C : { cost : 0, name : 'Spawning Pool'     },
        D : { cost : 0, name : 'Evolution Chamber' },
        E : { cost : 0, name : 'Spine Crawler'     },
        F : { cost : 0, name : 'Spore Crawler'     },
        G : { cost : 0, name : 'Hydralisk Den'     },
        H : { cost : 0, name : 'Baneling Nest'     },
        I : { cost : 0, name : 'Lair'              },
        J : { cost : 0, name : 'Roach Warren'      },
        K : { cost : 0, name : 'Infestation Pit'   },
        L : { cost : 0, name : 'Spire'             },
        M : { cost : 0, name : 'Nydus Network'     },
        N : { cost : 0, name : 'Hive'              },
        O : { cost : 0, name : 'Ultralisk Cavern'  },
        P : { cost : 0, name : 'Greater Spire'     },
        Q : { cost : 0, name : 'Creep Tumor'       },
        a : { cost : 0, name : 'Larva'             },
        b : { cost : 1, name : 'Drone'             },
        c : { cost : 0, name : 'Overlord'          },
        d : { cost : 1, name : 'Zergling'          },
        e : { cost : 2, name : 'Queen'             },
        f : { cost : 2, name : 'Hydralisk'         },
        g : { cost : 1, name : 'Baneling'          },
        h : { cost : 0, name : 'Overseer'          },
        i : { cost : 2, name : 'Roach'             },
        j : { cost : 2, name : 'Infestor'          },
        k : { cost : 0, name : 'Infested Terran'   },
        l : { cost : 2, name : 'Mutalisk'          },
        m : { cost : 2, name : 'Corruptor'         },
        n : { cost : 0, name : 'Nydus Worm'        },
        o : { cost : 6, name : 'Ultralisk'         },
        p : { cost : 4, name : 'Brood Lord'        },
        q : { cost : 0, name : 'Broodling'         },
        r : { cost : 0, name : 'Changeling'        },
        cost : 6
    }
})(jQuery);
