tree = {
    divWidth:154,
    divHeight:74,
    person:0,
    marriages:{},
    parents:{},
    children:{},
    ancestors:{},
    ancestorLink:'',
    padding:31,
    noPlaced:0,
    marrCount:0,
    marrLinePadding:10,
    /**
    * Adds a person to the tree
    * 
    * @param int id The id of the person you are adding to the tree.
    * @param int left Location from the left
    * @param int top Location from the top
    * @param boolean m True if person is male. false if they are not.
    */
    addPerson:function(id,left,top,m,marriage){
        if(m==null || m==true){
            var genderClass = 'male';
        }else{
            var genderClass = 'female';
        }
        $('#tree').append('<div id="p-'+id+'" class="person '+genderClass+'"></div>');
        $('#p-'+id).attr('style','left:'+left+'px;top:'+top+'px;');
        html =
            
            '<div class="name"><a href="'+this.ancestorLink+this.ancestors[id]['treeLink']+'">'+this.ancestors[id]['firstName']+' '+this.ancestors[id]['surname']+'</a></div>';
            if(this.ancestors[id]['img']){
                html += this.ancestors[id]['img'];
            }
            html += 
            '<ul>'+
                '<li><b>B</b>'+this.ancestors[id]['dob']+'</li>'+
                '<li><b>D</b>'+this.ancestors[id]['dod']+'</li>'+
            '</ul>'+
            '<div class="bottom"><a href="'+this.ancestors[id]['link']+'" target="_blank">More Info</a></div>';
        $('#p-'+id).append(html);
        this.noPlaced++;
    },
    /**
    * This is where the building of the tree is started
    */
    start:function(){
        //Get start possion of first person
        var startLeft = this.startLeft();
        var startTop = this.startTop();
        //Set gender
        if(this.ancestors[this.person]['gender']==1){
            var male = true;
        }else{
            var male = false;
        }
        //Add first person to tree.
        this.addPerson(this.person,startLeft,startTop,male);
        if(startTop>400){
            $('#tree').scrollTo( (startTop- 200), 800, {queue:true} );
        }
        //Adds marriages to family tree.
        var noMarriages = this.marriages.length;
        //Set the startTop of the origanl ancestor.
        var oriStart = startTop;
        if(noMarriages>0){
            //Calculates position of first marriage.
            startTop = startTop-this.divHeight-this.padding;
            //Adds each marriage to tree.
            $.each(this.marriages,
                function(k,id){
                    //Works out if current ancestor is male or female.
                    if(tree.ancestors[id]['gender']==1){
                        var male = true;
                    }else{
                        var male = false;
                    }
                    //Add ancestor to tree.
                    tree.addPerson(id,startLeft,startTop,male,true);
                    //Calculates postition of next marriage.
                    startTop = startTop-this.divHeight-this.padding;
                }
            );
        }
        startTop = oriStart;
        //GEt number of children
        var noChildren = this.children.length
        //Get new start location for children.
        startLeft = startLeft + this.divWidth + this.padding;
        startTop = startTop - this.childrenHeight()-1;
        if(noChildren>0){
            //Loop through each child and place it on the tree.
            $.each(this.children,function(k,id){
                if(tree.ancestors[id]['gender']==1){
                    var male = true;
                }else{
                    var male = false;
                }
                tree.addPerson(id,startLeft,startTop,male);
                startTop += (tree.padding + tree.divHeight);
            });
        }
        var noParents = this.parents.length
        //Get new start location for children.
        startLeft = startLeft - ((this.divWidth + this.padding) * 2);
        if(noParents == 1){
            startTop = this.startTop();
        }else{
            startTop = this.startTop() - ((this.divHeight + this.padding) / 2);
        }
        if(noParents>0){
            $.each(this.parents,function(k,id){
                if(id!=null){
                    if(tree.ancestors[id]['gender']==1){
                        var male = true;
                    }else{
                        var male = false;
                    }
                    tree.addPerson(id,startLeft,startTop,male);
                    startTop += (tree.padding + tree.divHeight);
                }
            });
        }
        this.addLines();
    },
    /**
    * Returns the left start posision of the first user.
    */
    startLeft:function(){
        var w = $('#tree').width();
        return ((w/2) - (this.divWidth/2));
    },
    /**
    * Returns the top start posision of the first user.
    */
    startTop:function(){
        var h = $('#tree').height();
        var start = (h/2) - (this.divHeight/2);
        childrenHeight = this.childrenHeight();
        if(childrenHeight > start){
            start = childrenHeight
        }
        marriagesHeight = this.marriagesHeight();
        if(marriagesHeight>start){
            start = marriagesHeight;
        }
        return start+1;
    },
    /**
    * Calculates the location the child should start.
    */
    childrenHeight:function(){
        var noChildren = this.children.length;
        var childrenHeight =((noChildren * this.divHeight) + ((noChildren-1) * this.padding)- (this.padding*2) -14) / 2;
        return childrenHeight;
    },
    marriagesHeight:function(){
        var noMarriages = this.marriages.length;
        var marriagesHeight = (noMarriages * this.divHeight) + (noMarriages * this.padding);
        return marriagesHeight;
    },
    addLines:function(){
        if(this.marriages.length>0){
            $.each(this.marriages,function(k,id){
                //alert("#p"+id);
                var pos = $("#p-"+id).position();
                var left = pos.left + (tree.divWidth/2) - (tree.marrLinePadding/2);
                var top = pos.top + tree.divHeight+1;
                tree.line(tree.padding,1,left,top);
                var left = pos.left + (tree.divWidth/2) + (tree.marrLinePadding/2);
                var top = pos.top + tree.divHeight+1;
                tree.line(tree.padding,1,left,top);
            });
        }
        if(this.children.length>0){
            //Children verticle line
            var left = this.startLeft() + this.divWidth + (this.padding/2);
            var top = this.startTop() - this.childrenHeight() + (this.divHeight/2);
            var width = 1;
            var height = this.childrenHeight()*2 + 2;
            this.line(height,width,left,top);
            //Children horizantle lines
            var pos = {};
            $.each(this.children,function(k,id){
                pos = $('#p-'+id).position();
                left = pos.left - (tree.padding/2);
                top = pos.top + (tree.divHeight/2);
                width = (tree.padding/2);
                height = 1;
                tree.line(height,width,left,top);
            });
            //Main person right line
            var left = this.startLeft() + this.divWidth +2;
            var top = this.startTop() + (this.divHeight/2);
            var width = (this.padding/2) -1;
            var height = 1;
            this.line(height,width,left,top);
        }
        if(this.parents.length>0){
            //Main person left line
            var left = this.startLeft() - (this.padding/2);
            var top = this.startTop() + (this.divHeight/2);
            var width = (this.padding/2);
            var height = 1;
            this.line(height,width,left,top);
            var i = 1;
            var vertTop;
            var vertBottom;
            $.each(this.parents,function(k,id){
                pos = $('#p-'+id).position();
                left = pos.left + tree.divWidth+1;
                top = pos.top + (tree.divHeight/2);
                width = (tree.padding/2)-1;
                height = 1;
                tree.line(height,width,left,top);
                if(i==1){
                    vertTop = pos.top + (tree.divHeight/2);
                }
                if(i==tree.parents.length){
                    vertBottom = pos.top + (tree.divHeight/2);
                }
                i++;
            });
            //Parents verticle line
            var left = this.startLeft() - (this.padding/2);
            var top = vertTop;
            var width = 1;
            var height = vertBottom - vertTop +1;
            this.line(height,width,left,top);
        }
    },
    line:function(height,width,left,top){
        $('#tree').append('<div class="line" style="height:'+height+'px;width:'+width+'px;left:'+left+'px;top:'+top+'px;"></div>');
        
    }
}