$(document).ready(function() {
	fitToContentHeight();
	copyColumnWidth();		

	if(/MSIE 6/i.test(navigator.userAgent))
		fixIE6Layout();
	
	//setTimeout(5000, fitToContentHeight);
});

/*** auctions ***/
function copyColumnWidth() {
	var partsTds = $("#Parts td.date");
	if(partsTds.length == 0)
		return;

	$("#Preview td.date").css({width: $(partsTds).width()});
}

/*** catalogue ***/
function clickButton(button) {
	eval(button.href.replace("javascript:", ""));
}

var textBox, searchButton, resetButton;
function setupSearchHandlers(textBoxID, searchButtonID, resetButtonID) {
	textBox = get(textBoxID);
	searchButton = get(searchButtonID);
	resetButton = get(resetButtonID);
	textBox.onkeypress = function(evt) {
		if(evt == null)
			evt = event;
		if(evt.keyCode == 13)
			clickButton(getRelevantButton());
		else
    		setTimeout(switchButtons, 0);
    	return true;
   }
}

function startSearch(str) {
	textBox.value = str;
	clickButton(searchButton);
}

function getRelevantButton() {
	return (isResetButtonRelevant() ? resetButton : searchButton);
}

function isResetButtonRelevant() {
	if(textBox.value.length == 0)
		return (textBox.defaultValue.length != 0);
	else
		return (textBox.value == textBox.defaultValue);	
}

function switchButtons() {
	var reset = isResetButtonRelevant();
	resetButton.style["display"] = (reset ? "" : "none");
	searchButton.style["display"] = (reset ? "none" : "");
}

/*** all pages ***/
function fitToContentHeight() {
	var main = get("Main");
	var content = get("ContentColumn");
	if(!main || !content || get("DisableFitToContent"))
		return;
	
	var nodes = content.childNodes;
	var height = 0;	
	for(i = 0; i < nodes.length; i++) {
		if(nodes[i].nodeType == 1)
			height = Math.max(height, nodes[i].offsetTop + nodes[i].offsetHeight);
	}
	
	//is div#ContentColumn empty?
	if(height == 0)
		return;

	//ensure minimal height of 400px
	height = Math.max(400, height);

	main.style["height"] = (content.offsetTop + height + 100) + "px";
}

function fixIE6Layout() {
	fitHeightToParentBottom("Frame", 40);
	fitHeightToParentBottom("Left");
	fitHeightToParentBottom("ContentColumn");
	moveToParentBottom("Footer");
}

/*** utility functions ***/
function reloadDocument() {
	window.location.href = window.location.href;
}

function get(id) {
	return document.getElementById(id);
} 

function fitHeightToParentBottom(id, margin) {
	if(!margin)
		margin = 0;

	var el = get(id);
	var par = el.parentNode;
	
	el.style["height"] = (par.offsetHeight - el.offsetTop - margin) + "px";
}

function moveToParentBottom(id, margin) {
	if(!margin)
		margin = 0;

	var el = get(id);
	var par = el.parentNode;
	
	el.style["top"] = (par.offsetHeight - el.offsetHeight - margin) + "px";
}

function openFile(fileName) {
	win4490 = open(fileName,"","width=400,height=500,left=300,top=250,dependent=no,location=no,toolbar=no,menubar=no,resizable=no,scrollbars=yes");
}
