
(function() {
    
    $.fn.figureSignpost = function(params) {
        
        var _self = $(this);
        var bplist = params.bodylists;
        
        /* objekt casti tela (body part) */
        function bp(options) {
            
            this.notifyListeners = function () {
                if (this.parent)
                this.parent.view();
                
                for(var i=0;i<this.listener.length;i++) {
                    this.listener[i].addClass('act');
                }
            }
            
            this.hideAll = function (e) {
                if (this.bodyparts) {
                    for(var i=0;i<this.bodyparts.length;i++) {
                        this.bodyparts[i].hide(e);
                    }
                }
            }
            
            this.hideSelf = function (e) {
                if (!e.data.bubbles)
                this.hideAll(e);
            
                for(var i=0;i<this.listener.length;i++) {
                    this.listener[i].removeClass('act');
                }
                
                if (this.parent && e.data.bubbles) {
                    this.parent.hide(e);
                }
            }
            
            this.parent = options.parent ? options.parent : null;
            this.image = options.image;
            this.coords = options.coords;
            this.size = options.size;
            this.position = options.position;
            this.name = options.name;
            this.tag = options.tag;
            this.bodyparts = options.bodyparts;
            this.areaType = options.areaType ? options.areaType : 'poly';
            
            this.map = null;
            this.listener = null;
            
            this.view = options.view ? options.view : this.notifyListeners;
            this.hide = options.hide ? options.hide : this.hideSelf;
            
            this.createMap = function () {
                this.map = $('<map name="'+this.tag+'_map" id="'+this.tag+'_map"/>');
            }
        }
        
        var man_front = function() {
            
            this.figure = new bp({
                'image': '/img/figure/man_front.png',
                'size': [78,227],
                'position': [0,0],
                'coords': '',
                'name': 'Muž',
                'tag': 'man-front',
                'view': function() {},
                'hide': function() {},
                'bodyparts': new Array()
            });
            
            this.head = new bp({
                'parent': figure,
                'image': '/img/figure/man_head.gif',
                'coords': '39,34,32,30,28,24,26,18,25,11,28,7,33,4,38,3,43,4,48,6,51,11,51,18,50,25,48,30',
                'size': [78,78],
                'position': [0,0],
                'name': 'Hlava',
                'tag': 'hlava',
                'view': function() {
                    var obj = this;
                    
                    if (obj.parent)
                    obj.parent.view();
                            
                    overlay.attr('usemap','#'+obj.map.attr('name'))
                           .css('width',obj.size[0]+'px')
                           .css('height',obj.size[1]+'px')
                           .mouseout({object: obj},obj.hide);
                            
                    obj.listener[0].addClass('act');
                },
                'hide': function(e) {
                    var obj;
                    var allowHide = false;

                    if (e && !e.data.bubbles) {
                        obj = e.data.object;
                        
                        var offset = obj.listener[0].offset();
                        var size = {x: obj.listener[0].width(), y: obj.listener[0].height()};
                        var curpos = {x: e.pageX, y: e.pageY};

                        if (offset.left >= curpos.x ||
                            offset.left+size.x <= curpos.x ||
                            offset.top >= curpos.y ||
                            offset.top+size.y <= curpos.y) {
                                allowHide = true;
                            }
                    } else if (e && e.data.bubbles) {
                        obj = this;
                        allowHide = true
                    }
                    
                    if (allowHide) {
                        overlay.attr('usemap','#'+figure[gndr][side].map.attr('name'))
                               .css('width',figure[gndr][side].size[0])
                               .css('height',figure[gndr][side].size[1])
                               .mouseout(null);

                        obj.hideSelf(e);
                    }
                },
                'bodyparts': new Array()
            });
            
            /* man's front */
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_front/bricho.gif',
                    'coords': '23,88,25,97,29,102,34,104,40,103,44,101,48,97,50,90,50,83,46,78,40,73,33,72,26,76,22,82',
                    'size': [33,33],
                    'position': [72,22],
                    'name': 'Břicho',
                    'tag': 'bricho'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_front/genit.gif',
                    'coords': '20,98,28,104,35,105,41,104,46,102,51,99,54,96,56,104,38,115,37,126,33,127,33,116,19,106',
                    'size': [40,33],
                    'position': [95,19],
                    'name': 'Genitálie',
                    'tag': 'genitalie'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_front/hrudnik.gif',
                    'coords': '21,74,26,74,34,67,37,67,45,75,51,76,55,74,57,69,57,60,59,51,59,47,55,44,38,45,19,44,17,52,17,67',
                    'size': [44,34],
                    'position': [43,17],
                    'name': 'Hrudník',
                    'tag': 'hrudnik'
                }));
            
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_front/krk.gif',
                    'coords': '29,37,32,35,32,31,35,34,39,35,45,31,48,36,42,41,35,41',
                    'size': [22,11],
                    'position': [31,28],
                    'name': 'Krk',
                    'tag': 'krk'
                }));

            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_front/leva_noha.gif',
                    'coords': '34,222,35,205,37,197,37,127,39,123,39,115,57,104,57,126,50,157,52,171,51,183,42,208,46,218',
                    'size': [27,125],
                    'position': [101,34],
                    'name': 'Levá noha',
                    'tag': 'noha'
                }));

            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_front/leva_ruka.gif',
                    'coords': '60,39,68,47,69,55,72,66,72,78,74,83,74,102,73,109,75,117,73,125,66,131,62,125,63,117,64,112,67,106,65,95,62,87,61,73,58,65',
                    'size': [21,92],
                    'position': [41,57],
                    'name': 'Levá ruka',
                    'tag': 'ruka'
                }));

            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_front/prava_noha.gif',
                    'coords': '19,105,32,116,32,127,35,129,36,164,36,198,33,209,16,210,16,207,28,203,32,198,32,187,28,163,17,130',
                    'size': [32,109],
                    'position': [102,15],
                    'name': 'Pravá noha',
                    'tag': 'noha'
                }));

            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_front/prava_ruka.gif',
                    'coords': '17,42,12,48,11,52,9,69,9,78,5,87,5,108,3,117,3,125,7,129,11,128,14,121,14,115,10,109,13,100,18,89,19,72,17,68',
                    'size': [20,89],
                    'position': [41,1],
                    'name': 'Pravá ruka',
                    'tag': 'ruka'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_front/skin01.gif',
                    'coords': '50,36,67,44,73,64,73,79,76,85,75,101,75,111,76,121,73,128,67,133,63,125,66,115,68,107,66,93,64,84,64,77,59,66,56,80,56,99,59,108,59,122,54,147,52,154,52,165,54,177,49,191,45,202,46,209,44,210,46,220,41,225,34,225,34,209,16,209,17,207,31,200,33,192,30,181,28,169,28,162,25,157,25,150,19,132,19,106,21,95,21,80,19,76,18,91,10,107,10,111,13,119,13,126,9,130,2,123,1,116,5,107,6,85,9,78,11,63,12,49,17,42,26,38,32,34,32,30,28,24,27,18,25,11,34,2,44,2,52,9,52,20,51,27,47,31,47,36,50,35,56,28,56,5,46,0,31,0,21,10,24,26,27,31,12,41,6,50,6,67,1,84,0,106,0,128,5,132,10,134,15,128,16,137,21,152,21,160,24,165,24,174,29,192,26,199,12,207,12,214,30,214,30,225,49,226,52,220,48,212,50,207,49,202,60,177,56,156,60,132,66,139,78,131,83,118,79,101,81,77,76,54,71,39,52,33',
                    'size': [78,216],
                    'position': [11,0],
                    'name': 'Pokožka',
                    'tag': 'pokozka'
                }));
                
            /* man's head */                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/man_head/krk.gif',
                    'coords': '28,52,28,65,31,68,38,69,44,69,49,67,51,65,51,52,45,58,36,58',
                    'size': [26,20],
                    'position': [52,25],
                    'name': 'Krk',
                    'tag': 'krk'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/man_head/leve_oko.gif',
                    'coords': '42,28,55,36',
                    'size': [16,11],
                    'position': [27,39],
                    'name': 'Levé oko',
                    'tag': 'oci',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/man_head/leve_ucho.gif',
                    'coords': '56,33,61,44',
                    'size': [6,14],
                    'position': [33,54],
                    'name': 'Levé ucho',
                    'tag': 'usi',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/man_head/mozek.gif',
                    'coords': '25,25,54,25,54,16,50,11,44,8,35,8,28,11,25,17',
                    'size': [32,18],
                    'position': [8,22],
                    'name': 'Mozek',
                    'tag': 'mozek',
                    'areaType': 'poly'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/man_head/nos.gif',
                    'coords': '35,33,45,46',
                    'size': [10,18],
                    'position': [30,33],
                    'name': 'Nos',
                    'tag': 'nos',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/man_head/prave_oko.gif',
                    'coords': '25,28,38,36',
                    'size': [16,11],
                    'position': [27,21],
                    'name': 'Pravé oko',
                    'tag': 'oci',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/man_head/prave_ucho.gif',
                    'coords': '19,32,24,44',
                    'size': [6,14],
                    'position': [33,16],
                    'name': 'Pravé ucho',
                    'tag': 'usi',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/man_head/usta.gif',
                    'coords': '31,45,48,52',
                    'size': [15,10],
                    'position': [45,30],
                    'name': 'Ústa',
                    'tag': 'usta',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/man_head/skin_head01.gif',
                    'coords': '21,25,21,31,18,31,18,45,27,53,27,59,12,67,12,73,66,73,66,65,53,58,53,51,62,41,62,32,58,32,58,24,64,24,64,28,67,28,67,44,57,55,71,64,71,77,9,77,9,64,23,56,14,46,14,28,16,28,16,25',
                    'size': [38,39],
                    'position': [21,19],
                    'name': 'Pleť',
                    'tag': 'plet'
                }));
                
            this.figure.bodyparts.push(this.head);
            return this.figure;
        };

        var man_back = function () {
            
            this.figure = new bp({
                'image': '/img/figure/man_back.png',
                'coords': '',
                'size': [78,227],
                'position': [0,0],
                'name': 'Muž',
                'tag': 'man-back',
                'view': function() {},
                'hide': function() {},
                'bodyparts': new Array()
            });
            
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_back/hlava.gif',
                    'coords': '29,28,25,20,24,11,27,5,32,3,37,3,43,3,49,7,51,13,51,19,50,25,45,31,36,32,32,31',
                    'size': [29,32],
                    'position': [1,24],
                    'name': 'Vlasy',
                    'tag': 'vlasy'                
                }));

            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_back/krk.gif',
                    'coords': '31,31,29,36,33,39,43,39,47,36,44,32',
                    'size': [19,10],
                    'position': [30,29],
                    'name': 'Krk',
                    'tag': 'krk'
                }));
                    
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_back/leva_noha.gif',
                    'coords': '17,113,22,118,27,121,34,122,42,122,41,163,45,174,46,185,44,196,44,206,48,215,47,221,35,221,27,223,20,223,14,217,17,214,24,213,30,211,34,207,34,200,29,180,29,168,25,162,25,153,17,131',
                    'size': [36,113],
                    'position': [112,13],
                    'name': 'Levá noha',
                    'tag': 'noha'
                }));

            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_back/leva_ruka.gif',
                    'coords': '15,42,23,57,22,67,22,74,18,81,18,86,16,93,9,107,14,116,15,122,13,130,9,130,3,126,3,112,4,88,8,77,10,51',
                    'size': [24,88],
                    'position': [42,1],
                    'name': 'Levá ruka',
                    'tag': 'ruka'
                }));
                    
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_back/prava_noha.gif',
                    'coords': '43,121,42,163,47,173,47,181,46,190,45,198,51,183,53,176,50,160,53,147,56,135,59,118,53,122,48,123',
                    'size': [18,84],
                    'position': [116,42],
                    'name': 'Pravá noha',
                    'tag': 'noha'
                }));
                    
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_back/prava_ruka.gif',
                    'coords': '67,46,72,64,72,76,74,83,74,106,74,117,74,128,70,132,65,131,62,125,63,116,65,110,67,105,63,87,63,76,58,69,61,58',
                    'size': [18,86],
                    'position': [46,58],
                    'name': 'Pravá ruka',
                    'tag': 'ruka'
                }));
                    
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_back/zada.gif',
                    'coords': '16,41,23,56,23,65,22,74,20,80,20,93,31,96,41,97,47,96,55,92,54,85,54,79,56,71,59,61,63,52,66,44,48,35,44,40,33,41,27,37',
                    'size': [51,62],
                    'position': [36,16],
                    'name': 'Záda',
                    'tag': 'zada'
                }));
                    
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_back/zadek.gif',
                    'coords': '19,95,17,112,23,118,30,121,38,121,42,120,47,122,54,120,59,116,59,107,55,94,43,98,35,99',
                    'size': [44,29],
                    'position': [94,17],
                    'name': 'Zadek',
                    'tag': 'zadek'
                }));
                    
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/man_back/skin01.gif',
                    'coords': '49,27,46,31,46,35,64,42,68,47,68,53,72,68,72,77,75,82,75,98,73,107,75,116,75,125,71,132,66,132,63,125,64,116,67,108,67,100,64,88,63,81,63,76,58,69,54,85,56,97,60,109,59,123,52,150,50,160,51,166,52,178,45,200,45,211,48,216,47,222,33,222,21,224,13,217,15,215,28,213,33,210,34,207,34,198,29,180,29,167,24,160,24,152,17,131,17,109,20,94,20,78,16,93,9,109,15,118,14,128,9,130,1,124,1,116,4,108,4,86,8,77,12,45,17,41,21,41,31,36,31,31,27,27,26,33,19,37,12,37,7,43,5,53,4,70,1,84,0,102,0,129,8,135,13,133,19,152,19,164,25,171,25,185,31,201,30,207,23,210,11,211,7,217,13,224,51,225,53,218,52,211,48,205,48,198,58,177,55,158,61,133,64,136,75,136,81,125,81,113,79,107,81,96,80,78,78,71,73,48,71,40,63,36,52,32',
                    'size': [77,196],
                    'position': [30,0],
                    'name': 'Pokožka',
                    'tag': 'pokozka'
                }));
                
            return this.figure;
        };

        var woman_front = function () {
            
            this.figure = new bp({
                'image': '/img/figure/woman_front.png',
                'size': [78,227],
                'position': [0,0],
                'coords': '',
                'name': 'Žena',
                'tag': 'woman-front',
                'view': function() {},
                'hide': function() {},
                'bodyparts': new Array()
            });
            
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/woman_front/bricho.gif',
                    'coords': '42,82,37,82,33,84,30,87,28,91,26,94,26,98,26,102,27,105,30,108,33,110,37,112,42,112,46,110,50,107,52,103,52,98,52,93,50,89,46,84',
                    'size': [31,31],
                    'position': [82,24],
                    'name': 'Břicho',
                    'tag': 'bricho'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/woman_front/genit.gif',
                    'coords': '22,103,21,107,29,114,32,119,38,124,41,126,44,123,47,118,51,112,55,107,58,104,57,101,51,108,46,111,42,113,38,113,31,110,27,106',
                    'size': [39,27],
                    'position': [99,20],
                    'name': 'Genitálie',
                    'tag': 'genitalie'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/woman_front/hrudnik.gif',
                    'coords': '23,84,30,84,37,79,41,79,47,83,54,84,55,74,57,69,56,57,53,53,47,53,42,55,33,55,27,53,21,53,19,56,19,67,22,72',
                    'size': [39,33],
                    'position': [53,19],
                    'name': 'Prsa a hrudní koš',
                    'tag': 'hrudnik'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/woman_front/krk.gif',
                    'coords': '31,39,30,46,34,49,40,49,48,47,43,40,39,44,35,43',
                    'size': [17,12],
                    'position': [40,29],
                    'name': 'Krk',
                    'tag': 'krk'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/woman_front/leva_noha.gif',
                    'coords': '38,202,38,207,35,211,35,224,43,225,46,222,43,212,43,206,50,181,51,175,51,158,51,154,59,124,53,124,51,120,52,115,58,107,47,119,41,127,45,157,46,169,45,177,41,190',
                    'size': [27,121],
                    'position': [106,34],
                    'name': 'Levá noha',
                    'tag': 'noha'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/woman_front/leva_ruka.gif',
                    'coords': '60,50,56,72,60,86,60,97,60,105,53,115,52,120,53,123,61,123,64,115,65,106,67,99,68,88,67,80,64,66',
                    'size': [18,75],
                    'position': [49,52],
                    'name': 'Levá ruka',
                    'tag': 'ruka'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/woman_front/prava_noha.gif',
                    'coords': '35,207,34,222,25,221,24,217,27,209,30,202,30,190,29,178,32,165,25,143,21,127,19,117,20,108,40,127,45,162,45,170',
                    'size': [27,118],
                    'position': [107,19],
                    'name': 'Pravá noha',
                    'tag': 'noha'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/woman_front/prava_ruka.gif',
                    'coords': '16,50,21,74,17,93,16,118,20,126,22,137,18,140,12,132,12,117,9,105,9,93,10,73,12,60',
                    'size': [14,91],
                    'position': [49,9],
                    'name': 'Pravá ruka',
                    'tag': 'ruka'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                    'image': '/img/figure/cut/woman_front/skin01.gif',
                    'coords': '44,42,44,45,56,47,61,51,64,61,79,84,68,103,52,158,51,180,44,210,46,223,42,226,23,221,30,200,29,177,33,164,23,140,16,139,12,130,12,116,9,100,13,55,16,50,21,47,28,47,31,44,31,41,26,41,24,43,20,43,10,51,8,70,8,135,12,143,21,144,26,158,29,166,25,180,26,200,20,224,27,226,53,226,47,208,56,178,56,158,73,101,73,83,64,49,59,43',
                    'size': [62,206],
                    'position': [21,8],
                    'name': 'Pokožka',
                    'tag': 'pokozka'
                }));
            
            this.head = new bp({
                'parent': this.figure,
                'image': '/img/figure/woman_head.gif',
                'coords': '31,19,28,25,28,33,30,37,33,41,38,42,42,40,45,35,46,24,39,19',
                'size': [78,78],
                'position': [0,0],
                'name': 'Hlava',
                'tag': 'hlava',
                'view': function() {
                    var obj = this;
                    
                    if (obj.parent)
                    obj.parent.view();
                            
                    overlay.attr('usemap','#'+obj.map.attr('name'))
                           .css('width',obj.size[0]+'px')
                           .css('height',obj.size[1]+'px')
                           .mouseout({object: obj},obj.hide);
                            
                    obj.listener[0].addClass('act');
                },
                'hide': function(e) {
                    var obj;
                    var allowHide = false;

                    if (e && !e.data.bubbles) {
                        obj = e.data.object;
                        
                        var offset = obj.listener[0].offset();
                        var size = {x: obj.listener[0].width(), y: obj.listener[0].height()};
                        var curpos = {x: e.pageX, y: e.pageY};

                        if (offset.left >= curpos.x ||
                            offset.left+size.x <= curpos.x ||
                            offset.top >= curpos.y ||
                            offset.top+size.y <= curpos.y) {
                                allowHide = true;
                            }
                    } else if (e && e.data.bubbles) {
                        obj = this;
                        allowHide = true
                    }
                    
                    if (allowHide) {
                        overlay.attr('usemap','#'+figure[gndr][side].map.attr('name'))
                               .css('width',figure[gndr][side].size[0])
                               .css('height',figure[gndr][side].size[1])
                               .mouseout(null);

                        obj.hideSelf(e);
                    }
                },
                'bodyparts': new Array()
            });

            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/woman_head/krk.gif',
                    'coords': '31,54,31,65,34,70,40,71,46,70,50,66,50,54,43,60,38,60',
                    'size': [21,19],
                    'position': [54,29],
                    'name': 'Krk',
                    'tag': 'krk'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/woman_head/leve_oko.gif',
                    'coords': '42,29,56,39',
                    'size': [15,12],
                    'position': [28,40],
                    'name': 'Levé oko',
                    'tag': 'oci',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/woman_head/leve_ucho.gif',
                    'coords': '55,33,60,46',
                    'size': [6,13],
                    'position': [37,52],
                    'name': 'Levé ucho',
                    'tag': 'usi',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/woman_head/mozek.gif',
                    'coords': '26,26,54,26,54,18,50,13,43,10,37,10,31,12,26,17',
                    'size': [31,17],
                    'position': [10,23],
                    'name': 'Mozek',
                    'tag': 'mozek',
                    'areaType': 'poly'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/woman_head/nos.gif',
                    'coords': '36,34,45,47',
                    'size': [9,17],
                    'position': [32,34],
                    'name': 'Nos',
                    'tag': 'nos',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/woman_head/prave_oko.gif',
                    'coords': '24,29,38,39',
                    'size': [15,12],
                    'position': [28,22],
                    'name': 'Pravé oko',
                    'tag': 'oci',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/woman_head/prave_ucho.gif',
                    'coords': '20,34,25,46',
                    'size': [6,14],
                    'position': [36,19],
                    'name': 'Pravé ucho',
                    'tag': 'usi',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/woman_head/usta.gif',
                    'coords': '33,47,48,55',
                    'size': [15,10],
                    'position': [47,31],
                    'name': 'Ústa',
                    'tag': 'usta',
                    'areaType': 'rect'
                }));
                
            this.head.bodyparts.push(
                new bp({
                    'parent': this.head,
                    'image': '/img/figure/cut/woman_head/skin_head01.gif',
                    'coords': '23,27,23,33,19,33,19,45,30,56,30,63,20,67,20,73,63,73,63,67,51,61,51,55,62,42,62,33,57,33,57,26,62,26,62,29,67,29,67,45,56,58,68,66,68,76,17,76,17,66,26,59,15,46,15,30,19,30,19,27',
                    'size': [36,35],
                    'position': [27,20],
                    'name': 'Pleť',
                    'tag': 'plet'
                }));
                
            this.figure.bodyparts.push(this.head);
            return this.figure;
        };

        var woman_back = function() {
            
            this.figure = new bp({
                'image': '/img/figure/woman_back.png',
                'coords': '',
                'size': [78,227],
                'position': [0,0],
                'name': 'Žena',
                'view': function() {},
                'hide': function() {},
                'tag': 'woman-back',
                'bodyparts': new Array()
            });

            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                        'image': '/img/figure/cut/woman_back/hlava.gif',
                        'coords': '37,9,31,11,27,13,24,17,22,21,21,26,21,31,22,36,26,39,30,41,34,42,39,42,43,42,47,40,51,37,53,32,54,27,53,22,51,17,47,12',
                        'size': [34,34],
                        'position': [9,21],
                        'name': 'Vlasy',
                        'tag': 'vlasy'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                        'image': '/img/figure/cut/woman_back/krk.gif',
                        'coords': '31,42,43,42,45,47,37,49,29,47',
                        'size': [19,8],
                        'position': [42,28],
                        'name': 'Krk',
                        'tag': 'krk'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                        'image': '/img/figure/cut/woman_back/leva_noha.gif',
                        'coords': '20,126,32,162,32,167,29,178,30,201,26,213,22,218,26,220,32,221,36,220,34,215,36,211,35,203,37,195,39,126,32,128,26,128',
                        'size': [21,98],
                        'position': [123,19],
                        'name': 'Levá noha',
                        'tag': 'noha'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                        'image': '/img/figure/cut/woman_back/leva_ruka.gif',
                        'coords': '13,54,21,73,19,82,17,93,17,119,20,132,22,138,17,140,11,132,12,119,9,106,9,93,10,75',
                        'size': [15,89],
                        'position': [51,8],
                        'name': 'Levá ruka',
                        'tag': 'ruka'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                        'image': '/img/figure/cut/woman_back/prava_noha.gif',
                        'coords': '59,123,53,126,40,126,40,153,39,161,39,172,38,183,38,203,37,212,35,217,38,221,44,221,49,220,47,216,45,210,44,204,48,191,51,169,51,159',
                        'size': [25,99],
                        'position': [123,35],
                        'name': 'Pravá noha',
                        'tag': 'noha'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                        'image': '/img/figure/cut/woman_back/prava_ruka.gif',
                        'coords': '60,52,56,74,60,87,60,105,60,123,64,111,67,99,68,93,69,82',
                        'size': [14,73],
                        'position': [52,56],
                        'name': 'Pravá ruka',
                        'tag': 'ruka'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                        'image': '/img/figure/cut/woman_back/zada.gif',
                        'coords': '14,53,19,48,24,47,29,47,33,49,36,50,39,50,46,47,53,47,57,49,60,51,57,63,55,70,54,77,53,85,53,93,56,100,41,102,23,100,24,96,24,91,24,86,23,80,22,74,21,67,17,59',
                        'size': [47,56],
                        'position': [47,14],
                        'name': 'Záda',
                        'tag': 'zada'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                        'image': '/img/figure/cut/woman_back/zadek.gif',
                        'coords': '22,100,39,103,56,101,58,109,59,119,58,123,51,125,45,125,37,125,33,127,25,127,19,125,19,112',
                        'size': [43,32],
                        'position': [97,18],
                        'name': 'Zadek',
                        'tag': 'zadek'
                }));
                
            this.figure.bodyparts.push(
                new bp({
                    'parent': this.figure,
                        'image': '/img/figure/cut/woman_back/skin01.gif',
                        'coords': '43,42,44,47,57,48,62,53,69,87,69,95,64,119,53,159,53,173,45,207,46,213,51,219,50,222,21,222,21,216,25,214,29,200,28,177,31,166,30,162,21,140,16,140,11,133,11,117,9,105,12,53,18,47,29,46,30,44,30,42,26,42,18,43,8,52,8,135,13,144,18,144,27,165,24,178,25,200,22,210,17,215,17,225,53,225,56,218,50,210,50,202,57,173,57,159,73,93,73,86,65,51,60,46',
                        'size': [62,182],
                        'position': [41,8],
                        'name': 'Pokožka',
                        'tag': 'pokozka'
                }));
                
            return this.figure;
        };

        var gndr = 0;
        var side = 0;
        var overlay = $('<img class="figure-overlay" usemap="#figure_map" src="/img/figure/blank.gif" alt=""/>');
        
        var figure = [
            [new woman_front(),new woman_back()],
            [new man_front(),new man_back()]
        ];
        
        function recursive_loader(obj) {
            var register = {
                'element': null,
                'listener': null
            }
            var objects = new Array();
            
            register.listener = $('<div class="'+obj.tag+' figure-bp" style="background:url('+obj.image+');top:'+obj.position[0]+'px;left:'+obj.position[1]+'px;width:'+obj.size[0]+'px;height:'+obj.size[1]+'px;"></div>');
            if(obj.coords != '') {
                register.controller = $('<area shape="'+obj.areaType+'" href="/domaci-lekar/?area='+obj.tag+'&amp;gender='+(!gndr ? 'zena' : 'muz')+'" coords="'+obj.coords+'">');
                register.controller.bind('mouseover',{object: obj},function(){
                    obj.view();
                }).bind('mouseout',{object: obj},function(e){
                    obj.hide(e);
                });
            }
            
            obj.listener = new Array();
            
            if (obj.bodyparts) {
                var bpart;
                obj.createMap();
                for(var i=0;i<obj.bodyparts.length;i++) {
                    bpart = recursive_loader(obj.bodyparts[i]);
                    
                    obj.map.append(bpart.controller);
                    register.listener.append(bpart.listener);

                    for (var o in bpart.objects) {
                        if (objects[o]) $.merge(objects[o],bpart.objects[o]);
                        else objects[o] = bpart.objects[o];
                    }
                }
                _self.append(obj.map);
            }
            
            obj.listener.push(register.listener);
            
            objects[obj.tag] = new Array(obj);
            register.objects = objects;
            return register;
        }
        
        function reload() {
            var element;
            _self.empty();

            bplist.each(function(){
                $(this).empty();
            });
            
            element = recursive_loader(figure[gndr][side]);
            element.listener.css('display','block');
            overlay.attr('usemap','#'+figure[gndr][side].tag+'_map')
                   .css('width',figure[gndr][side].size[0])
                   .css('height',figure[gndr][side].size[1]);

            _self.append(element.listener);
            _self.append(overlay);
            
            /*
             * vytvoreni vsech zaregistrovanych menu
             */
            for (var key in element.objects) {
                var name = 
                    (key == 'ruka' ? 'Ruce' : 
                    (key == 'noha' ? 'Nohy' :
                    (key == 'oci' ? 'Oči' :
                    (key == 'usi' ? 'Uši' : element.objects[key][0].name))));
                
                if  (key.indexOf('hlava') == -1 && key.indexOf('man') == -1)
                bplist.each(function(){
                    var obj = element.objects[key];
                    var li = $('<li></li>');
                    var a = $('<a></a>');
                    var i;
                
                    a.attr('href','/domaci-lekar/?area='+key+'&gender='+(!gndr ? 'zena' : 'muz'));
                    a.attr('class','more');
                    a.html(name);
                    
                    for(i=0;i<obj.length;i++) {
                        (function (n){
                            a.bind('mouseover',function(){
                                obj[n].view();
                            });
                        })(i);
                    }
                    
                    for(i=0;i<obj.length;i++) {
                        (function (n){
                            a.bind('mouseout',{object: obj[n], bubbles: true},function(e){
                                obj[n].hide(e);
                            });
                        })(i);
                    }
                
                    li.append(a);
                    $(this).append(li);
                
                    for(i=0;i<obj.length;i++)
                        obj[i].listener.push(a);
                });
            }
        }
        
        function toggle() {
            gndr = (gndr+1)%2;
            reload();
        }
        
        function turn() {
            side = (side+1)%2;
            reload();
        }
        
        $('.'+_self.attr('id')+'_turn').click(function(){
            turn();
            return false;
        });

        $('.'+_self.attr('id')+'_woman').click(function(){
            gndr = 0;
            reload();
            return false;
        });
        
        $('.'+_self.attr('id')+'_man').click(function(){
            gndr = 1;
            reload();
            return false;
        });
        
        reload();
    };
    
}) (jQuery);

