function popBlock()
{	
	$(this)
		.show()
		.siblings()
			.hide()
	
	
}

function pop1block(which)
{
	var $tab = $(this).parent()
	var nodetype = $tab
		.get(0)
		.id
		.split('tab')[0]
	$('#'+nodetype+'block').trigger('pop')
	$tab
		.addClass('selected')
		.siblings()
			.removeClass('selected')
	return false

}

$(function() {
	$('.home')
		.bind('pop', popBlock)
	$('.tabs li a')
		.each(function() {
			if ($(this).text().length==1)	// &raquo;
				$(this).click(function() {return true})
			else
				$(this).click(pop1block)
			} )

	if (layout)
	{
		$('.tabs li')
			.not('[id*="blank"]')
			.draggable( {
				revert:'invalid',
				revertDuration: 300,
				helper:function() {
					return $(this)
						.clone()
						.width($(this).width())
						.css('border-bottom','thin gray solid')
						.css('text-decoration','none')
				},
				opacity: .8,
				cursor:'move'
				} )
			.each(function() {
				this.onselectstart = function() {return false;}
				} )
		$('.home')
			.droppable( {
				drop: function(event, ui) {
					var nodetype = ui.draggable.get(0).id.split('tab')[0]
					var $block = $('#'+nodetype+'block')
					var isOpen = $block.is(":visible")
					var sibs = $block.siblings().length
					
					if (sibs)
					{
						// if it was open, open another one
						if (isOpen)
							ui.draggable
								.siblings()
								.first()
								.children()
								.click()
					}
					else
					{
						$oldBlockGroup = $block.parent()
						$oldTabGroup = ui.draggable.parents('div.tabs')
					}
					
					if (this.id=='blankblock' || this.id=='blank2block')
					{
						// create a new block set
						$(this)
							.parent()
							.prev()
							.before('<div></div>')
							.prev()
							.append($block)
							// and create a new tab block
							.before('<div class=tabs><ul></ul></div>')
							.prev()
							.children()
							.append(ui.draggable)
						// pop new block open
						ui.draggable
							.children()
							.click()
							
					}
					else
					{
							
						// move tab
						$(this)
							.parent()
							.prev()
							.find('ul')
							.append(ui.draggable)
						// move block
						$block
							.insertAfter(this)
					}
						
					// if old block empty, remove
					if (!sibs)
					{
						$oldBlockGroup.remove()
						$oldTabGroup.remove()
						// it was selected, so we want it not to be anymore
						// unless we're starting a new block
						if (this.id!='blankblock' && this.id!='blank2block')
						{
							$block.hide()
							ui.draggable
								.removeClass('selected')
						}
					}
					
					// make block active
					ui.draggable
						.click()
					},
				accept: function(item) {
					if (
					$(this)
						.parent()
						.prev()
						.find('li')
						.length
						> 2) return false
					return true
					},
				hoverClass: 'highlighted'
				} )
	}
	} )

