// pane.js

/***********************************************************************************************************************************
									PUBLIC (goes into API)
***********************************************************************************************************************************/
function MdSplitter_Pane (config)
{
	this.Log = new MdControlsNamespace.Log(config["logLevel"], true);
	
	this.ID = config["ID"];
	this.splitter = config["splitter"];
	this.Index = config["index"];
	this.indexInSplitItems = config["indexInSplitItems"];

	this.width  = config["width"];
	if (this.width.indexOf('px') > -1) 
	{
		this.width = parseInt(this.width, 10);
	}

	this.height = config["height"];
	if (this.height.indexOf('px') > -1) 
	{
		this.height = parseInt(this.height, 10);
	}
	
	this.originalDimensions = 
	{
		width  : this.width,
		height : this.height
	}
	
	if (config["originalWidth"] != null)
	{
		this.originalDimensions.width = config["originalWidth"];
	}
	
	if (config["originalHeight"] != null)
	{
		this.originalDimensions.height = config["originalHeight"];
	}

	this.persistScrollPosition = config["persistScrollPosition"];

	// init events
	this.supportedEvents = [
							"OnClientPaneCollapsed", 	  "OnClientPaneExpanded"	 , "OnClientPaneResized",
							"OnClientBeforePaneCollapse", "OnClientBeforePaneExpand", "OnClientBeforePaneResize"
							
							];
	for (var i=0; i< this.supportedEvents.length; i ++) 
	{
		this[this.supportedEvents[i]] = config[this.supportedEvents[i]]; 
	}
	
	this.contentUrl = config["contentUrl"];	
	
	this.minWidth = config["minWidth"];
	this.maxWidth = config["maxWidth"];

	this.minHeight = config["minHeight"];
	this.maxHeight = config["maxHeight"];
	
	this.initiallyCollapsed = config["initiallyCollapsed"];
	
	// when collapse is done its direction is kept
	// because after postback we should collapse with the
	// same splitbar
	this.collapsedDirection = config["collapsedDirection"];
	
	this.isCollapsed = false;
	this.expandedSize = 0;
	
	if (this.initiallyCollapsed) 
	{
		this.expandedSize = parseInt(config["expandedSize"]);
	}
	
	this.locked = config["locked"];

	this.stateString = "";

	this.childSplitter = null;
	if (config["childSplitterID"]) {
		this.childSplitter = eval(config["childSplitterID"]);
	}

	this.stateFieldId = config["stateFieldId"];
	this.enableScrolling = config["enableScrolling"];
	this.scrollLeft = config["scrollLeft"];
	this.scrollTop = config["scrollTop"];	
	
	this.lastCollapseDirection = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward;
	
	// extend with helper objects
	this.box     = window.MdControlsNamespace.Box;
	
	this.containerElement = document.getElementById('Md_SPLITTER_PANE_' + this.ID);
	
	this.contentContainerElement = document.getElementById('Md_SPLITTER_PANE_CONTENT_' + this.ID);
	
	/*
		Define proxy methods that will simplify the code and
		will speedup the control
		
		This is done so you do not have to write "If (splitter.IsVertical)? GetWidth : GetHeight", to get 
		the variable size of the pane.
		
	*/
	if (this.splitter.IsVertical()) 
	{
		this.getVarSize    	= this.GetWidth;
		this.setVarSize    	= this.SetWidth;
		this.getOrigVarSize = this.getOrigWidth;
		this.setOrigVarSize = this.setOrigWidth;
		this.getVarMinSize 	= this.GetMinWidth;
		this.getVarMaxSize 	= this.GetMaxWidth;
		this.getAvailIncreaseDelta = this.getWidthAvailIncreaseDelta;
		this.getAvailDecreaseDelta = this.getWidthAvailDecreaseDelta;
	}
	else 
	{
		this.getVarSize    	= this.GetHeight;
		this.setVarSize    	= this.SetHeight;
		this.getOrigVarSize = this.getOrigHeight;
		this.setOrigVarSize = this.setOrigHeight;
		this.getVarMinSize 	= this.GetMinHeight;
		this.getVarMaxSize 	= this.GetMaxHeight;
		this.getAvailIncreaseDelta = this.getHeightAvailIncreaseDelta;
		this.getAvailDecreaseDelta = this.getHeightAvailDecreaseDelta;
	}
	window[this.ID] = this;
}

