var rollerOldOnload = window.onload;
window.onload = function(e) {
	rollerInit();
	if (rollerOldOnload) {
		rollerOldOnload();
	}
};

var rollerCnt = 0;
function rollerInit() {
	var page = window.location.href.split("?").shift().split("/").pop();
	var page = (page ? page : "home").split(".html").join("");
		
	var elements = document.getElementsByTagName("*");
	for (var i = 0; i < elements.length; i ++) {
		
		aw = elements[i].className.split(" ").shift().split("-");
		if (aw[0] == "unrolled" || aw[0] == "rolled") {
			var n = (page+"_"+rollerCnt).replace(/[^\w\d]+/g,"_");
						
			var o = (elements[i].title ? elements[i].title : "").split("|",2);
			
			var useTitle = o.length ? o.shift() : "";
			elements[i].title = "";
			
			var options = o.length ? o.shift().split(",") : [];
			
			var unrolled = aw[0] != "rolled";
			
			var v = docCookies.getItem("rollstate_"+n);
			
			if (options.indexOf("onetime") != -1 && v == undefined && docCookies.getItem("visited_"+page)) {
				unrolled = 0;
			} else if (v != undefined) {
				unrolled = Number(v);
			}

			elements[i].style.overflow = "hidden";
			elements[i].style.position = "relative";
			
			if (options.indexOf("nofade") == -1) {
				var mask = document.createElement("div");
				mask.className = "rollMask";
				mask.style.position = "absolute";
				mask.style.top = "0px";
				mask.style.width = elements[i].offsetWidth + "px";
				mask.style.height = elements[i].offsetHeight + "px";
				elements[i].appendChild(mask);
			} else {
				var mask = false;
			}
			
			var control = document.createElement("div");
			control.mask = mask;
			control.el = elements[i];
			control.options = options;
			control.useTitle = useTitle;
			
			setRollerControl(control,!unrolled);
			control.style.position = "absolute";
			control.style.top = "0px";
			control.style.cursor = "pointer";
			control.id = "rollerControl"+rollerCnt;

			control.n = n; 
			control.unrollInt = null;
			rollerCnt ++;
			
			control.onclick = function() {
				var r = this.className == "rollButton";
				docCookies.setItem("rollstate_"+this.n,r?0:1);
				if (this.unrollInt) clearInterval(this.unrollInt);
				this.unrollInt = setInterval("rollerStep("+(r?"0":"1")+",'"+this.id+"')",80);
				setRollerControl(this,r);
			};
						
			elements[i].appendChild(control);
			
			control.origHeight = elements[i].offsetHeight;
			
			setRollerPosition(control,control.pos = unrolled);
		
		}
	}
	docCookies.setItem("visited_"+page,1);
};

function rollerStep(targ,id) {
	var control = document.getElementById(id);

	control.pos += (targ - control.pos) / 8;

	if (setRollerPosition(control,control.pos,targ)) {
		clearInterval(control.unrollInt);
	}
};

function setRollerControl(control,rolled) {
	control.className = rolled ? "unrollButton" : "rollButton";
	control.title = rolled ? "Click to expand" : "Click to shrink";
	control.innerHTML = control.useTitle && rolled ? control.useTitle : "";	
};

function setRollerPosition(control,v,targ) {
	var h = control.offsetHeight;
	control.el.style.height = Math.round(((control.origHeight - h) * v) + h) + "px";
	if (control.mask) {
		if ((control.mask.style.opacity = (1-Math.min(1, v * 4)) * 0.8) == 0) {
			control.mask.style.display = "none";
		} else {
			control.mask.style.display = "block";
		}
	}
	return Math.round(((control.origHeight - h) * v) + h) == (targ ? control.origHeight : h);
};

