function showBusy() {
	var busy = document.getElementById('busy');
	var hg = window.innerHeight;
	var wg = window.innerWidth;
	busy.style.top = document.body.scrollTop + ((window.innerHeight) ? window.innerHeight : ((document.all) ? document.body.offsetHeight : 0)) / 2 - 15;
	busy.style.left = document.body.scrollLeft + ((window.innerWidth) ? window.innerWidth : ((document.all) ? document.body.offsetWidth : 0)) / 2 - 15;
	busy.style.visibility = 'visible';
}
function hideBusy() {
	var busy = document.getElementById('busy');
	busy.style.visibility = 'hidden';
}
function ReceiveData (req, btn) {
	if (req.readyState == 4) {
		hideBusy();
		if (req.responseJS.debug) {
/*			if (document.getElementById('debug'))
				document.getElementById('debug').innerHTML = '<pre>' + htmlspecialchars(req.responseText, 'ENT_QUOTES') + '</pre>';
			else*/
				alert(req.responseText);
		}
		if (req.responseJS.error != '')
			alert('Error:\n' + req.responseJS.error);
		else {
			if (req.responseJS.alert) {
				alert(req.responseJS.alert);
			}
			if (req.responseJS.closeCount) {
				closeCont();
			}
			if (req.responseJS.view) {
				for (var i = 0; i < req.responseJS.view.length; i ++) {
					if (req.responseJS.view[i] && req.responseJS.view[i].container && document.getElementById(req.responseJS.view[i].container)) {
						var container = document.getElementById(req.responseJS.view[i].container);
						if (req.responseJS.view[i].inner || req.responseJS.view[i].inner == '' || req.responseJS.view[i].inner == 0) {
							container.innerHTML = req.responseJS.view[i].inner;
							document.getElementById(req.responseJS.view[i].container).parentNode.appendChild(document.getElementById(req.responseJS.view[i].container));
							if (req.responseJS.view[i].scrolltop && document.body.scrollTop > req.responseJS.view[i].scrolltop)
								document.body.scrollTop = req.responseJS.view[i].scrolltop;
							if (req.responseJS.view[i].focusid && document.getElementById(req.responseJS.view[i].focusid))
								document.getElementById(req.responseJS.view[i].focusid).focus();
						}
						else if (req.responseJS.view[i].value || req.responseJS.view[i].value == '' || req.responseJS.view[i].value == 0)
							container.value = req.responseJS.view[i].value;
					}
				}
			}
			if (req.responseJS.referer) {
				if (req.responseJS.referer != '.') {
					window.location.href = req.responseJS.referer;
				}
				else {
					location.reload(true);
				}
			}
		}
		if (btn)
			btn.style.visibility = 'visible';
	}
}
function SendData(op, btn, conf, formid, QuotedText) {
	if (conf)
		answerconf = confirm(conf);
	if (!conf || answerconf) {
		var req = new JsHttpRequest();
		if (btn)
			btn.style.visibility = 'hidden';
		showBusy();
		req.onreadystatechange = function () {
			ReceiveData(req, btn);
		}
		req.open(null, p_http + 'opbg.php?op=' + op, true);
		if (document.getElementById('formtosend') && !formid)
			req.send(document.getElementById('formtosend'));
		else if (formid && document.getElementById(formid))
			req.send( document.getElementById(formid) );
		else if (QuotedText) {
			req.send( {quoted: "" + QuotedText + ""});
		}
		else
			req.send(null);
	}
}
function hideSelectors(object, flg) {
	for (var childItem in object.childNodes) {
		if (object.childNodes[childItem]) {
			if (object.childNodes[childItem].nodeName == "SELECT")
				object.childNodes[childItem].style.visibility = flg;
			else
				hideSelectors(object.childNodes[childItem], flg);
		}
	}
}
function fadeBody(p) {
	hideSelectors(document.getElementById('body'), "hidden");
	var div = document.createElement('div');
	div.id = 'divbg';
	div.className = "showbg";
	div.style.left = '0';
	div.style.top = '0';
	div.style.height = p[1];
	div.onclick = function() { closeCont(); }
	document.body.appendChild(div);
}
function unfadeBody() {
	hideSelectors(document.getElementById('body'), "visible");
}
function closeCont() {
	unfadeBody();
	document.body.removeChild(document.getElementById('divbg'));
	document.body.removeChild(document.getElementById('showcontainer'));
}
function showSS(key, arr, nofade) {
	showBusy();
	var p = getPageSize();
	if (!nofade) {
		fadeBody(p);
		var div = document.createElement('div');
		div.id = 'showcontainer';
		div.className = "showss";
	}
	else {
		var div = document.getElementById('showcontainer');
		div.style.visibility = 'hidden';
	}
	div.innerHTML = "<div class=formhdr><span>" + ((key > 0) ? "<a onclick=\"showSS(" + (key - 1) + ", ['" + arr.toString().replace(/,/g,"','") + "'], 1);\"><img src=\"./img/show_prev_black.png\"></a> " : "<img src=\"./img/show_prev_gray.png\">") + " " + ((key < arr.length - 1) ? "<a onclick=\"showSS(" + (key + 1) + ", ['" + arr.toString().replace(/,/g,"','") + "'], 1);\"><img src=\"./img/show_next_black.png\"></a> " : "<img src=\"./img/show_next_gray.png\">") + "</span> <img src=\"./img/close.png\" onclick=\"closeCont();\" style=\"cursor: pointer;\"></div>";
	var img = new Image();
	img.id = "showimage";
	div.appendChild(img);
	img.onload = function() {
		this.parentNode.style.left = Math.ceil((p[2] - this.width) / 2 - 5);
		this.parentNode.style.top = Math.ceil((p[3] - this.height) / 2 - 5 + document.body.scrollTop);
		this.parentNode.style.visibility = 'visible';
		hideBusy();
	}
	img.src = p_http + p_ss + arr[key] + '.jpg';
	img.onclick = function() { closeCont(); }
	if (!nofade)
		document.body.appendChild(div);
}
function showYT(key, arr, nofade) {
	showBusy();
	var p = getPageSize();
	if (!nofade) {
		fadeBody(p);
		var div = document.createElement('div');
		div.id = 'showcontainer';
		div.className = "showss";
	}
	else {
		var div = document.getElementById('showcontainer');
		div.style.visibility = 'hidden';
	}
	div.style.left = Math.ceil((p[2] - 390) / 2);
	div.style.top = Math.ceil((p[3] - 310) / 2 + document.body.scrollTop);
	div.innerHTML = "<div class=formhdr><span>" + ((key > 0) ? "<a onclick=\"showYT(" + (key - 1) + ", ['" + arr.toString().replace(/,/g,"','") + "'], 1);\"><img src=\"./img/show_prev_black.png\"></a> " : "<img src=\"./img/show_prev_gray.png\">") + " " + ((key < arr.length - 1) ? "<a onclick=\"showYT(" + (key + 1) + ", ['" + arr.toString().replace(/,/g,"','") + "'], 1);\"><img src=\"./img/show_next_black.png\"></a> " : "<img src=\"./img/show_next_gray.png\">") + "</span> <img src=\"./img/close.png\" onclick=\"closeCont();\" style=\"cursor: pointer;\"></div>";
	var embed = document.createElement('embed');
	embed.type = 'application/x-shockwave-flash';
	embed.src = 'http://www.youtube.com/v/' + arr[key];
	embed.quality = 'high';
	embed.wmode = 'transparent';
	embed.width = 380;
	embed.height = 300;
	div.appendChild(embed);
	document.body.appendChild(div);
	hideBusy();
}
function  getPageSize(){
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	return [pageWidth,pageHeight,windowWidth,windowHeight];
}
function noimg(obj) {
	obj.src = p_http + p_img + "no_img.png";
}
function getReview(appid, id) {
	var p = getPageSize();
	fadeBody(p);
	var div = document.createElement('div');
	div.id = 'showcontainer';
	div.className = "newreview";
	div.style.left = Math.ceil((p[2] - 600) / 2);
	div.style.top = Math.ceil((p[3] - 310) / 2 + document.body.scrollTop);
	document.body.appendChild(div);
	SendData('getreview&appid=' + appid + '&id=' + ((id) ? id : ''));
}
function btnSearch() {
	var val = document.getElementById('search').value;
	if (val)
		SendData('search&text=' + val);
}

