﻿var myusername = "", mynickname = "", mychatcookie;
var timeouts = new Object();
var main_timeout = null;
var lastids = new Object();
var auth_cookie;
var service;

//var baseurl = "http://10.0.0.4/PlatformCommon/";
//var basechatservice = "http://10.0.0.4:8080/ChatService/";
//var loginurl = "http://10.0.0.39:7080/accounts/accounts/";

var baseurl = "http://media.fourhooks.ro/bar/";
var basechatservice2 = "http://chat2.fourhooks.ro/ChatService/";
var basechatservice = "http://chat.fourhooks.ro/ChatService/";
var loginurl = "http://accounts.fourhooks.ro/";

var sessionkey;

var loaded = false;

String.prototype.startsWith = function(s) {
    return (this.length >= s.length && this.substr(0, s.length) == s);
};

String.prototype.endsWith = function(s) {
    return (this.length >= s.length && this.substr(this.length-s.length, s.length) == s);
};

function user_click(e)
{
	var dest_username = e.target.getAttribute("username");
	var dest_displayname = e.target.getAttribute("displayname");
	//alert( dest_username);
	var room = dest_username;
	if( !document.getElementById( "chat_popup_"+room))
		start_chat( dest_displayname, room);
	open_popup( "_"+room);
}

function close_bar()
{
	close_all_popups();
	$("#platform_bar").remove();
	clearTimeout( main_timeout);
}

function users_loaded(o)
{
	if( !o)
	{
		alert( "Please login!");
		close_bar();
		return;
	}
	var html = "";
	for( var i = 0; i < o.length; i++)
	{
		var username = o[i].split('/')[0];
		var displayname = o[i].split('/')[1];
		var pos1 = o[i].indexOf('/');
		var userstatus = o[i].substring(o[i].indexOf('/',pos1+1)+1);
		if( userstatus != "")
			html += "<a class=\"user\" username=\""+username+"\" displayname=\""+displayname+"\">"+displayname+" ("+userstatus+")</a>";
		else
			html += "<a class=\"user\" username=\""+username+"\" displayname=\""+displayname+"\">"+displayname+"</a>";
	}
	if( o.length == 0)
		html = "<div class=\"none\">Nu este nimeni online</div>";
	$('#chat_popup_onlineusers').html( html);
	$('#chat_popup_onlineusers a.user').bind("click", user_click);
}

function document_click(e)
{
	if (!e && event) e = event;
	if ( !e.target && e.srcElement ) {
	   e.target = e.srcElement;
	}
	
	var elem = e.target;
	
	do {
		if( !elem)
			break;
		if( (elem.id+"").indexOf( "chat_popup_") == 0 || (elem.id+"").indexOf( "chatconv_") == 0)
			return;
		elem = elem.parentNode;
	} while(elem != document.body);
							  
	close_all_popups();
}

function close_all_popups()
{
	// look for an open popup
	$("a.chat_link").each( function(i) { 
		if( $(this).is(".extended"))
		{
			var suffix = this.id.substring( "chat_link".length);
			//clearTimeout( timeouts[suffix.substring(1)]);
			$(this).removeClass( "extended");
			$("#chat_popup"+suffix).hide(); 
			$(document).unbind("click");
		}
	});
}

function chat_contents_loaded(o)
{
	if( typeof console != "undefined")
		console.log( "GetChatContents finished");
	var destusername = o.room;
	if( !lastids[destusername] || o.lastid > lastids[destusername])
	{
		var conv = document.getElementById("chatconv_"+destusername);
		var scrolledBottom = (conv.scrollTop == (conv.scrollHeight - conv.offsetHeight));
	 
 		var html = "";
 		var authors = $("span.username", conv);
		var olddisplayname = authors.size() > 0 ? $(authors.get(authors.size()-1)).text() : "";
		for( var i = 0; i < o.messages.length; i++)
		{
			var displayname = o.messages[i].substring( 0, o.messages[i].indexOf( ':'));
			var message = o.messages[i].substring( o.messages[i].indexOf( ':')+1);
			if( displayname != olddisplayname)
				html += "<div><span class=\"username\">"+displayname+"</span>: "+message+"</div>";
			else
				html += "<div>"+message+"</div>";
			olddisplayname = displayname;
		}
		conv.innerHTML = conv.innerHTML+html;	
		
		// scroll to bottom
		if( scrolledBottom)
			conv.scrollTop = conv.scrollHeight - conv.offsetHeight;
			
		var firsttime = !lastids[destusername];
		lastids[destusername] = o.lastid;
		
		if( !is_popup_open( "_"+destusername))
		{
			if( !destusername.startsWith("main") || !firsttime)
				$("#chat_link_"+destusername).addClass( "unread");
		} else {
			var data = "{\"srcuser\":\""+myusername+"\",\"destuser\":\""+destusername+"\",\"lastid\":\""+lastids[destusername]+"\",\"cookie\":\""+mychatcookie+"\"}";
			$.getJSON( basechatservice+"MarkConversationRead?msg="+escape(data)+"&method=?", null);
		}
	} 
	timeouts[destusername] = setTimeout( "get_chat_messages(\""+destusername+"\");", 100);
}

