/* Глобальные переменные */
var timer	= null;
var showdiv	= 0;
var overdiv	= 0;
var present 	= 0;
var isNav	= null;
var x = -1;
var y = -1;

var png_cnt = 0;
var png_arr = new Array();

function pngdo(n){

	for(i=0;i<png_cnt;i++){
		png_arr[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src="+png_arr[i].style.backgroundImage.substr(4,png_arr[i].style.backgroundImage.length-5)+", image)";
	}

	return true;
}

function pngfix(obj){

	obj.runtimeStyle.behavior = "none";

	if (isIE7)return false;

	for(i=0;i<png_cnt;i++){
		if (png_arr[i] == obj)return false;
	}

	obj.runtimeStyle.backgroundImage = "none";

	png_arr[png_cnt] = obj;
	png_cnt++;

	return true;
}

function detect_browser(){

	if (document.layers) {navigator.family = "nn4"}
	if (document.all) {navigator.family = "ie4"}
	if (window.navigator.userAgent.toLowerCase().match("gecko")) {
		navigator.family = "gecko"
	}

	isNav = (navigator.appName.indexOf("Netscape") !=-1);
	if (isNav){
		document.captureEvents(Event.MOUSEMOVE);
	}
	if (navigator.appName.indexOf("Opera") !=-1){
		navigator.family = "opera";
	}
	document.onmousemove = handlerMM;
}

function window_open(url, width, height, name, scroll_bar, location){

	_top = Math.ceil((screen.height - height)/2);
	_left = Math.ceil((screen.width - width)/2);

	if(scroll_bar != true){
		var scroll = 'no';
	}else{
		var scroll = 'yes';
	}
	if(typeof location != "undefined" && location == true){
		var location = 'yes';
	}else{
		var location = 'no';
	}

	if(name == undefined){
		name = Math.round(999999 * Math.random());
	}
	new_win = window.open(url, name , 'width=' + width + ',height=' + height + ',location='+ location +',status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=' + scroll + ',top=' + _top + ',left=' + _left);
	new_win.focus();
	return new_win;
}

function submit_to_popup(url, width, height, form_name, name, location){

	_top = Math.ceil((screen.height - height)/2);
	_left = Math.ceil((screen.width - width)/2);

	if (typeof location != "undefined" && location == true){
		var location = 'yes';
	}else{
		var location = 'no';
	}

	new_win = window.open('', name , 'width=' + width + ',height=' + height + ',location='+ location +',status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=no,top=' + _top + ',left=' + _left);
	new_win.name = name;
	new_win.focus();
	form_name.action = url;
	form_name.target = name;
	form_name.submit();
	return false;
}

function window_close(do_refresh, url){

	if(do_refresh == true){
		if (url == undefined){
			try {
				window.opener.location.reload();
			}
			catch(err){
				window.close();
			}
		}else {
			try {
				window.opener.location.href = url;
			}
			catch(err){
				window.close();
			}
		}
	}
	window.close();
	return false;
}

function paging_go(url, page_num, url_postfix){

	var page = parseInt(page_num);
	if (typeof url_postfix == "undefined"){
		url_postfix = '';
	}
	if (!isNaN(page)){
		if (page - 1 < 0) page = 0;
		document.location.href = url + (page - 1) + '.html'+url_postfix;
	}else{
		var z = document.getElementById('page_nr');
		z.value = '';
	}
	return false;
}


select_all_status = true;
function select_all(val, elements_name){

	var el = document.getElementsByName(elements_name);

	for (i = 0; i< el.length; i++){
		el[i].checked = select_all_status;
	}
	select_all_status = !select_all_status;
}

function show_context_menu(prf_id, prf_nick, prf_phs_id, host_name, session_id){

	if (prf_id == '') return;
	timer = setTimeout("build_context_menu('" + prf_id + "','" + prf_nick + "','" + prf_phs_id + "','" + host_name + "','" + session_id + "')", 600);
}

function highlightCell(cell, visible){
	if (visible){
		cell.className = 'context_menu_';
	}else{
		cell.className = 'context_menu';
	}
}

