// 1.1 
var profile_hide_timeout = 5000;

var profile_mouse_x = 0;
var profile_mouse_y = 0;

var ua = navigator.userAgent.toLowerCase();
//Определим Internet Explorer
profile_isIE = (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1);
//Opera
profile_isOpera = (ua.indexOf("opera") != -1);
//Gecko = Mozilla + Firefox + Netscape
profile_isGecko = (ua.indexOf("gecko") != -1);
//Safari, используется в MAC OS
profile_isSafari = (ua.indexOf("safari") != -1);
//Konqueror, используется в UNIX-системах
profile_isKonqueror = (ua.indexOf("konqueror") != -1);

function profile_convert() {// all converts of the elements
	try {attachBlankToAllLinks();} catch(e) {};// try to convert links to target=_blank (profile only)
	$els = $$('a.js_profile_over');
	$each($els, function (el, i, obj) {
		el.addEvent('mouseenter', function(event){profile_over('http://s'+BM_MAIN_DOMAIN+'/profile/?act=over&rel_id='+el.type, true)});
		el.addEvent('mouseleave', function(event){profile_over('http://s'+BM_MAIN_DOMAIN+'/profile/?act=over&rel_id='+el.type, false)});
	});	
	window.utils.inputFocus();//use 'default' property for inputs and textareas
}

//rewrite all the 'js_profile_over' classes
try {
	window.addEvent('domready', function() {
		profile_convert();
	});
} catch(e) {}

// TODO: rewrite using the form
function profile_sendmail_simple(href, tAreaId) {
	var tArea = document.getElementById(tAreaId);
	if (tArea.value == '') return false;
	href = href + '&text=' + encodeURIComponent(tArea.value);
	profile_send_request(href, 'profile_sendmail_simple_cb');
}

function profile_sendmail_simple_cb(res) {
	if (res > 0) {
		profile_message('profile_big_sendmail', false);
		profile_message('profile_big_message_ok', true);
	}
	if (res == -1) {// not logged
		ShowLoginForm(window.location.href);
	}
}


var profile_glob_last_cb = '';
var profile_glob_message = '';
function profile_send_request(href, callback) {
	profile_glob_last_cb = callback;
	var req  = href+'&callback=profile_send_request_callback'; 
	bObj = new JSONscriptRequestUnique(req); 
	bObj.buildScriptTag(); 
	bObj.addScriptTag();
	return true;	
}

function profile_send_request_callback(res) {
	if (profile_glob_last_cb != '') eval(profile_glob_last_cb+'('+res+')');
}

var profile_glob_toHide = '';
var profile_glob_toShow = '';
function profile_button(toHide, toShow, href, messageId) {
	profile_glob_toHide = toHide;
	profile_glob_toShow = toShow;
	if (messageId!='') profile_glob_message = messageId;
	profile_send_request(href, 'profile_button_cb');
	return false;
} 

function profile_button_cb(res) {
	if (res > 0) {
		if (profile_glob_toHide != '') {
			document.getElementById(profile_glob_toHide).style.display='none';
			profile_glob_toHide = '';
		}
		if (profile_glob_toShow != '') {
			document.getElementById(profile_glob_toShow).style.display='block';
			profile_glob_toShow = '';
		}
		profile_message(profile_glob_message, true);
	}
	if (res == -1) {// not logged
		ShowLoginForm(window.location.href);
	}
}


function profile_hideObj(name)
{
    var obj=document.getElementById(name); 
    if(obj)
    {
        obj.style.visibility='hidden';
        obj.style.position='absolute';
        obj.style.zIndex='-1';
    }
    obj=null;

}

function profile_showObj(name)
{
    var obj=document.getElementById(name); 
    obj.style.position='';
    obj.style.zIndex='';
    obj.style.visibility='visible';
    obj=null;
}

function profile_showObjAbs(name)
{
    var obj=document.getElementById(name); 
    obj.style.visibility='visible';
    obj.style.zIndex='10';
    obj=null;
}