function get_chat_messages( room)
{
//	var lastid = 0;
//	if( lastids[room])
//		lastid = lastids[room];
//	if( typeof console != "undefined")
//		console.log( "GetChatContents...");
//	$.getJSON( basechatservice+"GetChatContents/"+sessionkey+"/"+myusername+"/"+room+"/"+lastid+"/"+mychatcookie+"?method=?", chat_contents_loaded);
}

function send_chat_message( e)
{
	var myid = e.target.id;
	var destuser = myid.substring( myid.indexOf( "_")+1);
	var destname = $("#chat_link_"+destuser).text();
	var tosend = $("#chatinput_"+destuser)[0].value;
	if( tosend != "")
	{
		tosend = tosend.replace( /\"/g, "\\\"");
		var data = "{\"srcuser\":\""+myusername+"\",\"destuser\":\""+destuser+"\",\"message\":\""+tosend+"\",\"cookie\":\""+mychatcookie+"\"}";
		$.getJSON( basechatservice+"PostMessage?msg="+escape(data)+"&method=?", null);
		$("#chatinput_"+destuser)[0].value = "";
	/*	if( timeouts[destuser])
			clearTimeout( timeouts[destuser]);
		get_chat_messages( destuser);	   */
	}
}

function recompute_popup_offsets()
{
	var offsetx = 16;
	$("a.chat_link").each( function(i) { 
		if( !$(this).hasClass("left"))
		{
			var suffix = this.id.substring( "chat_link".length);
			document.getElementById("chat_popup"+suffix).style.right = offsetx+"px";
			offsetx += this.clientWidth+2; 
		}
	} );
}

function close_chat( room)
{
	if( timeouts[room])
		clearTimeout( timeouts[room]);
	if( lastids[room])
		delete lastids[room];
	$("#chat_popup_"+room).remove();
	$("#chat_link_"+room).remove();
	
	recompute_popup_offsets()
}

function start_chat( displayname, room)
{
	if( !add_popup_button( displayname, "_"+room))
		return;
	
	var chat_window = document.getElementById("chat_popup_"+room);
	
	if( !room.startsWith("main"))
	{
		var chat_close = document.createElement( "div");
		chat_close.setAttribute( "class", "close_button");
		chat_close.appendChild( document.createTextNode( "X"));
		$(chat_close).bind( "click", function( e) { close_chat( room); });
		chat_window.appendChild( chat_close);
	}
	
	var chat_conversation = document.createElement( "div");
	chat_conversation.setAttribute( "id", "chatconv_"+room);
	chat_conversation.setAttribute( "class", "conversation");
	chat_window.appendChild( chat_conversation);
	
	var chat_input = document.createElement( "input");
	chat_input.setAttribute( "type", "text");
	chat_input.setAttribute( "class", "input_text");
	chat_input.setAttribute( "id", "chatinput_"+room);
	chat_window.appendChild( chat_input);
	$(chat_input).bind( "keydown", chat_input_key);
	
	var chat_send = document.createElement( "input");
	chat_send.setAttribute( "type", "button");
	chat_send.setAttribute( "class", "input_button");
	chat_send.value = "Scrie";
	chat_send.setAttribute( "id", "chatsend_"+room);
	chat_window.appendChild( chat_send);
	$(chat_send).bind( "click", send_chat_message);
	
	lastids[room] = 0;
	
	get_chat_messages(room);
}

function chat_input_key( e)
{
	if( e.keyCode == 13)
		send_chat_message( e);
}

function is_popup_open( suffix)
{
	return $("#chat_link"+suffix).hasClass( "extended");
}

function findPos(obj) {
	var curleft = curtop = 0;
	do {
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
	} while (obj = obj.offsetParent);
	return [curleft,curtop];
}

