function isIE6() {
/*
        if(navigator.appName == 'Microsoft Internet Explorer') {
                var tmp = navigator.appVersion.match(/(MSIE )([0-9])/);
                if(tmp[2] < 7) {
                        alert('NOT GOOD');
                } else {
                        alert('GOOD');
                }
        }
*/

        // option 2
        if(navigator.appVersion.match(/MSIE [0-6]/)) {
		return true;
        }
	return false;
}

function initChat() {

	var w = 800; 
	var h = 600;

	if ( isIE6() ) {
		return;
	}

	if ( window.innerWidth ) {
		w = window.innerWidth - 16;
		h = window.innerHeight;
	} else if ( document.body.offsetWidth ) {
		w = document.body.offsetWidth;
		h = document.body.offsetHeight;
	}
	if ( document.getElementById( 'chatDiv' ) ) {
		var chatDiv = document.getElementById( 'chatDiv' );
		chatDiv.style.width = w + 'px';
		chatDiv.style.display = 'block';
	}
}

function toggleChatFriends() {
	if ( document.getElementById( 'chatFriendsDiv' ) ) {
		var chatFriends = document.getElementById( 'chatFriendsDiv' );
		var chatFriendsCont = document.getElementById( 'chatFriendsCont' );
		if ( chatFriends.style.visibility == 'visible' ) {
			chatFriends.style.visibility = 'hidden';
		} else {
			chatFriends.style.visibility = 'visible';
			if ( document.getElementById( 'newChatNotice' ) ) {
				document.getElementById( 'newChatNotice' ).style.display = 'none';
			}
		}
//		chatFriends.style.top = '-' + (chatFriendsCont.clientHeight + 40) + 'px';
	}
}

function sendChatMsg( ev, inp, id ) {
	var keynum;

	if(window.event) { // IE
		keynum = ev.keyCode;
	} else if(ev.which) { // Netscape/Firefox/Opera
		keynum = ev.which;
	}
	if ( keynum == 13 ) {
		var msg = inp.value;
		inp.value = '';
		var e_msg = msg.replace( /\&/g,'%26' ); 
		makeRequestChat( 'ajax/chatFriendPopup.php?id='  + id + '&action=write&msg=' + encodeURI( e_msg ), 'friendChatCont' + id );
		return false;
	} else {
		return true;
	}
}

function refreshFriendWin(id) {
	if ( document.getElementById( 'friend' + id ) ) {
		setTimeout( 'refreshFriendWin(' + id + ' ) ', 1000 );
		if ( document.getElementById('chat' + id + 'LU' ) ) {
			var lu = document.getElementById('chat' + id + 'LU' ).innerHTML;
			makeRequestChat( 'ajax/chatFriendPopup.php?id='  + id + '&lu=' + lu, 'friendChatCont' + id );
		} else {
			makeRequestChat( 'ajax/chatFriendPopup.php?id='  + id, 'friendChatCont' + id );
		}
	}
}