function profile_hideObjAbs(name)
{
    var obj=document.getElementById(name); 
    obj.style.visibility='hidden';
    obj.style.zIndex='-1';

    obj=null;
}

function profile_getClientSize()
{
  return {'w': document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth, 'h':document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight};
}

function profile_getClientSize() {
  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 {'w': myWidth, 'h': myHeight};
}

function profile_getScrollXY() {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return { 'x': scrOfX, 'y': scrOfY };
	}

function profile_getPosFast(obj)
{
    var x = obj.offsetLeft;
    var y = obj.offsetTop;
    obj=obj.offsetParent;
    while(obj.offsetParent)
    {
        x+=obj.offsetLeft;
        y+=obj.offsetTop;
        obj=obj.offsetParent;
    }
    return {'x': x, 'y': y};
}

//shows float div with id=divId
var profile_over_cache = new Array();
var profile_over_first_call = true;
var profile_over_timeout = null;
var profile_over_lock = false;
var profile_over_current_href = '';
//var profile_over_default_inner = '<div class="profile_gray" style="width: 292px;"><div class="pl_out"></div><div class="pl_in"></div><div class="profile_pad3"><div class="profile_pad3"><div class="profile"><div class="pl_out bordgray"></div><div class="pl_in bordgray"></div><div class="profile_pad2" style="width: 262px; height:150px"><div class="pr_out bordtop"></div><div class="pr_in bordtop"></div>loading...</div></div></div>';
var profile_over_default_inner = '';
function profile_over(href, isShow) {
    clearTimeout(profile_over_timeout);
    if (isShow) profile_over_hide();
    
	divId = 'profile_over_container';
	if (profile_over_first_call) {// initiating
		headLoc = document.getElementsByTagName("body").item(0);
		containerObj = document.createElement("div");
		containerObj.setAttribute("id", "profile_over_container");
		containerObj.onmouseup= function() {profile_over_cache[profile_over_current_href]=null;};
		containerObj.onmouseover= function() {profile_over_prolong();};
		containerObj.onmouseout= function() {profile_over_timeout = setTimeout('profile_over_hide()', 300);};
		headLoc.appendChild(containerObj);
		containerObj.innerHTML = profile_over_default_inner;
		containerObj.style.position = "absolute";
		containerObj.style.zIndex = 100;
		profile_over_first_call = false;
	}
	
	if (href=='') return false;
	
    obj=document.getElementById(divId);
    
    if (isShow) {
    	
    	if (obj.style.display == 'block') {
    		obj.style.display='none';// hide previose
    		obj.innerHTML = profile_over_default_inner;
    	}
		profile_over_current_href = href;
		//if (profile_over_cache[href]) {
		//	obj.innerHTML = profile_over_cache[href];
		//} else {
			bObj = new JSONscriptRequestUnique(href); 
			bObj.buildScriptTag(); 
			bObj.addScriptTag();
		//}
		
   		profile_over_timeout = setTimeout('profile_over_show()', 500);
		
    	
    } else {
    	profile_over_timeout = setTimeout('profile_over_hide()', 200);
    	//obj.style.display='none';
    	//obj.innerHTML = profile_over_default_inner;
    }
    return false;
}

function profile_over_show() {
	divId = 'profile_over_container';	
    obj=document.getElementById(divId);	
	// TODO: here we need a smart finding the position to show
	var w = 300;
	var h = 150;
	var winCenterW = profile_getClientSize()['w']/2;
	var winCenterH = profile_getClientSize()['h']/2;

	if (profile_mouse_y - profile_getScrollXY()['y'] <= winCenterH)
		obj.style.top=profile_mouse_y + 5 + "px";
	else
		obj.style.top=profile_mouse_y - h - 5 + "px";

		if (profile_mouse_x - profile_getScrollXY()['x'] <= winCenterW)
		obj.style.left=profile_mouse_x + 5 + "px";
	else
		obj.style.left=profile_mouse_x - w - 5 + "px";
		
	obj.style.opacity = 0;
	//obj.style.filter = 'alpha(opacity=0)';
	obj.style.display='block';
	
	try {
		if (profile_isIE) throw 'IE';
		try {
			myFx = new Fx.Style(obj, 'opacity').start(0,1);
		} catch(e) {
			myFx = new Fx.Tween(obj).start('opacity', 0,1); // moo 1.2
		}
	} catch(e) {
    	obj.style.opacity = 100;
	}
}