function getWindowSize()
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth,myHeight];
}

function open_popup( suffix)
{
	if( suffix == "_chat_signin")
	{
		if( myusername == "")
		{
			var domain = /:\/\/([^\/]+)/.exec(window.location.href)[1];
			if( service == "coolio" || service == "urbee" || service == "metropotam")
				window.location = loginurl+"?destination="+escape(window.location)+"&service="+service;
			else
				window.location = "http://"+domain+"/login_html?destination="+escape(window.location);
			return;
		}
		close_chat( "chat_signin");
		change_status( "");
		start_chat_windows();
		return;
	}
	
	close_all_popups();
	$("#chat_popup"+suffix).show(); 
	$("#chat_link"+suffix).addClass( "extended");
	$(document).bind("click", document_click);
	$("#chatinput"+suffix).focus();
	var conv = document.getElementById("chatconv"+suffix);
	if( conv)
		conv.scrollTop = conv.scrollHeight - conv.offsetHeight;
	// mark as read
	var destusername = suffix.substring( 1);
	if( destusername != "onlineusers" && destusername != "add_stuff" && destusername != "status")
	{
		var data = "{\"srcuser\":\""+myusername+"\",\"destuser\":\""+destusername+"\",\"lastid\":\""+lastids[destusername]+"\",\"cookie\":\""+mychatcookie+"\"}";
		$.getJSON( basechatservice+"MarkConversationRead?msg="+escape(data)+"&method=?", null);
		$("#chat_link"+suffix).removeClass( "unread");
		recompute_popup_offsets();
	}
}

function add_popup_button( displayname, suffix)
{
	if( !document.getElementById( "platform_bar"))
		return false;

	var offsetx = 16;
	$("a.chat_link").each( function(i) { offsetx += $(this).hasClass("left") ? 0 : this.clientWidth+2; } );
	if( offsetx + 70 > document.getElementById( "platform_bar").clientWidth-210)
		return false;

	var chat_link_div = document.createElement( "a");
	chat_link_div.appendChild( document.createTextNode( displayname));
	chat_link_div.setAttribute( "class", "chat_link");
	chat_link_div.setAttribute( "id", "chat_link"+suffix);
	document.getElementById("platform_bar_inner").appendChild( chat_link_div);
	$(chat_link_div).bind("click", function(e) { 
		var suffix = e.target.id.substring( "chat_link".length);
		open_popup( suffix);
		return false;
	} );
	
	var chat_popup = document.createElement( "div");
	chat_popup.setAttribute( "id", "chat_popup"+suffix);
	chat_popup.setAttribute( "class", "chat_popup");
	chat_popup.style.right = offsetx + "px";
	document.body.appendChild( chat_popup);
	return true;
}

function sync_chat_windows(o)
{
	if( !o)
		return;
	for( var i = 0; i < o.length; i++)
	{
		var username = o[i].room;
		var displayname = o[i].nick;
		var userstatus = o[i].status;
		if( !document.getElementById("chat_popup_"+username))
		{
			start_chat( displayname, username);
			$("#chat_link_"+username).addClass( "unread");
		}
		chat_contents_loaded( o[i]);
	}
	recompute_popup_offsets();
}

function got_user_data( o)
{
	users_loaded( o.d.onlineusers);
	sync_chat_windows( o.d.conversations);
	fill_status_window( o.d.currentstatus);
	main_timeout = setTimeout( "refresh_online_users();", 100);
}

function refresh_online_users()
{
	if( document.getElementById( "chat_link_main_"+service) == null)
		setTimeout( "start_chat(\"Chat\", \"main_"+service+"\");", 100);
		
	var arrRooms = "[";
	var arrLastIds = "[";
	for( var o in lastids)
	{
		if( arrRooms != "[")
			arrRooms += ",";
		arrRooms += "\""+o+"\"";
		if( arrLastIds != "[")
			arrLastIds += ",";
		arrLastIds += "\""+lastids[o]+"\"";
	}
	arrRooms += "]";
	arrLastIds += "]";
		
	var data = "{\"sessionkey\":\""+sessionkey+"\",\"username\":\""+myusername+"\",\"cookie\":\""+mychatcookie+"\",\"rooms\":"+arrRooms+",\"lastids\":"+arrLastIds+",\"service\":\""+service+"\"}";
	$.getJSON( basechatservice+"GetUserData?msg="+escape(data)+"&method=?", got_user_data);
}

function read_cookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+2,ind1-1));
}