/*
var dragObject = null
var mouseOffset = null
var originalTab = null

// find the absolute position of an object
function findPosition(obj)
{
	var curleft = curtop = 0
	if (obj.offsetParent)
		do {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		} while (obj = obj.offsetParent)
	return [curleft, curtop]
}

// pass the object being clicked on and the event object, if any
function startDrag(item, event)
{
	return false	// replacing with jquery ui draggable
	e = event || window.event	// adjust for IE	
	
	// create a new, draggable object
	box = document.createElement('div')
	box.className = 'dragBox'
//	box.style.height = item.offsetHeight+'px'
	box.innerHTML = item.innerHTML
	pos = findPosition(item)
	box.style.left = pos[0]+'px'
	box.style.top = pos[1]+'px'
	document.body.appendChild(box)
	dragObject = box
	originalTab = item
	mouseOffset =[  mouseX(e) - pos[0], mouseY(e) - pos[1] ]
	document.onmousemove = mouseMove
	document.onmouseup = endDrag
	
}

// figure out which layout index contains the given block ID
function findIndex(id, column)
{
	pieces = id.split('block')	// just removes 'block' from the ID	
	if (typeof(pieces) == 'string')
	firstItem = pieces
	else firstItem = pieces[0]
	
	pieces = firstItem.split('tab')	// also remove 'tab' from the ID
	if (typeof(pieces) == 'string')
	type = pieces
	else type = pieces[0]
	
	col = tabs[column]
	returnValue=''
	for (k=0; k<col.length; k++)
	{
		row = col[k]
		for (j=0; j<row.length; j++)
			if (row[j]==type)
				return k
	}
	return -1 // only happens if not present in column
	
}

// this function I got from about.com, takes scrolling into account for various browsers
function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}

function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}

function mouseMove(e)
{
	e = e || window.event
	if (dragObject!=null) 
	{
		dragObject.style.display = 'block'
		x = mouseX(e)
		y = mouseY(e)
		dragObject.style.left = (x - mouseOffset[0])+'px'
		dragObject.style.top = (y - mouseOffset[1])+'px'
		
		// see if we've entered/left a block
		blocks = document.getElementsByTagName('div')
		for (i=0; i<blocks.length; i++)
			if (blocks[i].className=='capsule home')
			{
				pos = findPosition(blocks[i])
				leftEdge = pos[0]
				rightEdge = leftEdge + blocks[i].offsetWidth
				topEdge = pos[1]
				bottomEdge = topEdge + blocks[i].offsetHeight
				if (x > leftEdge && x < rightEdge && y > topEdge && y < bottomEdge)
				{
					if (leftEdge > 100) column=1 ; 
					else column=0
					index = findIndex(blocks[i].id, column)
					// see if already two tabs there
					if (tabs[column][index].length < 3) 
						// see if already current location
						if (index != findIndex(originalTab.parentNode.id, column))
						{
							blocks[i].style.backgroundColor = 'linen'
						}
				}
				else
					blocks[i].style.backgroundColor = 'white'
			}
	}
}

// fixes ID numbers and rebuilds "tabs" table
function renumberRows()
{
	tabs = new Array(new Array(), new Array())
	blocks = document.getElementsByTagName('div')
	rowCount = 0
	colCount = 0
	for (j=0; j<blocks.length; j++)
		if (blocks[j].className=='tabs')
		{
			if (findPosition(blocks[j]).shift() > 100)
			{
				if (colCount==0)
				{
					colCount = 1
					rowCount = 0
				}
			}
			blocks[j].id = colCount+'-'+rowCount
			// create new array for tabs table
			LIs = blocks[j].getElementsByTagName('li')
			newRow = new Array()
			for (q=0; q<LIs.length; q++)
			{
				li = LIs[q]
				newRow.push(li.id.split('tab').shift())
			}
			tabs[colCount].push(newRow)
			rowCount++
		}
}


function endDrag()
{
	
	// see if we let go in a block
	blocks = document.getElementsByTagName('div')
	for (i=0; i<blocks.length; i++)
		if (blocks[i].className=='capsule home')
			if (blocks[i].style.backgroundColor != 'white' && blocks[i].style.backgroundColor != '')
			{
				blocks[i].style.backgroundColor = 'white'
				// add the tab to this block
				column = 0
				row = findIndex(blocks[i].id, 0)
				if (row==-1)
				{
					row = findIndex(blocks[i].id, 1)
					column = 1
				}
				
				// prepare to move the tab
				tabDiv = document.getElementById(column+'-'+row)
				uls = tabDiv.getElementsByTagName('ul')
				ul = uls[0]
				origLI = originalTab.parentNode
				origUL = origLI.parentNode
				
				// create a new tab div if moved into a blank area
				if (tabs[column][row][0] == 'blank' || tabs[column][row][0] == 'blank2')
				{
					// set the id of the empty tab block to one row more
					prevTabDiv = tabDiv
					prevTabDiv.id = column+'-'+(row+1)

					// create new tab block
					tabDiv = document.createElement('div')
					tabDiv.className='tabs'
					tabDiv.id= column+'-'+row
					
					// create a ul inside
					ul = document.createElement('ul')
					tabDiv.appendChild(ul)
					
					// insert the new tab div before the "blank" one
					prevTabDiv.parentNode.insertBefore(tabDiv, prevTabDiv)
				}
				
				// choose first (or second) tab in old set if moved one was previously selected
				// first see what row it's in
				origCol = 0
				origRow = findIndex(origLI.id,0)
				if (origRow==-1)
				{
					origCol=1
					origRow = findIndex(origLI.id, 1)	
				}
				// now find original tab
				if (origLI.className.split('selected').length > 1 && origUL.getElementsByTagName('li').length > 1)
				{
					LIs = origUL.getElementsByTagName('li')
					if (LIs.item(0).id==origLI.id)
						popBlock(tabs[origCol][origRow][1])
					else
						popBlock(tabs[origCol][origRow][0])
				}
				

				// now move the tab
				origUL.removeChild(origLI)
				// add to new place in div
				ul.appendChild(origLI)
				if (!origUL.getElementsByTagName('li').length)
				{
					// remove the div which contains the UL
					origUL.parentNode.parentNode.removeChild(origUL.parentNode)
				}
				
				// move the content block
				strs = origLI.id.split('tab')
				ntype = strs[0]
				block = document.getElementById(ntype+'block')
				block.parentNode.removeChild(block)
				tabDiv.parentNode.insertBefore(block, tabDiv.nextSibling)
				
				// rearrange the global arrays
				renumberRows()
				
				// select just-moved tab
				popBlock(ntype)
				
				
			}

	// clean up dragging
	dragObject.parentNode.removeChild(dragObject)
	dragObject.style.display='none'
	dragObject = null
	mouseOffset = null
	document.onmousemove=null
	document.onmouseup=null
}
 */
 
function saveLayout()
{
	// assemble tab list
	var tabs = {
		"left": new Array,
		"right": new Array
	}
	
	$('.homepageleft .tabs')
		.not('.blank')
		.each( function() {
			tabs.left.push(new Array)
			$(this)
				.find('li')
				.not('[id*="blank"]')
				.each(function() {
					tabs.left[tabs.left.length-1]
						.push(this.id.split('tab')[0])
					} )
			} )
			
	$('.homepageright .tabs')
		.not('.blank')
		.each( function() {
			tabs.right.push(new Array)
			$(this)
				.find('li')
				.not('[id*="blank"]')
				.each(function() {
					tabs.right[tabs.right.length-1]
						.push(this.id.split('tab')[0])
					} )
			} )
			
	$('#layoutinput')
		.val(JSON.stringify(tabs))
		.parents('form')
		.submit() 

	return
	var input = document.getElementById('layoutinput')
	input.value = tabs[0].join('|') + '//' + tabs[1].join('|')
	input.form.submit()
}

function restoreDefault()
{
	var input = document.getElementById('layoutinput')
	input.value = 'default'
	input.form.submit()
}