function profile_jsonp(href, callback, container) {
	if (href.indexOf('?') == -1) href +='?'; else href+='&';
	bObj = new JSONscriptRequestUnique(href+'_pcb='+callback+'&_cntnr='+container); 
	bObj.buildScriptTag(); 
	bObj.addScriptTag();
}


function profile_create_container(id) {
	if (!document.getElementById(id)) {
		headLoc = document.getElementsByTagName("body").item(0);
		containerObj = document.createElement("div");
		headLoc.appendChild(containerObj);
		containerObj.setAttribute("id", id);
		containerObj.style.position = "absolute";
		containerObj.style.zIndex = 100;
	}
	containerObj.innerHTML='';
    return document.getElementById(id);
}


//shows float div with id=divId
function profile_float(href) {
	obj = profile_create_container('profile_float_container');
	obj.style.top=profile_mouse_y + 5 + "px";
	obj.style.left=profile_mouse_x + 5 + "px";
	profile_jsonp(href, 'profile_float_show', 'profile_float_container');
    return false;
}

function profile_float_show(content) {
	obj = profile_create_container('profile_float_container');
	// TODO: here we need a smart finding the position to show
	obj.innerHTML=content;
	obj.style.opacity = 0;
	obj.style.display='block';
	try {
		//obj.style.filter = 'alpha(opacity=0)';
		if (profile_isIE) throw 'IE';
		try {
			myFx = new Fx.Style(obj, 'opacity').start(0,1);
		} catch(e) {
			myFx = new Fx.Tween(obj).start('opacity', 0,1); // moo 1.2
		}	    
	} catch(e) {
		obj.style.opacity = 100;
	}    
}

function profile_over_prolong() {
	clearTimeout(profile_over_timeout);
}

function profile_over_hide() {
	try {
		divId = 'profile_over_container';
		obj=document.getElementById(divId);
		obj.style.display='none';
		obj.innerHTML = profile_over_default_inner;
	} catch(e) {}
}


function profile_over_callback(content) {
	obj=document.getElementById('profile_over_container');
	profile_over_cache[profile_over_current_href] = content;
	obj.innerHTML = content;
}

function profile_showFloat(div2hide, div2show, divId) {
	try {// hide if need
	    obj2hide=document.getElementById(div2hide);
	    obj2hide.style.display='none';
	} catch(e) {}
    obj2show=document.getElementById(div2show);
    obj2show.style.display='block';
    obj=document.getElementById(divId);
    pos = profile_getPosFast(obj2show);
    obj.style.top = pos.y + 28 + "px";
    obj.style.left = pos.x - 245 + "px";
    obj.style.display='block';
    return false;
}

function profile_hideFloat(div2hide, div2show, divId) {
    obj2show=document.getElementById(div2show);
    obj2hide=document.getElementById(div2hide);
    obj=document.getElementById(divId);
    obj2hide.style.display='none';
    obj2show.style.display='block';
    obj.style.display='none';
    return false;
}

var profile_message_timeout=null;
var profile_message_last='';
// shows the DIV for some time, then hides
function profile_message(divId, isShow, isFixed) { // isFixed=true - div will not disappear with time
	if (divId == 'profile_big_sendmail' && !profile_user_logged) {
		ShowLoginForm(window.location.href);
		return false;
	}
	if (!document.getElementById(divId)) return false;
    obj=document.getElementById(divId);
	obj.style.display='none';
	if (profile_message_timeout) {
	    clearTimeout(profile_message_timeout);		
	    if (o = document.getElementById(profile_message_last))
	    	o.style.display='none';
	}	
    if (isShow) {

    	profile_message_last = divId;
        obj.style.display='block';
		if (!isFixed) 
		profile_message_timeout = setTimeout('profile_message("'+divId+'",false)', profile_hide_timeout);
    }
    return false;
}
//=====================================================================================================
// JSONP
function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl; 
    this.noCacheIE = '&r=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}
