function redirect(command) {
	url = command;
	for (var i = 1; i < arguments.length; i++) {
		url += (url.indexOf('?') > 0 ? '&' : '?') + arguments[i];
	}
	document.location = url;
}

function reload() {
	document.location = document.location;
}

function getBodyWidth() {
	return document.body.offsetWidth;
	if (window.innerWidth) return window.innerWidth
	else return document.body.offsetWidth;
}
function getBodyHeight() {
	if (window.innerHeight) return window.innerHeight
	else return document.body.offsetHeight;
}

function site_size(){
	var width_max = 1200;
	var obj = document.getElementById('site_size');
	obj.style.width = '991px';
	var obj_width = (obj.offsetWidth);
	
	var body_width = getBodyWidth();
	var body_height = getBodyHeight();
	
	if (body_width > obj_width){
		if (body_width > width_max){
			obj.style.width = width_max + 'px';
			obj_width = width_max;
		} else {
			obj.style.width = body_width + 'px';
			obj_width = body_width;
		}
	}
	$('.site_size').css("width",obj_width + "px");
}
function site_size_forum(){
	var width_max = 1690;
	var obj = document.getElementById('site_size');
	obj.style.width = '991px';
	var obj_width = (obj.offsetWidth);
	
	var body_width = getBodyWidth();
	var body_height = getBodyHeight();
	
	if (body_width > obj_width){
		if (body_width > width_max){
			obj.style.width = width_max + 'px';
			obj_width = width_max;
		} else {
			obj.style.width = body_width + 'px';
			obj_width = body_width;
		}
	}
}

function divSetPositionAndShow(obj, x, y) {
	var width = (obj.offsetWidth);
	var left = (5 + x + width > getBodyWidth()) ? getBodyWidth() - width + document.body.scrollLeft - 5 : x + document.body.scrollLeft + 10;
	with (obj) {
		style.left = left;
		style.top = (15 + y + (obj.offsetHeight != 0)?obj.offsetHeight:270 > getBodyHeight()) ? getBodyHeight() - (obj.offsetHeight != 0)?obj.offsetHeight:270 + document.body.scrollTop - 20 : y + document.body.scrollTop - 10;
		style.display = 'block';
	}
}

function placeInCenter(obj) {
	placeInTopLeft(obj);
	obj.style.left = Math.max(0, parseInt(getBodyWidth()/2) + document.body.scrollLeft - parseInt(obj.style.width.replace('px', '')/2));
	obj.style.top = Math.max(0, parseInt(getBodyHeight()/2) + document.body.scrollTop - parseInt(obj.style.height.replace('px', '')/2));
}
function placeInTopLeft(obj) {
	obj.style.left = 0;
	obj.style.top = 0;
}

function dialog(text, dialogType, yesClick, focusObject,auctionId,yesWindowLocation) {

	if (this.isShow) this.hide();
	
	var body = document.createElement("div");
	var head = document.createElement("div");
	var textField = document.createElement("div");
	
	var yesButton = document.createElement("input");
	var noButton = document.createElement("input");
	var cancelButton = document.createElement("input");

	body.className = 'dialog';
	body.id = 'dialog'+auctionId;
	body.style.width = "335px";
	body.style.height = "225px";

	placeInCenter(body);

	head.className = "dialogHead";
	head.innerHTML = 'Auctions';

	textField.className = "dialogMessage";
	
	yesButton.type = "button";
	yesButton.className = 'dialogButton';
	yesButton.value = (dialogType == 'confirm') ? "yes" : "OK";

	noButton.type = "button";
	noButton.className = (dialogType == 'confirm') ? 'dialogButton' : 'nullClass';
	noButton.value = "no";

	cancelButton.type = "button";
	cancelButton.className = (dialogType == 'confirm') ? 'dialogButton' : 'nullClass';
	cancelButton.value = "cancel";

	function setEvent(element, event, callBack) {
		if (element.addEventListener) {
			element.addEventListener(event, callBack, false);
		} else if (element.attachEvent) {
			element.attachEvent('on' + event, callBack);
		} else {
			element['on' + event] = callBack;
		}
	}

	if (yesClick) {
		yesButton.onclick = yesClick;
	} 
	if (yesWindowLocation) {
	    yesButton.onclick = function(){window.location = yesWindowLocation;	};
	}
	setEvent(yesButton, "click", function() {
		if (focusObject && focusObject != '') focusObject.focus();
		body.style.display = "none";
		this.isShow = false;
		return true;
	});

	noButton.onclick = function() {
		if (focusObject && focusObject != '') focusObject.focus();
		body.style.display = "none";
		this.isShow = false;
		return false;
	}
	cancelButton.onclick = function() {
		if (focusObject && focusObject != '') focusObject.focus();
		body.style.display = "none";
		this.isShow = false;
		return false;
	}

	if (text) {
		textField.innerHTML = '<center><table border="0" height="90px;" width="80%" cellspacing="0" cellpadding="0"><tr><td class="dialogMessage_table" valign="top" style="vertical-align:top;">'+text+'</td></tr></table></center>';
	}

	this.hide = function() {
		if (focusObject && focusObject != '') focusObject.focus();
		this.body.style.display = "none";
		this.isShow = false;
	}

	this.body = body;
	body.appendChild(head);
	body.appendChild(textField);
	
	body.appendChild(yesButton);
	body.appendChild(noButton);
	body.appendChild(cancelButton);


	document.body.appendChild(body);
	
	if (dialogType == 'confirm') {
		cancelButton.focus();
	} else {
		yesButton.focus();
	}
	
	this.isShow = true;
}

function popup(title, width, height,won ) {
	if (this.isShow) this.hide();
	
	var body = document.createElement("div");
	var head = document.createElement("div");
	var content = document.createElement("div");
	var closeButton = document.createElement("input");
	
	body.className = 'popup';
	body.style.width = (width) ? width+"px" : "600px";
	body.style.height = (height) ? height+"px" : "450px";
	placeInCenter(body);
	
	if (title) {
		headHTML = '';
		headHTML += "<table border=\"0\" cellpadding=\"6\" cellspacing=\"0\" style=\"width: 100%;\" align=\"center\">";
		headHTML += "<tr><td colspan=\"3\" style=\"padding: 6px\" id=\"tdTitle\" class=\"caption\">"+ title +"</td>";
		headHTML += "<td id=\"tdCloseButton\" align=\"right\" valign=\"top\"><input type=\"button\" class=\"button\" value=\"Close\" onclick=\"javascript: hide()";
if (won == true) {headHTML += "; document.getElementById('won').style.display = 'block'"; }
		headHTML += ";\"></td></tr></table>";
		head.innerHTML = headHTML;
	}
	
	content.align = "center";
	
	this.hide = function() {
		this.body.style.display = "none";
		this.isShow = false;
	}
	this.setPopupContent = function(cont) {
		content.innerHTML = cont;
	}

	this.isShow = true;
	
	this.body = body;
	
	document.body.appendChild(body);
	if (title) body.appendChild(head);
	body.appendChild(content);
}

function highBidder(auctionId, page) {
	if (!page) page = 1;
	ajax.sendRequest('buyer.HighBidder', function(response) {
		//alert(response);
		if (response.status == 'OK') {
			setPopupContent(response.table);
		} else {
			//TODO handle error
		}
	}, {
		'auctionId' : auctionId,
		'page' : page
	});
}