//var intervals = Array();
var totalWindows = 0;
var fWindows = Array();
function toggleFriendWin(id,name,pic, pich, picw, status) {
	if ( document.getElementById( 'friend' + id ) ) {
		if ( fWindows[1] == id ) {
			fWindows[1] = fWindows[2];
			fWindows[2] = fWindows[3];
			fWindows[3] = 0;
		} else if ( fWindows[2] == id ) {
			fWindows[2] = fWindows[3];
			fWindows[3] = 0;
		} else {
			fWindows[3] = 0;
		}			
		totalWindows--;
		var d = document.getElementById( 'friend' + id );
		d.parentNode.removeChild( d );
//		clearInterval( intervals["i" + id] );
		for( i = 1; i <= 3; i++ ) {
			if ( document.getElementById( 'friendFloat' + fWindows[i] ) ) {
				document.getElementById( 'friendFloat' + fWindows[i] ).style.right = (239 * i + 33) + 'px';
			}
		}
	} else {
		if ( totalWindows == 3 ) {
			return true;
		}
		totalWindows++;
		fWindows[totalWindows] = id;
		document.getElementById( 'friendWinCont' ).innerHTML = document.getElementById( 'friendWinCont' ).innerHTML + 					
					'<div id="friend' + id + '" style="float: right; margin-left: 3px; width: 236px;">' +
						'<div class="friendPopupBaseL">' +
							'<img src="images/chatElRight.png" alt="" width="6" height="20" border="0">' +
						'</div>' +
						'<div class="tah11 friendPopupBaseM">' +
							'<div class="friendPopupBaseMName">' +
								'<a href="full_profile.php?user_id=' + id + '" class="tah11 black" onmouseover="show_hint2( event, \'Виж профила\');" onmousemove="move_hint2(event);" onmouseout="hide_hint();">' +
									'<img src="images/friendsFriendsModMinimize.png" height="16" width="16" border="0">' +
								'</a> &nbsp; ' +
								'<a href="compose.php?user_id=' + id + '" class="tah11 black" onmouseover="show_hint2( event, \'Напиши писмо\');" onmousemove="move_hint2(event);" onmouseout="hide_hint();">' +
									'<img src="images/email.png" height="16" width="16" border="0">' +
								'</a> &nbsp; ' +
								'<a href="#" onclick="toggleEmotes(\'' + id + '\');" class="tah11 black" onmouseover="show_hint2( event, \'Покажи емотиконите\');" onmousemove="move_hint2(event);" onmouseout="hide_hint();">' +
									'<img src="images/emotes/smile.gif" height="16" width="16" border="0">' +
								'</a>' +
							'</div>' +
//							'<div class="friendPopupBaseMClose"><a href="#" onclick="toggleFriendWin(\'' +id+ '\',\'' + name + '\'); return false;"><img src="images/chatPopupClose.png" alt="" width="16" height="16" border="0"></a></div>' +
						'</div>' +
						'<div class="friendPopupBaseR">' +
							'<img src="images/chatElLeft.png" alt="" width="6" height="20" border="0">' +
						'</div>' +
						'<div class="friendPopupWin" id="friendFloat' + id + '">' +
							'<div class="friendPopupWinTL">' +
								'<img src="images/chatPopupTopLeft.png" alt="" width="8" height="8" border="0">' +
							'</div>' +
							'<div class="friendPopupWinTM"></div>' +
							'<div class="friendPopupWinTR">' +
								'<img src="images/chatPopupTopRight.png" alt="" width="8" height="8" border="0">' +
							'</div>' +
							'<div class="friendPopupWinCont">' +
								'<div class="tah11" style="float: left; background: #efefef; width: 226px;">' +
									'<div style="float: left; margin-left: 8px; margin-bottom: 8px; background: #efefef;">' +
										'<a href="full_profile.php?user_id=' + id + '"><img src="' + pic + '" alt="" width="' + picw + '" height="' + pich + '" border="0"></a>' +
									'</div>' +
									'<div style="float: left; margin-left: 8px; margin-top: 0px;">' +
										'<div><a href="full_profile.php?user_id=' + id + '"><strong>' + name + '</strong></a></div>' +
										'<div style="margin-top: 5px;" class="dotOrange tah10">' + status + '</div>' +
									'</div>' +
									'<div style="float: right; margin-right: 5px;">' +
										'<div style="margin-top: 0px;" class="dotOrange tah10"><a href="#" onclick="toggleFriendWin(\'' +id+ '\',\'' + name + '\'); return false;"><img src="images/chatPopupClose.png" alt="" width="16" height="16" border="0"></a></div>' +
									'</div>' +

								'</div>' +
								'<div class="chatWindowCont tah11">' +
									'<div id="friendChatCont' + id + '">' +
									'</div>' +
									'<div class="chatWriteLine">' +
										'<div class="chatWriteLineImg" >' +
											'<img src="images/chatWrite.png" alt="" width="14" height="11" border="0" style="display: block; float: left;">' +
										'</div>' +
										'<div class="chatWriteLineInp" >' +
											'<input type="text" name="chatWrite" maxlength="255" maxsize="255" onkeypress="return sendChatMsg( event, this, \'' + id + '\');" value="" class="tah11 chatWriteLineInput" id="writeInp'+id+'">' +
										'</div>' +
										'<div style="clear: both;"></div>' +
										'<div style="height: 3px; width: 1px;"></div>' +
										'<div id="emotes' + id + '" class="emotesDiv">' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\':) \');">' +
												'<img src="images/emotes/smile.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\':( \');">' +
												'<img src="images/emotes/sad.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\'(blush) \');">' +
												'<img src="images/emotes/blush.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\':D \');">' +
												'<img src="images/emotes/laugh.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\';) \');">' +
												'<img src="images/emotes/wink.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\':P \');">' +
												'<img src="images/emotes/tongue.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\':| \');">' +
												'<img src="images/emotes/indif.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\'(flower) \');">' +
												'<img src="images/emotes/flower.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\'(kiss) \');">' +
												'<img src="images/emotes/kiss.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\'8) \');">' +
												'<img src="images/emotes/8.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\':S \');">' +
												'<img src="images/emotes/s.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\'(woo) \');">' +
												'<img src="images/emotes/woo.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\':X \');">' +
												'<img src="images/emotes/x.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\'(angry) \');">' +
												'<img src="images/emotes/angry.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\'(yes) \');">' +
												'<img src="images/emotes/yes.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
											'<a href="#" onclick="addToInp(\'writeInp' + id + '\',\'(no) \');">' +
												'<img src="images/emotes/no.gif" alt="" width="18" height="18" border="0">' +
											'</a>' +
										'</div>' +
									'</div>' +
								'</div>' +
							'</div>' +
						'</div>' +
					'</div>';
//		intervals["i" + id] = setInterval( 'refreshFriendWin( ' + id + ' )', 1000 );
		setTimeout( 'refreshFriendWin(' + id + ' ) ', 1000 );
		for( i = 1; i <= 3; i++ ) {
			if ( document.getElementById( 'friendFloat' + fWindows[i] ) ) {
				document.getElementById( 'friendFloat' + fWindows[i] ).style.right = (239 * i + 33) + 'px';
			}
		}
		makeRequestChat( 'ajax/chatFriendPopup.php?id='  + id, 'friendChatCont' + id );
	}
}

