var z5host = (("https:" == document.location.protocol) ? "https://" : "http://");
z5base = z5host + 'static.z5chat.com/';
z5build = '2794';
z5FpVersion = '9.0.124';

Include('swfobject.js');

z5chat = new Object();
z5chat.id = '';
z5chat.userId = '';
z5chat.userName = '';
z5chat.width = '100%';
z5chat.height = '100%';

z5chat.Show = function()
{
	var flashvars = { 
		z5ID:this.id, 
		userId:this.userId,
		userName:this.userName, 
		rootPath:z5base,
		app:'videochat/videochat.swf?build=' + z5build
	};
	
	var params = { allowscriptaccess:'always' };
	swfobject.embedSWF(z5base + 'flashcoms.swf?build=' + z5build,
			"videochat_swf", this.width, this.height, z5FpVersion, null, flashvars, params);
	document.write(MakeSwfHolder("videochat_swf"));
}

z5chat.OpenVideochatWindow = function(roomID, langID, width, height)
{
    var url = z5base + 'videochat/videochat.php?z5ID=' + this.id;
    url += '&userID=' + this.userId;
    url += '&userName=' + this.userName;
    if(roomID) url += '&roomID=' + roomID;
    if(langID) url += '&langID=' + langID;
    
    if(!width) width = 765;
    if(!height) height = 560;
    
    OpenWindow(url, 'z5chat', width, height, true);
}

z5chat.OpenVideochatFullscreen = function(roomID, langID)
{
	this.OpenVideochatWindow(userID, userName, roomID, langID, screen.width, screen.height);
}


function Include(lib)
{
	document.write(unescape("%3Cscript src='" + z5base + 'js/' + lib 
		+ "' type='text/javascript'%3E%3C/script%3E"));
}

function MakeSwfHolder(swfID)
{
	return '<div id="' + swfID + '">'
		+ '<p>You need to install or upgrade Adobe Flash Player.</p>'
		+ '<p>Version ' + z5FpVersion + ' or higher is required.</p>'
		+ '<p><a href="http://get.adobe.com/flashplayer/">Get Adobe Flash player</a></p>'
		+ '</div>';
}

function OpenWindow(url, name, width, height, isResizeAble)
{
    name = name.replace(/[^\w\d]/g, "_"); // added flag g
    name = name.replace(/undefined/, '_');
    name = name.replace(/\./, '_');
    
    url = encodeURI(url); // added to fix bug in IE 7 with usernames which has spaces
    
    var left = Math.round((screen.width - width) / 2);
    var top = Math.round((screen.height - height) / 2);
    var styleStr = MakeStyle(left, top, width, height, isResizeAble);
    
    var res = window.open(url, name, styleStr);
    return res;
}

function MakeStyle(left, top, width, height, isResizeAble)
{
    var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no';
    styleStr += ',resizable=' + (isResizeAble ? 'yes' : 'no');
    styleStr += ',width=' + width + ',height=' + height;
    styleStr += ',left=' + left + ',top=' + top;
    styleStr += ',screenX=' + left + ',screenY=' + top;
    return styleStr;
}