function change_status(status)
{
	if( status == "Offline")
		clearTimeout( main_timeout);
	if( status == "")
		status = "Online";
		
	var tosend = status+"";
	tosend = tosend.replace( /\"/g, "\\\"");
	var data = "{\"username\":\""+myusername+"\",\"cookie\":\""+mychatcookie+"\",\"status\":\""+tosend+"\",\"service\":\""+service+"\"}";
	$.getJSON( basechatservice+"ChangeStatus?data="+escape(data)+"&method=?", null);
	
	//status = $('<div/>').text(status).html();

	if( status == "Online")
		status = "";
	$("#chat_popup_status span.status").html( status);
	$("#status_input").val( "");
	if( status != "")
		$("#chat_popup_status div.current").show();
	else
		$("#chat_popup_status div.current").hide();
	close_all_popups();
	if( status == "Offline")
		setTimeout( "window.location.reload( false );", 500);
}

function status_click(e)
{
	change_status( e.target.title);
}

function status_keydown( e)
{
	if( e.keyCode == 13)
		change_status( e.target.value);
}

function fill_status_window( status)
{
	var currentstatus = $("#chat_popup_status span.status").text();
	if( document.getElementById('chat_popup_status').innerHTML+"" == "" || status != currentstatus)
	{
		var status_enc = status;//$('<div/>').text(status).html();
	
		var html = "";
		html += "<a class=\"user\" title=\"Online\">Online</a>";
		html += "<a class=\"user\" title=\"Invizibil\">Invizibil</a>";
		html += "<a class=\"user\" title=\"Offline\">Offline</a>";
		html += "<div class=\"current\" style=\"display:none\">Status curent: <span class=\"status\">"+status_enc+"</span></div>";
		html += "<div class=\"change\">Status: <input type=\"text\" class=\"input_text\" id=\"status_input\"/></div>";

		$('#chat_popup_status').html( html);
		$('#status_input').bind("keydown", status_keydown);
		$('#chat_popup_status a.user').bind("click", status_click);
		if( status != "")
			$("#chat_popup_status div.current").show();
	}
}

function start_chat_windows()
{
	add_popup_button( "Status", "_status");
	setTimeout( "add_popup_button( \"Useri online\", \"_onlineusers\");	refresh_online_users();	", 100);
}

String.prototype.replaceAll = function(pcFrom, pcTo){
	var i = this.indexOf(pcFrom);
	var c = this;
	
	while (i > -1){
		c = c.replace(pcFrom, pcTo); 
		i = c.indexOf(pcFrom);
	}
	return c;
} 

function load_bar2(o)
{
	if( !loaded)
	{
		basechatservice = basechatservice2;
		load_bar(o);
	}
}