JSONscriptRequest.scriptCounter = 1;
JSONscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    if (profile_isIE) src = this.fullUrl + this.noCacheIE;
    else src = this.fullUrl;
    this.scriptObj.setAttribute("src", src);
    this.scriptObj.setAttribute("id", this.scriptId);
}
JSONscriptRequest.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);  
}
JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}

function JSONscriptRequestUnique(fullUrl) {
    this.fullUrl = fullUrl; 
    this.noCacheIE = '&r=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptIdUnique';
}
JSONscriptRequestUnique.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    if (profile_isIE) src = this.fullUrl + this.noCacheIE;
    else src = this.fullUrl;
    this.scriptObj.setAttribute("src", src);
    this.scriptObj.setAttribute("id", this.scriptId);
}
JSONscriptRequestUnique.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);  
}
JSONscriptRequestUnique.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}
//=====================================================================================================

function UtilsObject() {
	// utils properties here
}

mixin(UtilsObject.prototype, {
	inputFieldActivate : function (obj, mode) {
		if (mode) {// focus
			if (obj.value == obj.title) {
				obj.value = ''
				//obj.className = obj.className.replace(new RegExp("(^|\\s)gray(?:\\s|$)"),"$1");
				$(obj).removeClass('gray')
			}
		} else {// blur
			if (obj.value == '') {
				obj.value = obj.title
				//obj.className = obj.className+" gray"
				$(obj).addClass('gray')
			}
		}
		return false
	},

	isValidEmail : function (str) {
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0)
	},
	
	getObjProps : function(obj) {
	    var w = obj.offsetWidth
	    var h = obj.offsetHeight
	    var x = obj.offsetLeft
	    var y = obj.offsetTop
	    obj=obj.offsetParent
	    while(obj.offsetParent) {
	        x+=obj.offsetLeft
	        y+=obj.offsetTop
	        obj=obj.offsetParent
	    }
	    return {'x': x, 'y': y, 'w': w, 'h': h}
	},

	sendJSONP: function(href) {
		bObj = new JSONscriptRequest(href)
		bObj.buildScriptTag()
		bObj.addScriptTag()		
	},
	
	obj: function(id) {
		return document.getElementById(id)
	},
	
    addEvent: function(obj,name,func){   
        if(obj.addEventListener) {    
            obj.addEventListener(name, func, false);    
        }
        else if (obj.attachEvent) {    
          obj.attachEvent('on'+name, func); 
        }
        else {throw 'Error'};
      }
})


window.utils = {
	inputFocus : function () {
	  	$each($$('INPUT', 'TEXTAREA'), function (obj) {
	  		if (obj.getProperty('default')) {
	  			$(obj).addEvent("focus", function() {
					if ($(this).value == $(this).getProperty('default')) {
						$(this).value = '';
						$(this).removeClass('gray');				
					}
				});
				$(obj).addEvent("blur", function() {
					if ($(this).value == '' || $(this).value == $(this).getProperty('default')) {
						$(this).value = $(this).getProperty('default');
						$(this).addClass('gray');
					}
				});
				if ($(obj).value != '' && $(obj).value != $(obj).getProperty('default')) $(obj).removeClass('gray');
				if ($(obj).value == '') $(obj).value = $(obj).getProperty('default');
	  		}
	  	});
	}
}

var profileUtils = new UtilsObject()
//=====================================================================================================