MdSplitter_Pane.prototype.Init = function () 
{
	if (this.Inited) return;
		
	this.Log.Debug(["Pane.Init start ID[%d]", this.ID]);
	
	if (this.IsExternalContent())
	{
		this.setExternalContent(this.contentUrl);
	}

	// add the event handlers (for mover, mout, click)
	MdControlsNamespace.DomEventMixin.Initialize(this);

	var paneElement = this.GetContainerElement();
	
	var contentContainer = this.GetContentContainerElement();
	
	if (!this.IsExternalContent()) {
		this.AttachDomEvent(contentContainer, "scroll" , "OnScroll");
	}
	
	MdControlsNamespace.EventMixin.Initialize(this);

	var supportedEvents = this.supportedEvents;	
	
	paneElement = null;
	
	if (this.initiallyCollapsed) 
	{
		var self = this;
		var collapseFunc = function () {
			self.doInitialCollapse();
		}
		
		// run after the splitter is loaded so we are sure that all panes and bars are inited
		this.splitter.AttachEvent("OnClientLoaded", collapseFunc);		
	}
	
	if (this.enableScrolling && this.persistScrollPosition) 
	{
		var self = this;
		var scrollFunc = function ()
		{
			self.SetScrollPos(self.scrollLeft, self.scrollTop);
		}
		
		// run after the splitter is loaded so we are sure that all panes and bars are inited
		this.splitter.AttachEvent("OnClientLoaded", scrollFunc);
	}

	this.SaveState();
	this.Inited = true;
	this.Log.Debug(["Pane.Init end ID[%d]", this.ID]);
}


MdSplitter_Pane.prototype.SaveState = function () 
{	
	var hiddenField = document.getElementById(this.stateFieldId);
	var scrollPos = this.GetScrollPos();

	var stateObj = {
		"originalWidth"     : this.originalDimensions.width,
		"originalHeight"    : this.originalDimensions.height,
		"width"  			: this.GetWidth(),
		"height" 			: this.GetHeight(),
		"collapsed" 		: this.IsCollapsed(),
		"collapsedDirection" : this.lastCollapseDirection,
		"scrollLeft" 		: scrollPos.left,
		"scrollTop" 		: scrollPos.top,
		"expandedSize" 		: this.expandedSize,
		"contentUrl"		: this.GetContentUrl(),
	
		"minWidth" 			: this.minWidth,
		"maxWidth" 			: this.maxWidth,
		
		"minHeight" 		: this.minHeight,
		"maxHeight" 		: this.maxHeight,
		"locked" 			: this.locked
	}
	
	//return;	// TODO - use  the JSON.stringify only when neeeded - this is a very serious drawback ...

	var stateStr = MdControlsNamespace.JSON.stringify(stateObj);

	this.stateString = stateStr;
	hiddenField.value = stateStr;	
	
}

MdSplitter_Pane.prototype.GetState = function () 
{
	return this.stateString;
}

MdSplitter_Pane.prototype.Dispose = function () 
{	
	this.DisposeDomEventHandlers();
}

MdSplitter_Pane.prototype.Resize = function (delta, resizeDirection) 
{
	this.splitter.resizePanes(delta, this, resizeDirection);
}

MdSplitter_Pane.prototype.Print = function (arrCssFiles) 
{

	var sFeatures = 'width=' + this.GetWidth() + 'px, height=' + this.GetHeight() + 'px, scrollbars=1';
	var previewWndUrl  = (this.IsExternalContent()) ? this.GetContentUrl() : 'about:blank' ;
	var previewWnd = window.open(previewWndUrl, '', sFeatures, false);
	
	if (this.IsExternalContent())
	{
		try {	
			var t = function () {
				previewWnd.print();				
			}
			setTimeout(t, 1000); // wait until the content is loaded
			
		} catch(e) {}
		return;		
	}

	var styleStr = "";
	if (arrCssFiles) {
		styleStr = "<head>";
		
		for (var i=0; i < arrCssFiles.length; i ++) 
		{
			styleStr += "<link href = '" + arrCssFiles[i] + "' rel='stylesheet' type='text/css'></link>";	
		}
		styleStr += "</head>"; 
	}

	var popupContent = styleStr + "<body>" + this.GetContent() + "</body>"

	previewWnd.document.open();
	previewWnd.document.write(popupContent);
	previewWnd.document.close();

	previewWnd.print();
}


