function window_open(url, win_width, win_height) {
	
	var screen_width = window.screen.width;
	var screen_height = window.screen.height;
	
	var win_left = screen_width / 2 - win_width / 2;
	var win_top = screen_height / 2 - win_height / 2;
	
	var win = window.open(url, "popup", "width=" + win_width + ", height=" + win_height + ", scrollbars=1, location=0, menubar=0, resizable=0, status=0, titlebar=0, toolbar=0, screenX=100, left=" + win_left + ", screenY=30, top=" + win_top);
 	
	win.focus();
	
}

function popupWindow(w, h) {
	
	var sw = window.screen.width;
	var sh = window.screen.height;
	
	var l = sw / 2 - w / 2;
	var t = sh / 2 - h / 2;
	
	window.resizeTo(w, h);
	window.moveTo(l, t);
	
}

var args = null;
var time = 0;

function checkheight(w, h) {
	
	if (self.innerHeight) {
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) {
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	if (x != w || y != w) {
		reposite();
	}
	
	clearTimeout(time);
	
}

function reposite() {
	
	if (args == null) {
		args = reposite.arguments;
	}
	
	var maxw = 0;
	
	for (var i = 0; i < args.length; i += 4) {
		
		// если это не нижняя картинка
		if (typeof args[i] == 'string' && args[i].substring(0, 4) != 'bot_') {
			maxw = args[i+3] > maxw ? args[i+3] : maxw;
			continue;
		}
		
		var pic = document.getElementById(args[i]);
		
		if (pic == null) {
			continue;
		}
		
		pic.style.top =  (document.getElementById('mtab').scrollHeight - pic.style.height.replace('px','') - args[i+2]) + 'px';
		pic.style.left = (document.getElementById('mtab').scrollWidth  - pic.style.width.replace('px','') - args[i+1]) + 'px';
		pic.style.display = 'block';
		
		// если нижняя картинка выползает наверх (маркер например)
		if (args[i+2] > 0) {
			maxw = args[i+3] > maxw ? args[i+3] : maxw;
		}
		
	}
	
	if (self.innerHeight) {
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) {
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	if (x > 1280) {
		x = 1280;
	}
	
	m_width = x - 400 - maxw;
	
	pic = document.getElementById('mainer');
	
	if (pic == null) {
		return;
	}
	
	pic.style.width = m_width + 'px';
	pic.style.display = 'block';
	
	time = setTimeout(checkheight, 10, x, y);
	
}

function reBallKill(who) {
	b1 = document.getElementById(who);
	b1.style.display = 'none';
}

function reBall(who) {
	b1 = document.getElementById(who);
	var flag  = 0;
	var count = 0;
	var q_top = 0;
	var q_left = 0;
	var ux = 0;
	var uy = 0;
	var dx = 0;
	var dy = 0;
	while((flag == 0) && (count<30)) {
		q_top =  287  + Math.round(Math.random(425)*(document.getElementById('balldiv').scrollHeight- 260));
		q_left = 390 + Math.round(Math.random(864)*(document.getElementById('balldiv').scrollWidth - 85));
		flag = 1;
		  for(k = 0; k<document.all.length; k++)
			if(document.all[k] != null)
				if((document.all[k]['id'] > '') && (document.all[k]['placeholder'] > 0))
				{
					t = document.all[k];
					t = document.getElementById(t.id);
					if(t.style.display == 'none')
						continue;

					ux = 365;
					uy = 87;

					if(t.id.substring(0,4) == 'ball' || t.placeholder > 1)
					{
						ux = (t.style.left.replace('px','')-0);
						uy = (t.style.top.replace('px','')-0);// + 75;
						dx = ux + (t.style.width.replace('px','')-0);
						dy = uy + (t.style.height.replace('px','')-0);
//                            alert(ux+' - '+dx);
//                            document.getElementById('ball_4').style.left = dx+'px';
//                            document.getElementById('ball_4').style.top  = dy+'px';
//                            document.getElementById('ball_4').style.display = 'block';
/*
						document.getElementById('ball_3').style.left = ux + 10;
						document.getElementById('ball_3').style.top = uy + 10;
						document.getElementById('ball_3').style.display = 'block'; 
						alert(t.id + ' = ' + ux + 'x' + dx + ' | ' + uy + 'x' + dy);                        
*/
//                            alert(t.id + ' = ' + ux + 'x' + dx + ' | ' + uy + 'x' + dy);
						
					}
					else
					{
						ux = ux + (t.offsetLeft-0);
						uy = uy + (t.offsetTop-0);
						dx = ux + (t.offsetWidth-0);
						dy = uy + (t.offsetHeight-0);
					}

					if(t.parentNode['id'].substring(0,4) == 'cell')
					{
						u = document.getElementById(t.parentNode['id']);
						ux = ux + u.offsetLeft;
						uy = uy + u.offsetTop;
						dx = dx + u.offsetLeft;
						dy = dy + u.offsetTop;
					}
					
					if( q_left > ux && q_left < dx && q_top > uy && q_top < dy )
						flag = 0;
					if( q_left + 50 > ux && q_left + 50 < dx && q_top + 50 > uy && q_top + 50 < dy )
						flag = 0;
				}
		count = count + 1;
	}
	if(flag > 0 && count<30)
	{
		b1.style.left = q_left;
		b1.style.top  = q_top;
		b1.style.display = 'block';
	}    
}

function move_prepare(who,what) {
	document.getElementById(who).tabIndex = what;
	if(what > 0) {
		document.getElementById(who).old_x = event.x - document.getElementById(who).style.left.replace('px','');
		document.getElementById(who).old_y = event.y - document.getElementById(who).style.top.replace('px','');
		move_ball(who);
	}
}

function move_ball(who) {
	if(document.getElementById(who).tabIndex > 0) {
		// if((event.x > 375) && (event.x < document.getElementById('mtab').scrollWidth-50))
		// if((event.y > 220) && (event.y < document.getElementById('mtab').scrollHeight - 280))           
		if((event.x - document.getElementById(who).old_x > 340) && (event.x - document.getElementById(who).old_x < document.getElementById('mtab').scrollWidth-50))
			document.getElementById(who).style.left = (event.x - document.getElementById(who).old_x - 0) + 'px';
		if((event.y -  document.getElementById(who).old_y > 0) && (event.y -  document.getElementById(who).old_y < document.getElementById('mtab').scrollHeight - 217))
			document.getElementById(who).style.top = (event.y -  document.getElementById(who).old_y - 0) + 'px';
	}
}

function kill_ball(who,died) {
	document.getElementById(who).border = '1';
	document.getElementById(who).style.display = 'none';
	document.getElementById(died).style.top  = (document.getElementById(who).style.top.replace('px','') - 28) + 'px';
	document.getElementById(died).style.left = (document.getElementById(who).style.left.replace('px','') - 0) + 'px';
	document.getElementById(died).style.display = 'block';
}