function ProfileObject(name, caller) {
    this.hide_message=1000	// should be set in child	
	this.hideMessage_time_out = null // timeout holder
	this.container_name = ''// should be set in child	
	this.container_obj = null	
	this.caller = caller
	this.container_content = ''
	this.hide_on_load_elements = [] // should be set in child	
	this.hide_on_load_elements_classes = []
	this.mx = 0
	this.my = 0
	this.visible = false
	this.progress = false // true when animation in progress
	if (!document.getElementById(name)) {
		headLoc = document.getElementsByTagName("body").item(0)
		this.container_obj = document.createElement("div")
		headLoc.appendChild(this.container_obj)
		this.container_obj.setAttribute("id", name)
		this.container_obj.style.position = "absolute"
		this.container_obj.style.zIndex = 100
	}
    this.container_props = ''
	this.caller_props = profileUtils.getObjProps(this.caller)
	
	//this.container_obj.innerHTML=''
}
mixin(ProfileObject.prototype, {
	jsonp : function (callback, href, additional_uri) {
		if (!href) href=''
		href = this.href+href
		if (href.indexOf('?') == -1) href +='?'; else href+='&'
		if (additional_uri) href += additional_uri + '&'
		bObj = new JSONscriptRequest(href+'_pcb='+callback+'&_cntnr='+this.container_name)
		bObj.buildScriptTag()
		bObj.addScriptTag()
	},	
	
	toggleWindow : function () {
		if (this.container_obj.style.display=='none') this.loadWindow()
		else this.hideWindow()
	},
	
	hideWindow : function(timeout) {
		if (this.progress) return false
		if (timeout == undefined) {
		    //this.hideAlphaElements()
		    try{
		    	if (profile_isIE) throw 'IE'
				try {
					myFx = new Fx.Style(this.container_obj, 'opacity').start(1,0);
				} catch(e) {
					myFx = new Fx.Tween(this.container_obj).start('opacity', 1,0); // moo 1.2
				}		    	
	    	} catch(e) {
				obj.style.opacity = 0;
			}		    
		    this.timeout_window_hide = setTimeout(this.container_name+'.killWindow()', 600)
		    this.progress = true
		}
		
		else this.timeout_window_hide = setTimeout(this.container_name+'.hideWindow()', timeout)
		try {showHideFlash(true)} catch(e) {}
		return false // for links with #
	},		
	
	killWindow : function() {
		this.container_obj.style.display='none'
		this.restoreAlphaElements()		
		this.visible = false
		this.progress = false
		//document.getElementById(this.container_name).innerHTML=''
	},	
	
	loadWindow : function(additional_uri) {
		this.my=profile_mouse_y
		this.mx=profile_mouse_x
		if (this.container_content == '') this.jsonp(this.container_name+'.showWindow', '', additional_uri)
		else this.showWindow()
	},
	
	showWindow : function (content) {
		try {showHideFlash(false)} catch(e) {}
		
//		content = content.replace(/\\'/g, "'")
		if (this.progress) return false
		clearTimeout(this.timeout_window_hide);
		obj = this.container_obj
		if (content) {
			this.container_content = content
			obj.innerHTML=this.container_content
			if (this.container_props =='') this.container_props = profileUtils.getObjProps(this.container_obj)
			this.findPosition() 
		}
		//this.hideAlphaElements()
    	obj.style.opacity = 0
		obj.style.display='block'		
    	try {
			if (profile_isIE) throw 'IE'
			try {
				myFx = new Fx.Style(obj, 'opacity').start(0,1)
			} catch(e) {
				myFx = new Fx.Tween(obj).start('opacity', 0,1) // moo 1.2
			}		    
    	} catch(e) {
			obj.style.opacity = 100;
		}
    	//var ta = $(obj).getElement('textarea[class=textarea_text]');
    	//ta.focus();
    	
	    this.visible = true
	    this.progress = true
	    setTimeout(this.container_name + '.activateWindow()', 600);
	},
	
	activateWindow : function () {
		try{
			var ta = $(this.container_obj).getElement('textarea[class=textarea_text]');
			if (ta !== undefined) ta.focus();
		} catch(e) {}
	    this.progress = false	
	    //this.restoreAlphaElements()
	},

	hideAlphaElements : function () {
		try{
			if (profile_isIE) 
				for (var i=0; i < this.hide_on_load_elements.length; i++) { 
					this.hide_on_load_elements_classes[i] = profileUtils.obj(this.container_name+'_'+this.hide_on_load_elements[i]).className 
					profileUtils.obj(this.container_name+'_'+this.hide_on_load_elements[i]).className = ''
			}
		}catch(e){}
	},	
	
	restoreAlphaElements : function () {
		try{
			if (profile_isIE)
				for (var i=0; i < this.hide_on_load_elements.length; i++) { 
					profileUtils.obj(this.container_name+'_'+this.hide_on_load_elements[i]).className = this.hide_on_load_elements_classes[i]
				}
		}catch(e){}
	},
	
	hideMessage : function() {
		document.getElementById(this.message_container).style.display='none'
		return false // for links with #
	},

	loadMessage : function(type) {
		if (document.getElementById(this.message_container_name)) {
			this.message_container = this.message_container_name
			this.jsonp(this.container_name+'.showMessage', '?_mtype='+type)
		}
		return false; // for links with #
	},	
	
	showMessage : function(content) {
		document.getElementById(this.message_container).innerHTML=content
		document.getElementById(this.message_container).style.display='block'
	},	
	
	findPosition : function() { 
		// TODO: here we need a smart finding the position to show
		this.container_obj.style.top=this.my + 5 + "px"
		this.container_obj.style.left=this.mx + 5 + "px"
	}	
})

