var tkc_forcehc = false;
var tkc_redirect = false;

function tkc_debug(msg)
{
	dbg = window.document.getElementById('debug');
	if (dbg)
	{
		dbg.innerHTML = msg + "<br />" + dbg.innerHTML;
	} 
}

function tkc_joinedfc()
{
	if (tkc_forcehc == 1)
	{
		var_hcrequested = 1; 
		rpc2_TKC_requestHC();
	}
}
function tkc_joinedchat()
{
	window.document.getElementById('cam_resize').style.display = 'block';
	window.focus();
}

function tkc_chatSysMessage(msgType) 
{
	tkc_debug('tkc_chatSysMessage: ' + msgType);
	switch(msgType)
	{
		case 'joined_hc':
			tkc_joinedchat();
			break;
		case 'joined_fc':
			tkc_joinedfc();
			tkc_joinedchat();
			break;

		case 'logout':
		case 'buy_coins':
		case 'listing':
		case 'kicked':
		case 'cam_offline':
			if (!window.document.getElementById('debug'))
			{
				if (!tkc_redirect)
				{
					top.location = top.location + '&action=info&reason='+msgType;
					tkc_redirect = true;
				}
			}
			else
			{
				tkc_debug = top.location + '&action=info&reason='+msgType;
			}
			break;

		case 'cam_in_hc':
			if (var_hcrequested == 1)
			{
				top.location = top.location + '&status=2';
			}
			else if (var_hcrequested == 0)
			{
				top.location = top.location + '&action=info&reason=hardcore';
			}
			break;

		case 'connection_failed':
			top.location.reload();
			break;
	}
}

function tkc_getVSID(vsid)
{
	tkc_debug('tkc_getVSID: ' + vsid);
	document.getElementById('cam_content').src = cam_content + vsid;
	document.getElementById('cam_content').style.display = 'block';
}

function tkc_close(url)
{
	try {
		rpc2_TKC_chatLogout();
	}
	catch(err) {}

	top.close(); 
	if (url)
	{
		opener.location = url;
	}
	else
	{
		opener.location.reload();
	}
	opener.focus();
}

function tkc_chatGetData(param)
{
	tkc_debug('tkc_chatGetData: ' + param);
	var params = param.split('::');
	var variable = params[0];
	var value = params[1];
	if (variable == 'getClientCoins')
	{
		if (value != '0' && value != '')
		{
			var el = document.getElementById('coins');
			if (el)
			{
				el.innerHTML = Math.round(value / 20 * cam_cpm);
			}
		}
		setTimeout("rpc2_TKC_getClientCoins()", 5000);
	}
}

function restorescreen()
{
	window.resizeTo(page_w + 20, page_h + 110);
	window.moveTo(Math.round((screen.width - page_w) / 2), Math.round((screen.height - page_h) / 2));
	window.focus();

	page = document.getElementById('centering');
	page.style.width = page_envelope;
	page = document.getElementById('content');
	page.style.width = page_content;

	cam = document.getElementById('cam_picture');
	cam.style.height = cam_pic_h;
	cam.style.width = cam_pic_w;
	cam_env = document.getElementById('cam_left');
	cam_env.style.width = cam_pic_w;

	chat = document.getElementById('cam_chat');
	chat.style.height = cam_chat_h;
	chat.style.width = cam_chat_w;

	document.getElementById('cam_link_full').style.display = 'inline';
	document.getElementById('cam_link_restore').style.display = 'none';
}

var page_w = getWindowWidth();
var page_h = getWindowHeight();
var page_x;
var page_y;
var page_envelope;
var page_content;
var cam_pic_w;
var cam_pic_h;
var cam_chat_w;
var cam_chat_h;
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

function fullscreen()
{
	full_w = screen.width;
	full_h = screen.height;
	old_w = getWindowWidth();
	window.resizeTo(full_w, full_h - 60);
	window.focus();
	if (getWindowWidth() == old_w && !is_chrome)
	{
		document.getElementById('cam_link_full').style.display = 'none';
		document.getElementById('cam_link_error').style.display = 'block';
		return;
	}
	if (is_chrome)
	{
		width = full_w - 60; // remove sidebars
		height = full_h - 60 - 80; // remove header and footer
	}
	else
	{
		width = getWindowWidth() - 60; // remove sidebars
		height = getWindowHeight() - 60 - 80; // remove header and footer
	}
	cam_width = width / 3 * 2;
	tkc_debug(cam_width);
	cam_height = cam_width / 4 * 3;
	if (cam_height > height - 150)
	{
		cam_height = height - 150;
		cam_width = cam_height / 3 * 4;
	}
	chat_width = width - cam_width - 40; 
	chat_height = height;
	
	page = document.getElementById('centering');
	page_envelope = page.style.width; 
	page.style.width = 'auto';
	page = document.getElementById('content');
	page_content = page.style.width;
	page.style.width = 'auto';

	cam = document.getElementById('cam_picture');
	cam_pic_w = cam.style.width;
	cam_pic_h = cam.style.height;
	cam.style.height = cam_height + 'px';
	cam.style.width = cam_width + 'px';
	cam_env = document.getElementById('cam_left');
	cam_env.style.width = cam_width + 'px';

	chat = document.getElementById('cam_chat');
	cam_chat_w = chat.style.width;
	cam_chat_h = chat.style.height;
	chat.style.height = cam_height + 'px';
	chat.style.width = chat_width + 'px';

	document.getElementById('cam_link_full').style.display = 'none';
	document.getElementById('cam_link_restore').style.display = 'inline';
}

function startUserCam()
{
	cam = document.getElementById('cam_user');
	cam.style.width = '215px';
	cam.style.height = '138px';
	document.getElementById('cam_link_cam').style.display = 'none';
}

function getWindowWidth()
{
	if(typeof(window.innerWidth) == 'number') 
	{
	    //Non-IE
	    w = window.innerWidth - 20;
  	} 
  	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) 
  	{
	    //IE 6+ in 'standards compliant mode'
    	w = document.documentElement.clientWidth;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
	    //IE 4 compatible
	    w = document.body.clientWidth;
  	}
	return w;
}

function getWindowHeight()
{
	if(typeof(window.innerHeight) == 'number') 
	{
	    //Non-IE
	    h = window.innerHeight - 20;
  	} 
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) 
	{
	    //IE 6+ in 'standards compliant mode'
    	h = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
	    //IE 4 compatible
	    h = document.body.clientHeight;
  	}
	return h;
}