function show_vip_menu(host_name){
	menu = '<div class="java_menu" id="popup_width_p">'+
			'<div class="j_menu" style="line-height:130%;padding:10px;"><table cellpadding="0" cellspacing="0" width="120"><tr><td align="center">'+
				'<img src="/images/brill.gif" width="47" height="33"><br />'+
				'<b>'+locale.brill_from_vip[lang]+'</b><br /><br />'+
				'<a href="#" onclick="window_open(\''+host_name+'/window/extras/extra/290.html\', 408, 500); return false">'+locale.how_to_vip[lang]+'</a><br />'+
			'<\/td><\/tr><\/table><\/div>'+
		'<\/div>';

	object_position(0, 0, menu, 'popup_width_p');
}


function build_context_menu(prf_id, prf_nick, prf_phs_id, host_name, session_id){

	menu = '<div class="java_menu" id="popup_width" style="width:170px;padding:0px">'+
					'<div class="head"><a href="friend/'+ prf_id +'.html">'+ prf_nick +'<\/a><\/div>'+
					'<div class="close"><img src="'+skin_close_img+'" onclick="overdiv=0; close_context_menu(); return false;"><\/div>'+
					'<table cellspacing="0" cellpadding="0" class="j_menu" style="width:100%"><tr><td>'+
							'<table cellpadding="0" cellspacing="0" style="width:100%">'+
								'<tr onMouseover="highlightCell(this, true)" onMouseout="highlightCell(this, false)" onclick="window_open(\''+ host_name +'/window/friend/photo_view/'+ prf_id +'/'+ prf_phs_id +'.html\', 743, 653); return false;">'+
									'<td style="width:20px;padding-left: 7px; height: 21px;"><img src="/skin/all/context_menu/0.gif" alt=""><\/td>'+
									'<td nowrap>&nbsp;' + locale.vote_photo[lang] + '<\/td>'+
								'<\/tr>'+
								'<tr onMouseover="highlightCell(this, true)" onMouseout="highlightCell(this, false)" onclick="window_open(\''+ host_name +'/window/messages/send/'+ prf_id +'.html\', 540, 430, \'wnd_' + prf_id + '\', false, false); return false;">'+
									'<td style="width:20px;padding-left: 7px; height: 21px;"><img src="/skin/all/context_menu/1.gif" alt=""><\/td>'+
									'<td nowrap>&nbsp;' + locale.send_message[lang] + '<\/td>'+
								'<\/tr>'+
								'<tr onMouseover="highlightCell(this, true)" onMouseout="highlightCell(this, false)" onclick="document.location.href = \''+ host_name +'/friend/'+ prf_id +'/gallery.html\'">'+
									'<td style="width:20px;padding-left: 7px; height: 21px;"><img src="/skin/all/context_menu/2.gif" alt=""><\/td>'+
									'<td nowrap>&nbsp;' + locale.photo_gallery[lang] + '<\/td>'+
								'<\/tr>'+
								'<tr onMouseover="highlightCell(this, true)" onMouseout="highlightCell(this, false)" onclick="document.location.href = \''+ host_name +'/friend/'+ prf_id +'/friends.html\'">'+
									'<td style="width:20px;padding-left: 7px; height: 21px;"><img src="/skin/all/context_menu/4.gif" alt=""><\/td>'+
									'<td nowrap>&nbsp;' + locale.friends[lang] + '<\/td>'+
								'<\/tr>'+
								'<tr onMouseover="highlightCell(this, true)" onMouseout="highlightCell(this, false)" onclick="window_open(\''+ host_name +'/window/favorites/set/'+ prf_id +'.html\', 422, 286); return false;">'+
									'<td style="width:20px;padding-left: 7px; height: 21px;"><img src="/skin/all/context_menu/5.gif" alt=""><\/td>'+
									'<td nowrap>&nbsp;' + locale.favorite[lang] + '<\/td>'+
								'<\/tr>'+
								'<tr onMouseover="highlightCell(this, true)" onMouseout="highlightCell(this, false)" onclick="document.location.href = \''+ host_name +'/friend/'+ prf_id +'/blog.html\'">'+
									'<td style="width:20px;padding-left: 7px; height: 21px;"><img src="/skin/all/context_menu/6.gif" alt=""><\/td>'+
									'<td nowrap>&nbsp;' + locale.guestbook[lang] + '<\/td>'+
								'<\/tr>'+
								'<tr onMouseover="highlightCell(this, true)" onMouseout="highlightCell(this, false)" onclick="document.location.href = \''+ host_name +'/friend/'+ prf_id +'/messages.html\'">'+
									'<td style="width:20px;padding-left: 7px; height: 21px;"><img src="/skin/all/context_menu/7.gif" alt=""><\/td>'+
									'<td nowrap>&nbsp;' + locale.msg_archive[lang] + '<\/td>'+
								'<\/tr>'+
								'<tr onMouseover="highlightCell(this, true)" onMouseout="highlightCell(this, false)" onclick="document.location.href = \''+ host_name + '/extras/present/'+ prf_id +'.html\'">'+
									'<td style="width:20px;padding-left: 7px; height: 21px;"><img src="/skin/all/context_menu/8.gif" alt=""><\/td>'+
									'<td nowrap>&nbsp;' + locale.present[lang] + '<\/td>'+
								'<\/tr>'+
								'<tr onMouseover="highlightCell(this, true)" onMouseout="highlightCell(this, false)" onclick="window_open(\'/window/friend/write_complain/'+ prf_id +'.html\', 422, 286); return false;">'+
									'<td style="width:20px;padding-left: 7px; height: 21px;"><img src="/skin/all/btn_ico/complain.gif" alt="" /><\/td>'+
									'<td nowrap>&nbsp;' + locale.write_complain[lang] + '<\/td>'+
								'<\/tr>'+
				'<\/table>'+
			'<\/td><\/tr><\/table><\/div>';

	object_position(0, 0, menu, 'popup_width');

}