MdSplitter_Pane.prototype.Collapse = function (direction) 
{
	if (this.IsCollapsed()) return true;
 
	if (!direction) direction = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward;
	
	var splitBar = this.getTargetSplitBar(direction);
	
	var isCollapseSuccess = false; 
	if (splitBar != null) 
	{
		var collapseDirection = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward;
		if (splitBar.Index < this.Index) collapseDirection = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Backward;
		isCollapseSuccess = splitBar.collapseTargetPane(collapseDirection);
	}
	else 
	{
		isCollapseSuccess = this.splitter.collapsePane(this, direction);
	}
	
	return isCollapseSuccess;
}

MdSplitter_Pane.prototype.Expand = function (direction) 
{
	if (!this.IsCollapsed()) return true;
	
	if (!direction) direction = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward;
	
	var splitBar = this.getTargetSplitBar(direction);
	
	var isExpandSuccess = false;

	if (splitBar != null) 
	{
		var expandDirection = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward;
		if (splitBar.Index < this.Index) expandDirection = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Backward;

		isExpandSuccess = splitBar.collapseTargetPane(expandDirection);
	}
	else 
	{
		isExpandSuccess = this.splitter.expandPane(this, direction);
	}
	return isExpandSuccess;
	
}

MdSplitter_Pane.prototype.OnScroll = function () 
{
	if (this.persistScrollPosition) {
		this.SaveState();
	}
}

MdSplitter_Pane.prototype.SetContent = function (contentHtml) 
{
	
	if (this.IsExternalContent())
	{
		var contentContainer = this.GetContentContainerElement();
		contentContainer.style.overflow  = this.contentOverflow.overflow;
		contentContainer.style.overflowX = this.contentOverflow.overflowX;
		contentContainer.style.overflowY = this.contentOverflow.overflowY;
		this.contentUrl = null;
		this.SaveState();
	}
	
	var contentHolder = this.GetContentContainerElement();
	contentHolder.innerHTML = contentHtml;
}

MdSplitter_Pane.prototype.GetContent = function () 
{
	if (this.IsExternalContent()) return '';
	
	var contentHolder = this.GetContentContainerElement();
	return contentHolder.innerHTML;
}

MdSplitter_Pane.prototype.SetContentUrl = function (url) 
{
	
	this.setExternalContent(url);
	
	this.contentUrl = url;
	this.SaveState();
}

MdSplitter_Pane.prototype.GetContentUrl = function () 
{
	if (!this.IsExternalContent()) return "";

	return this.contentUrl;
	
}

MdSplitter_Pane.prototype.IsExternalContent = function () 
{
	return (this.contentUrl != null && this.contentUrl != '');
}

MdSplitter_Pane.prototype.SetMinWidth = function (minWidth) 
{
	this.minWidth = minWidth;
	this.SaveState();
}

MdSplitter_Pane.prototype.GetMinWidth = function () 
{
	var paneMinWidth = this.minWidth;
	
	var calculatedMinWidth = 0;
	if (this.IsSplitterContainer() && this.childSplitter.IsVertical()) 
	{
		calculatedMinWidth = this.childSplitter.GetMinWidth();
	} 

	return Math.max(paneMinWidth, calculatedMinWidth);
}

MdSplitter_Pane.prototype.SetMinHeight = function (height) 
{
	this.minHeight = height;
	this.SaveState();
}

MdSplitter_Pane.prototype.GetMinHeight = function () 
{
	var paneMinHeight = this.minHeight;

	var calculatedMinHeight = 0;
	if (this.IsSplitterContainer() && !this.childSplitter.IsVertical()) 
	{
		calculatedMinHeight = this.childSplitter.GetMinHeight();
	} 

	return Math.max(paneMinHeight, calculatedMinHeight);
}

MdSplitter_Pane.prototype.SetMaxWidth = function (width) 
{
	this.maxWidth = width;
	this.SaveState();
}

