// initial sizes
var leftPanelInitWidth = 230;
var leftPanelMinWidth = 50;
var rightPanelMinWidth = 100;
var sliderWidth = 3;
var collapsedSliderWidth = 5;
var collapseClickAreaWidth = 2;
var sliderMarginLeft = 5;

// variables for sliding panel
//var sliderInDragMode = false;
var doNotCollapse = true;
var canMoveSlider = true;
var sliderClickX;
var sliderPosX;
var leftPanelCurrentWidth = leftPanelInitWidth;
var mainbodyOnmousemoveOld;
var mainbodyOnmouseupOld;


/*function log(txt)
{
	document.getElementById('content').innerHTML += txt;
}*/

// reset position of separator.
function resetSlider(pos, newwidth)
{
	var slider = document.getElementById('slider');
	if (slider)
	{
		if (pos == null) {pos = leftPanelInitWidth;}  // no args - use default position
		slider.style.left = (pos + sliderMarginLeft) + 'px';
		document.getElementById('leftSide').style.width = pos + 'px';
		document.getElementById('content').style.left = pos + 'px';
		
		if (newwidth) // if defined new slider width, apply it
		{
			slider.style.width = newwidth + 'px';
			document.getElementById('content').style.paddingLeft = newwidth + 'px';
		}
	}
}

//   hide/show left panel
function toggleLeftCollapse()
{
	if (doNotCollapse) return;
	doNotCollapse = true;
	var leftPan = document.getElementById('leftSide');
	if (leftPan.style.display == 'none') // show Left Panel
	{
		//if (collapsed) {log('====== toggleLeftCollapse: olready collapsed ================'); return;}
		//log('toggleLeftCollapse: resetSlider('+ leftPanelCurrentWidth + ', ' + sliderWidth +');');
		resetSlider(leftPanelCurrentWidth, sliderWidth);
		//log(' - OK!<br />');
		//log('toggleLeftCollapse: leftPan.style.display = "block";');
		leftPan.style.display = 'block';
		//log(' - OK!<br />');
		canMoveSlider = true;
	}
	else // hide Left Panel
	{
		//log('toggleLeftCollapse: leftPan.style.display = "none";');
		leftPan.style.display = 'none';
		//log(' - OK!<br />');
		//log('toggleLeftCollapse: resetSlider(0, ' + collapsedSliderWidth +');');
		resetSlider(0, collapsedSliderWidth);
		//log(' - OK!<br />');
		canMoveSlider = false;
	}
}


function sliderMouseDown(event)
{
	doNotCollapse = false;
	sliderClickX = event.clientX;
	var slider = document.getElementById('slider');
	if (document.addEventListener)
	{
		document.addEventListener('mousemove', sliderMove, true);
		document.addEventListener('mouseup', sliderMouseUp, true);
	}
	else if (document.attachEvent)
	{
		slider.setCapture();
		slider.attachEvent('onmousemove', sliderMove);
		slider.attachEvent('onmouseup', sliderMouseUp);
		slider.attachEvent('onlosecapture', sliderMouseUp);
	}
	if (event.stopPropogation) event.stopPropogation();
	else event.cancelBubble = true;
	
	//log('sliderMouseDown: event.clientX =' + event.clientX + '<br />');
}

function sliderMove(e)
{
	if (!e) {e = window.event;}
	var newpos = e.clientX - sliderClickX + leftPanelCurrentWidth;
	if (Math.abs(e.clientX - sliderClickX) > collapseClickAreaWidth) doNotCollapse = true;
	if ((newpos >= leftPanelMinWidth) && (newpos <= document.getElementById('mainBody').offsetWidth - rightPanelMinWidth - sliderWidth) && canMoveSlider)
	{
		sliderPosX = newpos;
		document.getElementById('slider').style.left = newpos + 'px';
	}
	if (e.stopPropogation) e.stopPropogation();
	else e.cancelBubble = true;
	
	//log('sliderMove: e.clientX =' + e.clientX + ' sliderPosX=' + sliderPosX + ' leftPanelCurrentWidth=' + leftPanelCurrentWidth + '<br />');
}

function sliderMouseUp(e)
{
	if (!e) {e = window.event;}
	
	var slider = document.getElementById('slider');
	if (document.removeEventListener)
	{
		document.removeEventListener('mousemove', sliderMove, true);
		document.removeEventListener('mouseup', sliderMouseUp, true);
	}
	else if (document.detachEvent)
	{
		slider.detachEvent('onlosecapture', sliderMouseUp);
		slider.detachEvent('onmousemove', sliderMove);
		slider.detachEvent('mouseup', sliderMouseUp);
		slider.releaseCapture();
	}
	if (e.stopPropogation) e.stopPropogation();
	else e.cancelBubble = true;
	
	
	//log('sliderMouseUp: e.clientX =' + e.clientX + 'canMoveSlider=' + canMoveSlider);
	if (doNotCollapse)
	{
		if (canMoveSlider)
		{
			//log(' | moveslider...');
			resetSlider(sliderPosX);
			leftPanelCurrentWidth = sliderPosX;
			//log(' OK!<br />');
		}
	}
	else
	{
		//log(' | call collapse<br />');
		toggleLeftCollapse();
		//log('sliderMouseUp: toggleLeftCollapse() finished<br />');
	}
	
	
	

}