function chatRefreshFriendList() {
	if ( isIE6() ) {
		return;
	}
	makeRequestChat('ajax/chatFriendsList.php?','chatFriendsAjax');
	setTimeout( "chatRefreshFriendList2()", 7000);
}



function chatAfterRefresh(scrollAmount) {
	document.getElementById("chatFriendsAjax").scrollTop = scrollAmount;
	if ( document.getElementById( 'chatNewMsgs' ) &&  document.getElementById( 'chatLogoCont' ) ) {
		if ( document.getElementById( 'chatNewMsgs' ).value > 0 ) {
			document.getElementById( 'chatLogoCont' ).innerHTML = '<a href="#" onclick="toggleChatFriends(); return false;" class="tah11 black"><img src="images/chatLogoBlink2.gif" alt="" width="16" height="16" border="0"></a>';
			if ( document.getElementById( 'ChatAlert' ) ) {
				document.getElementById( "ChatAlert" ).innerHTML = '<a href="#" title="Чат" onclick="toggleChatFriends(); return false;"><img src="images/chatBlink.gif" width="23" height="15" hspace="0" vspace="0" border="0">Чат</a>';
			}
			if ( document.getElementById( 'chatIco' ) ) {
				document.getElementById( 'chatIco' ).innerHTML = '<img src="images/chatIco1.gif" width="19" height="19" alt="" border="0" class="profileIco" />';
			}
			if ( document.getElementById( 'chatFriendsDiv' ) ) {
				var chatFriends = document.getElementById( 'chatFriendsDiv' );
				if ( chatFriends.style.visibility != 'visible' ) {
					if ( document.getElementById( 'newChatNotice' ) ) {
						document.getElementById( 'newChatNotice' ).style.display = 'block';
					}
				}
			}
		} else {
			document.getElementById( 'chatLogoCont' ).innerHTML = '<a href="#" onclick="toggleChatFriends(); return false;" class="tah11 black"><img src="images/chatLogo.png" alt="" width="16" height="16" border="0"></a>';
			if ( document.getElementById( 'chatIco' ) ) {
				document.getElementById( 'chatIco' ).innerHTML = '<img src="images/chatIco2.gif" width="19" height="19" alt="" border="0" class="profileIco" />';
			}
			if ( document.getElementById( 'newChatNotice' ) ) {
				document.getElementById( 'newChatNotice' ).style.display = 'none';
			}
			if ( document.getElementById( 'ChatAlert' ) ) {
				document.getElementById( 'ChatAlert' ).innerHTML = '<a href="#" title="Чат" onclick="toggleChatFriends(); return false;"><img src="images/chat.jpg" width="23" height="15" hspace="0" vspace="0" border="0">Чат</a>';
			}
		}
	}
}

function chatRefreshFriendList2() {
	if ( isIE6() ) {
		return;
	}
	setTimeout( "chatRefreshFriendList2()", 7000);
	makeRequestChat2('ajax/chatFriendsList.php?','chatFriendsAjax', 'chatAfterRefresh(\'' + document.getElementById("chatFriendsAjax").scrollTop + '\');' );
}


function addToInp( inp, str ) {
	if ( document.getElementById(inp) ) {
		document.getElementById(inp).value = document.getElementById(inp).value + str;
		document.getElementById(inp).focus();
	}
}

function toggleEmotes(id) {
	if ( document.getElementById('emotes' + id) ) {
		if ( document.getElementById('emotes' + id).style.display != 'block' ) {
			document.getElementById('emotes' + id).style.display = 'block';
		} else {
			document.getElementById('emotes' + id).style.display = 'none';
		}
	}
}