// JavaScript Document
function getOffsetLeft(elm) {
        var mOffsetLeft = elm.offsetLeft;
        var mOffsetParent = elm.offsetParent;
		if(!mOffsetParent) mOffsetParent=0;
        var parents_up = 1;

        while(parents_up > 0) {
                mOffsetLeft += mOffsetParent.offsetLeft;
                mOffsetParent = mOffsetParent.offsetParent;
                parents_up--;
        }

        return mOffsetLeft;
}
var babbler=false;
$(document).ready(function(){
		//Кнопка «Голосовать»
		$('#voter').click(function(){
			if( $(this).attr('rel')=='auth_only' || vote_obj==false ) return false;
			$(this).hide();
			$(this).parent().animate({
				height: "29px",
				width: "175px"
			}, 600 );
			$('#rates').fadeIn('slow');
			$('#volImage').css('clip',"rect(0px, 87px, 20px, 87px)");
			return false;
		});
		//Цифры голосования
		$('.vb').each(function(){
			$(this).mouseover(function(){
				var pos=new Array();
				pos.push(87);
				pos.push(parseInt($(this).attr('href')));
				$('#volImage').css('clip',"rect(0px, "+Math.max(pos[0],pos[1])+"px, 20px, "+Math.min(pos[0],pos[1])+"px)");
			});
			$(this).click(function(){
				//$('#hiddenFrame').attr('src','/lib/voter.php?obj='+vote_obj+'&vote='+$(this).html());
				layoutUsersPage('vote','obj='+vote_obj+'&vote='+$(this).html());
				return false;
			});

		});
		//Если не авторизирован
		$("a[rel='auth_only']").click(function(){
			if(!babbler) babbler=document.getElementById('bubblectrl');
			if(babbler==undefined) return false;
				var tst=this,i=2;
				while(tst.tagName!='li' && i>0 ) {tst=tst.parentNode; i--;}

				var offs=$(tst).position( );
				$(babbler).css('margin-left',(offs.left)+'px');
				$(babbler).fadeIn('slow');
			return false;
		});
});

var timeOutBubble;
function layoutUsersPage(action,value,send) {
	//action => {vote, friend, send_friend, mail?}
	send=send || 0;
	clearTimeout(timeOutBubble);
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if(req.responseJS) {
				if(req.responseJS.answer && req.responseJS['class']) {
					var obj=$(document.getElementById("volumer"));
					var bt=document.getElementById("bubble");
					if(bt!=undefined){ $(bt).fadeIn("slow"); $(bt).addClass("plus")}
					obj.html(req.responseJS.answer);
					obj.attr('class',req.responseJS['class']);
					$("#bubble").css("backgroundPosition",req.responseJS.backPosition+"px 0px");
					timeOutBubble=setTimeout('$("#bubble").fadeOut("slow")', 10000);
					if(req.responseJS.run_previewMessages && req.responseJS.run_previewMessages==1) {
						previewMessages([0,0]);
					}
					if(req.responseJS.send && req.responseJS.send==1){
						$('#u_addFriend').parent().get(0).className='u-vote';
						$('#u_addFriend').attr('send',0);
						$('#u_addFriend').attr('title','');
					}
					if(req.responseJS.send && req.responseJS.send==0){
						$('#u_addFriend').parent().get(0).className='u-unvote';
						$('#u_addFriend').attr('send',1);
						$('#u_addFriend').attr('title','');
					}
				}
			}
			if(req.responseText!='') alert(req.responseText);
	    }
	}
	req.open(null, '/lib/voter.php', true);
	req.send( { q: value,
				action: action,
				send: send } );
}
