/* params:
 
  gigya_chat_conf = {
		site, 
		stream, 
		cid, 
		width, 
		height,
		uid,
		userName,
		userProfileURL,
		userPhotoURL, 
		linkText, 
		linkURL, 
		style, 
		attributes, 
		render, 
		container, 
		admin,
		readOnly,
		twitterQuery
	}
 */
if (typeof gigya == "undefined") gigya = {};
gigya.chat = {
	
	setDef: function (obj, f, v) { 
		if (typeof obj[f]=="undefined" || obj[f]+""=="") 
			obj[f]=v;
	},
		
	getURLParams: function(conf)
	{
		var params = {};
		for (key in conf)
			params[key] = conf[key];
		
		delete params.width;
		delete params.height;
		delete params.style;
		delete params.attributes;
		delete params.container;
		delete params.render;
		delete params.admin;
		delete params.trace;
		delete params.readOnly;
		//delete params.server;
		
		return params;
	},
	
	getURL: function(conf)
	{
		var urlParams = this.getURLParams(conf);
		if (conf.admin)
			urlParams.a = 1;
		if (conf.trace)
			urlParams.t = 1;
		if (typeof conf.readOnly != "undefined")
		{
			if (conf.readOnly)
				urlParams.ro = 1;
			else
				urlParams.ro = 0;
		}
		
		this.setDef(conf,"server","livechat.gigya.com");
		var pa = new Array();
		for (var key in urlParams) 
		{
			if(urlParams[key] === undefined || urlParams[key] === null)continue
			pa.push(key+"="+escape(urlParams[key]));
		}
		//return "http://"+conf.server+"/chat.jsp?" + pa.join("&");
		var protocol = document.location.protocol;
		if(protocol == "file:")protocol = "http:"
		return protocol  + "//"+conf.server+"/chat.htm?" + pa.join("&");
	},

	show: function(conf)
	{
		if (typeof conf != "undefined")
		{
			var url = this.getURL(conf);

			this.setDef(conf, "width", 450);	
			this.setDef(conf, "height", 500);
			this.setDef(conf, "style", "");		
			this.setDef(conf, "attributes", "");
			
			var html = "<iframe scrolling='no' frameborder='0' style='width:"+conf.width+"px;height:"+conf.height+"px;"+conf.style+"' src='"+url+"' "+conf.attributes+"></iframe>";
			if (typeof conf.container != "undefined")
			{
				
				var container;
				if (typeof conf.container == "object")
					container = conf.container;
				else
					container = document.getElementById(conf.container);

				if (container!=null)
				{
					container.innerHTML = html;
				}
			} else
			{
				document.write(html);
			}
		}
	}	
};

if (typeof gigya_chat_conf != "undefined")
{
	gigya.chat.setDef(gigya_chat_conf,"render", true);
	if (gigya_chat_conf.render)
	{
		gigya.chat.show(gigya_chat_conf);
	}
}