MdSplitter_Pane.prototype.GetMaxWidth = function () 
{
	var paneMaxWidth = this.getMaxSize(true);	
	this.Log.Debug(['Pane.GetMaxWidth maxWidth[%d], paneID[%d]', paneMaxWidth, this.ID]);

	return paneMaxWidth;
}

MdSplitter_Pane.prototype.SetMaxHeight = function (height) 
{
	this.maxHeight = height;
	this.SaveState();
}

MdSplitter_Pane.prototype.GetMaxHeight = function () 
{
	var paneMaxHeight = this.getMaxSize(false);
	this.Log.Debug(['Pane.GetMaxHeight maxHeight[%d], paneID[%d]', paneMaxHeight, this.ID]);

	return paneMaxHeight;
}


MdSplitter_Pane.prototype.SetWidth = function (width) 
{
	if (width < 0 || width == this.width) 
	{
		return;
	}
	this.setWidth(width);

	if (this.IsSplitterContainer()) 
	{
		this.childSplitter.SetWidth(this.GetInnerWidth());
	}
}

MdSplitter_Pane.prototype.SetHeight = function (height) 
{

	if (height < 0 || height == this.height) 
	{
		return;
	}
	
	this.setHeight(height);

	if (this.IsSplitterContainer()) 
	{
		this.childSplitter.SetHeight(this.GetInnerHeight());
	}
}

MdSplitter_Pane.prototype.GetInnerWidth = function (height) 
{
	return this.width;
	if (this.IsSplitterContainer())
	{
		return this.width;
	}
	 
	if (this.width > 2*this.splitter.panesBorderSize) return this.width - 2*this.splitter.panesBorderSize;

	return 0;
}

MdSplitter_Pane.prototype.GetInnerHeight = function (height) 
{
	return this.height;
	if (this.IsSplitterContainer())
	{
		return this.height;
	}

	if (this.height > 2*this.splitter.panesBorderSize) return this.height - 2*this.splitter.panesBorderSize;

	return 0;
}

MdSplitter_Pane.prototype.GetWidth = function () 
{
	return this.width;
}

MdSplitter_Pane.prototype.GetHeight = function () 
{
	return this.height;
}

MdSplitter_Pane.prototype.GetSplitter = function () 
{
	return this.splitter;
}

MdSplitter_Pane.prototype.IsSplitterContainer = function () 
{
	// if child splitter is defined and if it exist (maybe disposed with Ajax)
	return (this.childSplitter != null && document.getElementById('Md_SPLITTER_' + this.childSplitter.ID) != null)
}

MdSplitter_Pane.prototype.GetContainerElement = function () 
{
	return this.containerElement;
}

MdSplitter_Pane.prototype.GetContentContainerElement = function () 
{
	return this.contentContainerElement;
}

MdSplitter_Pane.prototype.GetExtContentContainerElement = function () 
{
	return this.extContentContainerElement;
}


MdSplitter_Pane.prototype.IsLocked = function () 
{
	return this.locked;
}

MdSplitter_Pane.prototype.Lock = function () 
{
	this.locked = true;
	this.SaveState();
}

MdSplitter_Pane.prototype.UnLock = function () 
{
	this.locked = false;
	this.SaveState();
}

MdSplitter_Pane.prototype.IsCollapsed = function () 
{
	return this.isCollapsed;
}

MdSplitter_Pane.prototype.IsExpanded = function () 
{
	return !this.IsCollapsed();
}

MdSplitter_Pane.prototype.ResetSize = function () 
{
	this.setWidth(this.originalDimensions.width);
	this.setHeight(this.originalDimensions.height);
}

MdSplitter_Pane.prototype.IsFixedSize = function () 
{
	return this.isFixedUnit(this.getVarSize());
}

MdSplitter_Pane.prototype.IsFreeSize = function () 
{
	var varSize = this.getVarSize(); 

	if (varSize == "")
	{
		return true;
	}
	
	return false;
}

MdSplitter_Pane.prototype.IsInitialFreeSize = function () 
{
	var varSize = this.getOrigVarSize(); 

	if (varSize == "")
	{
		return true;
	}
	
	return false;
}