function load_bar(o)
{
	loaded = true;
	
	sessionkey = Math.random();
	
	var newcss = document.createElement( "link");
	newcss.setAttribute( "rel", "stylesheet");
	newcss.setAttribute( "type", "text/css");
	newcss.setAttribute( "href", baseurl+"platform_bar.css?ver=4");
	document.getElementsByTagName("head").item(0).appendChild( newcss);

	var newdiv = document.createElement( "div");
	newdiv.setAttribute( "id", "platform_bar");
	document.body.appendChild( newdiv);
	
	var platform_bar_inner = document.createElement( "div");
	platform_bar_inner.setAttribute( "id", "platform_bar_inner");
	newdiv.appendChild( platform_bar_inner);
	
	var logo_main = document.createElement( "img");
	logo_main.setAttribute( "src", baseurl+"main.png");
	logo_main.setAttribute( "alt", "Start");
	logo_main.setAttribute( "width", 16);
	logo_main.setAttribute( "height", 16);
	
	var popup_div = document.createElement( "a");
	popup_div.appendChild( logo_main);
	popup_div.setAttribute( "class", "chat_link left");
	popup_div.setAttribute( "id", "chat_link_add_stuff");
	platform_bar_inner.appendChild( popup_div);
	$(popup_div).bind("click", function(e) { 
		open_popup( "_add_stuff");
		return false;
	} );
	
	var chat_popup = document.createElement( "div");
	chat_popup.setAttribute( "id", "chat_popup"+"_add_stuff");
	chat_popup.setAttribute( "class", "chat_popup");
	chat_popup.style.left = 17 + "px";
	document.body.appendChild( chat_popup);
	
	var i;
	var tasks = ["Concursuri", "Astazi in Bucuresti","Evenimente", "Ultimele fotografii", "Adauga un produs", "Adauga un wishlist"];
	var urls = ["http://concurs.metropotam.ro/","http://metropotam.ro/dailyevents_html","http://invita.ro/organizer/", "http://100eyes.ro/photogallery/latestalbums_html", 
		"http://coolio.ro/?add", "http://coolio.ro/?addwish"];
	var html = "";
	for( i = 0; i < tasks.length; i++)
	{
		var url = urls[i];
		url = url.replace( /{username}/g, myusername);
		var app = url.substring( 7, url.indexOf( "/", 8)-3);
		if( app == "concurs.metropotam")
			app = "metropotam";
		html += "<a class=\"command\" href=\""+url+"\"><img src=\""+baseurl+""+app+".png\" alt=\""+app+".ro\" width=\"16\" height=\"16\" />"+tasks[i]+"</a>";
	}
	
	if( myusername == "")
	{
		if( document.getElementById("fhchatname"))
		{
			var domain = /:\/\/([^\/]+)/.exec(window.location.href)[1];
			var real_login_url;
			if( service == "coolio" || service == "urbee" || service == "metropotam")
				real_login_url = loginurl+"?destination="+escape(window.location)+"&service="+service;
			else
				real_login_url = "http://"+domain+"/login_html?destination="+escape(window.location);
			html += "<a class=\"command\" href=\""+real_login_url+"\"><img src=\""+baseurl+"main.png\" width=\"16\" height=\"16\" />Login</a>";
		}
	} else {
		html += "<a class=\"command\" href=\""+loginurl+"do_account_logout\"><img src=\""+baseurl+"main.png\" width=\"16\" height=\"16\" />Logout</a>";
	}
	
	$("#chat_popup_add_stuff").html( html);
							
	var apps = ["Metropotam", "Invita", "Coolio", "Urbee", "100Eyes", "UrbanBite", "MetropoTravel", "Twitter", "Facebook" ];
	for( i = 0; i < apps.length; i++)
	{
		var site = apps[i].toLowerCase()+".ro";
		var app_img = document.createElement( "img");
		app_img.setAttribute( "src", baseurl+""+apps[i].toLowerCase()+".png");
		app_img.setAttribute( "alt", site);
		app_img.setAttribute( "width", 16);
		app_img.setAttribute( "height", 16);
		
		var app_div = document.createElement( "a");
		app_div.appendChild( app_img);
		app_div.setAttribute( "title", site);
		app_div.setAttribute( "class", "app_title icon_" + apps[i].toLowerCase());
		if( (document.location.host+"").toLowerCase().indexOf(site) >= 0)
			app_div.setAttribute( "class", "app_title app_title_selected");
		app_div.appendChild( document.createTextNode( " "));   
		app_div.setAttribute( "href", "http://"+site+"/");
		app_div.setAttribute( "onclick", "if(typeof pageTracker != 'undefined') pageTracker._trackPageview('/"+apps[i].toLowerCase()+"');");
		platform_bar_inner.appendChild( app_div);
		
		if( apps[i] == "Twitter")
		{				
			app_div.setAttribute( "title", "Pune pe Twitter!");
			app_div.removeAttribute( "href");
			app_div.removeAttribute( "onclick");
			$(app_div).click( function() {
			/*	var defaults = {
					version:    '2.0.1',
					login:      'metropogroup',
					apiKey:     'R_d24146822a12c641d586eaea85adda64',
					history:    '0',
					longUrl:    window.location 
				};

				// Build the URL to query
				var daurl = "http://api.bit.ly/shorten?"
					+"version="+defaults.version
					+"&longUrl="+defaults.longUrl
					+"&login="+defaults.login
					+"&apiKey="+defaults.apiKey
					+"&history="+defaults.history
					+"&format=json&callback=?";	*/
					
				var daurl = "http://s4h.ro/api.aspx?apikey=E4B4AF8BDAEE47bbACE952F25F7697B3&format=jsonp&longurl="+encodeURIComponent(window.location)+"&callback=?";
				
				var w = window.open("http://twitter.com",'_blank','');

				// Utilize the bit.ly API
				$.getJSON(daurl, function(data){
					//alert(data);
					// Make a good use of short URL
					//alert();
					//var shortUrl = data.results[window.location].shortUrl;
					var shortUrl = data.value;
					var title = document.title;
					
					if( title.indexOf( "»") >= 0 && title.lastIndexOf( "»") < title.length - 5)
					{
						title = title.substring( title.lastIndexOf("»")+1, title.length);
						while( title.length > 0 && title.charAt(0) == ' ')
							title = title.substr( 1, title.length-1);
					}
					
					if( title.endsWith( "::"))
						title = title.substr( 0, title.length-2);
						
					if( title.indexOf( "::") >= 0)
					{
						title = title.substring( title.lastIndexOf("::")+2, title.length);
						while( title.length > 0 && title.charAt(0) == ' ')
							title = title.substr( 1, title.length-1);
					}
					
					title = title.replace(/[^a-zA-Z 0-9\-]+/g,'');
					var url = (title + " - " + shortUrl);
					url = url.replaceAll( " ", "+");
					url = "http://www.twitter.com/home?status="+url;
					w.location.href = url;
				});	  
			});
		} else
		if( apps[i] == "Facebook")
		{
			app_div.setAttribute( "title", "Pune pe Facebook!");
			app_div.removeAttribute( "href");
			app_div.removeAttribute( "onclick");
			$(app_div).click( function() {
				var d=document,f='http://www.facebook.com/share',l=d.location,e=encodeURIComponent,p='.php?src=bm&v=4&i=1240320322&u='+e(l.href)+'&t='+e(d.title);1;try{if (!/^(.*\.)?facebook\.[^.]*$/.test(l.host))throw(0);share_internal_bookmarklet(p)}catch(z) {a=function() {if (!window.open(f+'r'+p,'_blank',''))l.href=f+p};if (/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}}void(0);
			});
		}
	}
	
	$(window).resize(function () { 
		close_all_popups();
	});
	
	if( o.d.isonline)
	{
		start_chat_windows();
	} else 
	if( o.d.authenticated)
		add_popup_button( "Intra pe chat ("+o.d.usersonline+" user"+(o.d.usersonline == 1 ? "" : "i")+" online)", "_chat_signin");
	else
	if( document.getElementById("fhchatname"))
		add_popup_button( "Login pentru chat ("+o.d.usersonline+" user"+(o.d.usersonline == 1 ? "" : "i")+" online)", "_chat_signin");
		
	setInterval( "checkForHiddenSelection();", 500);
}