//==============================================
function Messenger(name, caller, owner_id) {
    Messenger.superclass.constructor.apply(this, arguments)
    this.owner_id = owner_id
	this.container_name = name
	this.message_container_name = 'message_container'
	this.href = 'http://talk'+BM_MAIN_DOMAIN+'/service_wrapper/'
	this.main_content = this.container_name+'_message_main'	
	this.hide_on_load_elements = ['tohide_shadow_left', 'tohide_shadow_pixel2', 'tohide_shadow_left_corner_top', 'tohide_shadow_top' ,'tohide_shadow_right_corner_top', 'tohide_shadow_right', 'tohide_shadow_left_corner', 'tohide_shadow_pixel', 'tohide_shadow_right_corner', 'tohide_shadow_bottom']
	this.content = ''
	this.target = ''
	this.targets = []
	this.errors = [this.container_name+'_message_error1',this.container_name+'_message_error2']
	this.loadWindow()
}
extend(Messenger, ProfileObject)
mixin(Messenger.prototype, {

	loadWindow : function() {
		additional_uri = 'owner_id=' + this.owner_id
		return ProfileObject.prototype.loadWindow.apply(this, [additional_uri])
	},	
	
	showWindow : function(content) {
		if (this.progress) return false
		this.caller.style.fontWeight='700' // make bold
		return ProfileObject.prototype.showWindow.apply(this, [content])
	},	

	hideWindow : function(timeout) {
		if (this.progress) return false
		this.caller.style.fontWeight='400' // remove bold
		return ProfileObject.prototype.hideWindow.apply(this, [timeout])
	},		

	send : function() {
		if (this.checkForm()) {
			uri = 't=' + encodeURIComponent(this.target)
			uri = uri + '&owner_id=' + this.owner_id
			uri = uri + '&c=' + encodeURIComponent(this.content)
			link = this.caller.href
			if (document.location.hash != '') link = link.substr(0, link.length-1) + document.location.hash
			uri = uri + '&link=' + encodeURIComponent(link)
			uri = uri + '&ts=' + this.targets.toString()
			this.jsonp(this.container_name+'.sendCb', '?'+uri)
		}
	},

	checkForm : function() {
		this.hideError()
		if (this.owner_id == 'undefined' || this.owner_id == '') {
			if ( (this.target + this.targets.toString() == '') || (this.targets.toString() == '' && !profileUtils.isValidEmail(this.target)) ) {
				this.showError(this.container_name+'_message_error1')
			}
		} else {
			if (this.content == '') {
				this.showError(this.container_name+'_message_error2')
			}
		}
		if (document.getElementById(this.container_name+'_message_error_container').style.display == 'block') return false
		else return true
	},

	hideError : function() {
		for (var i=0; i < this.errors.length; i++) 
			document.getElementById(this.errors[i]).style.display='none'
		document.getElementById(this.container_name+'_message_error_container').style.display='none'
		try{document.getElementById(this.container_name+'_low_closer').style.display='block'} catch(e) {}
	},	
	
	showError : function(type) {
		try{document.getElementById(this.container_name+'_low_closer').style.display='none'} catch(e) {}
		document.getElementById(type).style.display='block'
		document.getElementById(this.container_name+'_message_error_container').style.display='block'
	},
	
	showSuccess : function() {
		document.getElementById(this.container_name+'_message_ok').style.display='block'
	},	
	
	hideMain : function() {
		this.hideError()
		document.getElementById(this.main_content).style.display='none'
	},	
	
	sendCb : function(result) {
		this.hideError()
		this.hideMain()
		this.showSuccess()
		this.hideWindow(2000)
		this.container_content = '' // force reload window on next use
		this.content = ''
		this.target = ''
		this.targets = []				
			
	},

	addTarget : function(tgt) {
		this.targets.push(tgt)
	},
	
	delTarget : function(tgt) {
		this.targets.pop(tgt)
	},
	
	findPosition : function() { //overloaded
		// TODO: here we need a smart finding the position to show
		y = this.caller_props.y + this.caller_props.h
		this.container_obj.style.top = y + "px"
		x = this.caller_props.x + this.caller_props.w/2 - this.container_props.w/2
		if (x<10) x = 10
		this.container_obj.style.left = x + "px"
	},
	
	checkAllContacts : function(obj, isOn) {
		//TODO: rewrite not using mootools
		li = $(obj).getChildren()
		this.targets = []
		for (i=0; i < li.length; i++) {
			checker = li[i].getChildren()[0]
			if (isOn) {
				checker.checked=true
				if (checker.value > 0) eval(this.container_name).addTarget(checker.value)
			} else {
				checker.checked=false
			}
		}
	}	
})

