function toggleBlock(el){
	typeof(el) == 'string' && (el = document.getElementById(el));
	
	try {
		!((el||0).style||0).display && (el.style.display = 'block');
	} catch(e) {
	}
	
	
	el.style.display = el.style.display != 'none'? 'none':'block';
}

function toggleInline(el){
	typeof(el) == 'string' && (el = document.getElementById(el));
	try {
		!((el || {}).style || {}).display && (el.style.display = 'inherit');   
	} catch(e) {
	}
	el.style.display = el.style.display != 'none'? 'none':'inline';
}

function handleSubjectSelect(){
	var sel = document.getElementById('mail_subject');
	if(sel.value && sel.value == 'report_abuse'){
		document.getElementById('report_abuse_email').style.display = 'block';
	} else {
		document.getElementById('report_abuse_email').style.display = 'none';
	}
	return true;
}
function toggleClass(e,c){
	if(!e) return false;
	if(typeof e.className != 'undefined'){		
		if(e.className.indexOf(c)!==-1){
			e.className = e.className.replace(new RegExp("(\\s|^)" + c + "(\\s|$)"),' ');
		} else {
			e.className += ' '+c;
		}
	}
	return false;
}
function removeClass(e,c){
	if(typeof e.className != 'undefined'){
		e.className = e.className.replace(new RegExp("(\\s|^)" + c + "(\\s|$)"),' ');
	}
}
function clearSearch(){
	return true;
}
function replaceHTML(what, where){
	if(!where){ return; }
	where.innerHTML = what||'';
}
function rate(li){
	UTIL.ajax(window.location,
	'ajax=true&no_user_action=rate&rate='+li.firstChild.firstChild.nodeValue,
	function(text){
		var nli = li.parentNode.parentNode;
		li = null;
		nli.parentNode.replaceChild(document.createTextNode(text), nli);
		nli = null;
	});
}
function generalRHandlr(rText){
	if(rText && (/\S/).test(rText)) alert(rText);
}
function commentRHandlr(rText){
	generalRHandlr(rText);
	
	var under 	= document.getElementById('commentsNumberUnder'),
		rhs		= document.getElementById('commentsNumberRHS');
	under.innerHTML = rhs.innerHTML = parseInt(under.innerHTML,10) + 1;
	paginate({'id':'paginatorReload'});
}
function swapComments(rText){
	document.getElementById('videoComments').innerHTML = rText;
}
function paginate(a){
	if(a.id != 'paginatorReload'){
		if(typeof ajaxPaginate == 'undefined' || !ajaxPaginate){
			return true;
		}
		if(!paginator || paginator.all == 1) return true;
	}
	
	var dif = +1, np = 0, q = '';
	if(a.id == 'paginatorPrev'){
		dif = -1;
	}
	if(a.id == 'paginatorReload'){
		np = 1;
		paginator.all = paginator.all>0?paginator.all:1;
	} else {
		np = paginator.current * 1 + dif;
	}
	
	if(np > 0 && np <= paginator.all){
		paginator.current = np;
		var n = document.getElementById('paginatorNext'),
			p = document.getElementById('paginatorPrev'),
			i = document.getElementById('paginatorInfo');	
		i && (i.innerHTML = 'Page '+ paginator.current +' out of '+paginator.all);
		if(np == 1) { (toggleClass(p,'disabled')) } else  removeClass(p,'disabled');
		if(np == paginator.all) { (toggleClass(n,'disabled'))} else  removeClass(n,'disabled');
		q = 'no_user_action=getComments&v='+__v+'&page='+np;	
		UTIL.ajax(window.location.pathname, q, swapComments);
	}
	return false;
}

function postComment(f){
	var t = f['comment'], q = '', cp = document.getElementById('commentPoster');
	!t && (t= f.getElementsByTagName('textarea')[0]);
	if(! (/\S/).test (t.value) ){
		alert('You cannot post an empty comment.');
		return false;
	}
	q = 'no_user_action=postCommentRemote&v='+__v+'&comment='+encodeURIComponent(t.value);	
	t.disabled=true;
	cp.value="POSTED";
	f.onsubmit = function(){return false};
	UTIL.ajax(window.location.pathname, q, commentRHandlr);
	return false;
}

function upFormUp(){
	showFUOverlay();
	return true;
}