function build_profile_menu(img_url, nick_str, prf_id, pos_x, pos_y, host, opener_url){

	if (typeof host == "undefined"){
		host = '';
	}

	if (typeof opener_url == "undefined"){
		opener_url = '';
	}

	on_click = '';
	if (opener_url != ''){
		on_click = 'onclick="opener.location.href=\''+ opener_url +'\'"';
	}

	menu = '<table ' + on_click + '  cellspacing="1" cellpadding="0" class="j_menu" style="width:124px">'+
		'<tr>'+
			'<td align="center" width="124">';
	if(prf_id != ''){
		menu += '<a href="' + host + '/friend/' + prf_id + '.html">';
	}
	menu += '<img src="'+ img_url +'">';
	if(prf_id != ''){
		menu += '<\/a>';
	}
	menu += '<br>'+ nick_str +'<\/td>'+
		'<\/tr>'+
		'<\/table>';
 	object_position(0, 0, menu, '');
	overdiv = 1;
}

function hide_context_menu(){

	if(timer){
		clearTimeout(timer);
		timer = null;
	}
	if(showdiv == "1"){
		overdiv = 0;
		setTimeout('close_context_menu()', 500);
	}
}

function close_context_menu(){
	if (overdiv == "0") {
		if(navigator.family == "nn4") {eval(document.object1.top="-500");}
		else if(navigator.family == "ie4"  || navigator.family == "opera"){document.getElementById("object1").innerHTML = "";}
		else if(navigator.family == "gecko") {	document.getElementById("object1").style.top = "-500px";}
		showdiv=0;
	}
}

function handlerMM( event ) {
    var tempX = 0;
    var tempY = 0;
    if (!event)event=window.event;
    if (document.all) {
        // must be IE
        tempX = event.clientX;// + document.body.scrollLeft;
        tempY = event.clientY;// + document.body.scrollTop;
    } else {
        tempX = event.pageX;
        tempY = event.pageY;
    }
    // prevent any negative values
    if (tempX < 0){tempX = 0}
    if (tempY < 0){tempY = 0}
    x = tempX;
    y = tempY;
}

function validate_max_lenght(obj, char_num){
	if (obj.value.length > char_num){
		obj.value = obj.value.substr(0, char_num);
	}
	return true;
}


