var isIE7=!(navigator.appVersion.indexOf("MSIE 7.")==-1);

function setupMask() {
	var mask=document.getElementById("messagebg");
			
	var fullHeight=getViewportHeight();
	var fullWidth=getViewportWidth();
	
	if(fullHeight>document.body.scrollHeight) {
		popHeight=fullHeight;
	} else {
		popHeight=document.body.scrollHeight;
	}
	
	if(fullWidth>document.body.scrollWidth) {
		popWidth=fullWidth;
	} else {
		popWidth=document.body.scrollWidth;
	}
	
	mask.style.height=popHeight+"px";
	mask.style.width=popWidth+"px";
}

function getViewportHeight() {
	if(window.innerHeight!=window.undefined) return window.innerHeight;
	if(document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if(document.body) return document.body.clientHeight;
}

function getViewportWidth() {
	if(window.innerWidth!=window.undefined) return window.innerWidth; 
	if(document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if(document.body) return document.body.clientWidth; 
}

function getScrollTop() {
	if(self.pageYOffset) {
		return self.pageYOffset;
	}
	else if(document.documentElement&&document.documentElement.scrollTop) {
		return document.documentElement.scrollTop;
	}
	else if(document.body) {
		return document.body.scrollTop;
	}
}

function addEvent(obj, evType, fn) {
	if(obj.addEventListener) {
    	obj.addEventListener(evType, fn, false);
	    return true;
	} else if(obj.attachEvent) {
    	var r=obj.attachEvent("on"+evType, fn);
	    return r;
 	} else {
    	return false;
	}
}

function centerPopupWindow() {
	var popup=document.getElementById("messagebox");
	if(popup!=null) {
		width=popup.offsetWidth;
		height=popup.offsetHeight;
		var scTop=parseInt(getScrollTop(),10);
		var scLeft=parseInt(document.body.scrollLeft,10);
		var fullHeight=getViewportHeight();
		var fullWidth=getViewportWidth();		
		popup.style.top=(scTop+((fullHeight-(height))/2))+"px";
		popup.style.left=(scLeft+((fullWidth-width)/2))+"px";
		popup.style.visibility="visible";
	}
}

addEvent(window, "resize", centerPopupWindow);
addEvent(window, "scroll", centerPopupWindow);

function removeElement(element) {
	element.parentNode.removeChild(element);	
}

function closePopupWindow() {
	var popup=document.getElementById("messagebg");
	if(popup!=null) {
		removeElement(popup);
	}
}

function setPostDeleted(postid) {
	var post=document.getElementById("post"+postid);
	if(post!=null) {
		removeElement(post);
	}
}

function showPost(postid) {
	document.location.href="/"+postid+"/#";	
}

function postComment(postid, key) {
	var author=document.getElementById("newcommentauthor").value;
	var email=document.getElementById("newcommentemail").value;
	var website=document.getElementById("newcommentwebsite").value;
	var text=document.getElementById("newcommenttext").value;
	var honeypot=document.getElementById("newcommenthoneypot").value;
	var error=false;
	if(author==null || author=="") {
		document.getElementById("newcommentauthorerror").style.display="block";
		error=true;
	} else {
		document.getElementById("newcommentauthorerror").style.display="none";
	}
	if(email==null || email=="" || !email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
		document.getElementById("newcommentemailerror").style.display="block";
		error=true;
	} else {
		document.getElementById("newcommentemailerror").style.display="none";
	}
	if(text==null || text=="") {
		document.getElementById("newcommenttexterror").style.display="block";
		error=true;
	} else {
		document.getElementById("newcommenttexterror").style.display="none";
	}
	if(error) return;
	var data=new Hash();
	data.set('author',author);
	data.set('email',email);
	data.set('website',website);
	data.set('text',text);
	data.set('honeypot',honeypot);
	data.set('authenticity_token',key);
	new Ajax.Updater('overlaycontainer', postid+'/comments/create', {evalScripts:true, method:"post", parameters:data});
}

function refreshPage() {
	window.location.reload();
}

function mouseOverButton(self) {
	self.style.top=isIE7?"5px":"0px";
}

function mouseOutButton(self) {
	self.style.top=isIE7?"-5px":"-10px";
}

function setMinHeight(viewportHeight) {
	var content=document.getElementById("content");
	content.style.minHeight = (viewportHeight-140)+"px";
}

var viewportheight_old=0;

function checkSize() {
	var height=getViewportHeight();
	if(height!=viewportheight_old) {
		viewportheight_old=height;
		setMinHeight(height);
	}
}

function setupWindowSizeCheck() {
	window.setInterval("checkSize()", 500);
	checkSize();
}

function toggleDiv(self, targetID) {
	var target=document.getElementById(targetID);
	if(target.style.display=="block") {
		target.style.display="none";
		self.className=self.className.replace(/dropdown_opened/,'dropdown_closed');
	} else {
		target.style.display="block";
		self.className=self.className.replace(/dropdown_closed/,'dropdown_opened');
	}
}