MdSplitter_Pane.prototype.SetFreeSize = function () 
{
	if (this.splitter.IsVertical()) {
		this.originalDimensions.width = "";		
	}
	else
	{
		this.originalDimensions.height = "";		
	} 
}


MdSplitter_Pane.prototype.IsPercentSize = function () 
{
	var varSize = this.getVarSize(); 
	if (varSize.toString().indexOf('%') > -1)
	{
		return true;
	}
	
	return false;
}

MdSplitter_Pane.prototype.isFixedUnit = function (size) 
{
	if (!size || size.toString().indexOf('%') > -1)
	{
		return false;
	}
	return true;
}


MdSplitter_Pane.prototype.GetScrollPos = function ()
{
	var contentContainer = this.GetContentContainerElement();
	return {
		left : contentContainer.scrollLeft,
		top  : contentContainer.scrollTop		
	}
}

MdSplitter_Pane.prototype.SetScrollPos = function (scrollLeft, scrollTop)
{
	var contentContainer = this.GetContentContainerElement();
	contentContainer.scrollLeft = scrollLeft;
	contentContainer.scrollTop  = scrollTop;
}

/***********************************************************************************************************************************
									PRIVATE 
***********************************************************************************************************************************/

MdSplitter_Pane.prototype.getWidthAvailDecreaseDelta = function () 
{
	if (this.IsCollapsed() || this.IsLocked()) return 0;
	var delta = this.GetWidth() - this.GetMinWidth()
	this.Log.Debug(['Pane.getWidthAvailDecreaseDelta delta[%d] pane[%d]', delta, this.ID])
	return delta;
}

MdSplitter_Pane.prototype.getWidthAvailIncreaseDelta = function () 
{
	if (this.IsCollapsed() || this.IsLocked()) return 0;
	var delta = this.GetMaxWidth() - this.GetWidth();
	this.Log.Debug(['Pane.getWidthAvailIncreaseDelta delta[%d] pane[%d]', delta, this.ID])
	return delta;
}

MdSplitter_Pane.prototype.getHeightAvailDecreaseDelta = function () 
{
	if (this.IsCollapsed() || this.IsLocked()) return 0;
	return this.GetHeight() - this.GetMinHeight();
}

MdSplitter_Pane.prototype.getHeightAvailIncreaseDelta = function () 
{
	if (this.IsCollapsed() || this.IsLocked()) return 0;
	return this.GetMaxHeight() - this.GetHeight();
}

MdSplitter_Pane.prototype.setHeight = function (height)
{
	this.Log.Debug(['Pane.setHeight height[%d] pane[%d]', height, this.ID])
	this.setSize(height, false);
}

MdSplitter_Pane.prototype.setWidth = function (width)
{
	this.Log.Debug(['Pane.setWidth width[%d] pane[%d]', width, this.ID])
	this.setSize(width, true);
}

// a helper mthod to avoid code repeating
MdSplitter_Pane.prototype.setSize = function (value, isWidth)
{
	value = parseInt(value, 10);
	
	var propName = (isWidth) ? "width" : "height";
	
	if (isNaN(value) || value == this[propName]) return;
	
	value = Math.max(value,0);
	
	this[propName] = value;
	
	var contentContainer = this.GetContentContainerElement();
	
	var setValueMethod = (isWidth) ?  "SetOuterWidth" : "SetOuterHeight";
	var getInnerValueMethod = (isWidth) ?  "GetInnerWidth" : "GetInnerHeight";
	
	if (contentContainer) 
	{
		this.box[setValueMethod](contentContainer, this[getInnerValueMethod]());
		if (this.IsExternalContent())
		{
			var extContentContainer = this.extContentContainerElement;
			this.box[setValueMethod](extContentContainer, this[getInnerValueMethod]());	
		}
	}
	
	this.box[setValueMethod](this.GetContainerElement(), value);	
	
	this.SaveState();
}

