function roll(lyr,on,submenu) {
	if (on) {
		lyr.childNodes[0].style.visibility = 'visible';
		if (submenu != '')
			document.getElementById('nav_' + submenu + '_menu').style.visibility = 'visible';
	} else {
		lyr.childNodes[0].style.visibility = 'hidden';
		if (submenu != '')
			document.getElementById('nav_' + submenu + '_menu').style.visibility = 'hidden';
	}
}
function search_clean(str) {
	str = String(str);
	if (str == '')
		return ''
	else
		return str + '/';
}
var resized = new Array();
window.onresize = handle_resize;
window.onscroll = handle_resize;
function handle_resize () {
	var winInHt = Math.max(Math.max(document.body.clientHeight, document.body.offsetHeight),document.body.scrollHeight);
	if (resized.length == 0)
		var winInWd = Math.max(Math.max(document.body.clientWidth, document.body.offsetWidth),document.body.scrollWidth);
	else
		var winInWd = Math.max(Math.max(document.body.clientWidth, document.body.offsetWidth));
	if (winInWd < 970)
		winInWd = 970;
	for (i=0;i<resized.length;i++) {
		if (resized[i][1] == 'fill') {
			document.getElementById(resized[i][0]).style.width = winInWd;
			document.getElementById(resized[i][0]).style.height = winInHt;
		} else if (resized[i][1] == 'center') {
			var scrollY = window.scrollY;
			if (isNaN(scrollY))
				scrollY = document.body.scrollTop;
			document.getElementById(resized[i][0]).style.top = scrollY + (document.body.clientHeight/2) - (document.getElementById(resized[i][0]).offsetHeight / 2);
			document.getElementById(resized[i][0]).style.left = (winInWd - document.getElementById(resized[i][0]).offsetWidth) / 2;
		}
	}
}
function dim_page(dim) {
	if (dim) {
		resized.push(new Array('dimmer','fill'));
		handle_resize();
		document.getElementById('dimmer').style.display = 'block';
	} else {
		document.getElementById('dimmer').style.display = 'none';
		clear_resize('dimmer');
	}
}
function is_user() {
	if (document.getElementById('username').innerHTML == 'guest')
		return false;
	else
		return true;
}
function forgot() {
	document.getElementById('signin').innerHTML = '<div class="signin_contents"><div class="popup_close signin_close"><a href="#" onClick="hide_box(\'signin\');return false;">X</a></div><div class="clear"></div><form id="forgot_form" onSubmit="do_forgot(this);return false;"><div class="signin_row"><b>Forgot your password?</b></div><div class="signin_row"><div class="signin_text">Email:</div><input type="text" name="username" id="username_box" class="signin_textbox"/></div><div class="signin_row"><div class="signin_text"></div><div class="generic_btn generic_btn_left"><a href="#" onClick="do_forgot(document.getElementById(\'forgot_form\'));return false;">Send Me My Password</a></div><div class="generic_btn generic_btn_left"><a href="/registration/">Join StupidVideos</a></div><div class="clear"></div></div><input type="submit" class="hidden"/></form></div>';
	document.getElementById('signin').style.zIndex = 500;
	resized.push(new Array('signin','center'));
	document.getElementById('signin').style.display = 'block';
	handle_resize();
	document.getElementById('username_box').focus();

}
function do_forgot(frm) {
	oXML = new XMLHttpRequest();
	var url = '/registration/send_password.php?e=' + frm.username.value + '&rnd=' + Math.random();
	oXML.open('GET',url,true);
	oXML.onreadystatechange = function() {
		if (oXML.readyState == 4)
			document.getElementById('signin').innerHTML = '<div class="signin_contents"><div class="popup_close signin_close"><a href="#" onClick="hide_box(\'signin\');return false;">X</a></div><div class="clear"></div><div class="signin_row"><b>Forgot your password?</b></div><div class="signin_row">An email containing your password has been sent to ' + frm.username.value + '.<br/><br/></div><div class="signin_row"><div class="signin_text"></div><div class="generic_btn generic_btn_left"><a href="#" onClick="hide_box(\'signin\');guestwall();return false;">Sign In</a></div><div class="generic_btn generic_btn_left"><a href="/registration/">Join StupidVideos</a></div><div class="clear"></div></div><input type="submit" class="hidden"/></form></div>';
	}
	oXML.send(null);
}
function guestwall() {
	sHTML = '<div class="signin_contents"><div class="popup_close signin_close"><a href="#" onClick="hide_box(\'signin\');return false;">X</a></div><div class="clear"></div><form id="signin_form" onSubmit="signin(this);return false;"><div class="signin_row"><div class="signin_text">Username:</div><input type="text" name="username" id="username_box" class="signin_textbox"/></div><div class="signin_row"><div class="signin_text">Password:</div><input type="password" name="pass" class="signin_textbox"/></div><div class="signin_row"><div class="signin_text"></div><input type="checkbox" name="persist"/> Remember Me?</div><div class="signin_row"><div class="signin_text"></div>';
	sHTML += '<div id="signin_btn" class="generic_btn generic_btn_left"><input type="submit" value="Login" style="border:0px;height:18px;overflow:hidden;margin:-2px;padding:0px;background:transparent;color:#FFF;font-family:arial;font-size:11px;font-weight:bold;"/></div>';
	sHTML += '<div class="generic_btn generic_btn_left"><a href="/registration/">Join StupidVideos</a></div><div id="forgot_password" class="btn_adjacent"><a href="#" onClick="hide_box(\'signin\');forgot();return false;">Forgot Password?</a></div><div class="clear"></div></div></form></div>';
	document.getElementById('signin').innerHTML = sHTML;
	document.getElementById('signin').style.zIndex = 500;
	resized.push(new Array('signin','center'));
	document.getElementById('signin').style.display = 'block';
	handle_resize();
	document.getElementById('username_box').focus();
}
function signout() {
	var oXML = new XMLHttpRequest();
	var url = '/registration/process_logout.php?t=' + document.getElementById('login_info').className + '&rnd=' + Math.random();
	oXML.open('GET',url,true);
	oXML.onreadystatechange = function() {
		if (oXML.readyState == 4) {
			if (typeof disable_smooth_signin != 'undefined' && disable_smooth_signin) {
				var loc = String(document.location);
				if (loc.indexOf('?') > -1)
					loc = loc + '&rnd=' + Math.random();
				else
					loc = loc + '?rnd=' + Math.random();
				document.location = loc;
			} else {
				document.getElementById('login_info').innerHTML = oXML.responseText;
			}
		}
	}
	oXML.send(null);
}
function signin(frm) {
	document.getElementById('forgot_password').innerHTML = '';
	document.getElementById('signin_btn').innerHTML = 'Logging In...';
	var oXML = new XMLHttpRequest();
	var url = '/registration/process_login.php?u=' + frm.username.value + '&p=' + encodeURIComponent(frm.pass.value) + '&x=' + frm.persist.checked + '&t=' + document.getElementById('login_info').className + '&rnd=' + Math.random();
	oXML.open('GET',url,true);
	oXML.onreadystatechange = function() {
		if (oXML.readyState == 4) {
			if (oXML.responseText == '<ERR/>') {
				document.getElementById('signin_form').innerHTML = 'That username and password do not<br/>exist in our database.<br/><br/><div class="generic_btn generic_btn_left"><a href="#" onClick="guestwall();return false;">Try Again</a></div>';
			} else if (oXML.responseText == '<UNCONF/>') {
				document.getElementById('signin_form').innerHTML = 'This username is unconfirmed. Please confirm<br/>by clicking on the link in your confirmation email.<br/><br/><div class="generic_btn generic_btn_left"><a href="/registration/processSignup.php?u=' + frm.username.value + '">Resend</a></div>';
			} else {
                window.location.reload(true);
				/*if (typeof disable_smooth_signin != 'undefined' && disable_smooth_signin) {
					var loc = String(document.location);
					if (loc.indexOf('?') > -1)
						loc = loc + '&rnd=' + Math.random();
					else
						loc = loc + '?rnd=' + Math.random();
					document.location = loc;
				} else {
					document.getElementById('login_info').innerHTML = oXML.responseText;
					document.getElementById('signin').innerHTML = '';
					document.getElementById('signin').style.display = 'none';
					clear_resize('signin');
					if (pending_args.length > 0)
						display_box(pending_args[1],pending_args[2]);
					else
						dim_page(false);
					pending_args = new Array();
				}*/
			}
		}
	}
	oXML.send(null);	
}	
var pending_args = new Array();
function display_box(text,do_guestwall) {
	dim_page(true);
	if (do_guestwall && !is_user()) {
		pending_args = new Array('popup',text,do_guestwall);
		guestwall();
	} else {
		document.getElementById('popup').innerHTML = text;
		document.getElementById('popup').style.zIndex = 500;
		resized.push(new Array('popup','center'));
		document.getElementById('popup').style.display = 'block';
		handle_resize();
	}
}
function display_download_box(text) {
    display_box(text, true);
	if (is_user()) {
        document.getElementById('popup').style.height = 165;
        document.getElementById('popup').style.width = 455;
        document.getElementById('popup').style.background = 'url(/images/nav/popupdl_bg.png) no-repeat;';
	}
}
function hide_box(id) {
	dim_page(false);
	document.getElementById(id).innerHTML = '';
	document.getElementById(id).style.display = 'none';
	clear_resize(id);
}
function clear_resize(id) {
	for(i=0;i<resized.length;i++) {
		if (resized[i][0] == id) {
			resized.splice(i,1);
			break;
		}
	}
}
function truncate(str,len) {
	if (str.length > len)
		return str.substr(0,len-2) + '...';
	else
		return str;
}

