$(document).ready(function(){
	var objBio = new Bio('divbio')
	objBio.init()
	objBio.resizeY(objBio.min_height)
	$('.togglebio').click(function(){
		objBio.clickHandler()
		return false
		}
	)
})

// Biography object
function Bio(id)
{
	this.id = id
	this.min_height
	this.num_chars
	this.characters_per_line = 100
	this.line_height_px = 16
	this.target_height = 96
	this.state = 'min'
	this._debug = false
	
	this.init = function()
	{
		this.calcNumChars()
		this.calcMinHeight()
		if(this._debug) console.log(this)
	}
	
	this.clickHandler = function()
	{
		if(this.state === 'min')
		{
			this.state = 'max'
			this.resizeY('100%')
			$('#showlink').hide()
			$('#hidelink').show()
		}
		else
		{
			this.state = 'min'
			this.resizeY(this.min_height)
			$('#showlink').show()
			$('#hidelink').hide()
		}
	}
	
    this.calcNumChars = function()
    {
    	this.num_chars = $('#' + this.id).html().length
    }
	
    this.resizeY = function(height_px)
    {
    	if(this._debug) console.log('resizeY height_px:' + height_px)
    	$('#'+this.id).height(height_px)
    	if(this.min_height < this.target_height)
    	{
			$('#divbiolinks').hide()
		}
    }
    
    this.calcMinHeight = function()
    {
    	if(this._debug) console.log('calcMinHeight')
    	if(this.hasImage())
    	{
	    	if(this._debug) console.log('calcMinHeight:hasImage:TRUE')
    		this.min_height = this.target_height
    	}
    	else
    	{
	    	if(this._debug) console.log('calcMinHeight:hasImage:FALSE')
    		this.min_height = (this.num_chars / this.characters_per_line) * this.line_height_px
    	}
    	
    	if(this.min_height < this.line_height_px)
    	{
	    	if(this._debug) console.log('calcMinHeight:less than line height')
    		this.min_height = this.line_height_px * 1
    	}
    	
    	if(this.min_height > this.line_height_px * 7)
    	{
	    	if(this._debug) console.log('calcMinHeight:greater than line height')
    		this.min_height = this.line_height_px * 7
    	}
    	
    	if(this._debug) console.log('this.min_height:' + Math.round(this.min_height))
    	this.min_height = Math.round(this.min_height + 16)
    }
    
    this.hasImage = function()
    {
    	var imgExists = ($('#' + id + ' img').height() > 1 && $('#' + id + ' img').width() > 1)
    	if(this._debug) console.log('hasImage:' + imgExists)
    }
}
    