MdSplitter_Pane.prototype.doInitialCollapse = function () 
{
	
	this.initialCollapseMode = true;
	this.Log.Debug(["Pane.doInitialCollapse ID[%s]", this.ID]);

	// find the right splitBar to execute the colllapsing
	var direction = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward;
	
	direction = this.collapsedDirection;
	var splitBar = this.getTargetSplitBar(direction);
	
	if (splitBar != null) 
	{
		
		var collapseDirection = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward;
		if (splitBar.indexInSplitItems < this.indexInSplitItems) collapseDirection = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Backward;
		this.Log.Debug(["Pane.doInitialCollapse [splitBar.collapseTargetPane] ID[%s]", this.ID]);
		splitBar.collapseTargetPane(collapseDirection);
	} 
	else
	{
		this.Log.Debug(["Pane.doInitialCollapse [splitter.collapsePane] ID[%s]", this.ID]);
		this.splitter.collapsePane(this, MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward);
	}
	
	this.initialCollapseMode = false;
}

MdSplitter_Pane.prototype.getOrigWidth = function ()
{
	return this.originalDimensions.width;
}

MdSplitter_Pane.prototype.getOrigHeight = function ()
{
	return this.originalDimensions.height;
}

MdSplitter_Pane.prototype.setOrigWidth = function (value)
{
	
	this.originalDimensions.width = value;
}

MdSplitter_Pane.prototype.setOrigHeight = function (value)
{
	this.originalDimensions.height = value;
}

MdSplitter_Pane.prototype.collapse = function (direction) 
{
	
	this.lastCollapseDirection = direction;
	
	this.GetContentContainerElement().style.display = 'none';
	
	if (this.splitter.IsVertical()) 
	{
		this.GetContainerElement().style.display = 'none';
	}
	else
	{ // collapse the TR
		document.getElementById('Md_SPLITTER_PANE_TR_' + this.ID).style.display = 'none';
		if (document.all && this.Index == 0) 
		{
			var splitBar = this.splitter.GetSplitBarByIndex(0);
			if (splitBar != null) 
			{
				splitBar.GetContainerElement().style.borderTop = "0px";
			}
		}
	} 
	
	this.isCollapsed = true;

	if (this.getVarSize() > 0 && (!this.initialCollapseMode || !this.expandedSize)) // little tricky but in case of initialCollapse we should ignore this 
	{
		this.expandedSize = this.getVarSize();
	}
	
	if (this.splitter.IsVertical()) 
	{
		this.width = 0;
	} 
	else 
	{
		this.height = 0;
	}
	this.SaveState();
}

MdSplitter_Pane.prototype.show = function () 
{
	this.GetContentContainerElement().style.display = '';

	if (this.splitter.IsVertical()) // collapse the TR 
	{
		this.GetContainerElement().style.display = '';
	}
	else
	{
		document.getElementById('Md_SPLITTER_PANE_TR_' + this.ID).style.display = '';	
	}

}

MdSplitter_Pane.prototype.expand = function (newSize) 
{
	
	this.show();	
	
	this.isCollapsed = false;
	
	this.setVarSize(newSize);

	// set the height to be the height of the splitter 
	// if the splitter has been resized while the pane was collapsed
	if (this.splitter.IsVertical()) 
	{
		this.setHeight(this.splitter.GetInnerHeight());
	}
	else
	{
		this.setWidth(this.splitter.GetInnerWidth());
	}
	
	this.callMdShow();
	this.Log.Debug(['Pane.expand newSize[%d], ID[%d]', newSize, this.ID]);
	this.SaveState();
}

MdSplitter_Pane.prototype.getTargetSplitBar = function (direction, doReverseSearch) 
{
	if (typeof(doReverseSearch) == "undefined") doReverseSearch = true;
	
	if (!direction) direction = MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward;
	
	var splitBarIndex = (direction == MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward) ? this.indexInSplitItems + 1 : this.indexInSplitItems - 1;
	this.Log.Debug(['Pane.getTargetSplitBar direction[%d], fwd[%d], splitBarIndex[%d], ID[%d]', direction,MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward, splitBarIndex, this.ID]);
	var splitBar = this.splitter.getSplitBarByItemIndex(splitBarIndex);
	
	if (splitBar != null && splitBar.IsCollapseDirectionEnabled(direction))
	{
		return splitBar;
	} 
	
	if (doReverseSearch)
	{
		// reverse the direction and search again
		direction = (direction == MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward) ? MdSplitterNamespace.Md_SPLITTER_DIRECTION.Backward : MdSplitterNamespace.Md_SPLITTER_DIRECTION.Forward;
		
		return this.getTargetSplitBar(direction, false);				
	}

	return null;	
}