function set_cookie_lang(language){

	var date = new Date();
	date.setTime(date.getTime() + ( 365 * 24 * 60 * 60 * 1000));
	var expires = "; expires=" + date.toGMTString();
	document.cookie = 'language=' + language + expires + "; path=/";
}
//------------------------------------------------------------------------------

function get_cookie(name){

	var ca = document.cookie.split(';');
	var nameEQ = name + "=";
	for(var i=0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function window_image(name, url){
        img_win = window.open('', name , 'dependent=yes,location=no,status=yes,toolbar=yes,menubar=yes,resizable=yes,scrollbars=yes');
        img_win.document.write('<html><body><a href="#" onclick="window.close()"><img onclick="window.close();" src="'+url+'" alt="close" border="0" \/><\/a><\/body><\/html>');
        img_win.document.close();
        img_win.focus();
        return img_win;
}

function add_swf_object(url, vars, w, h, version, mouse_over, mouse_out){
	if(version == "" || version == "undefined") {
	    version = "6,0,0,0";
	}
	if(mouse_over == "" || mouse_over == "undefined") {
	    mouse_over = "";
	}
	if(mouse_out == "" || mouse_out == "undefined") {
	    mouse_out = "";
	}

	document.write('<object type="application/x-shockwave-flash" onmouseout="'+mouse_out+'" onmouseover="'+mouse_over+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+version+'" width="'+w+'" height="'+h+'">\n');
    	document.write('<param name="movie" value="'+url+'"/>\n');
    	document.write('<param name="swliveconnect" value="true"/>\n');
    	document.write('<param name="AllowScriptAccess" value="always"/>\n');
	document.write('<param name="quality" value="high"/>\n');
	document.write('<param name="menu" value="false" />\n');
	document.write('<param name="FlashVars" value="'+vars+'"/>\n');
	document.write('<param name="wmode" value="transparent"/>\n');
	document.write('<embed swliveconnect="true" AllowScriptAccess="always" src="'+url+'" flashvars="'+vars+'" menu="false" wmode="transparent" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>\n');
	document.write('</object>\n');
}

function add_swf(url, vars, w, h, version, mouse_over, mouse_out){
	if(version == "" || version == "undefined") {
	    version = "6,0,0,0";
	}
	document.write('<object type="application/x-shockwave-flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+version+'" width="'+w+'" height="'+h+'">\n');
	document.write('<param name="movie" value="'+url+'"/>\n');
	document.write('<param name="quality" value="high"/>\n');
	document.write('<param name="menu" value="false" />\n');
	document.write('<embed src="'+url+'" flashvars="'+vars+'" menu="false" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>\n');
	document.write('</object>\n');
}


function get_swf_object(url, vars, skin, w, h, version, mouse_over, mouse_out){
	if(version == "" || version == "undefined") {
	    version = "6,0,0,0";
	}
	if(mouse_over == "" || mouse_over == "undefined") {
	    mouse_over = "";
	}
	if(mouse_out == "" || mouse_out == "undefined") {
	    mouse_out = "";
	}
	vars = vars +'&skin='+skin;

	obj = "";
	obj += '<object type="application/x-shockwave-flash" onmouseout="'+mouse_out+'" onmouseover="'+mouse_over+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+version+'" width="'+w+'" height="'+h+'">\n';
    	obj += ('<param name="movie" value="'+url+'">\n');
    	obj += ('<param name="swliveconnect" value="true"/>\n');
    	obj += ('<param name="AllowScriptAccess" value="always"/>\n');
	obj += ('<param name="quality" value="high">\n');
	obj += ('<param name="FlashVars" value="'+vars+'">\n');
	obj += ('<param name="menu" value="false">\n');
	obj += ('<param name="wmode" value="transparent">\n');
	obj += ('<embed swliveconnect="true" AllowScriptAccess="always" src="'+url+'" flashvars="'+vars+'" menu="false" wmode="transparent" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>\n');
	obj += ('</object>\n');

	return obj;
}


String.prototype.Trim = function() {
 	return (this.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]|[\\n]+$/, '').replace(/(\&nbsp\;|\<br\>)+$/, ''));
};


// для кривого Firefox, который что-то там не успевает сделать
function goWithTimeout(url){
	setTimeout('window.location.href = "'+url+'"', 1000);
}


/*
* Правильная ф-ция для флэша
*/
function add_flash(url, params, vars, w, h, version) {
	if(version == "" || version == "undefined") {
		version = "6,0,0,0";
	}
	document.write('<object type="application/x-shockwave-flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+version+'" width="'+w+'" height="'+h+'">\n');
	document.write('<param name="movie" value="'+url+'"/>\n');

	var a = new Array();
	a = params;
	var emb = "";
	for(var i = 0; i < a.length; i++) {
		document.write('<param name="'+a[i][0]+'" value="'+a[i][1]+'"/>\n');
		emb += a[i][0]+'="'+a[i][1]+'"';
	}
	document.write('<param name="flashvars" value="'+vars+'"/>\n');

	document.write('<embed src="'+url+'" flashvars="'+vars+'" '+emb+' pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>\n');
	document.write('</object>\n');
}


function object_position(x_offset, y_offset, menu_text, menu_id){

		if (navigator.family =="nn4"){

			document.object1.document.write(menu_text);
			document.object1.document.close();
			if (menu_id!="")
				contex_menu_get_position(menu_id);
			document.object1.left = x - x_offset;
			document.object1.top = y + y_offset;

		}else if (navigator.family =="opera"){
			obj = document.getElementById("object1");
			obj.innerHTML = menu_text;
			if (menu_id!="")
				contex_menu_get_position(menu_id);
			obj.style.pixelLeft = x - x_offset;
			obj.style.pixelTop = y + y_offset;
		}else if (navigator.family =="ie4"){
			obj = document.getElementById("object1");
			obj.innerHTML = menu_text;
			if (menu_id!="")
				contex_menu_get_position(menu_id);
			obj.style.pixelLeft = x - x_offset;
			obj.style.pixelTop = y + y_offset+document.documentElement.scrollTop;
		}else if (navigator.family == "gecko"){

			document.getElementById("object1").innerHTML = menu_text;
			if (menu_id!="")
				contex_menu_get_position(menu_id);
			document.getElementById("object1").style.left = (x - x_offset)+"px";
			document.getElementById("object1").style.top = (y + y_offset)+"px";

		}
		showdiv = 1;
}


function contex_menu_get_position(id){

	if(typeof id == 'undefined'){
		id = 'popup_width';
	}

	tabW = document.getElementById(id).offsetWidth;
	tabH = document.getElementById(id).offsetHeight;

	if (window.innerWidth){
		winW = window.innerWidth;
		winH = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth){
		winW = document.documentElement.clientWidth;
		winH = document.documentElement.clientHeight;
	}
	else if (document.body){
		winW = document.body.clientWidth;
		winH = document.body.clientHeight;
	}

	if (window.pageYOffset){
		scrT = window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop){
		scrT = document.documentElement.scrollTop;
	}
	else if (document.body){
		scrT = document.body.scrollTop;
	}

	if(x > winW - tabW) {
		x = winW - tabW - 25;
	}else{
		x = x + 5;
	}

	if((y - scrT) > (winH - tabH)) {
		y = scrT + winH - tabH - 5;
	}else{
		y = y - 5;
	}

}


/*
* ковертировать простые смайлы под стандарт [e..]
*/
function convert_smiles(msg){

	var smiles = new Array();
	smiles[':)'] = '[e01]';
	smiles[';)'] = '[e02]';
	smiles[':P'] = '[e03]';
	smiles[':]'] = '[e04]';
	smiles[':D'] = '[e05]';
	smiles['8}'] = '[e07]';
	smiles[':`('] = '[e08]';
	smiles[':\')'] = '[e17]';
	smiles[':('] = '[e10]';
	smiles['=0'] = '[e15]';
	smiles['(h)'] = '[e18]';
	smiles['(k)'] = '[e20]';
	smiles['(c)'] = '[e21]';
	smiles['(f)'] = '[e22]';

	for (smile in smiles) {
		while (msg.indexOf(smile) > -1) {
			msg = msg.replace(smile,smiles[smile]);
		}
	}

	return msg;
}

