/*@cc_on 
  var doc = document;
  eval('var document = doc');
@*/

var movePrototype = {
	target			:0,
	moveCron		:null,
	mb				:null,
	mbmt			:null,
	mbmts			:null,
	maxPosition		:null,
	minPosition		:null,
	content			:null,
	sidebar			:null,
	framelate		:15,
	defaultMarginT	:0,
	createElements:function(){
		this.mb      = document.getElementById("memberblock");
		this.content = document.getElementById("content");
		this.sidebar = document.getElementById("sidebar");
		this.defaultMarginT  = parseInt(Element.getStyle(this.mb,"margin-top").replace("px",''));
		new Ajax.Request("member.php", {
			method    : "get",
			parameters: "time="+ (new Date().getTime()),
			onSuccess : function(httpObj){
				json     = httpObj.responseText;
				name     = json.evalJSON().name;
				position = json.evalJSON().position;
				comment  = json.evalJSON().comment;
				src      = json.evalJSON().src;
				comment  = comment.replace(/\[\[br]]/ig,"<br />")
				//test source
				this.sidebar.style.position = "relative";
				this.mb.style.position = "absolute";
				//create elements
				this.mb.getElementsByTagName("p")[1].removeChild(this.mb.getElementsByTagName("p")[1].firstChild);
				this.mb.getElementsByTagName("p")[1].removeChild(this.mb.getElementsByTagName("p")[1].firstChild);
				span = document.createElement("span");
				span.appendChild(document.createTextNode(position));
				this.mb.getElementsByTagName("p")[1].appendChild(span);
				this.mb.getElementsByTagName("p")[1].appendChild(document.createTextNode(name));
				this.mb.getElementsByTagName("img")[0].src = src;
				this.mb.getElementsByTagName("img")[0].alt = name;
				this.mb.getElementsByTagName("p")[2].innerHTML = comment;
				this.setAction();
			}.bind(movePrototype)
		});
	},
	setAction:function(){
		//min position
		this.minPosition = 0;
		for(i=0; i<this.sidebar.childNodes.length-1; i++){
			if(this.sidebar.childNodes[i].nodeType==1 && this.sidebar.childNodes[i].id != "memberblock"){
				this.minPosition += this.sidebar.childNodes[i].offsetHeight + eval(Element.getStyle(this.sidebar.childNodes[i],"margin-top").replace("px",'') + "+" + Element.getStyle(this.sidebar.childNodes[i],"margin-bottom").replace("px",''));
			}
		}
		//set first parameter
		this.mbmt = this.minPosition;

		if(this.minPosition + this.mb.offsetHeight<this.content.offsetHeight){
			if(this.scrollY()>0)this.moveSelect();
			window.onscroll = function(){this.moveSelect();}.bind(movePrototype);
			window.onresize = function(){this.moveSelect();}.bind(movePrototype);
		}else{
			this.sidebar.style.height = this.minPosition + this.mb.offsetHeight + "px";
		}
	},
	moveSelect:function(){
		//set sidebarheight
		//max position
		this.maxPosition = this.content.offsetHeight-this.mb.offsetHeight;
		//min position
		this.minPosition = 0;
		for(i=0; i<this.sidebar.childNodes.length-1; i++){
			if(this.sidebar.childNodes[i].nodeType==1 && this.sidebar.childNodes[i].id != "memberblock"){
				this.minPosition += this.sidebar.childNodes[i].offsetHeight + eval(Element.getStyle(this.sidebar.childNodes[i],"margin-top").replace("px",'') + "+" + Element.getStyle(this.sidebar.childNodes[i],"margin-bottom").replace("px",''));
			}
		}
		//repair position
		this.target = this.mbmt - (Position.cumulativeOffset(this.mb)[1] - (this.scrollY() + this.windowHeight()*0.5 - (this.mb.offsetHeight*0.5 - this.defaultMarginT*0.5)));
		if(this.target>this.maxPosition){
			this.target = this.maxPosition;
		}else if(this.target<this.minPosition){
			this.target = this.minPosition;
		}
		this.moveAction();
	},
	moveAction:function(){
		if(this.moveCron)clearInterval(this.moveCron);
		this.moveCron = setInterval(this.moveManager.bind(movePrototype),this.framelate);
	},
	moveManager:function(){
		this.mbmts= this.target-this.mbmt;
		(this.mbmts > 0)?this.mbmt = this.mbmt + Math.ceil(this.mbmts * 0.1):this.mbmt = this.mbmt + Math.floor(this.mbmts * 0.1);
		this.mb.style.top = this.mbmt + "px";
		if(this.mbmts < 1 && this.mbmts > -1){
			this.mbmt = this.target;
			this.mb.style.top = this.target + "px";
			clearInterval(this.moveCron);
		}
	},
	scrollY:function(){
		return document.body.scrollTop  || document.documentElement.scrollTop;
	},
	windowHeight:function(){
		return document.viewport.getDimensions().height;
	}
}

window.onload = function(){movePrototype.createElements();};

