var pos_label=false;	var name="";
	if (navigator.appName == "Microsoft Internet Explorer")
	pos_label=true;	
	jQuery('#users_accor').accordion({active: true,alwaysOpen: false,navigation: true,autoheight: true,animated: "easeslide",header: "bdo"});	
	jQuery('#users_accor').activate(-1);
		
	function login()
	{
		$.ajax({
			   type: "GET",
			   url: "/talk/LogIn/",
			   data: "user="+user,
			   success: 
				   	function(res)
				   	{
					   //	alert(res);						
					}
			   });
	}
	
	function logout()
	{	
		$.ajax({
			   type: "GET",
			   url: "/talk/LogOut/",
			   data: "user="+user,
			   success: 
				   	function(res)
				   	{
   					   if(res)window.location.href='/membership/logout/';				
					}
			   });	
	}		
	function CambiarImg(thiss)
	{
		if(thiss.alt=="min")
		{
			thiss.src='/media/images/icons/box_maximize.png';
			thiss.alt='max';
		}else
		{
			thiss.src='/media/images/icons/box_minimize.png'; 			
			thiss.alt='min';
		}			
	}	
	var cant_chat=0;	var limite_chats=5;
	
	function CambioAccordion(class_name,cod_accor)
	{
		if(document.getElementById(cod_accor).className.charAt(0)!=class_name.charAt(0))
		{		
			$(document.getElementById(cod_accor)).removeClass();		
			$(document.getElementById(cod_accor)).addClass(class_name);
		}		
	}
	
	function AppendChat(cod_div,usr_name,usr_cod,state)
	{
		if(document.getElementById("chat_"+usr_cod))//esta ventana ya esta abierta
		{				
			CambioAccordion("luz_ventana","header_"+usr_cod);			
			return false;	
		}
		if(cant_chat>=limite_chats)
		{
			alert("solo se pueden abrir "+limite_chats+" ventanas");
			return false;
		}
		
		if(pos_label)		var pos_left_label="position: fixed; width:167px; height:200px; overflow:auto;";
		else 				var pos_left_label="position: relative; width:173px; height:220px; overflow:auto;";
		
		pos=Calcular_Pos(usr_cod);
		
		if(state=="closed")	img="box_maximize.png' alt='max'";	else img="box_minimize.png' alt='min'";
		
		$(document.getElementById(cod_div)).append("<div class='accor_conversation' style='left:"+pos+"px;' id='chat_"+usr_cod+"'>"		
		
		+"<bdo><div id='header_"+usr_cod+"' onclick='CambiarImg(document.getElementById(\"img_usr_"+usr_cod+"\"));"
		+"CambioAccordion(\"header_text\",\"header_"+usr_cod+"\");'  style='cursor:pointer;' class='header_text'>"
		+"<span><div align='center'><div class='headtxt' style='margin-top:1px;'>"+usr_name+"</div>"
		+"<div align='right'><img align='right' onclick='Destroy(\""+usr_cod+"\");' "
		+"style='padding-top:8px; width:15px; padding-right:0;'src='/media/images/icons/ico_close.png' border='0'></div>"
		+"<img align='right' style='padding-top:9px; padding-right:6px; cursor:pointer'"
		+"src='/media/images/icons/"+img+" border='0' id='img_usr_"+usr_cod+"'></div></span></div>"
		+"</bdo>"
			
		+"<div><table cellpadding='5' cellspacing='5' style='border:#ccc solid 1px; width:174px;' border='1'>"
    	+"<tr><td height='200' colspan='2'>"
			+"<label style='"+pos_left_label+";' id='label_"+usr_cod+"'></label><div id='emo_"+usr_cod+"'class='div_emotion' style='z-index=11;'></div></td></tr>"
		+"<tr><td colspan='2'><label><textarea id='field_"+usr_cod+"' style='width:170px; height:49px;' name='textfield'></textarea></label></td></tr>"
       	+"<tr><td><div align='right' class='box_btn_extrasmall'>"
		   +"<input onclick='AppendMsn(\""+usr_cod+"\",\""+usr_name+"\");' type='button' name='enviar_msn' value='Send'></div></td>"
		   +"<td><div align='left' class='box_btn_extrasmall'><input onclick='Show_Emotion(\""+usr_cod+"\");'"
		   +"type='button' name='add_emotion' value='Emotions'></div></td></tr>"
       	+"</table></div></div>");
       	
       	WriteValueLabelEmpty("label_"+usr_cod);		
		jQuery('#chat_'+usr_cod).accordion({active: true,alwaysOpen: false,navigation: true,autoheight: true,animated: "easeslide",header: "bdo"}).bind("change.ui-accordion", function(event, ui) {LowerScroll("label_"+usr_cod); });
		if(state=="closed")	jQuery('#chat_'+usr_cod).activate(-1);	else jQuery('#chat_'+usr_cod).activate(0);
		cant_chat++;			
	}
	
	var arr_pos= new Array("155~empty~","335~empty~","515~empty~","695~empty~","875~empty~","1055~empty~","1235~empty~");// width_accor=180, support 7 accors
	function Calcular_Pos(usr_cod)
	{
		for(var i=0; i<arr_pos.length; i++) 
		{
			r = arr_pos[i].split('~');// 0:pos , 1:state, 2:src
			if(r[1]=="empty")
			{
				arr_pos[i]=r[0]+"~full~"+usr_cod;
				return r[0];
			}			
		}
	}
	function Liberar_Pos(usr_cod)
	{
		for(var i=0; i<arr_pos.length; i++) 
		{
			r = arr_pos[i].split('~');// 0:pos , 1:state, 2:src
			if(r[2]==usr_cod)
			{
				arr_pos[i]=r[0]+"~empty~";
				return;
			}			
		}
	}
	
	function Show_Emotion(usr_cod)
	{
	   if(document.getElementById("emo_"+usr_cod).style.display=="")
		{
		   var str = '';
		   for(var i=0; i<emo_images.length; i++) 
		   {
		      r = emo_images[i].split('~');
		      str += '<img src="/media/images/emoticons/' + r[1] + '" alt="' + r[2] + ' ' + r[0] + '" title="' + r[2] + ' ' + r[0] + '" class="emotions" style="cursor:pointer" onclick="insertEmotion(\''+ r[0]+'\','+usr_cod+');" />';
		   }
		   document.getElementById("emo_"+usr_cod).innerHTML=str;
		   document.getElementById("emo_"+usr_cod).style.display='block';
		}else 	if(document.getElementById("emo_"+usr_cod).style.display=="block")	document.getElementById("emo_"+usr_cod).style.display='none';
		 else	document.getElementById("emo_"+usr_cod).style.display='block';		
	}
	
	function insertEmotion(cod_emotion,usr_cod)
	{
		document.getElementById("field_"+usr_cod).value=document.getElementById("field_"+usr_cod).value+" "+cod_emotion;
		document.getElementById("emo_"+usr_cod).style.display='none';
	}
		
	function AppendMsn(usr_cod,usr_name)
	{			
		var msg=document.getElementById("field_"+usr_cod).value;
		msg=MsnEmotion(msg);
		if(msg=="")
		return false;
		AppendArrayMsg("label_"+usr_cod,"<br>"+full_name+" says:<br>"+msg+"<br>");		
		$.ajax({
			   type: "POST",
			   url: "/talk/SaveMsn/",
			   data: "user="+user+"&message="+document.getElementById("field_"+usr_cod).value+"&id="+usr_cod,
			   success: 
				   	function(res)
				   	{
						if(res=="error")						
						$(document.getElementById("label_"+usr_cod)).append("<br><span style='color: #FF9933;'>Message: <br>---"+msg+"---<br>could not be sent.<br>User is not online anymore</span><br>");
						else
						$(document.getElementById("label_"+usr_cod)).append("<br>"+full_name+" says:<br>"+msg+"<br>");
						LowerScroll("label_"+usr_cod);
					}
			   });						
		document.getElementById("field_"+usr_cod).value='';	
	}
	
	function MsnEmotion(msn)
	{
		for(var i=0; i<emo_images.length; i++) 
		{
		    r = emo_images[i].split('~');		    
		    var band=true;
		    while(band)
		    {
				msn_new=msn.replace(r[0],'<img src="/media/images/emoticons/'+r[1]+'" class="emotions">');
				if(msn_new==msn)
				band=false;
				msn=msn_new;
			}			
		}		
		return msn;
	}
	
	function Destroy(usr_cod)
	{
		$(document.getElementById("chat_"+usr_cod)).remove();
		//DeleteWords("label_"+usr_cod);
		cant_chat--;
		Liberar_Pos(usr_cod);
	}
	function Ping_Users()
	{		  
		  
		  //$.getJSON("/talk/Users_Only/",{user:user},RespuestaJson); 
		  //setTimeout('Ping_Users()', 3000);
		  //return false;		  
	}

	function RespuestaJson(res)
	{			
	 	/*
		if(res.users_only!=document.getElementById('UsersOnly').innerHTML)
	 	{				
			u=res.users_only;
			u = u.replace(/::/g,"'");
			document.getElementById('label_cant_usrs').innerHTML="("+res.cant_usrs+")";
			document.getElementById('UsersOnly').innerHTML="<br>"+u;	
		}
		ShowMsn(res.msn);
		DeleteDiscuss(res.usrs_no_only);
		ShowDiscuss(res.usrs_only);
		*/
	}
	
	function DeleteDiscuss(usrs_no_only)
	{
		//alert("ssss");
		var cods=usrs_no_only.split("{USRID}");//global-msg
		for(i=0;i<cods.length;i++)
		{	
			if(document.getElementById("discuss_"+cods[i]))			
			//alert("existe elemento");
			document.getElementById("discuss_"+cods[i]).innerHTML="";
		}
	}

	function ShowDiscuss(usrs_only)
	{
		//alert("ssss");
		var cods=usrs_only.split("{ONLY}");
		for(i=0;i<cods.length;i++)
		{	
			var only=cods[i].split("{COD-USRNAME}");									//0: cod, 1:username
			if(document.getElementById("discuss_"+only[0]))
			document.getElementById("discuss_"+only[0]).innerHTML='<input name="Online" type="button" value="discuss" onclick="AppendChat(\'bar_chat\',\''+only[1]+'\',\''+only[0]+'\');" >';
		}
	}
	
	var array_msg=new Array();
	function ShowMsn(msns)
	{
		var msn_contct=msns.split("{|!|!|}");//global-msg
		for(i=0;i<msn_contct.length;i++)
		{	
			msns_g=msn_contct[i];			//msg-contact
			msns_g=msns_g.split("{!|!}");	// 0=id_contact, 1=username
			txt="";
			for(j=2;j<msns_g.length-1;j++)
			{
				msns=msns_g[j];				//id_emisor,msg
				msns=msns.split("{|!|}");	//0-> id_emisor,1-> mensaje				
				txt=txt+"<br>"+msns[0]+" says:<br>"+MsnEmotion(msns[1])+"<br>";	
			}		
			if(document.getElementById("chat_"+msns_g[0]))// esta ventana ia esta abierta
			{
				if(ComopareMsg("label_"+msns_g[0],txt))//esta ventana esta abierta y content distint
				{
					document.getElementById("label_"+msns_g[0]).innerHTML=txt;
					LowerScroll("label_"+msns_g[0]);
					CambioAccordion("luz_ventana","header_"+msns_g[0]);					
				}				
			}else if(txt!="")
			{				
				if(ComopareMsg("label_"+msns_g[0],txt))
				{
					AppendChat("bar_chat",msns_g[1],msns_g[0],"closed");
					document.getElementById("label_"+msns_g[0]).innerHTML=txt;
					LowerScroll("label_"+msns_g[0]);					
					CambioAccordion("luz_ventana","header_"+msns_g[0]);	
				}
				
			}						
		}
	}
	function LowerScroll(cod_label)
	{
		document.getElementById(cod_label).scrollTop=document.getElementById(cod_label).scrollHeight;
	}	
	
	function AppendArrayMsg(cod_label,msg)
	{
		for(var i=0; i<array_msg.length; i++) 
		{
			r = array_msg[i].split('~!');
			if(r[0]==cod_label)	
			{
				array_msg[i]=cod_label+"~!"+r[1]+msg;
				return true;				
			}		
		}
		array_msg.push(cod_label+"~!"+msg);
	}
	
	function DeleteWords(cod_label)
	{
		for(var i=0; i<array_msg.length; i++) 
		{
			r = array_msg[i].split('~!');	//0:cod_label, 1: val label
			if(r[0]==cod_label)//mismo contenido			
			{
				array_msg[i]="''~!''";
				return ;
			}
		}		
	}
	
	function WriteValueLabelEmpty(cod_label)
	{
		for(var i=0; i<array_msg.length; i++) 
		{
			r = array_msg[i].split('~!');	//0:cod_label, 1: val label
			if(r[0]==cod_label)//mismo contenido			
			{
				document.getElementById(cod_label).innerHTML=r[1];
				document.getElementById(cod_label).scrollTop = document.getElementById(cod_label).scrollHeight;
			}
		}
	}
	
	function ComopareMsg(cod_label,content)
	{
		var exist=false;
		for(var i=0; i<array_msg.length; i++) 
		{
			r = array_msg[i].split('~!');	//0:cod_label, 1: val label
			if(r[0]==cod_label)//mismo contenido			
				if(r[1]!=content)//contenido distinto
				{
					array_msg[i]=cod_label+"~!"+content;
					return true;				
				}else					//content iqual
				exist=true;
		}
		if(!exist && content!="")/// !(exist label at content equal)
		{
			array_msg.push(cod_label+"~!"+content);
			return true;
		}		
		return false;
	}
				
	Ping_Users();
	var emo_images = [    //cod,src,alias                
	"(A)~smile.gif~Smiley",
	"(B)~sadsmile.gif~Sad face",
	"(B)~bigsmile.gif~Big smile",
	"(C)~cool.gif~Cool smiley",
	"(D)~surprised.gif~Surprised smiley",
	"(E)~wink.gif~Winking smiley",
	"(F)~crying.gif~Crying face",
	"(G)~sweating.gif~Sweating",
	"(H)~speechless.gif~Speechless smiley",
	"(H)~kiss.gif~Kiss",
	"(I)~tongueout.gif~Smiley with tounge out",
	"(J)~blush.gif~Blush",
	"(K)~wondering.gif~Wondering",
	"(L)~sleepy.gif~Sleepy",
	"(LL)~dull.gif~Dull",
	"(M)~inlove.gif~In love",
	"(N)~cash.gif~Cash",
	"(O)~muscle.gif~Muscle",
	"(P)~cake.gif~Cake",
	"(Q)~beer.gif~Beer", 
	"(R)~dance.gif~Dancing",
	"(S)~ninja.gif~Ninja",
	"(T)~pizza.gif~Pizza",
	"(U)~coffee.gif~Coffee",
	"(V)~phone.gif~Phone",
	"(W)~movie.gif~Movie",
	"(X)~music.gif~Music",
	"(Y)~time.gif~Time",
	"(Z)~sun.gif~Sun",
	"(ZA)~rain.gif~Rain",
	"(ZB)~flower.gif~flower",
	"(ZC)~mail.gif~Mail",  
	"(ZD)~brokenheart.gif~Broken Heart", 
	"(ZC)~love.gif~Heart", 
	"(ZD)~handshake.gif~Hand Shake", 
	"(ZE)~no.gif~No", 
	"(ZF)~yes.gif~Yes", 
	"(ZG)~emo.gif~Emo",
	"(ZH)~punch.gif~Punch", 
	"(ZI)~shake.gif~Shake", 
	"(ZJ)~nod.gif~Nodding", 
	"(ZK)~happy.gif~Happy", 
	"(ZL)~whew.gif~Whew",
	"(ZM)~rofl.gif~Rolling on the floor laughing",
	"(ZN)~bow.gif~Bowing",
	"(ZO)~think.gif~Thinking",
	"(ZP)~clapping.gif~Clapping",
	"(ZQ)~giggle.gif~Giggle",
	"(ZR)~makeup.gif~Make-Up",
	"(ZS)~bear.gif~Hug",
	"(ZT)~wait.gif~Wait",
	"(ZU)~angel.gif~Angel",
	"(ZV)~devil.gif~Devil",
	"(ZW)~call.gif~Call",
	"(ZX)~hi.gif~Hi",
	"(ZY)~lipssealed.gif~My lips are sealed",
	"(ZZ)~nerd.gif~Nerdy",
	"(YA)~mmm.gif~Mmmmm...",
	"(YB)~worried.gif~Worried",
	"(YC)~party.gif~party",
	"(YD)~itwasntme.gif~It wasn't me!",
	"(YE)~angry.gif~Angry",
	"(YF)~doh.gif~Doh",
	"(YG)~puke.gif~Puking",
	"(YH)~yawn.gif~Yawn",
	"(YI)~talking.gif~Talking",
	"(UJ)~evilgrin.gif~Evil Grin",
	"(YK)~star.gif~Star"
];