MdSplitter_Pane.prototype.hideContent = function ()
{
	this.GetContentContainerElement().style.display="none";
	if (this.IsSplitterContainer())
	{
		var panes = this.childSplitter.GetPanes();
		for (var i = 0; i < panes.length; i ++)
		{
			panes[i].hideContent();
		}
	}
}

MdSplitter_Pane.prototype.showContent = function ()
{
	this.GetContentContainerElement().style.display="";
	if (this.IsSplitterContainer())
	{
		var panes = this.childSplitter.GetPanes();
		for (var i = 0; i < panes.length; i ++)
		{
			panes[i].showContent();
		}
	}
}

MdSplitter_Pane.prototype.callMdResize = function ()
{
	MdControlsNamespace.CallMdResize(this.GetContainerElement());
}

MdSplitter_Pane.prototype.callMdShow = function ()
{
	MdControlsNamespace.CallMdShow(this.GetContainerElement());
}

MdSplitter_Pane.prototype.setExternalContent = function (url) 
{

	var scrolling = (this.enableScrolling) ? "yes" : "no";
	var ffMarginBottom = (!document.all) ? "margin-bottom:-3px;" : "";
	var ifrmHtml = "<iframe id='Md_SPLITTER_PANE_EXT_CONTENT_" + this.ID + "' name='" + this.ID + "' src='" + url + "' frameborder=0 scrolling='" + scrolling + "' style='" + ffMarginBottom + "'></iframe>"
	this.GetContentContainerElement().innerHTML = ifrmHtml;	
			
	this.extContentContainerElement = document.getElementById('Md_SPLITTER_PANE_EXT_CONTENT_' + this.ID);
	
	var ifrmWidth = this.GetInnerWidth();
	var ifrmHeight = this.GetInnerHeight();

	if (this.isFixedUnit(ifrmWidth))
	{
		this.box.SetOuterWidth(this.extContentContainerElement, ifrmWidth);
	}

	if (this.isFixedUnit(ifrmHeight))
	{
		this.box.SetOuterHeight(this.extContentContainerElement, ifrmHeight );
	}

	var contentContainer = this.GetContentContainerElement();
	this.contentOverflow = {
		overflow  : contentContainer.style.overflow,
		overflowX : contentContainer.style.overflowX,
		overflowY : contentContainer.style.overflowY
	}

	contentContainer.style.overflow  = "hidden";
	contentContainer.style.overflowX = "hidden";
	contentContainer.style.overflowY = "hidden";
}

// a helper method to avoid code repeating
MdSplitter_Pane.prototype.getMaxSize = function (isWidth)
{
	var propName = (isWidth) ? "maxWidth" : "maxHeight"; 
	
	var paneMinMaxSize = (isWidth) ? this.maxWidth : this.maxHeight;

	var calculatedMaxSize = 0;
	if (this.IsSplitterContainer()) 
	{
		var skipChildRestrictions = false;
		
		/*
		 if there are locked panes in the nested splitter
		 do not count on them if they are in the opposite direction
		 e.g. if they are horizontal if we need to width
		*/
		if (isWidth && !this.childSplitter.IsVertical() ||
			!isWidth && this.childSplitter.IsVertical() )
		{
			skipChildRestrictions = true;
		}
		
		if (!skipChildRestrictions)
		{
			calculatedMaxSize = (isWidth) ? this.childSplitter.GetMaxWidth() : this.childSplitter.GetMaxHeight();
			if (calculatedMaxSize != null) 
			{
				if (paneMinMaxSize != null) 
				{
					paneMinMaxSize = Math.min(paneMinMaxSize, calculatedMaxSize);		
				} 
				else 
				{
					paneMinMaxSize = calculatedMaxSize;
				}
			}
		}		
	}

	return paneMinMaxSize;
}

//BEGIN_ATLAS_NOTIFY
if (typeof(Sys) != "undefined")
{
	if (Sys.Application != null && Sys.Application.notifyScriptLoaded != null)
	{
		Sys.Application.notifyScriptLoaded();
	}
}
//END_ATLAS_NOTIFY