function createWindow(cls, uniq, caller, owner_id) {
	if (!window[cls+"_"+uniq]) window[cls+"_"+uniq] = new window[cls](cls+"_"+uniq, caller, owner_id)
	else window[cls+"_"+uniq].toggleWindow()
	return false // for links with #
}


//=====================================================================================================
//копирует все свойства из src в dst,
//включая те, что в цепочке прототипов src до Object
function mixin(dst, src){
 // tobj - вспомогательный объект для фильтрации свойств,
 // которые есть у объекта Object и его прототипа
 var tobj = {} 
 for(var x in src){
     // копируем в dst свойства src, кроме тех, которые унаследованы от Object
     if((typeof tobj[x] == "undefined") || (tobj[x] != src[x])){
         dst[x] = src[x];
     }
 }
 // В IE пользовательский метод toString отсутствует в for..in
 if(document.all && !document.isOpera){
     var p = src.toString;
     if(typeof p == "function" && p != dst.toString && p != tobj.toString &&
      p != "\nfunction toString() {\n    [native code]\n}\n"){
         dst.toString = src.toString;
     }
 }
}

function extend(Child, Parent) {// создает потомка
    var F = function() { }
    F.prototype = Parent.prototype
    Child.prototype = new F()
    Child.prototype.constructor = Child
    Child.superclass = Parent.prototype    
}

profileUtils.addEvent(document, 'mousemove', function(e){ 
    e=e||window.event; 
    profile_mouse_x = 0;
    profile_mouse_y = 0;
	if (e.pageX || e.pageY)
    {
		profile_mouse_x = e.pageX;
		profile_mouse_y = e.pageY;
	}
	else if (e.clientX || e.clientY)
    {
		try {
			profile_mouse_x = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			profile_mouse_y = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		} catch(e) {}
		
	}
}); 
