var SearchResults = {};

SearchResults._startX = 0;
SearchResults._resizeLineStartX = 0;
SearchResults._startWidth = 0;
SearchResults._resizeInProgress = false;
SearchResults._mouseMoveDelegate = null;
SearchResults._mouseUpDelegate = null;
SearchResults._resizeLine = null;
SearchResults._timerCookie = null;

SearchResults.Resize = function(height){
	
	var containerHeaderHeight = 22;
	var containerFooterHeight = 2;
	var containerTopMargin = 5;
	var containerBottomMargin = 2;
	if(!BaseSiteBlock.Resize("SearchResults", null, height))
		return null;
	//var cnt = document.getElementById("SearchResults_Container");
	//cnt.style.backgroundColor = "red";
	//alert(cnt.style.border);
	var separatorCell = document.getElementById("SearchResultsHorizontalSeparatorCell");
	var currentSettingIsHorizontal = separatorCell.parentNode.style.display != "none";
		
	var list = document.getElementById("ArticleList");
	if(typeof(list.SetHeight) == 'undefined')
		return;
	var view = document.getElementById("ArticleView");
	var neededHeight = height - containerHeaderHeight - containerFooterHeight - containerTopMargin - containerBottomMargin;
	
	if(currentSettingIsHorizontal){
		list.SetHeight(neededHeight / 2 - 9);
		view.style.height = (neededHeight / 2) +"px";
	}else{
		list.SetHeight(neededHeight);
		view.style.height = (neededHeight) +"px";
	}
	
	// cheat - relative елементите избягват при resize
	var cell = document.getElementById("LayoutOptionsCell");
	cell.firstChild.style.position = "static";
	cell.firstChild.style.position = "relative";
	cell = document.getElementById("ListOptionsCell");
	cell.firstChild.style.position = "static";
	cell.firstChild.style.position = "relative";
}

SearchResults.ResizeToFreeSpace = function(){
	var width = document.documentElement.clientWidth;
	var height = document.documentElement.clientHeight;
	var header = document.getElementById("Header");
	var searchPanel = document.getElementById("SearchPanel");
	var footer = document.getElementById("Footer");
	var content = document.getElementById("Content");
	
	//alert(footer.offsetHeight);
	//var contentHeight = height - parseInt(header.style.marginTop) - header.offsetHeight - searchPanel.offsetHeight - footer.offsetHeight;
	var contentHeight = height - parseInt(header.style.marginTop == "" ? "0" : header.style.marginTop) - header.offsetHeight - searchPanel.offsetHeight - 77;
	//alert(height +" - "+ header.offsetHeight +" - "+ searchPanel.offsetHeight +" - "+ footer.offsetHeight +" = "+ contentHeight);
	var marginTop = parseInt(content.style.marginTop);
	var resizeHeight = contentHeight - marginTop;
	if(resizeHeight < 100)
		resizeHeight = 100;
	SearchResults.Resize(resizeHeight);
}

SearchResults.Window_Resize = function(e){
	SearchResults.ResizeToFreeSpace();
}

Utilities.Events.RegisterEventHandler(window, "onresize", SearchResults.Window_Resize);

SearchResults.ItemSelected = function(sender, e){
	var value = sender.GetValue(e.Item);
	SearchResults.BeginDisplayView(value);
}

SearchResults.ItemDoubleClick = function(sender, e){
	var value = sender.GetValue(e.Item);
	ArticleView.OnArticlePopUp(value);
}

SearchResults.ItemKeyUp = function(sender, e){
	
	if(e.KeyCode == 13){
		var value = sender.GetValue(e.Item);
		ArticleView.OnArticlePopUp(value);
	}
}

SearchResults.BeginDisplayView = function(id){
	if(SearchResults._timerCookie != null)
		window.clearTimeout(SearchResults._timerCookie);
	SearchResults._timerCookie = window.setTimeout(new Function("SearchResults.DisplayView('"+ id +"')"), 300);
}

