// $Id: common.js,v 1.34 2007/01/12 11:56:44 sen Exp $

var undefined;

function gebi(id){
	return document.getElementById(id)
}

function jsquote(str){
	return str.replace(/'/g,'&#39;').replace(/>/g,'&gt;').replace(/</g,'&lt;').replace(/&/g,'&amp;') //'
}

function copyBoard(txt){
	if(document.body.createTextRange) {
		var d=document.createElement('INPUT')
		d.type='hidden'
		d.value=txt
		document.body.appendChild(d).createTextRange().execCommand("Copy")
		document.body.removeChild(d)
		return
	} else try {
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect')
		var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper)
		gClipboardHelper.copyString(txt)
	} catch (e) {}
}

function getCoords(obj){
	var o=typeof(obj) == 'string' ? gebi(obj) : obj
	var ret={'l':o.offsetLeft,'t':o.offsetTop,'w':o.offsetWidth,'h':o.offsetHeight}
	while(o=o.offsetParent){
		ret.l+=o.offsetLeft
		ret.t+=o.offsetTop
	}
	return ret
}
var waitFuncId=0
function waitObj(id,evFunc){
	if(document.getElementById){
		if(typeof evFunc=='function'){
			window['waitFunc'+waitFuncId]=evFunc
			evFunc='waitFunc'+waitFuncId
			waitFuncId++
		}
		var obj=(id=='body')?document.body:document.getElementById(id)
		if(obj) window[evFunc]()
		else setTimeout("waitObj('"+id+"','"+evFunc+"')",100)
	}else{
		onload=evFunc
	}
}

function preloadImages() {
	var d = document;
	if(!d._prImg) d._prImg = new Array();
	var i, j = d._prImg.length, a = preloadImages.arguments;
	for (i=0; i<a.length; i++) {
		d._prImg[j] = new Image;
		d._prImg[j++].src = a[i];
	}
}

// ==============================================================================

function showError(error) {
	return showMsg("error.php?error="+error, 'Îøèáêà')
}