function checkForHiddenSelection()
{
	try {
		var platform_bar = document.getElementById( "platform_bar");
		var selObj = window.getSelection(); 
		var focusNode = selObj.focusNode;
		if( focusNode == null)
			return;
		focusNode = focusNode.parentNode;
		var p = focusNode;
		while( p && p != platform_bar)
			p = p.parentNode;
		if( p)
			return;
		var elemPos = findPos( focusNode);
		elemPos[0] -= document.documentElement.scrollLeft;
		elemPos[1] -= document.documentElement.scrollTop;
		
		var windowSize = getWindowSize();
		
		if( elemPos[1] >= windowSize[1]-30 && elemPos[1] <= windowSize[1])
			document.documentElement.scrollTop += 40;
	} catch(e)
	{
	}
}

$(document).ready(function() {
	if( document.getElementById("platform_bar"))
		return;
	if (top.location != location)
		return;
	
	if( document.getElementById("fhchatname"))
	{
		myusername = document.getElementById("fhchatname").innerHTML;
		mynickname = document.getElementById("fhchatnick").innerHTML;
		mychatcookie = document.getElementById("fhchatinfo").innerHTML;
	}
		
	var isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1 || navigator.userAgent.toLowerCase().indexOf('msie 7') != -1;
	if( isIE6)
		return;
		
	var apps = ["Metropotam", "Invita", "Coolio", "Urbee", "100Eyes", "RoBlogFest" ];
	for( i = 0; i < apps.length; i++)
	{
		var site = apps[i].toLowerCase()+".ro";
		if( (document.location.host+"").toLowerCase().indexOf(site) >= 0)
		{
			service = apps[i].toLowerCase();
			break;
		}
	}
	
//	if( myusername != "")
	{
		var data = "{\"username\":\""+myusername+"\",\"nickname\":\""+mynickname+"\",\"cookie\":\""+mychatcookie+"\",\"service\":\""+service+"\"}";
		$.getJSON( basechatservice+"AuthCookie?msg="+escape(data)+"&method=?", load_bar);
	//	$.getJSON( basechatservice2+"AuthCookie?msg="+escape(data)+"&method=?", load_bar2);
	}/* else {
		var anon = new Object();
		anon["d"] = new Object();
		anon["d"]["authenticated"] = false;
		anon["d"]["isonline"] = false;
		load_bar(anon);
	}	*/	
});
