/// <reference path='~/jQuery/jQuery-vsdoc.js' />
var _cep = Object()

_cep.OnLoad = function() {
	_cep.SelectedMenu = $('.menu.clientList.sel')

	_cep.ClientList = Object()
	_cep.ClientList.Div = $('div#clientList')
	_cep.ClientList.Table = $('div#clientList table')
	_cep.ClientList.Scroll = $('div#clientList div.scroll-pane')
	_cep.ClientList.margin = 12
	_cep.ClientList.padding = 0 // includes the width of the scroll bar

	// Pre-load the client list images
	for(var idx in _cep.list)
	{
		var img = new Image()
		img.src = _cep.list[idx]
		_cep.list[idx] = img
	}

	_cei.LoadInitImages()
}

_cep.HidePage = function() {
	_cep.ClientList.Div.hide()
	_cei.HidePage()
}

_cep.DrawPage = function() {

	if (_cep.list.length != 0 && ! _cep.ClientList.positioned) {
		_cep.ClientList.positioned = true

		// Size and position the Client List
		_cep.ClientList.top = _mp.Frame.height - _mp.Footer.height + _cep.ClientList.margin
		_cep.ClientList.left = _mp.LogoMenu.right + _mp.Frame.padding
		_cep.ClientList.left = 259
		_cep.ClientList.width = _mp.Frame.width - _mp.Frame.padding - _cep.ClientList.left
		_cep.CreateClientListTable()
		_cep.ClientList.Div.css({ opacity: 1.0,
															top: _cep.ClientList.top + 'px',
															left: _cep.ClientList.left + 'px',
															width: _cep.ClientList.width,
															height: _cep.ClientList.height
														})

		_cep.ClientList.Div.show()
		_cep.ClientList.Scroll.css({width:  _cep.ClientList.width,
																height: _cep.ClientList.height })

		_cep.ClientList.Scroll.jScrollPane()

	}
	else if (_cep.list.length != 0)
	{
		_cep.ClientList.Div.show()
	}
	else
		$('.scroll').hide()
	_cei.Init(_cei)
	_cei.DrawImage()

	return _cep.SelectedMenu;
}

// Create a table for the client list.
_cep.CreateClientListTable = function() {
		// Calculate a column width by finding the maximum width of any item
		var columnWidth = 0
		for (var idx in _cep.list) {
			var width = _cep.list[idx].width
			if (columnWidth < width)
				columnWidth = width
		}

	// Calculate the grid dimensions
	var paddedWidth = _cep.ClientList.width - _cep.ClientList.padding
	var gridCols = Math.floor(paddedWidth / columnWidth)
	var gridRows = Math.ceil(_cep.list.length / gridCols)
	columnWidth = Math.floor(paddedWidth / gridCols)			// Space the columns to fill the area
	for (var rowIdx = 0; rowIdx < gridRows; rowIdx++) {
		var row = document.createElement('tr')
		for (var colIdx = 0; colIdx < gridCols; colIdx++) {
			idx = (gridRows * colIdx) + rowIdx
			if (idx < _cep.list.length) {
				var td = document.createElement('td')
				td.width = columnWidth
				td.appendChild(_cep.list[idx])
				row.appendChild(td)
			}
		}
		_cep.ClientList.Table.append(row)
	}
	
	// Calculate the maximumum number of rows that fit into the maximum height
	var rowHeight = _cep.list[0].height
	var maxHeight = _mp.Footer.height - (_cep.ClientList.margin * 2)
	var maxRows = Math.floor(maxHeight / rowHeight)
	_cep.ClientList.height = maxRows * rowHeight
}