function showMsg(url, title, w, h) {
	w=w||480
	h=h||300
	var win = top.window;
	if (win.showModelessDialog) {
		var sFeatures = 'dialogWidth:' + w + 'px; dialogHeight:' + h + 'px; center:yes; help:no; status:no; unadorned:yes; scroll:no;';
		return win.showModelessDialog("msg.html", {win: win, src: url, title: title}, sFeatures);
	} else {
		return win.open(url, "", 'width=' + w + ',height=' + h + ',location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
	}
}

function showUserInfo(nick) {
	var url = "user_info.php?nick="+nick;
	window.open(url, "", "width=915,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showArtifactInfo(artifact_id,artikul_id,set_id) {
	var url = "artifact_info.php";
	if (artifact_id) url += "?artifact_id="+artifact_id;
	else if (artikul_id) url += "?artikul_id="+artikul_id;
	else if (set_id) url += "?set_id="+set_id;
	window.open(url, "", "width=730,height=550,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function showFightInfo(fight_id) {
	var url = "fight_info.php?fight_id="+fight_id;
	window.open(url, "", "width=990,height=700,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes");
}

function showBotInfo(bot_id, artikul_id) {
	var url = "bot_info.php";
	if (bot_id) {
		if (bot_id > 1000000000) bot_id -= 1000000000;
		url += "?bot_id="+bot_id;
	}	
	else if (artikul_id) url += "?artikul_id="+artikul_id;
	window.open(url, "", "width=915,height=700,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
}

function userPrvTag() {
	var win = window
	try { win = dialogArguments || window } catch(e) {}
	while (win.opener) win = win.opener;
	if (win.closed) return;
	try {
		for (i=0; i<arguments.length; i++) win.top.frames['chat'].chatPrvTag(arguments[i]);
	}
	catch (e) {}	
}

function userToTag() {
	var win = window;
	try { win = dialogArguments || window } catch(e) {}
	while (win.opener) win = win.opener;
	if (win.closed) return;
	try {
		for (i=0; i<arguments.length; i++) win.top.frames['chat'].chatToTag(arguments[i]);
	}
	catch (e) {}	
}

function userAttack(nick, url_error) {
	var rnd = Math.floor(Math.random()*1000000000);
	var url_success = 'fight.php?'+rnd;
	var urlATTACK = 'action_run.php?code=ATTACK&url_success='+url_success+'&url_error='+escape(url_error||'area.php')+'&in[nick]='+nick;
	if (!nick) return;
	try {
		if (!top.frames["main_frame"].frames["main"].__fight_php__) top.frames["main_frame"].frames["main"].location.href = urlATTACK;
	}
	catch (e) {}
}

function botAttack(bot_id, url_error) {
	var rnd = Math.floor(Math.random()*1000000000);
	var url_success = 'fight.php?'+rnd;
	var urlATTACK = 'action_run.php?code=ATTACK_BOT&url_success='+url_success+'&url_error='+escape(url_error||'area.php')+'&bot_id='+bot_id;
	if (!bot_id) return;
	try {
		if (!top.frames["main_frame"].frames["main"].__fight_php__) top.frames["main_frame"].frames["main"].location.href = urlATTACK;
	}
	catch (e) {}
}

function huntAttack(bot_id) {
	botAttack(bot_id,'hunt.php');
}

function showHelpHint() {
	var win = top.window;
	if (win.showModelessDialog) {
		var sFeatures = 'dialogWidth:480px; dialogHeight:300px; center:yes; help:no; status:no; unadorned:yes; scroll:no;';
		win.showModelessDialog("help_hint.php", win, sFeatures);
	} else {
		win.open("help_hint.php", "", "width=480,height=300,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
	}
	return false;
}

function showAgent(email) {
	if (!top.agent_user) {
		window.open('http://agent.mail.ru/');
		return false;
	}
	return true;
}


function artifactAlt(obj, evnt, show) {
	var div = gebi(obj.getAttribute('div_id'));
	if (!div) return;
	var act1 = obj.getAttribute('act1');
	var act2 = obj.getAttribute('act2');
	if (show == 2) {
		document.onmousemove=function(e) {artifactAlt(obj, e||event, 1)} 
		div.style.display = 'block';
		if (act1 || act2) {
			if (obj.tagName == 'IMAGE') {
				obj.src = ("images/itemact-"+ act1) + (act2 +".gif")
			} else {
				obj.style.backgroundImage = 'url(' + ("images/itemact-"+ act1) + (act2 +".gif") + ')'
			}
		}
	}
	if (!show) {
		if (act1 || act2) {
			if (obj.tagName == 'IMAGE') {
				obj.src = "images/d.gif"
			} else {
				obj.style.backgroundImage = 'url(' + "images/d.gif" + ')'
			}
		}
		div.style.display = 'none';
		document.onmousemove=function(){}
		return;
	}
	
	var ex = evnt.clientX + document.body.scrollLeft;
	var ey = evnt.clientY + document.body.scrollTop;

	if (act1 || act2) {
		obj.style.cursor = 'default'
		obj.onclick = function(){showArtifactInfo(obj.getAttribute('aid'))}
		var coord = getCoords(obj)
		var cont = gebi("item_list")
		var rel_x = (ex + cont.scrollLeft - coord.l)
		if (rel_x >= 40) {
			var rel_y = (ey + cont.scrollTop - coord.t)
			if (act1 != 0 && rel_y < 20) {
				obj.onclick = function(){try{artifactAct(obj, act1)}catch(e){}}
				try{obj.style.cursor = 'hand'} catch(e){}
				try{obj.style.cursor = 'pointer'} catch(e){}
			}
			if (act2 != 0 && rel_y >= 40) {
				obj.onclick = function(){try{artifactAct(obj, act2)}catch(e){}}
				try{obj.style.cursor = 'hand'} catch(e){}
				try{obj.style.cursor = 'pointer'} catch(e){}
			}
		}
	}
	var x = evnt.clientX + div.offsetWidth > document.body.clientWidth - 7 ? ex - div.offsetWidth - 10 : ex + 10;
	var y = evnt.clientY + div.offsetHeight > document.body.clientHeight - 7 ? ey - div.offsetHeight - 10 : ey + 10;

	if (x < 0 ) {
		x = ex - div.offsetWidth/2
	}
	if (x < 7 ) {
		x = 7
	}
	if (x > document.body.clientWidth - div.offsetWidth - 7) {
		x= document.body.clientWidth - div.offsetWidth - 7
	}

	div.style.left = x;
	div.style.top = y;
}

function updateBag() {
	var win = window
	try { win = dialogArguments ?  dialogArguments.win || dialogArguments : window } catch(e) {}
	while (win.opener) win = win.opener;
	if (win.closed) return false;
	
	try{
		var win_main = win.top.frames['main_frame'].frames['main']
		if(win_main.is_userphp) {
				win_main.location.href = win_main.urlMODE + '&update_swf=1'
			return true;	
		}
	}
	catch (e) {}
	return false;
}
function updateSwf(params) {
	var win = window
	try { win = dialogArguments ?  dialogArguments.win || dialogArguments : window } catch(e) {}
	while (win.opener) win = win.opener;
	if (win.closed) return;
	
	var url = 'main_iframe.php?mode=update_swf';
	if (!params) return;
	try {
		for (i in params) {
	  		url += '&tar[]='+i;
	  		if (params[i]) url += '&add['+i+']='+escape(params[i]);
		}
		win.top.frames['main_frame'].frames['main_hidden'].location.href = url;
	}
	catch (e) {}
}

function updateHP() {
	updateSwf({'lvl': '' ,'items': ''});
}
function fightRedirect(fight_id) {
	var rnd = Math.floor(Math.random()*1000000000);
	var url = 'fight.php?'+rnd;
	if (top.__lastFightId && (top.__lastFightId >= fight_id)) return;
	top.__lastFightId = fight_id;
	location.href = url;
}

function updatePartyLoot() {
	try {
		top.frames['main_frame'].frames['main_hidden'].location.href = 'main_iframe.php?mode=update_party';
	} catch (e) {};
}