function showFUOverlay(){
	var o = document.createElement('div'),
		m = o.cloneNode(false),
		im = new Image(32,32);
	im.src = 'img/preloader.gif';
	m.id="PLEASE_WAIT";	
	o.id='FUOVERLAY';
	o.innerHTML = '  &nbsp; ';
	m.innerHTML = 'Processing your request.<br />Please wait for this preloader to close.';
	document.body.appendChild(o);
	document.body.appendChild(m);
	o.style.top = '0px';
	if(window.innerHeight){
		o.style.height = window.innerHeight+'px';
	} else if (document.body.clientHeight){
		o.style.height = document.body.clientHeight+'px';
	} 
	o.style.display = 'block';
	m.style.marginTop = '-100px';
	m.style.marginLeft = '-150px';
	return;
}
function userUqHndlr(rText){
	if(rText && (rs = document.getElementById('userUqResult'))){
		if(rText=='unique'){
			rs.innerHTML = 'Available';
			rs.style.color = 'green';
		} else {
			rs.innerHTML = 'Already Taken';
			rs.style.color = 'red';
		}
	}
}
function usernameChanged(u,iu){
	if(u.value != iu){
		if(window.timeout){
			try{clearTimeout(window.timeout);}catch(e){}
		}
		window.timeout = setTimeout(checkUsernameUnique,550,iu);
		return true;
	} else {
		document.getElementById('userUqResult').innerHTML = 'NO CHANGE';
		document.getElementById('userUqResult').style.color='inherit';
		return false;
	}
}
function checkUsernameUnique(iu){
	var u 	= document.getElementById('username'),
		rs 	= document.getElementById('userUqResult');
	if(!u) return false;
	if(!u.value || !(/\S/).test(u.value)){u.focus(); return false};
	if(iu && u.value == iu) {
		rs.innerHTML = 'NO CHANGE';
		return false;
	} else {
		rs.innerHTML = 'CHECKING';
	}
	rs.style.color='inherit';
	UTIL.ajax(window.location,'ajax=true&no_user_action=userUq&username='+u.value,userUqHndlr);
	return false;
}
function showSendToFriend(){
	try{
		Mediabox.init(Mediabox);
		Mediabox.open('pages/just_sendtofriend.php?v='+__v,'Send To Friend');
	} catch (e) {
		window.location.search+='&showSendToFriend=true';
	}
	return false;
}
function showAddidionalInfo(el){
	document.getElementById('additionalInfo').style.display = 'block';
	el.parentNode.removeChild(el.nextSibling);
	el.parentNode.removeChild(el);
	return false	
}

/* ########################################################################################################################################################################################################################################################################*/
function createRequestObject(){
	var request_o;
	var browser = navigator.appName;
	if( browser == "Microsoft Internet Explorer"){
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		request_o = new XMLHttpRequest();
	}
	return request_o;
}
	

function handleData(str,page){
	var http = createRequestObject(); 
	if(injectWhere!=""){
		document.getElementById(injectWhere).style.display='';
		document.getElementById(injectWhere).innerHTML = loadingElem;
	}
	
	document.getElementById('loading').src=eval('loading_anim.src');
	
	if(!page){
		page = "_actions.php";
	}
	http.open('post', page, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//http.onreadystatechange = handleResponse; 
	http.onreadystatechange = function(){ handleResponse(http);}
	http.send(str);
}


function generateSeo(fromwhere,towhere, prefix){
	try{
		var v = document.getElementById(fromwhere).value;
		if((/\S/).test(v)) { document.getElementById(towhere).value = (prefix||'')+v.replace(/%20|_|\s|&|\.|\:|\+|;/g,'-').replace(/,|"|'|\?|~|`|=|!|@|#|\$|%|\^|&|\*/g,'').replace(/-+/g,'-').replace(/-$/,'').toLowerCase();
		} else { throw new Exception('Something went horribly wrong') };
	} catch (e){
		//alert(fromwhere);
		alert('Nothing to generate from.\nPlease try again.')
	}
}

function isSeoUnique(el, ommitid, fromTable){
	var _x = createRequestObject(), seo = '';
	if(typeof el == 'string') el = document.getElementById(el);
	if(el.value && (/^\S/).test(el.value)) seo = el.value
	else return;
	try{
		ommitid = ommitid||0;
		fromTable = fromTable||'';
		_x.open('post', '_actions.php', false);
		_x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		_x.onreadystatechange = function(){
			if(_x.readyState != 4) return;
			if(_x.responseText!='unique'){
				
				document.getElementById('seoNotUnique').innerHTML = (_x.responseText);
				el.value = '';
			} else {
				document.getElementById('seoNotUnique').innerHTML = ('');
			}
		}
		_x.send('no_action=is_seo_unique&seo='+seo+'&ommitid='+ommitid+'&fromTable='+fromTable);

	} catch (e){
		alert(e);
	}
}