SearchResults.DisplayView = function(id){
	var view = document.getElementById("ArticleView");
	if(view == null)
		return;
	view.innerHTML = "<div style='text-align: center; padding-top: 30px; font-size: 20px; color: #888888;'>Loading Article...</div>";
	
	var HttpRequest = new CHttpRequest();
    HttpRequest.QueryString = "AjaxType=HTML&RequestFile=UI.ArticleView&MethodName=XRender";
    HttpRequest.AssyncCall = true;
    HttpRequest.AddProperty("Arguments[MaterialID]", id);
    HttpRequest.Additional = view;
    HttpRequest.OnResponse = SearchResults.DisplayView_Callback;
    HttpRequest.Send();
}

SearchResults.DisplayView_Callback = function(httpRequest, Additional){
	Ajax.LoadContent(Additional, httpRequest.responseText);
}

SearchResults.Body_MouseMove = function(e){
	if(!SearchResults._resizeInProgress)
		return;
	var mouseDelta = e.clientX - SearchResults._startX;
	SearchResults._resizeLine.style.left = (SearchResults._resizeLineStartX + mouseDelta) +"px";
}

SearchResults.Body_MouseUp = function(e){
	if(!SearchResults._resizeInProgress)
		return;
	SearchResults._resizeInProgress = false;
	var mouseDelta = e.clientX - SearchResults._startX;
	Utilities.Events.UnRegisterEventHandler(document.getElementsByTagName("html")[0], "onmousemove", SearchResults._mouseMoveDelegate);
	Utilities.Events.UnRegisterEventHandler(document.getElementsByTagName("html")[0], "onmouseup", SearchResults._mouseUpDelegate);
	var viewCell = document.getElementById("SearchResultsViewCell");
	viewCell.style.width = (parseInt(viewCell.style.width) - mouseDelta) +"px";
	var List = document.getElementById("ArticleList");
	List.ResizeHeaderScrollContainer();
	SearchResults._resizeLine.style.display = "none";
	
	SettingsManager.SaveSetting("SearchResults.ViewCellWidth", parseInt(viewCell.style.width));
}

SearchResults._mouseMoveDelegate = SearchResults.Body_MouseMove;
SearchResults._mouseUpDelegate = SearchResults.Body_MouseUp;

SearchResults.ResizeCell_MouseDown = function(e){
	Utilities.Events.RegisterEventHandler(document.getElementsByTagName("html")[0], "onmousemove", SearchResults._mouseMoveDelegate);
	Utilities.Events.RegisterEventHandler(document.getElementsByTagName("html")[0], "onmouseup", SearchResults._mouseUpDelegate);
	SearchResults._startX = e.clientX;
	var cell = document.getElementById("SearchResultsResizeCell");
	SearchResults.CreateResizeLine(cell);
	SearchResults._resizeLineStartX = parseInt(SearchResults._resizeLine.style.left);
	SearchResults._resizeInProgress = true;
}

SearchResults.CreateResizeLine = function(cell){
	if(SearchResults._resizeLine == null){
		SearchResults._resizeLine = document.body.appendChild(document.createElement("DIV"));
		SearchResults._resizeLine.style.display = "none";
		SearchResults._resizeLine.style.position = "absolute";
		SearchResults._resizeLine.style.width = "5px";
		SearchResults._resizeLine.style.zIndex = "100";
		SearchResults._resizeLine.style.backgroundColor = "black";
		SearchResults._resizeLine.style.cursor = "w-resize";
		SearchResults._resizeLine.onmousedown = new Function("return false");
		SearchResults._resizeLine.ondragstart = new Function("event.returnValue = false; return false;");
		Controls.UI.Effects.SetOpacity(SearchResults._resizeLine, 10);
		//this._resizeLine.style.width = "2px";
	}
	var coords = Utilities.DOM.GetCoordinates(cell);
	SearchResults._resizeLine.style.left = (coords[0] + cell.offsetWidth - 3) +'px';
	SearchResults._resizeLine.style.top = coords[1] +'px';
	SearchResults._resizeLine.style.height = cell.offsetHeight +'px';
	SearchResults._resizeLine.style.display = "block";
	//alert(this._resizeLine.outerHTML);
	
}

